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 02c36e4
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions app/src/test/java/klalumiere/repertoire/SongRepositoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import androidx.test.platform.app.InstrumentationRegistry
import com.nhaarman.mockitokotlin2.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.StandardTestDispatcher
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 +26,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 +53,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 +78,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 +89,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 +116,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 +127,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 +146,6 @@ class SongRepositoryTest {
@After
fun closeResources() {
db.close()
dispatcherInjector.close()
contentResolverInjector.close()
}

Expand Down

0 comments on commit 02c36e4

Please sign in to comment.