forked from mozilla-mobile/firefox-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NimbusMessagingStorageTest.kt
809 lines (668 loc) · 27.9 KB
/
NimbusMessagingStorageTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.service.nimbus.messaging
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import mozilla.components.service.nimbus.messaging.ControlMessageBehavior.SHOW_NEXT_MESSAGE
import mozilla.components.support.test.any
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.never
import org.mockito.Mockito.spy
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.mozilla.experiments.nimbus.FeaturesInterface
import org.mozilla.experiments.nimbus.GleanPlumbInterface
import org.mozilla.experiments.nimbus.GleanPlumbMessageHelper
import org.mozilla.experiments.nimbus.NullVariables
import org.mozilla.experiments.nimbus.Res
import org.mozilla.experiments.nimbus.internal.FeatureHolder
import org.mozilla.experiments.nimbus.internal.NimbusException
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
@kotlinx.coroutines.ExperimentalCoroutinesApi
class NimbusMessagingStorageTest {
@Mock private lateinit var metadataStorage: MessageMetadataStorage
@Mock private lateinit var gleanPlumb: GleanPlumbInterface
private lateinit var storage: NimbusMessagingStorage
private lateinit var messagingFeature: FeatureHolder<Messaging>
private var malformedWasReported = false
private var malformedMessageIds = mutableSetOf<String>()
private val reportMalformedMessage: (String) -> Unit = {
malformedMessageIds.add(it)
malformedWasReported = true
}
private lateinit var featuresInterface: FeaturesInterface
@get:Rule
val coroutinesTestRule = MainCoroutineRule()
@Before
fun setup() {
MockitoAnnotations.openMocks(this)
NullVariables.instance.setContext(testContext)
val (messagingFeatureTmp, featuresInterfaceTmp) = createMessagingFeature()
messagingFeature = spy(messagingFeatureTmp)
featuresInterface = featuresInterfaceTmp
runBlocking {
`when`(metadataStorage.getMetadata())
.thenReturn(mapOf("message-1" to Message.Metadata(id = "message-1")))
`when`(metadataStorage.addMetadata(any()))
.thenReturn(mock())
}
storage = NimbusMessagingStorage(
testContext,
metadataStorage,
reportMalformedMessage,
gleanPlumb,
messagingFeature,
)
}
@After
fun tearDown() {
malformedWasReported = false
malformedMessageIds.clear()
}
@Test
fun `WHEN calling getCoenrollingFeatureIds THEN messaging is in that list`() {
assertTrue(FxNimbusMessaging.getCoenrollingFeatureIds().contains(MESSAGING_FEATURE_ID))
}
@Test
fun `WHEN calling getMessages THEN provide a list of available messages for a given surface`() =
runTest {
val homescreenMessage = storage.getMessages().first()
assertEquals("message-1", homescreenMessage.id)
assertEquals("message-1", homescreenMessage.metadata.id)
val notificationMessage = storage.getMessages().last()
assertEquals("message-2", notificationMessage.id)
}
@Test
fun `WHEN calling getMessages THEN provide a list of sorted messages by priority`() =
runTest {
val messages = mapOf(
"low-message" to createMessageData(style = "low-priority"),
"high-message" to createMessageData(style = "high-priority"),
"medium-message" to createMessageData(style = "medium-priority"),
)
val styles = mapOf(
"high-priority" to createStyle(priority = 100),
"medium-priority" to createStyle(priority = 50),
"low-priority" to createStyle(priority = 1),
)
val (messagingFeature, _) = createMessagingFeature(
styles = styles,
messages = messages,
)
val storage = NimbusMessagingStorage(
testContext,
metadataStorage,
reportMalformedMessage,
gleanPlumb,
messagingFeature,
)
val results = storage.getMessages()
assertEquals("high-message", results[0].id)
assertEquals("medium-message", results[1].id)
assertEquals("low-message", results[2].id)
}
@Test
fun `GIVEN pressed message WHEN calling getMessages THEN filter out the pressed message`() =
runTest {
val metadataList = mapOf(
"pressed-message" to Message.Metadata(id = "pressed-message", pressed = true),
"normal-message" to Message.Metadata(id = "normal-message", pressed = false),
)
val messages = mapOf(
"pressed-message" to createMessageData(style = "high-priority"),
"normal-message" to createMessageData(style = "high-priority"),
)
val styles = mapOf(
"high-priority" to createStyle(priority = 100),
)
val metadataStorage: MessageMetadataStorage = mock()
val (messagingFeature, _) = createMessagingFeature(
styles = styles,
messages = messages,
)
`when`(metadataStorage.getMetadata()).thenReturn(metadataList)
val storage = NimbusMessagingStorage(
testContext,
metadataStorage,
reportMalformedMessage,
gleanPlumb,
messagingFeature,
)
val results = storage.getMessages()
assertEquals(1, results.size)
assertEquals("normal-message", results[0].id)
}
@Test
fun `GIVEN dismissed message WHEN calling getMessages THEN filter out the dismissed message`() =
runTest {
val metadataList = mapOf(
"dismissed-message" to Message.Metadata(id = "dismissed-message", dismissed = true),
"normal-message" to Message.Metadata(id = "normal-message", dismissed = false),
)
val messages = mapOf(
"dismissed-message" to createMessageData(style = "high-priority"),
"normal-message" to createMessageData(style = "high-priority"),
)
val styles = mapOf(
"high-priority" to createStyle(priority = 100),
)
val metadataStorage: MessageMetadataStorage = mock()
val (messagingFeature, _) = createMessagingFeature(
styles = styles,
messages = messages,
)
`when`(metadataStorage.getMetadata()).thenReturn(metadataList)
val storage = NimbusMessagingStorage(
testContext,
metadataStorage,
reportMalformedMessage,
gleanPlumb,
messagingFeature,
)
val results = storage.getMessages()
assertEquals(1, results.size)
assertEquals("normal-message", results[0].id)
}
@Test
fun `GIVEN a message that the maxDisplayCount WHEN calling getMessages THEN filter out the message`() =
runTest {
val metadataList = mapOf(
"shown-many-times-message" to Message.Metadata(
id = "shown-many-times-message",
displayCount = 10,
),
"shown-two-times-message" to Message.Metadata(
id = "shown-two-times-message",
displayCount = 2,
),
"normal-message" to Message.Metadata(id = "normal-message", displayCount = 0),
)
val messages = mapOf(
"shown-many-times-message" to createMessageData(
style = "high-priority",
),
"shown-two-times-message" to createMessageData(
style = "high-priority",
),
"normal-message" to createMessageData(style = "high-priority"),
)
val styles = mapOf(
"high-priority" to createStyle(priority = 100, maxDisplayCount = 2),
)
val metadataStorage: MessageMetadataStorage = mock()
val (messagingFeature, _) = createMessagingFeature(
styles = styles,
messages = messages,
)
`when`(metadataStorage.getMetadata()).thenReturn(metadataList)
val storage = NimbusMessagingStorage(
testContext,
metadataStorage,
reportMalformedMessage,
gleanPlumb,
messagingFeature,
)
val results = storage.getMessages()
assertEquals(1, results.size)
assertEquals("normal-message", results[0].id)
}
@Test
fun `GIVEN a malformed message WHEN calling getMessages THEN provide a list of messages ignoring the malformed one`() =
runTest {
val messages = storage.getMessages()
val firstMessage = messages.first()
assertEquals("message-1", firstMessage.id)
assertEquals("message-1", firstMessage.metadata.id)
assertTrue(messages.size == 2)
assertTrue(malformedWasReported)
}
@Test
fun `GIVEN a malformed action WHEN calling sanitizeAction THEN return null`() {
val actionsMap = mapOf("action-1" to "action-1-url")
val notFoundAction =
storage.sanitizeAction("messageId", "no-found-action", actionsMap, false)
val emptyAction = storage.sanitizeAction("messageId", "", actionsMap, false)
val blankAction = storage.sanitizeAction("messageId", " ", actionsMap, false)
assertNull(notFoundAction)
assertNull(emptyAction)
assertNull(blankAction)
assertTrue(malformedWasReported)
}
@Test
fun `GIVEN a previously stored malformed action WHEN calling sanitizeAction THEN return null and not report malFormed`() {
val actionsMap = mapOf("action-1" to "action-1-url")
storage.malFormedMap["malformed-action"] = "messageId"
val action = storage.sanitizeAction("messageId", "malformed-action", actionsMap, false)
assertNull(action)
assertFalse(malformedWasReported)
}
@Test
fun `GIVEN a non-previously stored malformed action WHEN calling sanitizeAction THEN return null and report malFormed`() {
val actionsMap = mapOf("action-1" to "action-1-url")
val action = storage.sanitizeAction("messageId", "malformed-action", actionsMap, false)
assertNull(action)
assertTrue(storage.malFormedMap.containsKey("malformed-action"))
assertTrue(malformedWasReported)
}
@Test
fun `WHEN calling updateMetadata THEN delegate to metadataStorage`() = runTest {
storage.updateMetadata(mock())
verify(metadataStorage).updateMetadata(any())
}
@Test
fun `GIVEN a valid action WHEN calling sanitizeAction THEN return the action`() {
val actionsMap = mapOf("action-1" to "action-1-url")
val validAction = storage.sanitizeAction("messageId", "action-1", actionsMap, false)
assertEquals("action-1-url", validAction)
}
@Test
fun `GIVEN a valid action for control message WHEN calling sanitizeAction THEN return a empty action`() {
val actionsMap = mapOf("action-1" to "action-1-url")
val validAction = storage.sanitizeAction("messageId", "", actionsMap, true)
assertEquals("CONTROL_ACTION", validAction)
assertFalse(malformedWasReported)
}
@Test
fun `GIVEN a trigger action WHEN calling sanitizeTriggers THEN return null`() {
val triggersMap = mapOf("trigger-1" to "trigger-1-expression")
val notFoundTrigger =
storage.sanitizeTriggers("messageId", listOf("no-found-trigger"), triggersMap)
val emptyTrigger = storage.sanitizeTriggers("messageId", listOf(""), triggersMap)
val blankTrigger = storage.sanitizeTriggers("messageId", listOf(" "), triggersMap)
assertNull(notFoundTrigger)
assertNull(emptyTrigger)
assertNull(blankTrigger)
assertTrue(malformedWasReported)
}
@Test
fun `GIVEN a previously stored malformed trigger WHEN calling sanitizeTriggers THEN no report malformed and return null`() {
val triggersMap = mapOf("trigger-1" to "trigger-1-expression")
storage.malFormedMap[" "] = "messageId"
val trigger = storage.sanitizeTriggers("messageId", listOf(" "), triggersMap)
assertNull(trigger)
assertFalse(malformedWasReported)
}
@Test
fun `GIVEN a non previously stored malformed trigger WHEN calling sanitizeTriggers THEN report malformed and return null`() {
val triggersMap = mapOf("trigger-1" to "trigger-1-expression")
val trigger = storage.sanitizeTriggers("messageId", listOf(" "), triggersMap)
assertNull(trigger)
assertTrue(storage.malFormedMap.containsKey(" "))
assertTrue(malformedWasReported)
}
@Test
fun `GIVEN a valid trigger WHEN calling sanitizeAction THEN return the trigger`() {
val triggersMap = mapOf("trigger-1" to "trigger-1-expression")
val validTrigger = storage.sanitizeTriggers("messageId", listOf("trigger-1"), triggersMap)
assertEquals(listOf("trigger-1-expression"), validTrigger)
}
@Test
fun `GIVEN an eligible message WHEN calling isMessageEligible THEN return true`() {
val helper: GleanPlumbMessageHelper = mock()
val message = Message(
"same-id",
mock(),
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
`when`(helper.evalJexl(any())).thenReturn(true)
val result = storage.isMessageEligible(message, helper)
assertTrue(result)
}
@Test
fun `GIVEN a malformed message key WHEN calling reportMalformedMessage THEN record a malformed feature event`() {
val key = "malformed-message"
storage.reportMalformedMessage(key)
assertTrue(malformedWasReported)
verify(featuresInterface).recordMalformedConfiguration("messaging", key)
}
@Test
fun `GIVEN a malformed trigger WHEN calling isMessageEligible THEN return false`() {
val helper: GleanPlumbMessageHelper = mock()
val message = Message(
"same-id",
mock(),
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
`when`(helper.evalJexl(any())).then { throw NimbusException.EvaluationException("") }
val result = storage.isMessageEligible(message, helper)
assertFalse(result)
}
@Test
fun `GIVEN a previously malformed trigger WHEN calling isMessageEligible THEN return false and not evaluate`() {
val helper: GleanPlumbMessageHelper = mock()
val message = Message(
"same-id",
mock(),
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
storage.malFormedMap["trigger"] = "same-id"
`when`(helper.evalJexl(any())).then { throw NimbusException.EvaluationException("") }
val result = storage.isMessageEligible(message, helper)
assertFalse(result)
verify(helper, never()).evalJexl("trigger")
assertFalse(malformedWasReported)
}
@Test
fun `GIVEN a non previously malformed trigger WHEN calling isMessageEligible THEN return false and not evaluate`() {
val helper: GleanPlumbMessageHelper = mock()
val message = Message(
"same-id",
mock(),
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
`when`(helper.evalJexl(any())).then { throw NimbusException.EvaluationException("") }
assertFalse(storage.malFormedMap.containsKey("trigger"))
val result = storage.isMessageEligible(message, helper)
assertFalse(result)
assertTrue(storage.malFormedMap.containsKey("trigger"))
assertTrue(malformedWasReported)
}
@Test
fun `GIVEN none available messages are eligible WHEN calling getNextMessage THEN return null`() {
val spiedStorage = spy(storage)
val message = Message(
"same-id",
mock(),
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
doReturn(false).`when`(spiedStorage).isMessageEligible(any(), any(), any())
val result = spiedStorage.getNextMessage(HOMESCREEN, listOf(message))
assertNull(result)
}
@Test
fun `GIVEN an eligible message WHEN calling getNextMessage THEN return the message`() {
val spiedStorage = spy(storage)
val message = Message(
"same-id",
createMessageData(surface = HOMESCREEN),
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
doReturn(true).`when`(spiedStorage).isMessageEligible(any(), any(), any())
val result = spiedStorage.getNextMessage(HOMESCREEN, listOf(message))
assertEquals(message.id, result!!.id)
}
@Test
fun `GIVEN a message under experiment WHEN calling getNextMessage THEN call recordExposureEvent`() {
val spiedStorage = spy(storage)
val experiment = "my-experiment"
val messageData: MessageData = createMessageData(isControl = false, experiment = experiment)
val message = Message(
"same-id",
messageData,
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
doReturn(true).`when`(spiedStorage).isMessageEligible(any(), any(), any())
val result = spiedStorage.getNextMessage(HOMESCREEN, listOf(message))
verify(featuresInterface).recordExposureEvent("messaging", experiment)
assertEquals(message.id, result!!.id)
}
@Test
fun `GIVEN a control message WHEN calling getNextMessage THEN return the next eligible message`() {
val spiedStorage = spy(storage)
val experiment = "my-experiment"
val messageData: MessageData = createMessageData()
val controlMessageData: MessageData = createMessageData(isControl = true, experiment = experiment)
doReturn(SHOW_NEXT_MESSAGE).`when`(spiedStorage).getOnControlBehavior()
val message = Message(
"id",
messageData,
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
val controlMessage = Message(
"control-id",
controlMessageData,
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
doReturn(true).`when`(spiedStorage).isMessageEligible(any(), any(), any())
val result = spiedStorage.getNextMessage(
HOMESCREEN,
listOf(controlMessage, message),
)
verify(messagingFeature).recordExperimentExposure(experiment)
assertEquals(message.id, result!!.id)
}
@Test
fun `GIVEN a malformed control message WHEN calling getNextMessage THEN return the next eligible message`() {
val spiedStorage = spy(storage)
val messageData: MessageData = createMessageData()
// the message isControl, but has no experiment property.
val controlMessageData: MessageData = createMessageData(isControl = true)
doReturn(SHOW_NEXT_MESSAGE).`when`(spiedStorage).getOnControlBehavior()
val message = Message(
"id",
messageData,
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
val controlMessage = Message(
"control-id",
controlMessageData,
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
doReturn(true).`when`(spiedStorage).isMessageEligible(any(), any(), any())
val result = spiedStorage.getNextMessage(
HOMESCREEN,
listOf(controlMessage, message),
)
verify(messagingFeature).recordMalformedConfiguration("control-id")
assertEquals(message.id, result!!.id)
}
@Test
fun `GIVEN a control message WHEN calling getNextMessage THEN return the next eligible message with the correct surface`() {
val spiedStorage = spy(storage)
val experiment = "my-experiment"
val messageData: MessageData = createMessageData()
val incorrectMessageData: MessageData = createMessageData(surface = NOTIFICATION)
val controlMessageData: MessageData = createMessageData(isControl = true, experiment = experiment)
doReturn(SHOW_NEXT_MESSAGE).`when`(spiedStorage).getOnControlBehavior()
val message = Message(
"id",
messageData,
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
val incorrectMessage = Message(
"incorrect-id",
incorrectMessageData,
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
val controlMessage = Message(
"control-id",
controlMessageData,
action = "action",
mock(),
listOf("trigger"),
Message.Metadata("same-id"),
)
doReturn(true).`when`(spiedStorage).isMessageEligible(any(), any(), any())
var result = spiedStorage.getNextMessage(
HOMESCREEN,
listOf(controlMessage, incorrectMessage, message),
)
verify(messagingFeature, times(1)).recordExperimentExposure(experiment)
assertEquals(message.id, result!!.id)
result = spiedStorage.getNextMessage(
HOMESCREEN,
listOf(controlMessage, incorrectMessage),
)
verify(messagingFeature, times(2)).recordExperimentExposure(experiment)
assertNull(result)
}
@Test
fun `WHEN a storage instance is created THEN do not invoke the feature`() = runTest {
storage = NimbusMessagingStorage(
testContext,
metadataStorage,
reportMalformedMessage,
gleanPlumb,
messagingFeature,
)
// We should not be using the feature holder until getMessages is called.
verify(messagingFeature, never()).value()
}
@Test
fun `WHEN calling getMessage THEN return message with matching key OR null if doesn't exist`() =
runTest {
val messages = mapOf(
"low-message" to createMessageData(style = "low-priority"),
"high-message" to createMessageData(style = "high-priority"),
"medium-message" to createMessageData(style = "medium-priority"),
)
val styles = mapOf(
"high-priority" to createStyle(priority = 100),
"medium-priority" to createStyle(priority = 50),
"low-priority" to createStyle(priority = 1),
)
val (messagingFeature, _) = createMessagingFeature(
styles = styles,
messages = messages,
)
`when`(metadataStorage.getMetadata()).thenReturn(
mapOf(
"message-1" to Message.Metadata(id = "message-1"),
),
)
val storage = NimbusMessagingStorage(
testContext,
metadataStorage,
reportMalformedMessage,
gleanPlumb,
messagingFeature,
)
assertEquals("high-message", storage.getMessage("high-message")?.id)
assertEquals("medium-message", storage.getMessage("medium-message")?.id)
assertEquals("low-message", storage.getMessage("low-message")?.id)
assertEquals(null, storage.getMessage("no-message")?.id)
}
@Test
fun `GIVEN a message without text THEN reject the message and report it as malformed`() = runTest {
val (feature, _) = createMessagingFeature(
styles = mapOf(
"style-1" to createStyle(priority = 100),
),
triggers = mapOf("trigger-1" to "://trigger-1"),
messages = mapOf(
"missing-text" to createMessageData(text = ""),
"ok" to createMessageData(),
),
)
val storage = NimbusMessagingStorage(
testContext,
metadataStorage,
reportMalformedMessage,
gleanPlumb,
feature,
)
assertNotNull(storage.getMessage("ok"))
assertNull(storage.getMessage("missing-text"))
assertTrue(malformedMessageIds.contains("missing-text"))
assertFalse(malformedMessageIds.contains("ok"))
}
private fun createMessageData(
text: String = "text-1",
action: String = "action-1",
style: String = "style-1",
triggers: List<String> = listOf("trigger-1"),
surface: MessageSurfaceId = HOMESCREEN,
isControl: Boolean = false,
experiment: String? = null,
) = MessageData(
action = Res.string(action),
style = style,
trigger = triggers,
surface = surface,
isControl = isControl,
text = Res.string(text),
experiment = experiment,
)
private fun createMessagingFeature(
triggers: Map<String, String> = mapOf("trigger-1" to "trigger-1-expression"),
styles: Map<String, StyleData> = mapOf("style-1" to createStyle()),
actions: Map<String, String> = mapOf("action-1" to "action-1-url"),
messages: Map<String, MessageData> = mapOf(
"message-1" to createMessageData(surface = HOMESCREEN),
"message-2" to createMessageData(surface = NOTIFICATION),
"malformed" to createMessageData(action = "malformed-action"),
"blanktext" to createMessageData(text = ""),
),
): Pair<FeatureHolder<Messaging>, FeaturesInterface> {
val messaging = Messaging(
actions = actions,
triggers = triggers,
messages = messages,
styles = styles,
)
val featureInterface: FeaturesInterface = mock()
// "messaging" is a hard coded value generated from Nimbus.
val messagingFeature = FeatureHolder({ featureInterface }, "messaging") { _, _ ->
messaging
}
messagingFeature.withCachedValue(messaging)
return messagingFeature to featureInterface
}
private fun createStyle(priority: Int = 1, maxDisplayCount: Int = 5): StyleData {
val style1: StyleData = mock()
`when`(style1.priority).thenReturn(priority)
`when`(style1.maxDisplayCount).thenReturn(maxDisplayCount)
return style1
}
companion object {
private const val HOMESCREEN = "homescreen"
private const val NOTIFICATION = "notification"
}
}