@@ -29,6 +29,9 @@ import com.duckduckgo.app.CoroutineTestRule
2929import com.duckduckgo.app.notification.NotificationScheduler.ClearDataNotificationWorker
3030import com.duckduckgo.app.notification.NotificationScheduler.PrivacyNotificationWorker
3131import com.duckduckgo.app.notification.model.SchedulableNotification
32+ import com.duckduckgo.app.statistics.Variant
33+ import com.duckduckgo.app.statistics.VariantManager
34+ import com.duckduckgo.app.statistics.VariantManager.Companion.DEFAULT_VARIANT
3235import com.nhaarman.mockitokotlin2.mock
3336import com.nhaarman.mockitokotlin2.whenever
3437import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -47,6 +50,8 @@ class AndroidNotificationSchedulerTest {
4750
4851 private val clearNotification: SchedulableNotification = mock()
4952 private val privacyNotification: SchedulableNotification = mock()
53+ private val useOurAppNotification: SchedulableNotification = mock()
54+ private val variantManager: VariantManager = mock()
5055
5156 private val context = InstrumentationRegistry .getInstrumentation().targetContext
5257 private lateinit var workManager: WorkManager
@@ -59,7 +64,9 @@ class AndroidNotificationSchedulerTest {
5964 testee = NotificationScheduler (
6065 workManager,
6166 clearNotification,
62- privacyNotification
67+ privacyNotification,
68+ useOurAppNotification,
69+ variantManager
6370 )
6471 }
6572
@@ -76,6 +83,7 @@ class AndroidNotificationSchedulerTest {
7683
7784 @Test
7885 fun whenPrivacyNotificationClearDataCanShowThenPrivacyNotificationIsScheduled () = runBlocking<Unit > {
86+ setDefaultVariant()
7987 whenever(privacyNotification.canShow()).thenReturn(true )
8088 whenever(clearNotification.canShow()).thenReturn(true )
8189 testee.scheduleNextNotification()
@@ -85,6 +93,7 @@ class AndroidNotificationSchedulerTest {
8593
8694 @Test
8795 fun whenPrivacyNotificationCanShowButClearDataCannotThenPrivacyNotificationIsScheduled () = runBlocking<Unit > {
96+ setDefaultVariant()
8897 whenever(privacyNotification.canShow()).thenReturn(true )
8998 whenever(clearNotification.canShow()).thenReturn(false )
9099 testee.scheduleNextNotification()
@@ -94,6 +103,7 @@ class AndroidNotificationSchedulerTest {
94103
95104 @Test
96105 fun whenPrivacyNotificationCannotShowAndClearNotificationCanShowThenClearNotificationIsScheduled () = runBlocking<Unit > {
106+ setDefaultVariant()
97107 whenever(privacyNotification.canShow()).thenReturn(false )
98108 whenever(clearNotification.canShow()).thenReturn(true )
99109 testee.scheduleNextNotification()
@@ -103,13 +113,137 @@ class AndroidNotificationSchedulerTest {
103113
104114 @Test
105115 fun whenPrivacyNotificationAndClearNotificationCannotShowThenNoNotificationScheduled () = runBlocking<Unit > {
116+ setDefaultVariant()
106117 whenever(privacyNotification.canShow()).thenReturn(false )
107118 whenever(clearNotification.canShow()).thenReturn(false )
108119 testee.scheduleNextNotification()
109120
110121 assertNoNotificationScheduled()
111122 }
112123
124+ @Test
125+ fun whenInAppUsageVariantAndUseOurAppNotificationCanShowThenNotificationScheduled () = runBlocking {
126+ givenNoInactiveUserNotifications()
127+ setInAppUsageVariant()
128+ whenever(useOurAppNotification.canShow()).thenReturn(true )
129+
130+ testee.scheduleNextNotification()
131+
132+ assertNotificationScheduled(NotificationScheduler .UseOurAppNotificationWorker ::class .jvmName, NotificationScheduler .USE_OUR_APP_WORK_REQUEST_TAG )
133+ }
134+
135+ @Test
136+ fun whenInAppUsageVariantUseOurAppNotificationCannotShowThenNoNotificationScheduled () = runBlocking {
137+ givenNoInactiveUserNotifications()
138+ setInAppUsageVariant()
139+ whenever(useOurAppNotification.canShow()).thenReturn(false )
140+
141+ testee.scheduleNextNotification()
142+
143+ assertNoNotificationScheduled(NotificationScheduler .USE_OUR_APP_WORK_REQUEST_TAG )
144+ }
145+
146+ @Test
147+ fun whenInAppUsageSecondControlVariantThenNoNotificationScheduled () = runBlocking<Unit > {
148+ setInAppUsageSecondControlVariant()
149+ whenever(useOurAppNotification.canShow()).thenReturn(true )
150+
151+ testee.scheduleNextNotification()
152+
153+ assertNoNotificationScheduled(NotificationScheduler .USE_OUR_APP_WORK_REQUEST_TAG )
154+ }
155+
156+ @Test
157+ fun whenInAppUsageControlVariantThenNoNotificationScheduled () = runBlocking<Unit > {
158+ givenNoInactiveUserNotifications()
159+ setInAppUsageControlVariant()
160+ whenever(useOurAppNotification.canShow()).thenReturn(true )
161+
162+ testee.scheduleNextNotification()
163+
164+ assertNoNotificationScheduled(NotificationScheduler .USE_OUR_APP_WORK_REQUEST_TAG )
165+ }
166+
167+ @Test
168+ fun whenInAppUsageControlVariantAndPrivacyNotificationClearDataCanShowThenPrivacyNotificationIsScheduled () = runBlocking<Unit > {
169+ setInAppUsageControlVariant()
170+ whenever(privacyNotification.canShow()).thenReturn(true )
171+ whenever(clearNotification.canShow()).thenReturn(true )
172+ testee.scheduleNextNotification()
173+
174+ assertNotificationScheduled(PrivacyNotificationWorker ::class .jvmName)
175+ }
176+
177+ @Test
178+ fun whenInAppUsageControlVariantAndPrivacyNotificationCanShowButClearDataCannotThenPrivacyNotificationIsScheduled () = runBlocking<Unit > {
179+ setInAppUsageControlVariant()
180+ whenever(privacyNotification.canShow()).thenReturn(true )
181+ whenever(clearNotification.canShow()).thenReturn(false )
182+ testee.scheduleNextNotification()
183+
184+ assertNotificationScheduled(PrivacyNotificationWorker ::class .jvmName)
185+ }
186+
187+ @Test
188+ fun whenInAppUsageControlVariantAndPrivacyNotificationCannotShowAndClearNotificationCanShowThenClearNotificationScheduled () = runBlocking<Unit > {
189+ setInAppUsageControlVariant()
190+ whenever(privacyNotification.canShow()).thenReturn(false )
191+ whenever(clearNotification.canShow()).thenReturn(true )
192+ testee.scheduleNextNotification()
193+
194+ assertNotificationScheduled(ClearDataNotificationWorker ::class .jvmName)
195+ }
196+
197+ @Test
198+ fun whenInAppUsageControlVariantAndPrivacyNotificationAndClearNotificationCannotShowThenNoNotificationScheduled () = runBlocking<Unit > {
199+ setDefaultVariant()
200+ whenever(privacyNotification.canShow()).thenReturn(false )
201+ whenever(clearNotification.canShow()).thenReturn(false )
202+ testee.scheduleNextNotification()
203+
204+ assertNoNotificationScheduled()
205+ }
206+
207+ private suspend fun givenNoInactiveUserNotifications () {
208+ whenever(privacyNotification.canShow()).thenReturn(false )
209+ whenever(clearNotification.canShow()).thenReturn(false )
210+ }
211+
212+ private fun setInAppUsageVariant () {
213+ whenever(variantManager.getVariant()).thenReturn(
214+ Variant (
215+ " test" ,
216+ features = listOf (
217+ VariantManager .VariantFeature .InAppUsage ,
218+ VariantManager .VariantFeature .RemoveDay1AndDay3Notifications ,
219+ VariantManager .VariantFeature .KillOnboarding
220+ ),
221+ filterBy = { true }
222+ )
223+ )
224+ }
225+
226+ private fun setInAppUsageSecondControlVariant () {
227+ whenever(variantManager.getVariant()).thenReturn(
228+ Variant (
229+ " test" ,
230+ features = listOf (
231+ VariantManager .VariantFeature .RemoveDay1AndDay3Notifications ,
232+ VariantManager .VariantFeature .KillOnboarding
233+ ),
234+ filterBy = { true }
235+ )
236+ )
237+ }
238+
239+ private fun setInAppUsageControlVariant () {
240+ whenever(variantManager.getVariant()).thenReturn(Variant (" test" , features = emptyList(), filterBy = { true }))
241+ }
242+
243+ private fun setDefaultVariant () {
244+ whenever(variantManager.getVariant()).thenReturn(DEFAULT_VARIANT )
245+ }
246+
113247 private fun assertNotificationScheduled (workerName : String , tag : String = NotificationScheduler .UNUSED_APP_WORK_REQUEST_TAG ) {
114248 assertTrue(getScheduledWorkers(tag).any { it.tags.contains(workerName) })
115249 }
0 commit comments