@@ -15,20 +15,26 @@ import com.duckduckgo.autofill.impl.pixel.AutofillPixelNames.AUTOFILL_SERVICE_DI
15
15
import com.duckduckgo.autofill.impl.pixel.AutofillPixelNames.AUTOFILL_SERVICE_ENABLED_DAU
16
16
import com.duckduckgo.autofill.impl.pixel.AutofillPixelNames.AUTOFILL_TOGGLED_OFF_SEARCH
17
17
import com.duckduckgo.autofill.impl.pixel.AutofillPixelNames.AUTOFILL_TOGGLED_ON_SEARCH
18
+ import com.duckduckgo.autofill.impl.pixel.AutofillPixelParameters.LAST_USED_PIXEL_KEY
18
19
import com.duckduckgo.autofill.impl.securestorage.SecureStorage
19
20
import com.duckduckgo.autofill.impl.service.store.AutofillServiceStore
20
21
import com.duckduckgo.autofill.impl.store.InternalAutofillStore
22
+ import com.duckduckgo.autofill.store.AutofillPrefsStore
21
23
import com.duckduckgo.autofill.store.engagement.AutofillEngagementDatabase
22
24
import com.duckduckgo.common.test.CoroutineTestRule
25
+ import java.time.format.DateTimeFormatter
23
26
import kotlinx.coroutines.flow.flowOf
24
27
import kotlinx.coroutines.test.runTest
28
+ import org.junit.Assert.assertEquals
25
29
import org.junit.Assert.assertFalse
26
30
import org.junit.Assert.assertTrue
27
31
import org.junit.Before
28
32
import org.junit.Rule
29
33
import org.junit.Test
30
34
import org.junit.runner.RunWith
35
+ import org.mockito.kotlin.eq
31
36
import org.mockito.kotlin.mock
37
+ import org.mockito.kotlin.verify
32
38
import org.mockito.kotlin.whenever
33
39
34
40
@RunWith(AndroidJUnit4 ::class )
@@ -48,6 +54,7 @@ class DefaultAutofillEngagementRepositoryTest {
48
54
private val autofillServiceStore: AutofillServiceStore = FakeAutofillServiceStore ()
49
55
private val secureStorage: SecureStorage = mock()
50
56
private val deviceAuthenticator: DeviceAuthenticator = mock()
57
+ private val autofillPrefsStore: AutofillPrefsStore = mock()
51
58
52
59
@Before
53
60
fun setup () {
@@ -67,8 +74,15 @@ class DefaultAutofillEngagementRepositoryTest {
67
74
secureStorage = secureStorage,
68
75
deviceAuthenticator = deviceAuthenticator,
69
76
autofillServiceStore = autofillServiceStore,
77
+ autofillPrefsStore = autofillPrefsStore,
70
78
)
71
79
80
+ @Test
81
+ fun whenAutofilledThenLastUsedDateIsUpdated () = runTest {
82
+ testee.recordAutofilledToday()
83
+ verify(autofillPrefsStore).dataLastAutofilledDate = eq(todayString())
84
+ }
85
+
72
86
@Test
73
87
fun whenAutofilledButNotSearchedThenActiveUserPixelNotSent () = runTest {
74
88
testee.recordAutofilledToday()
@@ -88,6 +102,28 @@ class DefaultAutofillEngagementRepositoryTest {
88
102
AUTOFILL_ENGAGEMENT_ACTIVE_USER .verifySent()
89
103
}
90
104
105
+ @Test
106
+ fun whenActiveUserPixelSentWithNoLastUsedThenOmittedFromParameters () = runTest {
107
+ whenever(autofillPrefsStore.dataLastAutofilledDate).thenReturn(null )
108
+ testee.recordSearchedToday()
109
+ testee.recordAutofilledToday()
110
+
111
+ val pixelParams = AUTOFILL_ENGAGEMENT_ACTIVE_USER .getParametersForFiredPixel()!!
112
+ assertFalse(pixelParams.contains(LAST_USED_PIXEL_KEY ))
113
+ }
114
+
115
+ @Test
116
+ fun whenActiveUserPixelSentWithLastUsedThenIncludedInParameters () = runTest {
117
+ val lastUsedDate = yesterdayString()
118
+ whenever(autofillPrefsStore.dataLastAutofilledDate).thenReturn(lastUsedDate)
119
+
120
+ testee.recordSearchedToday()
121
+ testee.recordAutofilledToday()
122
+
123
+ val pixelParams = AUTOFILL_ENGAGEMENT_ACTIVE_USER .getParametersForFiredPixel()!!
124
+ assertEquals(lastUsedDate, pixelParams[LAST_USED_PIXEL_KEY ])
125
+ }
126
+
91
127
@Test
92
128
fun whenSearchedAutofillEnabledAndMoreThan9PasswordsThenEnabledUserPixelSent () = runTest {
93
129
givenUserHasPasswords(10 )
@@ -175,17 +211,31 @@ class DefaultAutofillEngagementRepositoryTest {
175
211
assertFalse(pixel.firedPixels.contains(pixelName))
176
212
}
177
213
214
+ private fun AutofillPixelNames.getParametersForFiredPixel (): Map <String , String >? = pixel.firedPixels[this .pixelName]
215
+
216
+ private fun yesterdayString (): String {
217
+ return DATE_FORMATTER .format(java.time.LocalDate .now().minusDays(1 ))
218
+ }
219
+
220
+ private fun todayString (): String {
221
+ return DATE_FORMATTER .format(java.time.LocalDate .now())
222
+ }
223
+
224
+ companion object {
225
+ private val DATE_FORMATTER = DateTimeFormatter .ofPattern(" yyyy-MM-dd" )
226
+ }
227
+
178
228
private class FakePixel : Pixel {
179
229
180
- val firedPixels = mutableListOf <String >()
230
+ val firedPixels = mutableMapOf <String , Map < String , String > >()
181
231
182
232
override fun fire (
183
233
pixel : PixelName ,
184
234
parameters : Map <String , String >,
185
235
encodedParameters : Map <String , String >,
186
236
type : PixelType ,
187
237
) {
188
- firedPixels.add( pixel.pixelName)
238
+ firedPixels[ pixel.pixelName] = parameters
189
239
}
190
240
191
241
override fun fire (
@@ -194,23 +244,23 @@ class DefaultAutofillEngagementRepositoryTest {
194
244
encodedParameters : Map <String , String >,
195
245
type : PixelType ,
196
246
) {
197
- firedPixels.add( pixelName)
247
+ firedPixels[ pixelName] = parameters
198
248
}
199
249
200
250
override fun enqueueFire (
201
251
pixel : PixelName ,
202
252
parameters : Map <String , String >,
203
253
encodedParameters : Map <String , String >,
204
254
) {
205
- firedPixels.add( pixel.pixelName)
255
+ firedPixels[ pixel.pixelName] = parameters
206
256
}
207
257
208
258
override fun enqueueFire (
209
259
pixelName : String ,
210
260
parameters : Map <String , String >,
211
261
encodedParameters : Map <String , String >,
212
262
) {
213
- firedPixels.add( pixelName)
263
+ firedPixels[ pixelName] = parameters
214
264
}
215
265
}
216
266
}
0 commit comments