Skip to content

Commit

Permalink
Merge branch 'replace-testcoroutinedispatcher-droid-279'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Sep 6, 2023
2 parents 02715c2 + 20fe581 commit 9786122
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import io.mockk.verify
import io.mockk.verifyOrder
import junit.framework.Assert.assertEquals
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runBlockingTest
import net.mullvad.mullvadvpn.compose.state.DeviceRevokedUiState
import net.mullvad.mullvadvpn.model.TunnelState
Expand Down Expand Up @@ -48,7 +48,7 @@ class DeviceRevokedViewModelTest {
DeviceRevokedViewModel(
mockedServiceConnectionManager,
mockedAccountRepository,
TestCoroutineDispatcher()
UnconfinedTestDispatcher()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import junit.framework.Assert.assertEquals
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.setMain
import net.mullvad.mullvadvpn.lib.ipc.Event
Expand Down Expand Up @@ -45,7 +45,7 @@ class LoginViewModelTest {

@Before
fun setup() {
Dispatchers.setMain(TestCoroutineDispatcher())
Dispatchers.setMain(UnconfinedTestDispatcher())
MockKAnnotations.init(this, relaxUnitFun = true)

every { mockedAccountRepository.accountCreationEvents } returns accountCreationTestEvents
Expand All @@ -59,7 +59,7 @@ class LoginViewModelTest {
LoginViewModel(
mockedAccountRepository,
mockedDeviceRepository,
TestCoroutineDispatcher()
UnconfinedTestDispatcher()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ class SettingsViewModelTest {
isOutdated = false,
isSupported = true
)
every { mockAppVersionInfoCache.version } returns "1.0"
every { mockAppVersionInfoCache.isSupported } returns true
every { mockAppVersionInfoCache.isOutdated } returns false

// Act, Assert
viewModel.uiState.test {
awaitItem() // Wait for initial value

serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
versionInfo.value = versionInfoTestItem
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package net.mullvad.mullvadvpn.lib.common.test

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description

class TestCoroutineRule(val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) :
@OptIn(ExperimentalCoroutinesApi::class)
class TestCoroutineRule(val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()) :
TestWatcher() {

override fun starting(description: Description?) {
override fun starting(description: Description) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
}

override fun finished(description: Description?) {
override fun finished(description: Description) {
super.finished(description)
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
// Replacement for cleanupTestCoroutines()
testDispatcher.scheduler.runCurrent()
}
}

0 comments on commit 9786122

Please sign in to comment.