-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
161 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/src/androidTest/java/com/karumi/jetpack/superheroes/worker/ThanosWorkerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.karumi.jetpack.superheroes.worker | ||
|
||
import androidx.room.Room | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation | ||
import androidx.work.OneTimeWorkRequest | ||
import androidx.work.WorkManager | ||
import androidx.work.testing.WorkManagerTestInitHelper | ||
import com.karumi.jetpack.superheroes.SuperHeroesApplication | ||
import com.karumi.jetpack.superheroes.common.SuperHeroesDatabase | ||
import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroEntity | ||
import com.karumi.jetpack.superheroes.domain.model.SuperHero | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.kodein.di.Kodein | ||
import org.kodein.di.erased.bind | ||
import org.kodein.di.erased.instance | ||
import java.util.concurrent.Executor | ||
|
||
class ThanosWorkerTest { | ||
|
||
private lateinit var database: SuperHeroesDatabase | ||
|
||
@Before | ||
fun setUp() { | ||
val app = ApplicationProvider.getApplicationContext<SuperHeroesApplication>() | ||
WorkManagerTestInitHelper.initializeTestWorkManager(app) | ||
database = Room.inMemoryDatabaseBuilder(app, SuperHeroesDatabase::class.java) | ||
.allowMainThreadQueries() | ||
.build() | ||
app.override(Kodein.Module("Test dependencies", allowSilentOverride = true) { | ||
bind<SuperHeroesDatabase>() with instance(database) | ||
bind<Executor>() with instance(Executor { | ||
getInstrumentation().runOnMainSync { it.run() } | ||
}) | ||
}) | ||
database.superHeroesDao().deleteAll() | ||
} | ||
|
||
@Test | ||
fun thanosWorkerDoNotDeleteAnyoneIfSuperHeroesAreLessThanTwo() { | ||
val superHeroes = insertSuperHeroes(1) | ||
|
||
enqueueWorker() | ||
|
||
assertEquals(superHeroes, getInsertedSuperHeroes()) | ||
} | ||
|
||
@Test | ||
fun thanosWorkerDeletesHalfTheSuperHeroesWhenThereAreMoreThanTwo() { | ||
insertSuperHeroes(10) | ||
|
||
enqueueWorker() | ||
|
||
assertEquals(5, getInsertedSuperHeroes().size) | ||
} | ||
|
||
@Test | ||
fun thanosWorkerDeletesHalfTheSuperHeroesRoundingDownWhenThereIsAnOddNumberOfSuperHeroes() { | ||
insertSuperHeroes(11) | ||
|
||
enqueueWorker() | ||
|
||
assertEquals(6, getInsertedSuperHeroes().size) | ||
} | ||
|
||
private fun enqueueWorker() = getInstrumentation().runOnMainSync { | ||
val work = OneTimeWorkRequest.Builder(ThanosWorker::class.java).build() | ||
WorkManager.getInstance().enqueue(work) | ||
} | ||
|
||
private fun insertSuperHeroes(numberOfSuperHeroes: Int): List<SuperHero> { | ||
val superHeroes = (1..numberOfSuperHeroes) | ||
.map { SuperHero("#$it", "SH $it", null, false, "") } | ||
|
||
database.superHeroesDao().insertAll(superHeroes.map { SuperHeroEntity(it) }) | ||
|
||
return superHeroes | ||
} | ||
|
||
private fun getInsertedSuperHeroes(): List<SuperHero> = | ||
database.superHeroesDao().getAllSuperHeroes().map { it.superHero } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/karumi/jetpack/superheroes/worker/ThanosWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.karumi.jetpack.superheroes.worker | ||
|
||
import android.content.Context | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import com.karumi.jetpack.superheroes.data.repository.LocalSuperHeroDataSource | ||
import org.kodein.di.KodeinAware | ||
import org.kodein.di.android.closestKodein | ||
import org.kodein.di.erased.instance | ||
|
||
class ThanosWorker( | ||
context: Context, | ||
params: WorkerParameters | ||
) : Worker(context, params), KodeinAware { | ||
|
||
override val kodein by closestKodein(context) | ||
private val localSuperHeroesDataSource: LocalSuperHeroDataSource by instance() | ||
|
||
override fun doWork(): Result { | ||
localSuperHeroesDataSource.deleteRandomHalf() | ||
return Result.success() | ||
} | ||
} |