Skip to content

Commit

Permalink
fix and add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikescamell committed Dec 16, 2024
1 parent 4161da2 commit e76ed72
Showing 1 changed file with 25 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ package com.duckduckgo.networkprotection.impl.subscription.settings
import app.cash.turbine.test
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.common.test.CoroutineTestRule
import com.duckduckgo.mobile.android.R as CommonR
import com.duckduckgo.navigation.api.GlobalActivityStarter.ActivityParams
import com.duckduckgo.networkprotection.api.NetworkProtectionAccessState
import com.duckduckgo.networkprotection.api.NetworkProtectionAccessState.LegacyNetPAccessState.Locked
import com.duckduckgo.networkprotection.api.NetworkProtectionAccessState.LegacyNetPAccessState.UnLocked
import com.duckduckgo.networkprotection.api.NetworkProtectionAccessState.NetPVisibilityState
import com.duckduckgo.networkprotection.api.NetworkProtectionState
import com.duckduckgo.networkprotection.api.NetworkProtectionState.ConnectionState.CONNECTED
import com.duckduckgo.networkprotection.api.NetworkProtectionState.ConnectionState.CONNECTING
import com.duckduckgo.networkprotection.api.NetworkProtectionState.ConnectionState.DISCONNECTED
import com.duckduckgo.networkprotection.impl.R
import com.duckduckgo.networkprotection.impl.pixels.NetworkProtectionPixelNames.NETP_SETTINGS_PRESSED
import com.duckduckgo.networkprotection.impl.subscription.settings.LegacyProSettingNetPViewModel.Command
import com.duckduckgo.networkprotection.impl.subscription.settings.LegacyProSettingNetPViewModel.NetPEntryState.Hidden
import com.duckduckgo.networkprotection.impl.subscription.settings.LegacyProSettingNetPViewModel.NetPEntryState.Pending
import com.duckduckgo.networkprotection.impl.subscription.settings.LegacyProSettingNetPViewModel.NetPEntryState.ShowState
import com.duckduckgo.networkprotection.impl.subscription.settings.ProSettingNetPViewModel.Command
import com.duckduckgo.networkprotection.impl.subscription.settings.ProSettingNetPViewModel.NetPEntryState.Activating
import com.duckduckgo.networkprotection.impl.subscription.settings.ProSettingNetPViewModel.NetPEntryState.Expired
import com.duckduckgo.networkprotection.impl.subscription.settings.ProSettingNetPViewModel.NetPEntryState.Hidden
import com.duckduckgo.networkprotection.impl.subscription.settings.ProSettingNetPViewModel.NetPEntryState.Subscribed
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Assert.*
Expand Down Expand Up @@ -65,7 +63,7 @@ class ProSettingNetPViewModelTest {
}

