diff --git a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/cohort/CohortStore.kt b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/cohort/CohortStore.kt index 3471e98c4d43..9ade4f7e036e 100644 --- a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/cohort/CohortStore.kt +++ b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/cohort/CohortStore.kt @@ -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 @@ -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") @@ -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( diff --git a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/cohort/CohortPixelInterceptorTest.kt b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/cohort/CohortPixelInterceptorTest.kt index e992c1b7ec18..3b646689cee2 100644 --- a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/cohort/CohortPixelInterceptorTest.kt +++ b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/cohort/CohortPixelInterceptorTest.kt @@ -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 @@ -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) } diff --git a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/cohort/RealCohortStoreTest.kt b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/cohort/RealCohortStoreTest.kt index 7afc166afba4..5cd5dbe98fa7 100644 --- a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/cohort/RealCohortStoreTest.kt +++ b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/cohort/RealCohortStoreTest.kt @@ -17,12 +17,16 @@ 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 @@ -30,19 +34,21 @@ import org.threeten.bp.LocalDate @ExperimentalCoroutinesApi class RealCohortStoreTest { - + @Mock + private lateinit var vpnFeaturesRegistry: VpnFeaturesRegistry private val sharedPreferencesProvider = mock() 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 @@ -60,6 +66,7 @@ class RealCohortStoreTest { @Test fun whenInitialCohortFirstCalledThenStoreInitialCohort() { + whenever(vpnFeaturesRegistry.isFeatureRegistered(AppTpVpnFeature.APPTP_VPN)).thenReturn(true) (cohortStore as RealCohortStore).onVpnStarted(TestScope()) assertEquals(LocalDate.now(), cohortStore.getCohortStoredLocalDate()) @@ -67,6 +74,7 @@ class RealCohortStoreTest { @Test fun whenInitialCohortSubsequentCalledThenNoop() { + whenever(vpnFeaturesRegistry.isFeatureRegistered(AppTpVpnFeature.APPTP_VPN)).thenReturn(true) val date = LocalDate.now().plusDays(3) cohortStore.setCohortLocalDate(date) @@ -74,4 +82,12 @@ class RealCohortStoreTest { assertEquals(date, cohortStore.getCohortStoredLocalDate()) } + + @Test + fun whenAppTpNotRegisteredThenDoNothingWithCohort() { + whenever(vpnFeaturesRegistry.isFeatureRegistered(AppTpVpnFeature.APPTP_VPN)).thenReturn(false) + (cohortStore as RealCohortStore).onVpnStarted(TestScope()) + + assertNull(cohortStore.getCohortStoredLocalDate()) + } }