Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klalumiere committed Jun 15, 2024
1 parent db72539 commit 5b41a80
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions app/src/test/java/klalumiere/repertoire/SongRepositoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.nhaarman.mockitokotlin2.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert.*
import org.junit.After
import org.junit.Before
Expand All @@ -24,17 +24,14 @@ class SongRepositoryTest {
private val contentUri = Uri.parse("content://arbitrary/uri")
private val songName = "Pearl Jam - Black"
private val songContent = "Sheets of empty canvas"
private lateinit var dispatcherInjector: DispatchersFactory.InjectForTests
private lateinit var context: Context
private lateinit var contentResolver: ContentResolver
private lateinit var contentResolverInjector: RepertoireContentResolverFactory.InjectForTests
private lateinit var db: AppDatabase
private lateinit var repository: SongRepository

@ExperimentalCoroutinesApi
@Before
fun createRepository() {
dispatcherInjector = DispatchersFactory.InjectForTests(UnconfinedTestDispatcher())
context = InstrumentationRegistry.getInstrumentation().targetContext
contentResolver = mock {
on {
Expand All @@ -54,21 +51,21 @@ class SongRepositoryTest {

@Test
fun addTakesPersistableUriPermission() {
runBlocking { repository.add(contentUri, songName) }
runTest { repository.add(contentUri, songName) }
verify(contentResolver).takePersistableUriPermission(contentUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

@Test
fun removeReleasesPersistableUriPermission() {
runBlocking { repository.remove(contentUri) }
runTest { repository.remove(contentUri) }
verify(contentResolver).releasePersistableUriPermission(contentUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

@Test
fun addAddsSongToDb() {
runBlocking { repository.add(contentUri, songName) }
runTest { repository.add(contentUri, songName) }
val song = Song(
uri = contentUri.toString(),
name = songName,
Expand All @@ -79,7 +76,7 @@ class SongRepositoryTest {

@Test
fun addRemovesExtensionFromSongName() {
runBlocking { repository.add(contentUri, "Pantera - Walk.md") }
runTest { repository.add(contentUri, "Pantera - Walk.md") }
val song = Song(
uri = contentUri.toString(),
name = "Pantera - Walk",
Expand All @@ -90,8 +87,8 @@ class SongRepositoryTest {

@Test
fun removeRemovesSongFromDb() {
runBlocking { repository.add(contentUri, songName) }
runBlocking { repository.remove(contentUri) }
runTest { repository.add(contentUri, songName) }
runTest { repository.remove(contentUri) }
assertTrue(repository.getAllSongs().getOrAwaitValue().isEmpty())
}

Expand All @@ -117,7 +114,7 @@ class SongRepositoryTest {
val repository = SongRepository(context).apply {
injectDatabaseForTests(db)
}
runBlocking { repository.add(contentUri) }
runTest { repository.add(contentUri) }

val song = Song(
uri = contentUri.toString(),
Expand All @@ -128,8 +125,10 @@ class SongRepositoryTest {
}
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun getSongContent() {
val injector = DispatchersFactory.InjectForTests(UnconfinedTestDispatcher())
val repository = SongRepository(context).apply {
injectDatabaseForTests(db)
}
Expand All @@ -145,7 +144,6 @@ class SongRepositoryTest {
@After
fun closeResources() {
db.close()
dispatcherInjector.close()
contentResolverInjector.close()
}

Expand Down

0 comments on commit 5b41a80

Please sign in to comment.