Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null pointers in unit tests #3883

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ internal class FireButtonViewModelTest {

whenever(mockAppSettingsDataStore.automaticallyClearWhenOption).thenReturn(ClearWhenOption.APP_EXIT_ONLY)
whenever(mockAppSettingsDataStore.automaticallyClearWhatOption).thenReturn(ClearWhatOption.CLEAR_NONE)
whenever(mockAppSettingsDataStore.selectedFireAnimation).thenReturn(FireAnimation.HeroFire)

testee = FireButtonViewModel(
mockAppSettingsDataStore,
Expand Down Expand Up @@ -200,9 +201,11 @@ internal class FireButtonViewModelTest {
fun whenNewFireAnimationSelectedThenUpdateViewState() = runTest {
val expectedAnimation = FireAnimation.HeroWater

testee.onFireAnimationSelected(expectedAnimation)

testee.viewState().test {
// expect HeroFire as a default which will happen when view state flow is created
assertEquals(FireAnimation.HeroFire, awaitItem().selectedFireAnimation)

testee.onFireAnimationSelected(expectedAnimation)
assertEquals(expectedAnimation, awaitItem().selectedFireAnimation)

cancelAndConsumeRemainingEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import com.duckduckgo.common.test.CoroutineTestRule
import kotlin.time.ExperimentalTime
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -65,9 +63,9 @@ class WelcomePageViewModelTest {
@Mock
private lateinit var defaultRoleBrowserDialog: DefaultRoleBrowserDialog

private val events = ConflatedBroadcastChannel<WelcomePageView.Event>()
private val events = MutableSharedFlow<WelcomePageView.Event>(replay = 1)

lateinit var viewModel: WelcomePageViewModel
private lateinit var viewModel: WelcomePageViewModel

private lateinit var viewEvents: Flow<WelcomePageView.State>

Expand All @@ -81,19 +79,14 @@ class WelcomePageViewModelTest {
defaultRoleBrowserDialog = defaultRoleBrowserDialog,
)

viewEvents = events.asFlow().flatMapLatest { viewModel.reduce(it) }
}

@After
fun teardown() {
events.close()
viewEvents = events.flatMapLatest { viewModel.reduce(it) }
}

@Test
fun whenOnPrimaryCtaClickedAndShouldNotShowDialogThenFireAndFinish() = runTest {
whenever(defaultRoleBrowserDialog.shouldShowDialog()).thenReturn(false)

events.send(WelcomePageView.Event.OnPrimaryCtaClicked)
events.emit(WelcomePageView.Event.OnPrimaryCtaClicked)

viewEvents.test {
assertTrue(awaitItem() == WelcomePageView.State.Finish)
Expand All @@ -106,7 +99,7 @@ class WelcomePageViewModelTest {
val intent = Intent()
whenever(defaultRoleBrowserDialog.createIntent(any())).thenReturn(intent)

events.send(WelcomePageView.Event.OnPrimaryCtaClicked)
events.emit(WelcomePageView.Event.OnPrimaryCtaClicked)

viewEvents.test {
assertTrue(awaitItem() == WelcomePageView.State.ShowDefaultBrowserDialog(intent))
Expand All @@ -118,7 +111,7 @@ class WelcomePageViewModelTest {
whenever(defaultRoleBrowserDialog.shouldShowDialog()).thenReturn(true)
whenever(defaultRoleBrowserDialog.createIntent(any())).thenReturn(null)

events.send(WelcomePageView.Event.OnPrimaryCtaClicked)
events.emit(WelcomePageView.Event.OnPrimaryCtaClicked)

viewEvents.test {
assertTrue(awaitItem() == WelcomePageView.State.Finish)
Expand All @@ -128,7 +121,7 @@ class WelcomePageViewModelTest {

@Test
fun whenOnDefaultBrowserSetThenCallDialogShownFireAndFinish() = runTest {
events.send(WelcomePageView.Event.OnDefaultBrowserSet)
events.emit(WelcomePageView.Event.OnDefaultBrowserSet)

viewEvents.test {
assertTrue(awaitItem() == WelcomePageView.State.Finish)
Expand All @@ -144,7 +137,7 @@ class WelcomePageViewModelTest {

@Test
fun whenOnDefaultBrowserNotSetThenCallDialogShownFireAndFinish() = runTest {
events.send(WelcomePageView.Event.OnDefaultBrowserNotSet)
events.emit(WelcomePageView.Event.OnDefaultBrowserNotSet)

viewEvents.test {
assertTrue(awaitItem() == WelcomePageView.State.Finish)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import com.duckduckgo.networkprotection.api.NetworkProtectionWaitlist.NetPWaitli
import com.duckduckgo.sync.api.DeviceSyncState
import kotlin.time.ExperimentalTime
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -103,6 +104,7 @@ class SettingsViewModelTest {
whenever(networkProtectionWaitlist.getState()).thenReturn(NotUnlocked)
whenever(networkProtectionState.isRunning()).thenReturn(false)
whenever(networkProtectionState.isEnabled()).thenReturn(false)
whenever(networkProtectionState.getConnectionStateFlow()).thenReturn(emptyFlow())
whenever(appTrackingProtection.isRunning()).thenReturn(false)
whenever(appTrackingProtection.isEnabled()).thenReturn(false)
whenever(appTrackingProtection.isOnboarded()).thenReturn(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.duckduckgo.autofill.impl.ui.credential.repository.DuckAddressStatusRe
import com.duckduckgo.common.test.CoroutineTestRule
import kotlin.reflect.KClass
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -92,6 +93,10 @@ class AutofillSettingsViewModelTest {
@Before
fun setup() {
whenever(webUrlIdentifier.isLikelyAUrl(anyOrNull())).thenReturn(true)

runTest {
whenever(mockStore.getAllCredentials()).thenReturn(emptyFlow())
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ class NetworkProtectionManagementViewModelTest {
MockitoAnnotations.openMocks(this)
vpnFeaturesRegistry = FakeVpnFeaturesRegistry()

whenever(networkProtectionRepository.reconnectStatus).thenReturn(NotReconnecting)
runTest {
whenever(networkProtectionRepository.reconnectStatus).thenReturn(NotReconnecting)
whenever(vpnStateMonitor.isAlwaysOnEnabled()).thenReturn(false)
whenever(vpnStateMonitor.vpnLastDisabledByAndroid()).thenReturn(false)
}

testee = NetworkProtectionManagementViewModel(
vpnStateMonitor,
vpnFeaturesRegistry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import kotlin.reflect.KClass
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
Expand Down Expand Up @@ -86,6 +87,8 @@ class SyncActivityViewModelTest {
syncStateMonitor = syncStateMonitor,
recoveryCodePDF = recoveryPDF,
)

whenever(syncStateMonitor.syncState()).thenReturn(emptyFlow())
}

@Test
Expand Down