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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
|
/* eslint max-nested-callbacks: ["error", 100] */
import { CFRPageActions, PageAction } from "modules/CFRPageActions.sys.mjs";
import { FAKE_RECOMMENDATION } from "./constants";
import { GlobalOverrider } from "test/unit/utils";
import { CFRMessageProvider } from "modules/CFRMessageProvider.sys.mjs";
describe("CFRPageActions", () => {
let sandbox;
let clock;
let fakeRecommendation;
let fakeHost;
let fakeBrowser;
let dispatchStub;
let globals;
let containerElem;
let elements;
let announceStub;
let fakeRemoteL10n;
let isElmVisibleStub;
let getWidgetStub;
const elementIDs = [
"urlbar",
"urlbar-input",
"contextual-feature-recommendation",
"cfr-button",
"cfr-label",
"contextual-feature-recommendation-notification",
"cfr-notification-header-label",
"cfr-notification-header-link",
"cfr-notification-header-image",
"cfr-notification-author",
"cfr-notification-footer",
"cfr-notification-footer-text",
"cfr-notification-footer-filled-stars",
"cfr-notification-footer-empty-stars",
"cfr-notification-footer-users",
"cfr-notification-footer-spacer",
"cfr-notification-footer-learn-more-link",
];
const elementClassNames = ["popup-notification-body-container"];
beforeEach(() => {
sandbox = sinon.createSandbox();
clock = sandbox.useFakeTimers();
isElmVisibleStub = sandbox.stub().returns(true);
getWidgetStub = sandbox.stub();
announceStub = sandbox.stub();
const A11yUtils = { announce: announceStub };
fakeRecommendation = { ...FAKE_RECOMMENDATION };
fakeHost = "mozilla.org";
fakeBrowser = {
documentURI: {
scheme: "https",
host: fakeHost,
},
ownerGlobal: window,
};
dispatchStub = sandbox.stub();
fakeRemoteL10n = {
l10n: {},
reloadL10n: sandbox.stub(),
createElement: sandbox.stub().returns(document.createElement("div")),
};
const gURLBar = document.createElement("div");
gURLBar.textbox = document.createElement("div");
globals = new GlobalOverrider();
globals.set({
RemoteL10n: fakeRemoteL10n,
promiseDocumentFlushed: sandbox
.stub()
.callsFake(fn => Promise.resolve(fn())),
PopupNotifications: {
show: sandbox.stub(),
remove: sandbox.stub(),
},
PrivateBrowsingUtils: { isWindowPrivate: sandbox.stub().returns(false) },
gBrowser: { selectedBrowser: fakeBrowser },
A11yUtils,
gURLBar,
isElementVisible: isElmVisibleStub,
CustomizableUI: { getWidget: getWidgetStub },
});
document.createXULElement = document.createElement;
elements = {};
const [body] = document.getElementsByTagName("body");
containerElem = document.createElement("div");
body.appendChild(containerElem);
for (const id of elementIDs) {
const elem = document.createElement("div");
elem.setAttribute("id", id);
containerElem.appendChild(elem);
elements[id] = elem;
}
for (const className of elementClassNames) {
const elem = document.createElement("div");
elem.setAttribute("class", className);
containerElem.appendChild(elem);
elements[className] = elem;
}
});
afterEach(() => {
CFRPageActions.clearRecommendations();
containerElem.remove();
sandbox.restore();
globals.restore();
});
describe("PageAction", () => {
let pageAction;
beforeEach(() => {
pageAction = new PageAction(window, dispatchStub);
});
describe("#addImpression", () => {
it("should call _sendTelemetry with the impression payload", () => {
const recommendation = {
id: "foo",
content: { bucket_id: "bar" },
};
sandbox.spy(pageAction, "_sendTelemetry");
pageAction.addImpression(recommendation);
assert.calledWith(pageAction._sendTelemetry, {
message_id: "foo",
bucket_id: "bar",
event: "IMPRESSION",
});
});
});
describe("#showAddressBarNotifier", () => {
it("should un-hideAddressBarNotifier the element and set the right label value", async () => {
await pageAction.showAddressBarNotifier(fakeRecommendation);
assert.isFalse(pageAction.container.hidden);
assert.equal(
pageAction.label.value,
fakeRecommendation.content.notification_text
);
});
it("should wait for the document layout to flush", async () => {
sandbox.spy(pageAction.label, "getClientRects");
await pageAction.showAddressBarNotifier(fakeRecommendation);
assert.calledOnce(global.promiseDocumentFlushed);
assert.callOrder(
global.promiseDocumentFlushed,
pageAction.label.getClientRects
);
});
it("should set the CSS variable --cfr-label-width correctly", async () => {
await pageAction.showAddressBarNotifier(fakeRecommendation);
const expectedWidth = pageAction.label.getClientRects()[0].width;
assert.equal(
pageAction.urlbarinput.style.getPropertyValue("--cfr-label-width"),
`${expectedWidth}px`
);
});
it("should cause an expansion, and dispatch an impression if `expand` is true", async () => {
sandbox.spy(pageAction, "_clearScheduledStateChanges");
sandbox.spy(pageAction, "_expand");
sandbox.spy(pageAction, "_dispatchImpression");
await pageAction.showAddressBarNotifier(fakeRecommendation);
assert.notCalled(pageAction._dispatchImpression);
clock.tick(1001);
assert.notEqual(
pageAction.urlbarinput.getAttribute("cfr-recommendation-state"),
"expanded"
);
await pageAction.showAddressBarNotifier(fakeRecommendation, true);
assert.calledOnce(pageAction._clearScheduledStateChanges);
clock.tick(1001);
assert.equal(
pageAction.urlbarinput.getAttribute("cfr-recommendation-state"),
"expanded"
);
assert.calledOnce(pageAction._dispatchImpression);
assert.calledWith(pageAction._dispatchImpression, fakeRecommendation);
});
it("should send telemetry if `expand` is true and the id and bucket_id are provided", async () => {
await pageAction.showAddressBarNotifier(fakeRecommendation, true);
assert.calledWith(dispatchStub, {
type: "DOORHANGER_TELEMETRY",
data: {
action: "cfr_user_event",
source: "CFR",
message_id: fakeRecommendation.id,
bucket_id: fakeRecommendation.content.bucket_id,
event: "IMPRESSION",
},
});
});
});
describe("#hideAddressBarNotifier", () => {
it("should hideAddressBarNotifier the container, cancel any state changes, and remove the state attribute", () => {
sandbox.spy(pageAction, "_clearScheduledStateChanges");
pageAction.hideAddressBarNotifier();
assert.isTrue(pageAction.container.hidden);
assert.calledOnce(pageAction._clearScheduledStateChanges);
assert.isNull(
pageAction.urlbar.getAttribute("cfr-recommendation-state")
);
});
it("should remove the `currentNotification`", () => {
const notification = {};
pageAction.currentNotification = notification;
pageAction.hideAddressBarNotifier();
assert.calledWith(global.PopupNotifications.remove, notification);
});
});
describe("#_expand", () => {
beforeEach(() => {
pageAction._clearScheduledStateChanges();
pageAction.urlbar.removeAttribute("cfr-recommendation-state");
});
it("without a delay, should clear other state changes and set the state to 'expanded'", () => {
sandbox.spy(pageAction, "_clearScheduledStateChanges");
pageAction._expand();
assert.calledOnce(pageAction._clearScheduledStateChanges);
assert.equal(
pageAction.urlbarinput.getAttribute("cfr-recommendation-state"),
"expanded"
);
});
it("with a delay, should set the expanded state after the correct amount of time", () => {
const delay = 1234;
pageAction._expand(delay);
// We expect that an expansion has been scheduled
assert.lengthOf(pageAction.stateTransitionTimeoutIDs, 1);
clock.tick(delay + 1);
assert.equal(
pageAction.urlbarinput.getAttribute("cfr-recommendation-state"),
"expanded"
);
});
});
describe("#_collapse", () => {
beforeEach(() => {
pageAction._clearScheduledStateChanges();
pageAction.urlbar.removeAttribute("cfr-recommendation-state");
});
it("without a delay, should clear other state changes and set the state to collapsed only if it's already expanded", () => {
sandbox.spy(pageAction, "_clearScheduledStateChanges");
pageAction._collapse();
assert.calledOnce(pageAction._clearScheduledStateChanges);
assert.isNull(
pageAction.urlbarinput.getAttribute("cfr-recommendation-state")
);
pageAction.urlbarinput.setAttribute(
"cfr-recommendation-state",
"expanded"
);
pageAction._collapse();
assert.equal(
pageAction.urlbarinput.getAttribute("cfr-recommendation-state"),
"collapsed"
);
});
it("with a delay, should set the collapsed state after the correct amount of time", () => {
const delay = 1234;
pageAction._collapse(delay);
clock.tick(delay + 1);
// The state was _not_ "expanded" and so should not have been set to "collapsed"
assert.isNull(
pageAction.urlbar.getAttribute("cfr-recommendation-state")
);
pageAction._expand();
pageAction._collapse(delay);
// We expect that a collapse has been scheduled
assert.lengthOf(pageAction.stateTransitionTimeoutIDs, 1);
clock.tick(delay + 1);
// This time it was "expanded" so should now (after the delay) be "collapsed"
assert.equal(
pageAction.urlbarinput.getAttribute("cfr-recommendation-state"),
"collapsed"
);
});
});
describe("#_clearScheduledStateChanges", () => {
it("should call .clearTimeout on all stored timeoutIDs", () => {
pageAction.stateTransitionTimeoutIDs = [42, 73, 1997];
sandbox.spy(pageAction.window, "clearTimeout");
pageAction._clearScheduledStateChanges();
assert.calledThrice(pageAction.window.clearTimeout);
assert.calledWith(pageAction.window.clearTimeout, 42);
assert.calledWith(pageAction.window.clearTimeout, 73);
assert.calledWith(pageAction.window.clearTimeout, 1997);
});
});
describe("#_popupStateChange", () => {
it("should collapse the notification and send dismiss telemetry on 'dismissed'", () => {
pageAction._expand();
sandbox.spy(pageAction, "_sendTelemetry");
pageAction._popupStateChange("dismissed");
assert.equal(
pageAction.urlbarinput.getAttribute("cfr-recommendation-state"),
"collapsed"
);
assert.equal(
pageAction._sendTelemetry.lastCall.args[0].event,
"DISMISS"
);
});
it("should remove the notification on 'removed'", () => {
pageAction._expand();
const fakeNotification = {};
pageAction.currentNotification = fakeNotification;
pageAction._popupStateChange("removed");
assert.calledOnce(global.PopupNotifications.remove);
assert.calledWith(global.PopupNotifications.remove, fakeNotification);
});
it("should do nothing for other states", () => {
pageAction._popupStateChange("opened");
assert.notCalled(global.PopupNotifications.remove);
});
});
describe("#dispatchUserAction", () => {
it("should call ._dispatchCFRAction with the right action", () => {
const fakeAction = {};
pageAction.dispatchUserAction(fakeAction);
assert.calledOnce(dispatchStub);
assert.calledWith(
dispatchStub,
{ type: "USER_ACTION", data: fakeAction },
fakeBrowser
);
});
});
describe("#_dispatchImpression", () => {
it("should call ._dispatchCFRAction with the right action", () => {
pageAction._dispatchImpression("fake impression");
assert.calledWith(dispatchStub, {
type: "IMPRESSION",
data: "fake impression",
});
});
});
describe("#_sendTelemetry", () => {
it("should call ._dispatchCFRAction with the right action", () => {
const fakePing = { message_id: 42 };
pageAction._sendTelemetry(fakePing);
assert.calledWith(dispatchStub, {
type: "DOORHANGER_TELEMETRY",
data: {
action: "cfr_user_event",
source: "CFR",
message_id: 42,
},
});
});
});
describe("#_blockMessage", () => {
it("should call ._dispatchCFRAction with the right action", () => {
pageAction._blockMessage("fake id");
assert.calledOnce(dispatchStub);
assert.calledWith(dispatchStub, {
type: "BLOCK_MESSAGE_BY_ID",
data: { id: "fake id" },
});
});
});
describe("#getStrings", () => {
let formatMessagesStub;
const localeStrings = [
{
value: "你好世界",
attributes: [
{ name: "first_attr", value: 42 },
{ name: "second_attr", value: "some string" },
{ name: "third_attr", value: [1, 2, 3] },
],
},
];
beforeEach(() => {
formatMessagesStub = sandbox
.stub()
.withArgs({ id: "hello_world" })
.resolves(localeStrings);
global.RemoteL10n.l10n.formatMessages = formatMessagesStub;
});
it("should return the argument if a string_id is not defined", async () => {
assert.deepEqual(await pageAction.getStrings({}), {});
assert.equal(await pageAction.getStrings("some string"), "some string");
});
it("should get the right locale string", async () => {
assert.equal(
await pageAction.getStrings({ string_id: "hello_world" }),
localeStrings[0].value
);
});
it("should return the right sub-attribute if specified", async () => {
assert.equal(
await pageAction.getStrings(
{ string_id: "hello_world" },
"second_attr"
),
"some string"
);
});
it("should attach attributes to string overrides", async () => {
const fromJson = { value: "Add Now", attributes: { accesskey: "A" } };
const result = await pageAction.getStrings(fromJson);
assert.equal(result, fromJson.value);
assert.propertyVal(result.attributes, "accesskey", "A");
});
it("should return subAttributes when doing string overrides", async () => {
const fromJson = { value: "Add Now", attributes: { accesskey: "A" } };
const result = await pageAction.getStrings(fromJson, "accesskey");
assert.equal(result, "A");
});
it("should resolve ftl strings and attach subAttributes", async () => {
const fromFtl = { string_id: "cfr-doorhanger-extension-ok-button" };
formatMessagesStub.resolves([
{ value: "Add Now", attributes: [{ name: "accesskey", value: "A" }] },
]);
const result = await pageAction.getStrings(fromFtl);
assert.equal(result, "Add Now");
assert.propertyVal(result.attributes, "accesskey", "A");
});
it("should return subAttributes from ftl ids", async () => {
const fromFtl = { string_id: "cfr-doorhanger-extension-ok-button" };
formatMessagesStub.resolves([
{ value: "Add Now", attributes: [{ name: "accesskey", value: "A" }] },
]);
const result = await pageAction.getStrings(fromFtl, "accesskey");
assert.equal(result, "A");
});
it("should report an error when no attributes are present but subAttribute is requested", async () => {
const fromJson = { value: "Foo" };
const stub = sandbox.stub(global.console, "error");
await pageAction.getStrings(fromJson, "accesskey");
assert.calledOnce(stub);
stub.restore();
});
});
describe("#_cfrUrlbarButtonClick", () => {
let translateElementsStub;
let setAttributesStub;
let getStringsStub;
beforeEach(async () => {
CFRPageActions.PageActionMap.set(fakeBrowser.ownerGlobal, pageAction);
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
fakeRecommendation,
dispatchStub
);
getStringsStub = sandbox.stub(pageAction, "getStrings").resolves("");
getStringsStub
.callsFake(async a => a) // eslint-disable-line max-nested-callbacks
.withArgs({ string_id: "primary_button_id" })
.resolves({ value: "Primary Button", attributes: { accesskey: "p" } })
.withArgs({ string_id: "secondary_button_id" })
.resolves({
value: "Secondary Button",
attributes: { accesskey: "s" },
})
.withArgs({ string_id: "secondary_button_id_2" })
.resolves({
value: "Secondary Button 2",
attributes: { accesskey: "a" },
})
.withArgs({ string_id: "secondary_button_id_3" })
.resolves({
value: "Secondary Button 3",
attributes: { accesskey: "g" },
})
.withArgs(
sinon.match({
string_id: "cfr-doorhanger-extension-learn-more-link",
})
)
.resolves("Learn more")
.withArgs(
sinon.match({ string_id: "cfr-doorhanger-extension-total-users" })
)
.callsFake(async ({ args }) => `${args.total} users`); // eslint-disable-line max-nested-callbacks
translateElementsStub = sandbox.stub().resolves();
setAttributesStub = sandbox.stub();
global.RemoteL10n.l10n.setAttributes = setAttributesStub;
global.RemoteL10n.l10n.translateElements = translateElementsStub;
});
it("should call `.hideAddressBarNotifier` and do nothing if there is no recommendation for the selected browser", async () => {
sandbox.spy(pageAction, "hideAddressBarNotifier");
CFRPageActions.RecommendationMap.delete(fakeBrowser);
await pageAction._cfrUrlbarButtonClick({});
assert.calledOnce(pageAction.hideAddressBarNotifier);
assert.notCalled(global.PopupNotifications.show);
});
it("should cancel any planned state changes", async () => {
sandbox.spy(pageAction, "_clearScheduledStateChanges");
assert.notCalled(pageAction._clearScheduledStateChanges);
await pageAction._cfrUrlbarButtonClick({});
assert.calledOnce(pageAction._clearScheduledStateChanges);
});
it("should set the right text values", async () => {
await pageAction._cfrUrlbarButtonClick({});
const headerLabel = elements["cfr-notification-header-label"];
const headerLink = elements["cfr-notification-header-link"];
const headerImage = elements["cfr-notification-header-image"];
const footerLink = elements["cfr-notification-footer-learn-more-link"];
assert.equal(
headerLabel.value,
fakeRecommendation.content.heading_text
);
assert.isTrue(
headerLink
.getAttribute("href")
.endsWith(fakeRecommendation.content.info_icon.sumo_path)
);
assert.equal(
headerImage.getAttribute("tooltiptext"),
fakeRecommendation.content.info_icon.label
);
const htmlFooterEl = fakeRemoteL10n.createElement.args.find(
/* eslint-disable-next-line max-nested-callbacks */
([, , args]) =>
args && args.content === fakeRecommendation.content.text
);
assert.ok(htmlFooterEl);
assert.equal(footerLink.value, "Learn more");
assert.equal(
footerLink.getAttribute("href"),
fakeRecommendation.content.addon.amo_url
);
});
it("should add the rating correctly", async () => {
await pageAction._cfrUrlbarButtonClick();
const footerFilledStars =
elements["cfr-notification-footer-filled-stars"];
const footerEmptyStars =
elements["cfr-notification-footer-empty-stars"];
// .toFixed to sort out some floating precision errors
assert.equal(
footerFilledStars.style.width,
`${(4.2 * 16).toFixed(1)}px`
);
assert.equal(
footerEmptyStars.style.width,
`${(0.8 * 16).toFixed(1)}px`
);
});
it("should add the number of users correctly", async () => {
await pageAction._cfrUrlbarButtonClick();
const footerUsers = elements["cfr-notification-footer-users"];
assert.isNull(footerUsers.getAttribute("hidden"));
assert.equal(
footerUsers.getAttribute("value"),
`${fakeRecommendation.content.addon.users}`
);
});
it("should send the right telemetry", async () => {
await pageAction._cfrUrlbarButtonClick();
assert.calledWith(dispatchStub, {
type: "DOORHANGER_TELEMETRY",
data: {
action: "cfr_user_event",
source: "CFR",
message_id: fakeRecommendation.id,
bucket_id: fakeRecommendation.content.bucket_id,
event: "CLICK_DOORHANGER",
},
});
});
it("should set the main action correctly", async () => {
sinon
.stub(CFRPageActions, "_fetchLatestAddonVersion")
.resolves("latest-addon.xpi");
await pageAction._cfrUrlbarButtonClick();
const mainAction = global.PopupNotifications.show.firstCall.args[4]; // eslint-disable-line prefer-destructuring
assert.deepEqual(mainAction.label, {
value: "Primary Button",
attributes: { accesskey: "p" },
});
sandbox.spy(pageAction, "hideAddressBarNotifier");
await mainAction.callback();
assert.calledOnce(pageAction.hideAddressBarNotifier);
// Should block the message
assert.calledWith(dispatchStub, {
type: "BLOCK_MESSAGE_BY_ID",
data: { id: fakeRecommendation.id },
});
// Should trigger the action
assert.calledWith(
dispatchStub,
{
type: "USER_ACTION",
data: { id: "primary_action", data: { url: "latest-addon.xpi" } },
},
fakeBrowser
);
// Should send telemetry
assert.calledWith(dispatchStub, {
type: "DOORHANGER_TELEMETRY",
data: {
action: "cfr_user_event",
source: "CFR",
message_id: fakeRecommendation.id,
bucket_id: fakeRecommendation.content.bucket_id,
event: "INSTALL",
},
});
// Should remove the recommendation
assert.isFalse(CFRPageActions.RecommendationMap.has(fakeBrowser));
});
it("should set the secondary action correctly", async () => {
await pageAction._cfrUrlbarButtonClick();
// eslint-disable-next-line prefer-destructuring
const [secondaryAction] =
global.PopupNotifications.show.firstCall.args[5];
assert.deepEqual(secondaryAction.label, {
value: "Secondary Button",
attributes: { accesskey: "s" },
});
sandbox.spy(pageAction, "hideAddressBarNotifier");
CFRPageActions.RecommendationMap.set(fakeBrowser, {});
secondaryAction.callback();
// Should send telemetry
assert.calledWith(dispatchStub, {
type: "DOORHANGER_TELEMETRY",
data: {
action: "cfr_user_event",
source: "CFR",
message_id: fakeRecommendation.id,
bucket_id: fakeRecommendation.content.bucket_id,
event: "DISMISS",
},
});
// Don't remove the recommendation on `DISMISS` action
assert.isTrue(CFRPageActions.RecommendationMap.has(fakeBrowser));
assert.notCalled(pageAction.hideAddressBarNotifier);
});
it("should send right telemetry for BLOCK secondary action", async () => {
await pageAction._cfrUrlbarButtonClick();
// eslint-disable-next-line prefer-destructuring
const blockAction = global.PopupNotifications.show.firstCall.args[5][1];
assert.deepEqual(blockAction.label, {
value: "Secondary Button 2",
attributes: { accesskey: "a" },
});
sandbox.spy(pageAction, "hideAddressBarNotifier");
sandbox.spy(pageAction, "_blockMessage");
CFRPageActions.RecommendationMap.set(fakeBrowser, {});
blockAction.callback();
assert.calledOnce(pageAction.hideAddressBarNotifier);
assert.calledOnce(pageAction._blockMessage);
// Should send telemetry
assert.calledWith(dispatchStub, {
type: "DOORHANGER_TELEMETRY",
data: {
action: "cfr_user_event",
source: "CFR",
message_id: fakeRecommendation.id,
bucket_id: fakeRecommendation.content.bucket_id,
event: "BLOCK",
},
});
// Should remove the recommendation
assert.isFalse(CFRPageActions.RecommendationMap.has(fakeBrowser));
});
it("should send right telemetry for MANAGE secondary action", async () => {
await pageAction._cfrUrlbarButtonClick();
// eslint-disable-next-line prefer-destructuring
const manageAction =
global.PopupNotifications.show.firstCall.args[5][2];
assert.deepEqual(manageAction.label, {
value: "Secondary Button 3",
attributes: { accesskey: "g" },
});
sandbox.spy(pageAction, "hideAddressBarNotifier");
CFRPageActions.RecommendationMap.set(fakeBrowser, {});
manageAction.callback();
// Should send telemetry
assert.calledWith(dispatchStub, {
type: "DOORHANGER_TELEMETRY",
data: {
action: "cfr_user_event",
source: "CFR",
message_id: fakeRecommendation.id,
bucket_id: fakeRecommendation.content.bucket_id,
event: "MANAGE",
},
});
// Don't remove the recommendation on `MANAGE` action
assert.isTrue(CFRPageActions.RecommendationMap.has(fakeBrowser));
assert.notCalled(pageAction.hideAddressBarNotifier);
});
it("should call PopupNotifications.show with the right arguments", async () => {
await pageAction._cfrUrlbarButtonClick();
assert.calledWith(
global.PopupNotifications.show,
fakeBrowser,
"contextual-feature-recommendation",
fakeRecommendation.content.addon.title,
"cfr",
sinon.match.any, // Corresponds to the main action, tested above
sinon.match.any, // Corresponds to the secondary action, tested above
{
popupIconURL: fakeRecommendation.content.addon.icon,
hideClose: true,
eventCallback: pageAction._popupStateChange,
persistent: false,
persistWhileVisible: false,
popupIconClass: fakeRecommendation.content.icon_class,
recordTelemetryInPrivateBrowsing:
fakeRecommendation.content.show_in_private_browsing,
name: {
string_id: "cfr-doorhanger-extension-author",
args: { name: fakeRecommendation.content.addon.author },
},
}
);
});
});
describe("#_cfrUrlbarButtonClick/cfr_urlbar_chiclet", () => {
let heartbeatRecommendation;
beforeEach(async () => {
heartbeatRecommendation = (await CFRMessageProvider.getMessages()).find(
m => m.template === "cfr_urlbar_chiclet"
);
CFRPageActions.PageActionMap.set(fakeBrowser.ownerGlobal, pageAction);
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
heartbeatRecommendation,
dispatchStub
);
});
it("should dispatch a click event", async () => {
await pageAction._cfrUrlbarButtonClick({});
assert.calledWith(dispatchStub, {
type: "DOORHANGER_TELEMETRY",
data: {
action: "cfr_user_event",
source: "CFR",
message_id: heartbeatRecommendation.id,
bucket_id: heartbeatRecommendation.content.bucket_id,
event: "CLICK_DOORHANGER",
},
});
});
it("should dispatch a USER_ACTION for chiclet_open_url layout", async () => {
await pageAction._cfrUrlbarButtonClick({});
assert.calledWith(dispatchStub, {
type: "USER_ACTION",
data: {
data: {
args: heartbeatRecommendation.content.action.url,
where: heartbeatRecommendation.content.action.where,
},
type: "OPEN_URL",
},
});
});
it("should block the message after the click", async () => {
await pageAction._cfrUrlbarButtonClick({});
assert.calledWith(dispatchStub, {
type: "BLOCK_MESSAGE_BY_ID",
data: { id: heartbeatRecommendation.id },
});
});
it("should remove the button and browser entry", async () => {
sandbox.spy(pageAction, "hideAddressBarNotifier");
await pageAction._cfrUrlbarButtonClick({});
assert.calledOnce(pageAction.hideAddressBarNotifier);
assert.isFalse(CFRPageActions.RecommendationMap.has(fakeBrowser));
});
});
describe("#showMilestonePopup", () => {
let milestoneRecommendation;
let fakeTrackingDBService;
beforeEach(async () => {
fakeTrackingDBService = {
sumAllEvents: sandbox.stub(),
};
globals.set({ TrackingDBService: fakeTrackingDBService });
CFRPageActions.PageActionMap.set(fakeBrowser.ownerGlobal, pageAction);
sandbox
.stub(pageAction, "getStrings")
.callsFake(async a => a) // eslint-disable-line max-nested-callbacks
.resolves({ value: "element", attributes: { accesskey: "e" } });
milestoneRecommendation = (await CFRMessageProvider.getMessages()).find(
m => m.template === "milestone_message"
);
});
afterEach(() => {
sandbox.restore();
globals.restore();
});
it("Set current date in header when earliest date undefined", async () => {
fakeTrackingDBService.getEarliestRecordedDate = sandbox.stub();
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
milestoneRecommendation,
dispatchStub
);
const [, , headerElementArgs] = fakeRemoteL10n.createElement.args.find(
/* eslint-disable-next-line max-nested-callbacks */
([, , args]) => args && args.content && args.attributes
);
assert.equal(
headerElementArgs.content.string_id,
milestoneRecommendation.content.heading_text.string_id
);
assert.equal(headerElementArgs.attributes.date, new Date().getTime());
assert.calledOnce(global.PopupNotifications.show);
});
it("Set date in header to earliest date timestamp by default", async () => {
let earliestDateTimeStamp = 1705601996435;
fakeTrackingDBService.getEarliestRecordedDate = sandbox
.stub()
.returns(earliestDateTimeStamp);
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
milestoneRecommendation,
dispatchStub
);
const [, , headerElementArgs] = fakeRemoteL10n.createElement.args.find(
/* eslint-disable-next-line max-nested-callbacks */
([, , args]) => args && args.content && args.attributes
);
assert.equal(
headerElementArgs.content.string_id,
milestoneRecommendation.content.heading_text.string_id
);
assert.equal(headerElementArgs.attributes.date, earliestDateTimeStamp);
assert.calledOnce(global.PopupNotifications.show);
});
});
});
describe("CFRPageActions", () => {
beforeEach(() => {
// Spy on the prototype methods to inspect calls for any PageAction instance
sandbox.spy(PageAction.prototype, "showAddressBarNotifier");
sandbox.spy(PageAction.prototype, "hideAddressBarNotifier");
});
describe("updatePageActions", () => {
let savedRec;
beforeEach(() => {
const win = fakeBrowser.ownerGlobal;
CFRPageActions.PageActionMap.set(
win,
new PageAction(win, dispatchStub)
);
const { id, content } = fakeRecommendation;
savedRec = {
id,
host: fakeHost,
content,
};
CFRPageActions.RecommendationMap.set(fakeBrowser, savedRec);
});
it("should do nothing if a pageAction doesn't exist for the window", () => {
const win = fakeBrowser.ownerGlobal;
CFRPageActions.PageActionMap.delete(win);
CFRPageActions.updatePageActions(fakeBrowser);
assert.notCalled(PageAction.prototype.showAddressBarNotifier);
assert.notCalled(PageAction.prototype.hideAddressBarNotifier);
});
it("should do nothing if the browser is not the `selectedBrowser`", () => {
const someOtherFakeBrowser = {};
CFRPageActions.updatePageActions(someOtherFakeBrowser);
assert.notCalled(PageAction.prototype.showAddressBarNotifier);
assert.notCalled(PageAction.prototype.hideAddressBarNotifier);
});
it("should hideAddressBarNotifier the pageAction if a recommendation doesn't exist for the given browser", () => {
CFRPageActions.RecommendationMap.delete(fakeBrowser);
CFRPageActions.updatePageActions(fakeBrowser);
assert.calledOnce(PageAction.prototype.hideAddressBarNotifier);
});
it("should show the pageAction if a recommendation exists and the host matches", () => {
CFRPageActions.updatePageActions(fakeBrowser);
assert.calledOnce(PageAction.prototype.showAddressBarNotifier);
assert.calledWith(
PageAction.prototype.showAddressBarNotifier,
savedRec
);
});
it("should show the pageAction if a recommendation exists and it doesn't have a host defined", () => {
const recNoHost = { ...savedRec, host: undefined };
CFRPageActions.RecommendationMap.set(fakeBrowser, recNoHost);
CFRPageActions.updatePageActions(fakeBrowser);
assert.calledOnce(PageAction.prototype.showAddressBarNotifier);
assert.calledWith(
PageAction.prototype.showAddressBarNotifier,
recNoHost
);
});
it("should hideAddressBarNotifier the pageAction and delete the recommendation if the recommendation exists but the host doesn't match", () => {
const someOtherFakeHost = "subdomain.mozilla.com";
fakeBrowser.documentURI.host = someOtherFakeHost;
assert.isTrue(CFRPageActions.RecommendationMap.has(fakeBrowser));
CFRPageActions.updatePageActions(fakeBrowser);
assert.calledOnce(PageAction.prototype.hideAddressBarNotifier);
assert.isFalse(CFRPageActions.RecommendationMap.has(fakeBrowser));
});
it("should not call `delete` if retain is true", () => {
savedRec.retain = true;
fakeBrowser.documentURI.host = "subdomain.mozilla.com";
assert.isTrue(CFRPageActions.RecommendationMap.has(fakeBrowser));
CFRPageActions.updatePageActions(fakeBrowser);
assert.propertyVal(savedRec, "retain", false);
assert.calledOnce(PageAction.prototype.hideAddressBarNotifier);
assert.isTrue(CFRPageActions.RecommendationMap.has(fakeBrowser));
});
it("should call `delete` if retain is false", () => {
savedRec.retain = false;
fakeBrowser.documentURI.host = "subdomain.mozilla.com";
assert.isTrue(CFRPageActions.RecommendationMap.has(fakeBrowser));
CFRPageActions.updatePageActions(fakeBrowser);
assert.propertyVal(savedRec, "retain", false);
assert.calledOnce(PageAction.prototype.hideAddressBarNotifier);
assert.isFalse(CFRPageActions.RecommendationMap.has(fakeBrowser));
});
});
describe("forceRecommendation", () => {
it("should succeed and add an element to the RecommendationMap", async () => {
assert.isTrue(
await CFRPageActions.forceRecommendation(
fakeBrowser,
fakeRecommendation,
dispatchStub
)
);
assert.deepInclude(CFRPageActions.RecommendationMap.get(fakeBrowser), {
id: fakeRecommendation.id,
content: fakeRecommendation.content,
});
});
it("should create a PageAction if one doesn't exist for the window, save it in the PageActionMap, and call `show`", async () => {
const win = fakeBrowser.ownerGlobal;
assert.isFalse(CFRPageActions.PageActionMap.has(win));
await CFRPageActions.forceRecommendation(
fakeBrowser,
fakeRecommendation,
dispatchStub
);
const pageAction = CFRPageActions.PageActionMap.get(win);
assert.equal(win, pageAction.window);
assert.equal(dispatchStub, pageAction._dispatchCFRAction);
assert.calledOnce(PageAction.prototype.showAddressBarNotifier);
});
});
describe("showPopup", () => {
let savedRec;
let pageAction;
let fakeAnchorId = "fake_anchor_id";
let fakeAltAnchorId = "fake_alt_anchor_id";
let TEST_MESSAGE;
let getElmStub;
let getStyleStub;
let isCustomizingStub;
beforeEach(() => {
TEST_MESSAGE = {
id: "fake_id",
template: "cfr_doorhanger",
content: {
skip_address_bar_notifier: true,
heading_text: "Fake Heading Text",
anchor_id: fakeAnchorId,
},
};
getElmStub = sandbox
.stub(window.document, "getElementById")
.callsFake(id => ({ id }));
getStyleStub = sandbox
.stub(window, "getComputedStyle")
.returns({ display: "block", visibility: "visible" });
isCustomizingStub = sandbox.stub().returns(false);
globals.set({
CustomizationHandler: { isCustomizing: isCustomizingStub },
});
savedRec = {
id: TEST_MESSAGE.id,
host: fakeHost,
content: TEST_MESSAGE.content,
};
CFRPageActions.RecommendationMap.set(fakeBrowser, savedRec);
pageAction = new PageAction(window, dispatchStub);
sandbox.stub(pageAction, "_renderPopup");
});
afterEach(() => {
sandbox.restore();
globals.restore();
});
it("should use anchor_id if element exists and is not a customizable widget", async () => {
await pageAction.showPopup();
assert.equal(fakeBrowser.cfrpopupnotificationanchor.id, fakeAnchorId);
});
it("should use anchor_id if element exists and is in the toolbar", async () => {
getWidgetStub.withArgs(fakeAnchorId).returns({ areaType: "toolbar" });
await pageAction.showPopup();
assert.equal(fakeBrowser.cfrpopupnotificationanchor.id, fakeAnchorId);
});
it("should use the cfr button if element exists but is in the widget overflow panel", async () => {
getWidgetStub
.withArgs(fakeAnchorId)
.returns({ areaType: "menu-panel" });
await pageAction.showPopup();
assert.equal(
fakeBrowser.cfrpopupnotificationanchor.id,
pageAction.button.id
);
});
it("should use the cfr button if element exists but is in the customization palette", async () => {
getWidgetStub.withArgs(fakeAnchorId).returns({ areaType: null });
isCustomizingStub.returns(true);
await pageAction.showPopup();
assert.equal(
fakeBrowser.cfrpopupnotificationanchor.id,
pageAction.button.id
);
});
it("should use alt_anchor_id if one has been provided and the anchor_id element cannot be found", async () => {
TEST_MESSAGE.content.alt_anchor_id = fakeAltAnchorId;
getElmStub.withArgs(fakeAnchorId).returns(null);
await pageAction.showPopup();
assert.equal(
fakeBrowser.cfrpopupnotificationanchor.id,
fakeAltAnchorId
);
});
it("should use alt_anchor_id if one has been provided and the anchor_id element is hidden by CSS", async () => {
TEST_MESSAGE.content.alt_anchor_id = fakeAltAnchorId;
getStyleStub
.withArgs(sandbox.match({ id: fakeAnchorId }))
.returns({ display: "none", visibility: "visible" });
await pageAction.showPopup();
assert.equal(
fakeBrowser.cfrpopupnotificationanchor.id,
fakeAltAnchorId
);
});
it("should use alt_anchor_id if one has been provided and the anchor_id element has no height/width", async () => {
TEST_MESSAGE.content.alt_anchor_id = fakeAltAnchorId;
isElmVisibleStub
.withArgs(sandbox.match({ id: fakeAnchorId }))
.returns(false);
await pageAction.showPopup();
assert.equal(
fakeBrowser.cfrpopupnotificationanchor.id,
fakeAltAnchorId
);
});
it("should use the button if the anchor_id and alt_anchor_id are both not visible", async () => {
TEST_MESSAGE.content.alt_anchor_id = fakeAltAnchorId;
getStyleStub
.withArgs(sandbox.match({ id: fakeAnchorId }))
.returns({ display: "none", visibility: "visible" });
getWidgetStub.withArgs(fakeAltAnchorId).returns({ areaType: null });
isCustomizingStub.returns(true);
await pageAction.showPopup();
assert.equal(
fakeBrowser.cfrpopupnotificationanchor.id,
pageAction.button.id
);
});
it("should use the default container if the anchor_id, alt_anchor_id, and cfr button are not visible", async () => {
TEST_MESSAGE.content.alt_anchor_id = fakeAltAnchorId;
getStyleStub
.withArgs(sandbox.match({ id: fakeAnchorId }))
.returns({ display: "none", visibility: "visible" });
getStyleStub
.withArgs(sandbox.match({ id: "cfr-button" }))
.returns({ display: "none", visibility: "visible" });
getWidgetStub.withArgs(fakeAltAnchorId).returns({ areaType: null });
isCustomizingStub.returns(true);
await pageAction.showPopup();
assert.equal(
fakeBrowser.cfrpopupnotificationanchor.id,
pageAction.container.id
);
});
});
describe("addRecommendation", () => {
it("should fail and not add a recommendation if the browser is part of a private window", async () => {
global.PrivateBrowsingUtils.isWindowPrivate.returns(true);
assert.isFalse(
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
fakeRecommendation,
dispatchStub
)
);
assert.isFalse(CFRPageActions.RecommendationMap.has(fakeBrowser));
});
it("should successfully add a private browsing recommendation and send correct telemetry", async () => {
global.PrivateBrowsingUtils.isWindowPrivate.returns(true);
fakeRecommendation.content.show_in_private_browsing = true;
assert.isTrue(
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
fakeRecommendation,
dispatchStub
)
);
assert.isTrue(CFRPageActions.RecommendationMap.has(fakeBrowser));
const pageAction = CFRPageActions.PageActionMap.get(
fakeBrowser.ownerGlobal
);
await pageAction.showAddressBarNotifier(fakeRecommendation, true);
assert.calledWith(dispatchStub, {
type: "DOORHANGER_TELEMETRY",
data: {
action: "cfr_user_event",
source: "CFR",
is_private: true,
message_id: fakeRecommendation.id,
bucket_id: fakeRecommendation.content.bucket_id,
event: "IMPRESSION",
},
});
});
it("should fail and not add a recommendation if the browser is not the selected browser", async () => {
global.gBrowser.selectedBrowser = {}; // Some other browser
assert.isFalse(
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
fakeRecommendation,
dispatchStub
)
);
});
it("should fail and not add a recommendation if the browser does not exist", async () => {
assert.isFalse(
await CFRPageActions.addRecommendation(
undefined,
fakeHost,
fakeRecommendation,
dispatchStub
)
);
assert.isFalse(CFRPageActions.RecommendationMap.has(fakeBrowser));
});
it("should fail and not add a recommendation if the host doesn't match", async () => {
const someOtherFakeHost = "subdomain.mozilla.com";
assert.isFalse(
await CFRPageActions.addRecommendation(
fakeBrowser,
someOtherFakeHost,
fakeRecommendation,
dispatchStub
)
);
});
it("should otherwise succeed and add an element to the RecommendationMap", async () => {
assert.isTrue(
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
fakeRecommendation,
dispatchStub
)
);
assert.deepInclude(CFRPageActions.RecommendationMap.get(fakeBrowser), {
id: fakeRecommendation.id,
host: fakeHost,
content: fakeRecommendation.content,
});
});
it("should create a PageAction if one doesn't exist for the window, save it in the PageActionMap, and call `show`", async () => {
const win = fakeBrowser.ownerGlobal;
assert.isFalse(CFRPageActions.PageActionMap.has(win));
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
fakeRecommendation,
dispatchStub
);
const pageAction = CFRPageActions.PageActionMap.get(win);
assert.equal(win, pageAction.window);
assert.equal(dispatchStub, pageAction._dispatchCFRAction);
assert.calledOnce(PageAction.prototype.showAddressBarNotifier);
});
it("should add the right url if we fetched and addon install URL", async () => {
fakeRecommendation.template = "cfr_doorhanger";
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
fakeRecommendation,
dispatchStub
);
const recommendation =
CFRPageActions.RecommendationMap.get(fakeBrowser);
// sanity check - just go through some of the rest of the attributes to make sure they were untouched
assert.equal(recommendation.id, fakeRecommendation.id);
assert.equal(
recommendation.content.heading_text,
fakeRecommendation.content.heading_text
);
assert.equal(
recommendation.content.addon,
fakeRecommendation.content.addon
);
assert.equal(
recommendation.content.text,
fakeRecommendation.content.text
);
assert.equal(
recommendation.content.buttons.secondary,
fakeRecommendation.content.buttons.secondary
);
assert.equal(
recommendation.content.buttons.primary.action.id,
fakeRecommendation.content.buttons.primary.action.id
);
delete fakeRecommendation.template;
});
it("should prevent a second message if one is currently displayed", async () => {
const secondMessage = { ...fakeRecommendation, id: "second_message" };
let messageAdded = await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
fakeRecommendation,
dispatchStub
);
assert.isTrue(messageAdded);
assert.deepInclude(CFRPageActions.RecommendationMap.get(fakeBrowser), {
id: fakeRecommendation.id,
host: fakeHost,
content: fakeRecommendation.content,
});
messageAdded = await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
secondMessage,
dispatchStub
);
// Adding failed
assert.isFalse(messageAdded);
// First message is still there
assert.deepInclude(CFRPageActions.RecommendationMap.get(fakeBrowser), {
id: fakeRecommendation.id,
host: fakeHost,
content: fakeRecommendation.content,
});
});
it("should send impressions just for the first message", async () => {
const secondMessage = { ...fakeRecommendation, id: "second_message" };
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
fakeRecommendation,
dispatchStub
);
await CFRPageActions.addRecommendation(
fakeBrowser,
fakeHost,
secondMessage,
dispatchStub
);
// Doorhanger telemetry + Impression for just 1 message
assert.calledTwice(dispatchStub);
const [firstArgs] = dispatchStub.firstCall.args;
const [secondArgs] = dispatchStub.secondCall.args;
assert.equal(firstArgs.data.id, secondArgs.data.message_id);
});
});
describe("clearRecommendations", () => {
const createFakePageAction = () => ({
hideAddressBarNotifier: sandbox.stub(),
});
const windows = [{}, {}, { closed: true }];
const browsers = [{}, {}, {}, {}];
beforeEach(() => {
CFRPageActions.PageActionMap.set(windows[0], createFakePageAction());
CFRPageActions.PageActionMap.set(windows[2], createFakePageAction());
for (const browser of browsers) {
CFRPageActions.RecommendationMap.set(browser, {});
}
globals.set({ Services: { wm: { getEnumerator: () => windows } } });
});
it("should hideAddressBarNotifier the PageActions of any existing, non-closed windows", () => {
const pageActions = windows.map(win =>
CFRPageActions.PageActionMap.get(win)
);
CFRPageActions.clearRecommendations();
// Only the first window had a PageAction and wasn't closed
assert.calledOnce(pageActions[0].hideAddressBarNotifier);
assert.isUndefined(pageActions[1]);
assert.notCalled(pageActions[2].hideAddressBarNotifier);
});
it("should clear the PageActionMap and the RecommendationMap", () => {
CFRPageActions.clearRecommendations();
// Both are WeakMaps and so are not iterable, cannot be cleared, and
// cannot have their length queried directly, so we have to check
// whether previous elements still exist
assert.lengthOf(windows, 3);
for (const win of windows) {
assert.isFalse(CFRPageActions.PageActionMap.has(win));
}
assert.lengthOf(browsers, 4);
for (const browser of browsers) {
assert.isFalse(CFRPageActions.RecommendationMap.has(browser));
}
});
});
describe("reloadL10n", () => {
const createFakePageAction = () => ({
hideAddressBarNotifier() {},
reloadL10n: sandbox.stub(),
});
const windows = [{}, {}, { closed: true }];
beforeEach(() => {
CFRPageActions.PageActionMap.set(windows[0], createFakePageAction());
CFRPageActions.PageActionMap.set(windows[2], createFakePageAction());
globals.set({ Services: { wm: { getEnumerator: () => windows } } });
});
it("should call reloadL10n for all the PageActions of any existing, non-closed windows", () => {
const pageActions = windows.map(win =>
CFRPageActions.PageActionMap.get(win)
);
CFRPageActions.reloadL10n();
// Only the first window had a PageAction and wasn't closed
assert.calledOnce(pageActions[0].reloadL10n);
assert.isUndefined(pageActions[1]);
assert.notCalled(pageActions[2].reloadL10n);
});
});
});
});
|