Skip to content

Commit

Permalink
Update cohort when AppTP is enabled only
Browse files Browse the repository at this point in the history
  • Loading branch information
karlenDimla committed Feb 15, 2023
1 parent 35c7d4b commit 2bd1611
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import android.content.SharedPreferences
import androidx.core.content.edit
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.di.scopes.VpnScope
import com.duckduckgo.mobile.android.vpn.AppTpVpnFeature
import com.duckduckgo.mobile.android.vpn.VpnFeaturesRegistry
import com.duckduckgo.mobile.android.vpn.prefs.VpnSharedPreferencesProvider
import com.duckduckgo.mobile.android.vpn.service.VpnServiceCallbacks
import com.duckduckgo.mobile.android.vpn.state.VpnStateMonitor.VpnStopReason
Expand Down Expand Up @@ -52,6 +54,7 @@ interface CohortStore {
)
class RealCohortStore @Inject constructor(
private val sharedPreferencesProvider: VpnSharedPreferencesProvider,
private val vpnFeaturesRegistry: VpnFeaturesRegistry,
) : CohortStore, VpnServiceCallbacks {

private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
Expand All @@ -70,10 +73,12 @@ class RealCohortStore @Inject constructor(
}

override fun onVpnStarted(coroutineScope: CoroutineScope) {
// skip if already stored
getCohortStoredLocalDate()?.let { return }
if (vpnFeaturesRegistry.isFeatureRegistered(AppTpVpnFeature.APPTP_VPN)) {
// skip if already stored
getCohortStoredLocalDate()?.let { return }

setCohortLocalDate(LocalDate.now())
setCohortLocalDate(LocalDate.now())
}
}

override fun onVpnStopped(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ package com.duckduckgo.mobile.android.vpn.cohort

import com.duckduckgo.app.global.api.FakeChain
import com.duckduckgo.app.global.api.InMemorySharedPreferences
import com.duckduckgo.mobile.android.vpn.VpnFeaturesRegistry
import com.duckduckgo.mobile.android.vpn.prefs.VpnSharedPreferencesProvider
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.threeten.bp.LocalDate

class CohortPixelInterceptorTest {
@Mock
private lateinit var vpnFeaturesRegistry: VpnFeaturesRegistry
private lateinit var cohortPixelInterceptor: CohortPixelInterceptor
private lateinit var cohortStore: CohortStore
private lateinit var cohortCalculator: CohortCalculator
Expand All @@ -36,12 +41,13 @@ class CohortPixelInterceptorTest {

@Before
fun setup() {
MockitoAnnotations.openMocks(this)
val prefs = InMemorySharedPreferences()
whenever(
sharedPreferencesProvider.getSharedPreferences(eq("com.duckduckgo.mobile.atp.cohort.prefs"), eq(true), eq(true)),
).thenReturn(prefs)

cohortStore = RealCohortStore(sharedPreferencesProvider)
cohortStore = RealCohortStore(sharedPreferencesProvider, vpnFeaturesRegistry)
cohortCalculator = RealCohortCalculator()
cohortPixelInterceptor = CohortPixelInterceptor(cohortCalculator, cohortStore)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,38 @@
package com.duckduckgo.mobile.android.vpn.cohort

import com.duckduckgo.app.global.api.InMemorySharedPreferences
import com.duckduckgo.mobile.android.vpn.AppTpVpnFeature
import com.duckduckgo.mobile.android.vpn.VpnFeaturesRegistry
import com.duckduckgo.mobile.android.vpn.prefs.VpnSharedPreferencesProvider
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.threeten.bp.LocalDate

@ExperimentalCoroutinesApi
class RealCohortStoreTest {

@Mock
private lateinit var vpnFeaturesRegistry: VpnFeaturesRegistry
private val sharedPreferencesProvider = mock<VpnSharedPreferencesProvider>()

private lateinit var cohortStore: CohortStore

@Before
fun setup() {
MockitoAnnotations.openMocks(this)
val prefs = InMemorySharedPreferences()
whenever(
sharedPreferencesProvider.getSharedPreferences(eq("com.duckduckgo.mobile.atp.cohort.prefs"), eq(true), eq(true)),
).thenReturn(prefs)

cohortStore = RealCohortStore(sharedPreferencesProvider)
cohortStore = RealCohortStore(sharedPreferencesProvider, vpnFeaturesRegistry)
}

@Test
Expand All @@ -60,18 +66,28 @@ class RealCohortStoreTest {

@Test
fun whenInitialCohortFirstCalledThenStoreInitialCohort() {
whenever(vpnFeaturesRegistry.isFeatureRegistered(AppTpVpnFeature.APPTP_VPN)).thenReturn(true)
(cohortStore as RealCohortStore).onVpnStarted(TestScope())

assertEquals(LocalDate.now(), cohortStore.getCohortStoredLocalDate())
}

@Test
fun whenInitialCohortSubsequentCalledThenNoop() {
whenever(vpnFeaturesRegistry.isFeatureRegistered(AppTpVpnFeature.APPTP_VPN)).thenReturn(true)
val date = LocalDate.now().plusDays(3)
cohortStore.setCohortLocalDate(date)

(cohortStore as RealCohortStore).onVpnStarted(TestScope())

assertEquals(date, cohortStore.getCohortStoredLocalDate())
}

@Test
fun whenAppTpNotRegisteredThenDoNothingWithCohort() {
whenever(vpnFeaturesRegistry.isFeatureRegistered(AppTpVpnFeature.APPTP_VPN)).thenReturn(false)
(cohortStore as RealCohortStore).onVpnStarted(TestScope())

assertNull(cohortStore.getCohortStoredLocalDate())
}
}

0 comments on commit 2bd1611

Please sign in to comment.