@Test
fun whenNetPSettingClickedThenReturnScreenForCurrentState() = runTest {
fun whenNetPSettingClickedThenNetPScreenOpened() = runTest {
val testScreen = object : ActivityParams {}
whenever(networkProtectionAccessState.getScreenForCurrentState()).thenReturn(testScreen)

Expand All @@ -80,9 +78,9 @@ class ProSettingNetPViewModelTest {
}

@Test
fun whenNetPIsNotUnlockedThenNetPEntryStateShouldShowHidden() = runTest {
fun whenNetPVisibilityStateIsHiddenThenNetPEntryStateIsHidden() = runTest {
whenever(networkProtectionState.getConnectionStateFlow()).thenReturn(flowOf(DISCONNECTED))
whenever(networkProtectionAccessState.getLegacyStateFlow()).thenReturn(flowOf(Locked))
whenever(networkProtectionAccessState.getStateFlow()).thenReturn(flowOf(NetPVisibilityState.Hidden))

proSettingNetPViewModel.onStart(mock())

Expand All @@ -95,108 +93,75 @@ class ProSettingNetPViewModelTest {
}

@Test
fun whenNetPStateIsInBetaButNotAcceptedTermsThenNetPEntryStateShouldShowPending() = runTest {
fun whenNetPVisibilityStateIsActivatingThenNetPEntryStateIsActivating() = runTest {
whenever(networkProtectionState.getConnectionStateFlow()).thenReturn(flowOf(DISCONNECTED))
whenever(networkProtectionAccessState.getLegacyStateFlow()).thenReturn(flowOf(UnLocked))
whenever(networkProtectionState.isOnboarded()).thenReturn(false)
whenever(networkProtectionAccessState.getStateFlow()).thenReturn(flowOf(NetPVisibilityState.Visible.Activating))

proSettingNetPViewModel.onStart(mock())

proSettingNetPViewModel.viewState.test {
assertEquals(
Pending,
Activating,
expectMostRecentItem().networkProtectionEntryState,
)
}
}

@Test
fun whenNetPStateIsInBetaAndOnboardedAndEnabledThenNetPEntryStateShouldCorrectShowState() = runTest {
fun whenNetPVisibilityStateConnectedAndAccessStateIsSubscribedThenNetPEntryStateIsSubscribedAndActive() = runTest {
whenever(networkProtectionState.getConnectionStateFlow()).thenReturn(flowOf(CONNECTED))
whenever(networkProtectionAccessState.getLegacyStateFlow()).thenReturn(flowOf(UnLocked))
whenever(networkProtectionState.isOnboarded()).thenReturn(true)
whenever(networkProtectionAccessState.getStateFlow()).thenReturn(flowOf(NetPVisibilityState.Visible.Subscribed))

proSettingNetPViewModel.onStart(mock())

proSettingNetPViewModel.viewState.test {
assertEquals(
ShowState(
icon = CommonR.drawable.ic_check_green_round_16,
subtitle = R.string.netpSubscriptionSettingsConnected,
),
Subscribed(isActive = true),
expectMostRecentItem().networkProtectionEntryState,
)
}
}

@Test
fun whenNetPStateIsInBetaAndNotOnboardedAndEnabledThenNetPEntryStateShouldCorrectShowState() = runTest {
whenever(networkProtectionState.getConnectionStateFlow()).thenReturn(flowOf(CONNECTED))
whenever(networkProtectionAccessState.getLegacyStateFlow()).thenReturn(flowOf(UnLocked))
whenever(networkProtectionState.isOnboarded()).thenReturn(false)

proSettingNetPViewModel.onStart(mock())

proSettingNetPViewModel.viewState.test {
assertEquals(
Pending,
expectMostRecentItem().networkProtectionEntryState,
)
}
}

@Test
fun whenNetPStateIsInBetaOnboardedAndEnabledThenNetPEntryStateShouldCorrectShowState() = runTest {
whenever(networkProtectionState.getConnectionStateFlow()).thenReturn(flowOf(CONNECTED))
whenever(networkProtectionAccessState.getLegacyStateFlow()).thenReturn(flowOf(UnLocked))
whenever(networkProtectionState.isOnboarded()).thenReturn(true)
fun whenNetPVisibilityStateDisconnectedAndAccessStateIsSubscribedThenNetPEntryStateIsSubscribedAndInactive() = runTest {
whenever(networkProtectionState.getConnectionStateFlow()).thenReturn(flowOf(DISCONNECTED))
whenever(networkProtectionAccessState.getStateFlow()).thenReturn(flowOf(NetPVisibilityState.Visible.Subscribed))

proSettingNetPViewModel.onStart(mock())

proSettingNetPViewModel.viewState.test {
assertEquals(
ShowState(
icon = CommonR.drawable.ic_check_green_round_16,
subtitle = R.string.netpSubscriptionSettingsConnected,
),
Subscribed(isActive = false),
expectMostRecentItem().networkProtectionEntryState,
)
}
}

@Test
fun whenNetPStateIsInBetaAndConnectingThenNetPEntryStateShouldCorrectShowState() = runTest {
fun whenNetPVisibilityStateIsConnectingThenNetPEntryStateIsSubscribedAndNotActive() = runTest {
whenever(networkProtectionState.getConnectionStateFlow()).thenReturn(flowOf(CONNECTING))
whenever(networkProtectionAccessState.getLegacyStateFlow()).thenReturn(flowOf(UnLocked))
whenever(networkProtectionState.isOnboarded()).thenReturn(true)
whenever(networkProtectionAccessState.getStateFlow()).thenReturn(flowOf(NetPVisibilityState.Visible.Subscribed))

proSettingNetPViewModel.onStart(mock())

proSettingNetPViewModel.viewState.test {
assertEquals(
ShowState(
icon = CommonR.drawable.ic_check_green_round_16,
subtitle = R.string.netpSubscriptionSettingsConnecting,
),
Subscribed(isActive = false),
expectMostRecentItem().networkProtectionEntryState,
)
}
}

@Test
fun whenNetPStateIsInBetaAndDisabledThenNetPEntryStateShouldCorrectShowState() = runTest {
fun whenNetPVisibilityStateIsExpiredThenNetPEntryStateIsExpired() = runTest {
whenever(networkProtectionState.getConnectionStateFlow()).thenReturn(flowOf(DISCONNECTED))
whenever(networkProtectionAccessState.getLegacyStateFlow()).thenReturn(flowOf(UnLocked))
whenever(networkProtectionState.isOnboarded()).thenReturn(true)
whenever(networkProtectionAccessState.getStateFlow()).thenReturn(flowOf(NetPVisibilityState.Visible.Expired))

proSettingNetPViewModel.onStart(mock())

proSettingNetPViewModel.viewState.test {
assertEquals(
ShowState(
icon = CommonR.drawable.ic_exclamation_yellow_16,
subtitle = R.string.netpSubscriptionSettingsDisconnected,
),
Expired,
expectMostRecentItem().networkProtectionEntryState,
)
}
Expand Down

0 comments on commit e76ed72

Please sign in to comment.