1919package com.duckduckgo.app.notification
2020
2121import androidx.test.platform.app.InstrumentationRegistry
22+ import androidx.work.OneTimeWorkRequestBuilder
2223import androidx.work.WorkInfo
2324import androidx.work.WorkManager
2425import com.duckduckgo.app.CoroutineTestRule
25- import com.duckduckgo.app.notification.NotificationScheduler.*
26+ import com.duckduckgo.app.notification.NotificationScheduler.ClearDataNotificationWorker
27+ import com.duckduckgo.app.notification.NotificationScheduler.PrivacyNotificationWorker
2628import com.duckduckgo.app.notification.model.SchedulableNotification
27- import com.duckduckgo.app.notification.model.SearchNotification
2829import com.duckduckgo.app.statistics.VariantManager
2930import com.duckduckgo.app.statistics.VariantManager.Companion.DEFAULT_VARIANT
3031import com.nhaarman.mockitokotlin2.any
3132import com.nhaarman.mockitokotlin2.mock
3233import com.nhaarman.mockitokotlin2.whenever
3334import kotlinx.coroutines.ExperimentalCoroutinesApi
3435import kotlinx.coroutines.runBlocking
35- import org.junit.After
3636import org.junit.Assert.assertTrue
3737import org.junit.Before
3838import org.junit.Rule
@@ -48,7 +48,6 @@ class AndroidNotificationSchedulerTest {
4848 private val variantManager: VariantManager = mock()
4949 private val clearNotification: SchedulableNotification = mock()
5050 private val privacyNotification: SchedulableNotification = mock()
51- private val searchPromptNotification: SearchNotification = mock()
5251
5352 private val context = InstrumentationRegistry .getInstrumentation().targetContext
5453 private var workManager = WorkManager .getInstance(context)
@@ -60,135 +59,81 @@ class AndroidNotificationSchedulerTest {
6059 testee = NotificationScheduler (
6160 workManager,
6261 clearNotification,
63- privacyNotification,
64- searchPromptNotification
62+ privacyNotification
6563 )
6664 }
6765
68- @After
69- fun resetWorkers () {
70- workManager.cancelAllWorkByTag(NotificationScheduler .CONTINUOUS_APP_USE_REQUEST_TAG )
71- }
72-
73- @Test
74- fun whenPrivacyNotificationClearDataAndSearchPromptCanShowThenBothAreScheduled () = runBlocking<Unit > {
75- whenever(privacyNotification.canShow()).thenReturn(true )
76- whenever(clearNotification.canShow()).thenReturn(true )
77- whenever(searchPromptNotification.canShow()).thenReturn(true )
78- testee.scheduleNextNotification()
79-
80- assertUnusedAppNotificationScheduled(PrivacyNotificationWorker ::class .jvmName)
81- assertContinuousAppUseNotificationScheduled(SearchPromptNotificationWorker ::class .jvmName)
82- }
83-
8466 @Test
85- fun whenPrivacyNotificationClearDataAndSearchPromptCanShowThenPrivacyNotificationScheduled () = runBlocking<Unit > {
67+ fun whenPrivacyNotificationClearDataCanShowThenPrivacyNotificationIsScheduled () = runBlocking<Unit > {
8668 whenever(privacyNotification.canShow()).thenReturn(true )
8769 whenever(clearNotification.canShow()).thenReturn(true )
88- whenever(searchPromptNotification.canShow()).thenReturn(false )
89- testee.scheduleNextNotification()
90-
91- assertUnusedAppNotificationScheduled(PrivacyNotificationWorker ::class .jvmName)
92- assertNoContinuousAppNotificationScheduled()
93- }
94-
95- @Test
96- fun whenPrivacyNotificationAndSearchPromptCanShowButClearDataCannotThenThenBothAreScheduled () = runBlocking<Unit > {
97- whenever(privacyNotification.canShow()).thenReturn(true )
98- whenever(clearNotification.canShow()).thenReturn(false )
99- whenever(searchPromptNotification.canShow()).thenReturn(true )
10070 testee.scheduleNextNotification()
10171
10272 assertUnusedAppNotificationScheduled(PrivacyNotificationWorker ::class .jvmName)
103- assertContinuousAppUseNotificationScheduled(SearchPromptNotificationWorker ::class .jvmName)
10473 }
10574
10675 @Test
107- fun whenPrivacyNotificationCanShowButClearDataAndSearchPromptCannotThenPrivacyNotificationScheduled () = runBlocking<Unit > {
76+ fun whenPrivacyNotificationCanShowButClearDataCannotThenPrivacyNotificationIsScheduled () = runBlocking<Unit > {
10877 whenever(privacyNotification.canShow()).thenReturn(true )
10978 whenever(clearNotification.canShow()).thenReturn(false )
110- whenever(searchPromptNotification.canShow()).thenReturn(false )
11179 testee.scheduleNextNotification()
11280
11381 assertUnusedAppNotificationScheduled(PrivacyNotificationWorker ::class .jvmName)
114- assertNoContinuousAppNotificationScheduled()
11582 }
11683
11784 @Test
118- fun whenPrivacyNotificationAndSearchPromptCannotShowAndClearNotificationCanShowThenBothAreScheduled () = runBlocking<Unit > {
85+ fun whenPrivacyNotificationCannotShowAndClearNotificationCanShowThenClearNotificationIsScheduled () = runBlocking<Unit > {
11986 whenever(privacyNotification.canShow()).thenReturn(false )
12087 whenever(clearNotification.canShow()).thenReturn(true )
121- whenever(searchPromptNotification.canShow()).thenReturn(true )
12288 testee.scheduleNextNotification()
12389
12490 assertUnusedAppNotificationScheduled(ClearDataNotificationWorker ::class .jvmName)
125- assertContinuousAppUseNotificationScheduled(SearchPromptNotificationWorker ::class .jvmName)
12691 }
12792
12893 @Test
129- fun whenPrivacyNotificationAndClearNotificationCannotShowButSearchPromptCanShowThenNotificationScheduled () = runBlocking<Unit > {
94+ fun whenPrivacyNotificationAndClearNotificationCannotShowThenNoNotificationScheduled () = runBlocking<Unit > {
13095 whenever(privacyNotification.canShow()).thenReturn(false )
13196 whenever(clearNotification.canShow()).thenReturn(false )
132- whenever(searchPromptNotification.canShow()).thenReturn(true )
13397 testee.scheduleNextNotification()
13498
135- assertContinuousAppUseNotificationScheduled(SearchPromptNotificationWorker ::class .jvmName)
13699 assertNoUnusedAppNotificationScheduled()
137100 }
138101
139102 @Test
140- fun whenPrivacyNotificationAndClearNotificationCannotShowButSearchPromptCanThenSearchPromptNotificationScheduled () = runBlocking<Unit > {
103+ fun whenNotificationIsScheduledOldJobsAreCancelled () = runBlocking<Unit > {
141104 whenever(privacyNotification.canShow()).thenReturn(false )
142105 whenever(clearNotification.canShow()).thenReturn(false )
143- whenever(searchPromptNotification.canShow()).thenReturn(true )
106+
107+ enqueueDeprecatedJobs()
144108
145109 testee.scheduleNextNotification()
146110
147- assertContinuousAppUseNotificationScheduled(SearchPromptNotificationWorker ::class .jvmName)
148- assertNoUnusedAppNotificationScheduled()
111+ NotificationScheduler .allDeprecatedNotificationWorkTags().forEach {
112+ assertTrue(getScheduledWorkers(it).isEmpty())
113+ }
149114 }
150115
151- @Test
152- fun whenNoNotificationCanShowThenNoNotificationScheduled () = runBlocking<Unit > {
153- whenever(privacyNotification.canShow()).thenReturn(false )
154- whenever(clearNotification.canShow()).thenReturn(false )
155- whenever(searchPromptNotification.canShow()).thenReturn(false )
156- testee.scheduleNextNotification()
116+ private fun enqueueDeprecatedJobs () {
117+ NotificationScheduler .allDeprecatedNotificationWorkTags().forEach {
118+ val request = OneTimeWorkRequestBuilder <PrivacyNotificationWorker >()
119+ .addTag(it)
120+ .build()
157121
158- assertNoNotificationScheduled()
122+ workManager.enqueue(request)
123+ }
159124 }
160125
161126 private fun assertUnusedAppNotificationScheduled (workerName : String ) {
162- assertTrue(getUnusedAppScheduledWorkers().any { it.tags.contains(workerName) })
163- }
164-
165- private fun assertContinuousAppUseNotificationScheduled (workerName : String ) {
166- assertTrue(getContinuousAppUseScheduledWorkers().any { it.tags.contains(workerName) })
127+ assertTrue(getScheduledWorkers(NotificationScheduler .UNUSED_APP_WORK_REQUEST_TAG ).any { it.tags.contains(workerName) })
167128 }
168129
169130 private fun assertNoUnusedAppNotificationScheduled () {
170- assertTrue(getUnusedAppScheduledWorkers().isEmpty())
171- }
172-
173- private fun assertNoContinuousAppNotificationScheduled () {
174- assertTrue(getContinuousAppUseScheduledWorkers().isEmpty())
175- }
176-
177- private fun assertNoNotificationScheduled () {
178- assertTrue(getUnusedAppScheduledWorkers().isEmpty())
179- assertTrue(getContinuousAppUseScheduledWorkers().isEmpty())
180- }
181-
182- private fun getUnusedAppScheduledWorkers (): List <WorkInfo > {
183- return workManager
184- .getWorkInfosByTag(NotificationScheduler .UNUSED_APP_WORK_REQUEST_TAG )
185- .get()
186- .filter { it.state == WorkInfo .State .ENQUEUED }
131+ assertTrue(getScheduledWorkers(NotificationScheduler .UNUSED_APP_WORK_REQUEST_TAG ).isEmpty())
187132 }
188133
189- private fun getContinuousAppUseScheduledWorkers ( ): List <WorkInfo > {
134+ private fun getScheduledWorkers ( tag : String ): List <WorkInfo > {
190135 return workManager
191- .getWorkInfosByTag(NotificationScheduler . CONTINUOUS_APP_USE_REQUEST_TAG )
136+ .getWorkInfosByTag(tag )
192137 .get()
193138 .filter { it.state == WorkInfo .State .ENQUEUED }
194139 }
0 commit comments