Skip to content

Commit 8a30a96

Browse files
authored
Feature/david/remove sticky search deprecated jobs (#800)
* we want to make sure that old work tags are cancelled * this could be private * better naming for this method * adding deprecated jobs before launching the test so it actually tests what's supposed to do
1 parent f2c16b7 commit 8a30a96

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

app/src/androidTest/java/com/duckduckgo/app/notification/AndroidNotificationSchedulerTest.kt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
package com.duckduckgo.app.notification
2020

2121
import androidx.test.platform.app.InstrumentationRegistry
22+
import androidx.work.OneTimeWorkRequestBuilder
2223
import androidx.work.WorkInfo
2324
import androidx.work.WorkManager
2425
import 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
2628
import com.duckduckgo.app.notification.model.SchedulableNotification
2729
import com.duckduckgo.app.statistics.VariantManager
2830
import com.duckduckgo.app.statistics.VariantManager.Companion.DEFAULT_VARIANT
@@ -31,7 +33,6 @@ import com.nhaarman.mockitokotlin2.mock
3133
import com.nhaarman.mockitokotlin2.whenever
3234
import kotlinx.coroutines.ExperimentalCoroutinesApi
3335
import kotlinx.coroutines.runBlocking
34-
import org.junit.After
3536
import org.junit.Assert.assertTrue
3637
import org.junit.Before
3738
import 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
}

app/src/main/java/com/duckduckgo/app/notification/AndroidNotificationScheduler.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,17 @@ class NotificationScheduler(
4646
) : AndroidNotificationScheduler {
4747

4848
override suspend fun scheduleNextNotification() {
49+
cancelAllUnnecessaryWork()
4950
scheduleInactiveUserNotifications()
5051
}
5152

53+
private fun cancelAllUnnecessaryWork(){
54+
allDeprecatedNotificationWorkTags().forEach { tag ->
55+
workManager.cancelAllWorkByTag(tag)
56+
}
57+
58+
}
59+
5260
private suspend fun scheduleInactiveUserNotifications() {
5361
workManager.cancelAllWorkByTag(UNUSED_APP_WORK_REQUEST_TAG)
5462

@@ -109,5 +117,11 @@ class NotificationScheduler(
109117

110118
companion object {
111119
const val UNUSED_APP_WORK_REQUEST_TAG = "com.duckduckgo.notification.schedule"
120+
121+
// below there is a list of TAGs that were used at some point but that are no longer active
122+
// we want to make sure that this TAGs are cancelled to avoid inconsistencies
123+
private const val CONTINUOUS_APP_USE_REQUEST_TAG = "com.duckduckgo.notification.schedule.continuous" // Sticky Search Experiment
124+
125+
fun allDeprecatedNotificationWorkTags() = listOf(CONTINUOUS_APP_USE_REQUEST_TAG)
112126
}
113127
}

0 commit comments

Comments
 (0)