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
2729import com.duckduckgo.app.statistics.VariantManager
2830import com.duckduckgo.app.statistics.VariantManager.Companion.DEFAULT_VARIANT
@@ -31,7 +33,6 @@ import com.nhaarman.mockitokotlin2.mock
3133import com.nhaarman.mockitokotlin2.whenever
3234import kotlinx.coroutines.ExperimentalCoroutinesApi
3335import kotlinx.coroutines.runBlocking
34- import org.junit.After
3536import org.junit.Assert.assertTrue
3637import org.junit.Before
3738import org.junit.Rule
@@ -98,19 +99,42 @@ class AndroidNotificationSchedulerTest {
9899 assertNoUnusedAppNotificationScheduled()
99100 }
100101
102+ @Test
103+ fun whenNotificationIsScheduledOldJobsAreCancelled () = runBlocking<Unit > {
104+ whenever(privacyNotification.canShow()).thenReturn(false )
105+ whenever(clearNotification.canShow()).thenReturn(false )
106+
107+ enqueueDeprecatedJobs()
108+
109+ testee.scheduleNextNotification()
110+
111+ NotificationScheduler .allDeprecatedNotificationWorkTags().forEach {
112+ assertTrue(getScheduledWorkers(it).isEmpty())
113+ }
114+ }
115+
116+ private fun enqueueDeprecatedJobs () {
117+ NotificationScheduler .allDeprecatedNotificationWorkTags().forEach {
118+ val request = OneTimeWorkRequestBuilder <PrivacyNotificationWorker >()
119+ .addTag(it)
120+ .build()
121+
122+ workManager.enqueue(request)
123+ }
124+ }
125+
101126 private fun assertUnusedAppNotificationScheduled (workerName : String ) {
102- assertTrue(getUnusedAppScheduledWorkers( ).any { it.tags.contains(workerName) })
127+ assertTrue(getScheduledWorkers( NotificationScheduler . UNUSED_APP_WORK_REQUEST_TAG ).any { it.tags.contains(workerName) })
103128 }
104129
105130 private fun assertNoUnusedAppNotificationScheduled () {
106- assertTrue(getUnusedAppScheduledWorkers( ).isEmpty())
131+ assertTrue(getScheduledWorkers( NotificationScheduler . UNUSED_APP_WORK_REQUEST_TAG ).isEmpty())
107132 }
108133
109- private fun getUnusedAppScheduledWorkers ( ): List <WorkInfo > {
134+ private fun getScheduledWorkers ( tag : String ): List <WorkInfo > {
110135 return workManager
111- .getWorkInfosByTag(NotificationScheduler . UNUSED_APP_WORK_REQUEST_TAG )
136+ .getWorkInfosByTag(tag )
112137 .get()
113138 .filter { it.state == WorkInfo .State .ENQUEUED }
114139 }
115-
116140}
0 commit comments