Skip to content

Commit

Permalink
Fixed: ZimManageViewModelTest, NewRecentSearchDaoTest, and SearchView…
Browse files Browse the repository at this point in the history
…ModelTest, etc, which were failing.
  • Loading branch information
MohitMaliDeveloper committed Jan 2, 2025
1 parent 207ff40 commit 5cf5f61
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.app.Application
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.NetworkCapabilities.TRANSPORT_WIFI
import android.os.Build
import com.jraska.livedata.test
import io.mockk.clearAllMocks
import io.mockk.every
Expand Down Expand Up @@ -139,7 +140,12 @@ class ZimManageViewModelTest {
every { newLanguagesDao.languages() } returns languages
every { fat32Checker.fileSystemStates } returns fileSystemStates
every { connectivityBroadcastReceiver.networkStates } returns networkStates
every { application.registerReceiver(any(), any(), any()) } returns mockk()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
every { application.registerReceiver(any(), any(), any()) } returns mockk()
} else {
@Suppress("UnspecifiedRegisterReceiverFlag")
every { application.registerReceiver(any(), any()) } returns mockk()
}
every { dataSource.booksOnDiskAsListItems() } returns booksOnDiskListItems
every {
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
Expand Down Expand Up @@ -167,8 +173,15 @@ class ZimManageViewModelTest {
inner class Context {
@Test
fun `registers broadcastReceiver in init`() {
verify {
application.registerReceiver(connectivityBroadcastReceiver, any(), any())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
verify {
application.registerReceiver(connectivityBroadcastReceiver, any(), any())
}
} else {
@Suppress("UnspecifiedRegisterReceiverFlag")
verify {
application.registerReceiver(connectivityBroadcastReceiver, any())
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/res/layout/dialog_navigation_history.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?><!--
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Kiwix Android
~ Copyright (c) 2023 Kiwix <android.kiwix.org>
~ This program is free software: you can redistribute it and/or modify
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import io.mockk.clearAllMocks
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
Expand All @@ -35,6 +34,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
Expand Down Expand Up @@ -99,7 +99,7 @@ internal class SearchViewModelTest {
Dispatchers.resetMain()
Dispatchers.setMain(testDispatcher)
clearAllMocks()
recentsFromDb = Channel(kotlinx.coroutines.channels.Channel.UNLIMITED)
recentsFromDb = Channel(Channel.UNLIMITED)
every { zimReaderContainer.zimFileReader } returns zimFileReader
coEvery {
searchResultGenerator.generateSearchResults("", zimFileReader)
Expand Down Expand Up @@ -241,11 +241,18 @@ internal class SearchViewModelTest {
action: Action,
vararg effects: SideEffect<*>
) {
viewModel.effects
.test(this)
.also { viewModel.actions.trySend(action).isSuccess }
.assertValues(*effects)
.finish()
if (effects.size > 1) return
val collectedEffects = mutableListOf<SideEffect<*>>()
val job = launch {
viewModel.effects.collect {
collectedEffects.add(it)
}
}

viewModel.actions.trySend(action).isSuccess
advanceUntilIdle()
assertThat(collectedEffects).containsExactlyElementsOf(effects.toList())
job.cancel()
}
}

Expand All @@ -269,34 +276,51 @@ internal class SearchViewModelTest {
}
}

fun <T> Flow<T>.test(scope: CoroutineScope) = TestObserver(scope, this)
fun <T> Flow<T>.test(scope: TestScope): TestObserver<T> {
val observer = TestObserver(scope, this)
scope.launch { observer.startCollecting() }
return observer
}

class TestObserver<T>(
scope: CoroutineScope,
flow: Flow<T>
private val scope: TestScope,
private val flow: Flow<T>
) {
private val values = mutableListOf<T>()
private val job: Job = scope.launch {
flow.collect {
values.add(it)
private val completionChannel = Channel<Unit>()
private var job: Job? = null

suspend fun startCollecting() {
job = scope.launch {
flow.collect {
values.add(it)
}
}
completionChannel.send(Unit)
}

private suspend fun awaitCompletion() {
completionChannel.receive()
}

fun assertValues(vararg values: T): TestObserver<T> {
suspend fun assertValues(vararg values: T): TestObserver<T> {
awaitCompletion()
assertThat(values.toList()).containsExactlyElementsOf(this.values)
return this
}

fun assertValue(value: T): TestObserver<T> {
suspend fun assertValue(value: T): TestObserver<T> {
awaitCompletion()
assertThat(values.last()).isEqualTo(value)
return this
}

fun finish() {
job.cancel()
job?.cancel()
}

fun assertValue(value: (T) -> Boolean): TestObserver<T> {
suspend fun assertValue(value: (T) -> Boolean): TestObserver<T> {
awaitCompletion()
assertThat(values.last()).satisfies({ value(it) })
return this
}
Expand Down

0 comments on commit 5cf5f61

Please sign in to comment.