@@ -39,6 +39,7 @@ import com.duckduckgo.sync.store.SyncUnavailableSharedPrefsStore
39
39
import java.time.LocalDateTime
40
40
import java.time.OffsetDateTime
41
41
import java.time.format.DateTimeFormatter
42
+ import kotlinx.coroutines.test.runTest
42
43
import org.junit.After
43
44
import org.junit.Assert.*
44
45
import org.junit.Before
@@ -60,6 +61,9 @@ class RealSyncUnavailableRepositoryTest {
60
61
private val syncNotificationBuilder = FakeNotificationBuilder ()
61
62
private val syncUnavailableStore = SyncUnavailableSharedPrefsStore (
62
63
sharedPrefsProv = TestSharedPrefsProvider (context),
64
+ appCoroutineScope = coroutineRule.testScope,
65
+ dispatcherProvider = coroutineRule.testDispatcherProvider,
66
+ createAsyncPreferences = true ,
63
67
)
64
68
private lateinit var workManager: WorkManager
65
69
private lateinit var testee: RealSyncUnavailableRepository
@@ -74,6 +78,7 @@ class RealSyncUnavailableRepositoryTest {
74
78
notificationManager,
75
79
syncNotificationBuilder,
76
80
workManager,
81
+ coroutineRule.testScope,
77
82
)
78
83
}
79
84
@@ -83,71 +88,71 @@ class RealSyncUnavailableRepositoryTest {
83
88
}
84
89
85
90
@Test
86
- fun whenServerBecomesAvailableThenSyncAvailable () {
87
- syncUnavailableStore.isSyncUnavailable = true
91
+ fun whenServerBecomesAvailableThenSyncAvailable () = runTest {
92
+ syncUnavailableStore.setSyncUnavailable( true )
88
93
testee.onServerAvailable()
89
- assertFalse(syncUnavailableStore.isSyncUnavailable)
90
- assertEquals(" " , syncUnavailableStore.syncUnavailableSince )
94
+ assertFalse(syncUnavailableStore.isSyncUnavailable() )
95
+ assertEquals(" " , syncUnavailableStore.getSyncUnavailableSince() )
91
96
}
92
97
93
98
@Test
94
- fun whenServerUnavailableThenSyncUnavailable () {
99
+ fun whenServerUnavailableThenSyncUnavailable () = runTest {
95
100
testee.onServerUnavailable()
96
- assertTrue(syncUnavailableStore.isSyncUnavailable)
101
+ assertTrue(syncUnavailableStore.isSyncUnavailable() )
97
102
}
98
103
99
104
@Test
100
- fun whenServerUnavailableThenUpdateTimestampOnce () {
105
+ fun whenServerUnavailableThenUpdateTimestampOnce () = runTest {
101
106
testee.onServerUnavailable()
102
- val unavailableSince = syncUnavailableStore.syncUnavailableSince
107
+ val unavailableSince = syncUnavailableStore.getSyncUnavailableSince()
103
108
assertTrue(unavailableSince.isNotEmpty())
104
109
testee.onServerUnavailable()
105
- assertEquals(unavailableSince, syncUnavailableStore.syncUnavailableSince )
110
+ assertEquals(unavailableSince, syncUnavailableStore.getSyncUnavailableSince() )
106
111
}
107
112
108
113
@Test
109
- fun whenServerUnavailableThenUpdateCounter () {
114
+ fun whenServerUnavailableThenUpdateCounter () = runTest {
110
115
testee.onServerUnavailable()
111
- assertTrue(syncUnavailableStore.isSyncUnavailable)
112
- assertEquals(1 , syncUnavailableStore.syncErrorCount )
116
+ assertTrue(syncUnavailableStore.isSyncUnavailable() )
117
+ assertEquals(1 , syncUnavailableStore.getSyncErrorCount() )
113
118
testee.onServerUnavailable()
114
119
testee.onServerUnavailable()
115
120
testee.onServerUnavailable()
116
- assertEquals(4 , syncUnavailableStore.syncErrorCount )
121
+ assertEquals(4 , syncUnavailableStore.getSyncErrorCount() )
117
122
}
118
123
119
124
@Test
120
- fun whenServerUnavailableThenScheduleNotification () {
125
+ fun whenServerUnavailableThenScheduleNotification () = runTest {
121
126
testee.onServerUnavailable()
122
- assertTrue(syncUnavailableStore.isSyncUnavailable)
127
+ assertTrue(syncUnavailableStore.isSyncUnavailable() )
123
128
val syncErrorNotification = workManager.getWorkInfosByTag(SYNC_ERROR_NOTIFICATION_TAG ).get()
124
129
assertEquals(1 , syncErrorNotification.size)
125
130
}
126
131
127
132
@Test
128
- fun whenErrorCounterReachesThresholdThenTriggerNotification () {
129
- syncUnavailableStore.syncErrorCount = RealSyncUnavailableRepository .ERROR_THRESHOLD_NOTIFICATION_COUNT
133
+ fun whenErrorCounterReachesThresholdThenTriggerNotification () = runTest {
134
+ syncUnavailableStore.setSyncErrorCount( RealSyncUnavailableRepository .ERROR_THRESHOLD_NOTIFICATION_COUNT )
130
135
testee.onServerUnavailable()
131
136
notificationManager.activeNotifications
132
137
.find { it.id == SYNC_ERROR_NOTIFICATION_ID } ? : fail(" Notification not found" )
133
138
}
134
139
135
140
@Test
136
- fun whenUserNotifiedTodayThenDoNotTriggerNotification () {
137
- syncUnavailableStore.userNotifiedAt = OffsetDateTime .now().format(DateTimeFormatter .ISO_LOCAL_DATE_TIME )
141
+ fun whenUserNotifiedTodayThenDoNotTriggerNotification () = runTest {
142
+ syncUnavailableStore.setUserNotifiedAt( OffsetDateTime .now().format(DateTimeFormatter .ISO_LOCAL_DATE_TIME ) )
138
143
testee.triggerNotification()
139
144
assertNull(notificationManager.activeNotifications.find { it.id == SYNC_ERROR_NOTIFICATION_ID })
140
145
}
141
146
142
147
@Test
143
- fun whenUserNotifiedYesterdayThenTriggerNotificationAndUpdateNotificationTimestamp () {
144
- syncUnavailableStore.userNotifiedAt = OffsetDateTime .now().minusDays(1 ).format(DateTimeFormatter .ISO_LOCAL_DATE_TIME )
148
+ fun whenUserNotifiedYesterdayThenTriggerNotificationAndUpdateNotificationTimestamp () = runTest {
149
+ syncUnavailableStore.setUserNotifiedAt( OffsetDateTime .now().minusDays(1 ).format(DateTimeFormatter .ISO_LOCAL_DATE_TIME ) )
145
150
testee.triggerNotification()
146
151
notificationManager.activeNotifications
147
152
.find { it.id == SYNC_ERROR_NOTIFICATION_ID } ? : fail(" Notification not found" )
148
153
149
154
val today = LocalDateTime .now().toLocalDate()
150
- val lastNotification = LocalDateTime .parse(syncUnavailableStore.userNotifiedAt , DateTimeFormatter .ISO_LOCAL_DATE_TIME ).toLocalDate()
155
+ val lastNotification = LocalDateTime .parse(syncUnavailableStore.getUserNotifiedAt() , DateTimeFormatter .ISO_LOCAL_DATE_TIME ).toLocalDate()
151
156
assertEquals(today, lastNotification)
152
157
}
153
158
@@ -162,8 +167,8 @@ class RealSyncUnavailableRepositoryTest {
162
167
}
163
168
164
169
@Test
165
- fun whenServerAvailableThenClearNotification () {
166
- syncUnavailableStore.syncErrorCount = RealSyncUnavailableRepository .ERROR_THRESHOLD_NOTIFICATION_COUNT
170
+ fun whenServerAvailableThenClearNotification () = runTest {
171
+ syncUnavailableStore.setSyncErrorCount( RealSyncUnavailableRepository .ERROR_THRESHOLD_NOTIFICATION_COUNT )
167
172
testee.onServerUnavailable()
168
173
notificationManager.activeNotifications
169
174
.find { it.id == SYNC_ERROR_NOTIFICATION_ID } ? : fail(" Notification not found" )
0 commit comments