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
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// See also browser/base/content/test/newtab/.
const { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
// A small 1x1 test png
const image1x1 =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg==";
function getBookmarksSize() {
return NewTabUtils.activityStreamProvider.executePlacesQuery(
"SELECT count(*) FROM moz_bookmarks WHERE type = :type",
{ params: { type: PlacesUtils.bookmarks.TYPE_BOOKMARK } }
);
}
function getHistorySize() {
return NewTabUtils.activityStreamProvider.executePlacesQuery(
"SELECT count(*) FROM moz_places WHERE hidden = 0 AND last_visit_date NOT NULL"
);
}
add_task(async function validCacheMidPopulation() {
let expectedLinks = makeLinks(0, 3, 1);
let provider = new TestProvider(done => done(expectedLinks));
provider.maxNumLinks = expectedLinks.length;
NewTabUtils.initWithoutProviders();
NewTabUtils.links.addProvider(provider);
let promise = new Promise(resolve =>
NewTabUtils.links.populateCache(resolve)
);
// isTopSiteGivenProvider() and getProviderLinks() should still return results
// even when cache is empty or being populated.
Assert.ok(!NewTabUtils.isTopSiteGivenProvider("example1.com", provider));
do_check_links(NewTabUtils.getProviderLinks(provider), []);
await promise;
// Once the cache is populated, we get the expected results
Assert.ok(NewTabUtils.isTopSiteGivenProvider("example1.com", provider));
do_check_links(NewTabUtils.getProviderLinks(provider), expectedLinks);
NewTabUtils.links.removeProvider(provider);
});
add_task(async function notifyLinkDelete() {
let expectedLinks = makeLinks(0, 3, 1);
let provider = new TestProvider(done => done(expectedLinks));
provider.maxNumLinks = expectedLinks.length;
NewTabUtils.initWithoutProviders();
NewTabUtils.links.addProvider(provider);
await new Promise(resolve => NewTabUtils.links.populateCache(resolve));
do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
// Remove a link.
let removedLink = expectedLinks[2];
provider.notifyLinkChanged(removedLink, 2, true);
let links = NewTabUtils.links._providers.get(provider);
// Check that sortedLinks is correctly updated.
do_check_links(NewTabUtils.links.getLinks(), expectedLinks.slice(0, 2));
// Check that linkMap is accurately updated.
Assert.equal(links.linkMap.size, 2);
Assert.ok(links.linkMap.get(expectedLinks[0].url));
Assert.ok(links.linkMap.get(expectedLinks[1].url));
Assert.ok(!links.linkMap.get(removedLink.url));
// Check that siteMap is correctly updated.
Assert.equal(links.siteMap.size, 2);
Assert.ok(links.siteMap.has(NewTabUtils.extractSite(expectedLinks[0].url)));
Assert.ok(links.siteMap.has(NewTabUtils.extractSite(expectedLinks[1].url)));
Assert.ok(!links.siteMap.has(NewTabUtils.extractSite(removedLink.url)));
NewTabUtils.links.removeProvider(provider);
});
add_task(async function populatePromise() {
let count = 0;
let expectedLinks = makeLinks(0, 10, 2);
let getLinksFcn = async function (callback) {
// Should not be calling getLinksFcn twice
count++;
Assert.equal(count, 1);
await Promise.resolve();
callback(expectedLinks);
};
let provider = new TestProvider(getLinksFcn);
NewTabUtils.initWithoutProviders();
NewTabUtils.links.addProvider(provider);
NewTabUtils.links.populateProviderCache(provider, () => {});
NewTabUtils.links.populateProviderCache(provider, () => {
do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
NewTabUtils.links.removeProvider(provider);
});
});
add_task(async function isTopSiteGivenProvider() {
let expectedLinks = makeLinks(0, 10, 2);
// The lowest 2 frecencies have the same base domain.
expectedLinks[expectedLinks.length - 2].url =
expectedLinks[expectedLinks.length - 1].url + "Test";
let provider = new TestProvider(done => done(expectedLinks));
provider.maxNumLinks = expectedLinks.length;
NewTabUtils.initWithoutProviders();
NewTabUtils.links.addProvider(provider);
await new Promise(resolve => NewTabUtils.links.populateCache(resolve));
Assert.equal(
NewTabUtils.isTopSiteGivenProvider("example2.com", provider),
true
);
Assert.equal(
NewTabUtils.isTopSiteGivenProvider("example1.com", provider),
false
);
// Push out frecency 2 because the maxNumLinks is reached when adding frecency 3
let newLink = makeLink(3);
provider.notifyLinkChanged(newLink);
// There is still a frecent url with example2 domain, so it's still frecent.
Assert.equal(
NewTabUtils.isTopSiteGivenProvider("example3.com", provider),
true
);
Assert.equal(
NewTabUtils.isTopSiteGivenProvider("example2.com", provider),
true
);
// Push out frecency 3
newLink = makeLink(5);
provider.notifyLinkChanged(newLink);
// Push out frecency 4
newLink = makeLink(9);
provider.notifyLinkChanged(newLink);
// Our count reached 0 for the example2.com domain so it's no longer a frecent site.
Assert.equal(
NewTabUtils.isTopSiteGivenProvider("example5.com", provider),
true
);
Assert.equal(
NewTabUtils.isTopSiteGivenProvider("example2.com", provider),
false
);
NewTabUtils.links.removeProvider(provider);
});
add_task(async function multipleProviders() {
// Make each provider generate NewTabUtils.links.maxNumLinks links to check
// that no more than maxNumLinks are actually returned in the merged list.
let evenLinks = makeLinks(0, 2 * NewTabUtils.links.maxNumLinks, 2);
let evenProvider = new TestProvider(done => done(evenLinks));
let oddLinks = makeLinks(0, 2 * NewTabUtils.links.maxNumLinks - 1, 2);
let oddProvider = new TestProvider(done => done(oddLinks));
NewTabUtils.initWithoutProviders();
NewTabUtils.links.addProvider(evenProvider);
NewTabUtils.links.addProvider(oddProvider);
await new Promise(resolve => NewTabUtils.links.populateCache(resolve));
let links = NewTabUtils.links.getLinks();
let expectedLinks = makeLinks(
NewTabUtils.links.maxNumLinks,
2 * NewTabUtils.links.maxNumLinks,
1
);
Assert.equal(links.length, NewTabUtils.links.maxNumLinks);
do_check_links(links, expectedLinks);
NewTabUtils.links.removeProvider(evenProvider);
NewTabUtils.links.removeProvider(oddProvider);
});
add_task(async function changeLinks() {
let expectedLinks = makeLinks(0, 20, 2);
let provider = new TestProvider(done => done(expectedLinks));
NewTabUtils.initWithoutProviders();
NewTabUtils.links.addProvider(provider);
await new Promise(resolve => NewTabUtils.links.populateCache(resolve));
do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
// Notify of a new link.
let newLink = makeLink(19);
expectedLinks.splice(1, 0, newLink);
provider.notifyLinkChanged(newLink);
do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
// Notify of a link that's changed sort criteria.
newLink.frecency = 17;
expectedLinks.splice(1, 1);
expectedLinks.splice(2, 0, newLink);
provider.notifyLinkChanged({
url: newLink.url,
frecency: 17,
});
do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
// Notify of a link that's changed title.
newLink.title = "My frecency is now 17";
provider.notifyLinkChanged({
url: newLink.url,
title: newLink.title,
});
do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
// Notify of a new link again, but this time make it overflow maxNumLinks.
provider.maxNumLinks = expectedLinks.length;
newLink = makeLink(21);
expectedLinks.unshift(newLink);
expectedLinks.pop();
Assert.equal(expectedLinks.length, provider.maxNumLinks); // Sanity check.
provider.notifyLinkChanged(newLink);
do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
// Notify of many links changed.
expectedLinks = makeLinks(0, 3, 1);
provider.notifyManyLinksChanged();
// Since _populateProviderCache() is async, we must wait until the provider's
// populate promise has been resolved.
await NewTabUtils.links._providers.get(provider).populatePromise;
// NewTabUtils.links will now repopulate its cache
do_check_links(NewTabUtils.links.getLinks(), expectedLinks);
NewTabUtils.links.removeProvider(provider);
});
add_task(async function oneProviderAlreadyCached() {
let links1 = makeLinks(0, 10, 1);
let provider1 = new TestProvider(done => done(links1));
NewTabUtils.initWithoutProviders();
NewTabUtils.links.addProvider(provider1);
await new Promise(resolve => NewTabUtils.links.populateCache(resolve));
do_check_links(NewTabUtils.links.getLinks(), links1);
let links2 = makeLinks(10, 20, 1);
let provider2 = new TestProvider(done => done(links2));
NewTabUtils.links.addProvider(provider2);
await new Promise(resolve => NewTabUtils.links.populateCache(resolve));
do_check_links(NewTabUtils.links.getLinks(), links2.concat(links1));
NewTabUtils.links.removeProvider(provider1);
NewTabUtils.links.removeProvider(provider2);
});
add_task(async function newLowRankedLink() {
// Init a provider with 10 links and make its maximum number also 10.
let links = makeLinks(0, 10, 1);
let provider = new TestProvider(done => done(links));
provider.maxNumLinks = links.length;
NewTabUtils.initWithoutProviders();
NewTabUtils.links.addProvider(provider);
await new Promise(resolve => NewTabUtils.links.populateCache(resolve));
do_check_links(NewTabUtils.links.getLinks(), links);
// Notify of a new link that's low-ranked enough not to make the list.
let newLink = makeLink(0);
provider.notifyLinkChanged(newLink);
do_check_links(NewTabUtils.links.getLinks(), links);
// Notify about the new link's title change.
provider.notifyLinkChanged({
url: newLink.url,
title: "a new title",
});
do_check_links(NewTabUtils.links.getLinks(), links);
NewTabUtils.links.removeProvider(provider);
});
add_task(async function extractSite() {
// All these should extract to the same site
[
"mozilla.org",
"m.mozilla.org",
"mobile.mozilla.org",
"www.mozilla.org",
"www3.mozilla.org",
].forEach(host => {
let url = "http://" + host;
Assert.equal(
NewTabUtils.extractSite(url),
"mozilla.org",
"extracted same " + host
);
});
// All these should extract to the same subdomain
["bugzilla.mozilla.org", "www.bugzilla.mozilla.org"].forEach(host => {
let url = "http://" + host;
Assert.equal(
NewTabUtils.extractSite(url),
"bugzilla.mozilla.org",
"extracted eTLD+2 " + host
);
});
// All these should not extract to the same site
[
"bugzilla.mozilla.org",
"bug123.bugzilla.mozilla.org",
"too.many.levels.bugzilla.mozilla.org",
"m2.mozilla.org",
"mobile30.mozilla.org",
"ww.mozilla.org",
"ww2.mozilla.org",
"wwwww.mozilla.org",
"wwwww50.mozilla.org",
"wwws.mozilla.org",
"secure.mozilla.org",
"secure10.mozilla.org",
"many.levels.deep.mozilla.org",
"just.check.in",
"192.168.0.1",
"localhost",
].forEach(host => {
let url = "http://" + host;
Assert.notEqual(
NewTabUtils.extractSite(url),
"mozilla.org",
"extracted diff " + host
);
});
// All these should not extract to the same site
[
"about:blank",
"file:///Users/user/file",
"chrome://browser/something",
"ftp://ftp.mozilla.org/",
].forEach(url => {
Assert.notEqual(
NewTabUtils.extractSite(url),
"mozilla.org",
"extracted diff url " + url
);
});
});
add_task(async function faviconBytesToDataURI() {
let tests = [
[{ favicon: "bar".split("").map(s => s.charCodeAt(0)), mimeType: "foo" }],
[
{
favicon: "bar".split("").map(s => s.charCodeAt(0)),
mimeType: "foo",
xxyy: "quz",
},
],
];
let provider = NewTabUtils.activityStreamProvider;
for (let test of tests) {
let clone = JSON.parse(JSON.stringify(test));
delete clone[0].mimeType;
clone[0].favicon = `data:foo;base64,${btoa("bar")}`;
let result = provider._faviconBytesToDataURI(test);
Assert.deepEqual(
JSON.stringify(clone),
JSON.stringify(result),
"favicon converted to data uri"
);
}
});
add_task(async function addFavicons() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamProvider;
// start by passing in a bad uri and check that we get a null favicon back
let links = [{ url: "mozilla.com" }];
await provider._addFavicons(links);
Assert.equal(
links[0].favicon,
null,
"Got a null favicon because we passed in a bad url"
);
Assert.equal(
links[0].mimeType,
null,
"Got a null mime type because we passed in a bad url"
);
Assert.equal(
links[0].faviconSize,
null,
"Got a null favicon size because we passed in a bad url"
);
// now fix the url and try again - this time we get good favicon data back
// a 1x1 favicon as a data URI of mime type image/png
const base64URL = image1x1;
links[0].url = "https://mozilla.com";
let visit = [
{
uri: links[0].url,
visitDate: timeDaysAgo(0),
transition: PlacesUtils.history.TRANSITION_TYPED,
},
];
await PlacesTestUtils.addVisits(visit);
let faviconData = new Map();
faviconData.set("https://mozilla.com", `${base64URL}#tippytop`);
await PlacesTestUtils.addFavicons(faviconData);
await provider._addFavicons(links);
Assert.equal(
links[0].mimeType,
"image/png",
"Got the right mime type before deleting it"
);
Assert.equal(
links[0].faviconLength,
links[0].favicon.length,
"Got the right length for the byte array"
);
Assert.equal(
provider._faviconBytesToDataURI(links)[0].favicon,
base64URL,
"Got the right favicon"
);
Assert.equal(
links[0].faviconSize,
1,
"Got the right favicon size (width and height of favicon)"
);
Assert.equal(links[0].faviconRef, "tippytop", "Got the favicon url ref");
// Check with http version of the link that doesn't have its own
const nonHttps = [{ url: links[0].url.replace("https", "http") }];
await provider._addFavicons(nonHttps);
Assert.equal(
provider._faviconBytesToDataURI(nonHttps)[0].favicon,
base64URL,
"Got the same favicon"
);
Assert.equal(
nonHttps[0].faviconLength,
links[0].faviconLength,
"Got the same favicon length"
);
Assert.equal(
nonHttps[0].faviconSize,
links[0].faviconSize,
"Got the same favicon size"
);
Assert.equal(
nonHttps[0].mimeType,
links[0].mimeType,
"Got the same mime type"
);
// Check that we do not collect favicons for pocket items
const pocketItems = [
{ url: links[0].url },
{ url: "https://mozilla1.com", type: "pocket" },
];
await provider._addFavicons(pocketItems);
Assert.equal(
provider._faviconBytesToDataURI(pocketItems)[0].favicon,
base64URL,
"Added favicon data only to the non-pocket item"
);
Assert.equal(
pocketItems[1].favicon,
null,
"Did not add a favicon to the pocket item"
);
Assert.equal(
pocketItems[1].mimeType,
null,
"Did not add mimeType to the pocket item"
);
Assert.equal(
pocketItems[1].faviconSize,
null,
"Did not add a faviconSize to the pocket item"
);
});
add_task(async function getHighlightsWithoutPocket() {
const addMetadata = url =>
PlacesUtils.history.update({
description: "desc",
previewImageURL: "https://image/",
url,
});
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
let links = await provider.getHighlights();
Assert.equal(links.length, 0, "empty history yields empty links");
// Add bookmarks
const now = Date.now();
const oldSeconds = 24 * 60 * 60; // 1 day old
let bookmarks = [
{
dateAdded: new Date(now - oldSeconds * 1000),
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
title: "foo",
url: "https://mozilla1.com/dayOld",
},
{
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
title: "foo",
url: "https://mozilla1.com/nowNew",
},
];
for (let placeInfo of bookmarks) {
await PlacesUtils.bookmarks.insert(placeInfo);
}
links = await provider.getHighlights();
Assert.equal(
links.length,
0,
"adding bookmarks without visits doesn't yield more links"
);
// Add a history visit
let testURI = "http://mozilla.com/";
await PlacesTestUtils.addVisits(testURI);
links = await provider.getHighlights();
Assert.equal(
links.length,
0,
"adding visits without metadata doesn't yield more links"
);
// Add bookmark visits
for (let placeInfo of bookmarks) {
await PlacesTestUtils.addVisits(placeInfo.url);
}
links = await provider.getHighlights();
Assert.equal(links.length, 2, "adding visits to bookmarks yields more links");
Assert.equal(
links[0].url,
bookmarks[1].url,
"first bookmark is younger bookmark"
);
Assert.equal(links[0].type, "bookmark", "first bookmark is bookmark");
Assert.ok(links[0].date_added, "got a date_added for the bookmark");
Assert.equal(
links[1].url,
bookmarks[0].url,
"second bookmark is older bookmark"
);
Assert.equal(links[1].type, "bookmark", "second bookmark is bookmark");
Assert.ok(links[1].date_added, "got a date_added for the bookmark");
// Add metadata to history
await addMetadata(testURI);
links = await provider.getHighlights();
Assert.equal(links.length, 3, "adding metadata yield more links");
Assert.equal(links[0].url, bookmarks[1].url, "still have younger bookmark");
Assert.equal(links[1].url, bookmarks[0].url, "still have older bookmark");
Assert.equal(links[2].url, testURI, "added visit corresponds to added url");
Assert.equal(links[2].type, "history", "added visit is history");
links = await provider.getHighlights({ numItems: 2 });
Assert.equal(links.length, 2, "limited to 2 items");
Assert.equal(links[0].url, bookmarks[1].url, "still have younger bookmark");
Assert.equal(links[1].url, bookmarks[0].url, "still have older bookmark");
links = await provider.getHighlights({ excludeHistory: true });
Assert.equal(links.length, 2, "only have bookmarks");
Assert.equal(links[0].url, bookmarks[1].url, "still have younger bookmark");
Assert.equal(links[1].url, bookmarks[0].url, "still have older bookmark");
links = await provider.getHighlights({ excludeBookmarks: true });
Assert.equal(links.length, 1, "only have history");
Assert.equal(links[0].url, testURI, "only have the history now");
links = await provider.getHighlights({
excludeBookmarks: true,
excludeHistory: true,
});
Assert.equal(links.length, 0, "requested nothing, get nothing");
links = await provider.getHighlights({ bookmarkSecondsAgo: oldSeconds / 2 });
Assert.equal(links.length, 2, "old bookmark filtered out with");
Assert.equal(links[0].url, bookmarks[1].url, "still have newer bookmark");
Assert.equal(links[1].url, testURI, "still have the history");
// Add a visit and metadata to the older bookmark
await PlacesTestUtils.addVisits(bookmarks[0].url);
await addMetadata(bookmarks[0].url);
links = await provider.getHighlights({ bookmarkSecondsAgo: oldSeconds / 2 });
Assert.equal(links.length, 3, "old bookmark returns as history");
Assert.equal(links[0].url, bookmarks[1].url, "still have newer bookmark");
Assert.equal(
links[1].url,
bookmarks[0].url,
"old bookmark now is newer history"
);
Assert.equal(links[1].type, "history", "old bookmark now is history");
Assert.equal(links[2].url, testURI, "still have the history");
// Bookmark the history item
await PlacesUtils.bookmarks.insert({
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
title: "now a bookmark",
url: testURI,
});
links = await provider.getHighlights();
Assert.equal(
links.length,
3,
"a visited bookmark doesn't appear as bookmark and history"
);
Assert.equal(
links[0].url,
testURI,
"history is now the first, i.e., most recent, bookmark"
);
Assert.equal(links[0].type, "bookmark", "was history now bookmark");
Assert.ok(links[0].date_added, "got a date_added for the now bookmark");
Assert.equal(
links[1].url,
bookmarks[1].url,
"still have younger bookmark now second"
);
Assert.equal(
links[2].url,
bookmarks[0].url,
"still have older bookmark now third"
);
// Test the `withFavicons` option.
await PlacesTestUtils.addFavicons(new Map([[testURI, image1x1]]));
links = await provider.getHighlights({ withFavicons: true });
Assert.equal(links.length, 3, "We're not expecting a change in links");
Assert.equal(links[0].favicon, image1x1, "Link 1 should contain a favicon");
Assert.equal(links[1].favicon, null, "Link 2 has no favicon data");
Assert.equal(links[2].favicon, null, "Link 3 has no favicon data");
});
add_task(async function getHighlightsWithPocketSuccess() {
await setUpActivityStreamTest();
// Add a bookmark
let bookmark = {
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
title: "foo",
description: "desc",
preview_image_url: "foo.com/img.png",
url: "https://mozilla1.com/",
};
const fakeResponse = {
list: {
123: {
time_added: "123",
image: { src: "foo.com/img.png" },
excerpt: "A description for foo",
resolved_title: "A title for foo",
resolved_url: "http://www.foo.com",
item_id: "123",
open_url: "http://www.getpocket.com/itemID",
status: "0",
},
456: {
item_id: "456",
status: "2",
},
},
};
await PlacesUtils.bookmarks.insert(bookmark);
await PlacesTestUtils.addVisits(bookmark.url);
NewTabUtils.activityStreamProvider.fetchSavedPocketItems = () => fakeResponse;
let provider = NewTabUtils.activityStreamLinks;
// Force a cache invalidation
NewTabUtils.activityStreamLinks._pocketLastUpdated =
Date.now() - 70 * 60 * 1000;
NewTabUtils.activityStreamLinks._pocketLastLatest = -1;
let links = await provider.getHighlights();
// We should have 1 bookmark followed by 1 pocket story in highlights
// We should not have stored the second pocket item since it was deleted
Assert.equal(links.length, 2, "Should have 2 links in highlights");
// First highlight should be a bookmark
Assert.equal(links[0].url, bookmark.url, "The first link is the bookmark");
// Second highlight should be a Pocket item with the correct fields to display
let pocketItem = fakeResponse.list["123"];
let currentLink = links[1];
Assert.equal(currentLink.url, pocketItem.resolved_url, "Correct Pocket item");
Assert.equal(currentLink.type, "pocket", "Attached the correct type");
Assert.equal(
currentLink.preview_image_url,
pocketItem.image.src,
"Correct preview image was added"
);
Assert.equal(
currentLink.title,
pocketItem.resolved_title,
"Correct title was added"
);
Assert.equal(
currentLink.description,
pocketItem.excerpt,
"Correct description was added"
);
Assert.equal(
currentLink.pocket_id,
pocketItem.item_id,
"item_id was preserved"
);
Assert.equal(
currentLink.open_url,
`${pocketItem.open_url}?src=fx_new_tab`,
"open_url was preserved"
);
Assert.equal(
currentLink.date_added,
pocketItem.time_added * 1000,
"date_added was added to pocket item"
);
NewTabUtils.activityStreamLinks._savedPocketStories = null;
});
add_task(async function getHighlightsWithPocketCached() {
await setUpActivityStreamTest();
let fakeResponse = {
list: {
123: {
time_added: "123",
image: { src: "foo.com/img.png" },
excerpt: "A description for foo",
resolved_title: "A title for foo",
resolved_url: "http://www.foo.com",
item_id: "123",
open_url: "http://www.getpocket.com/itemID",
status: "0",
},
456: {
item_id: "456",
status: "2",
},
},
};
NewTabUtils.activityStreamProvider.fetchSavedPocketItems = () => fakeResponse;
let provider = NewTabUtils.activityStreamLinks;
let links = await provider.getHighlights();
Assert.equal(
links.length,
1,
"Sanity check that we got 1 link back for highlights"
);
Assert.equal(
links[0].url,
fakeResponse.list["123"].resolved_url,
"Sanity check that it was the pocket story"
);
// Update what the response would be
fakeResponse.list["789"] = {
time_added: "123",
image: { src: "bar.com/img.png" },
excerpt: "A description for bar",
resolved_title: "A title for bar",
resolved_url: "http://www.bar.com",
item_id: "789",
open_url: "http://www.getpocket.com/itemID",
status: "0",
};
// Call getHighlights again - this time we should get the cached links since we just updated
links = await provider.getHighlights();
Assert.equal(links.length, 1, "We still got 1 link back for highlights");
Assert.equal(
links[0].url,
fakeResponse.list["123"].resolved_url,
"It was still the same pocket story"
);
// Now force a cache invalidation and call getHighlights again
NewTabUtils.activityStreamLinks._pocketLastUpdated =
Date.now() - 70 * 60 * 1000;
NewTabUtils.activityStreamLinks._pocketLastLatest = -1;
links = await provider.getHighlights();
Assert.equal(
links.length,
2,
"This time we got fresh links with the new response"
);
Assert.equal(
links[0].url,
fakeResponse.list["123"].resolved_url,
"First link is unchanged"
);
Assert.equal(
links[1].url,
fakeResponse.list["789"].resolved_url,
"Second link is the new link"
);
NewTabUtils.activityStreamLinks._savedPocketStories = null;
});
add_task(async function getHighlightsWithPocketFailure() {
await setUpActivityStreamTest();
NewTabUtils.activityStreamProvider.fetchSavedPocketItems = function () {
throw new Error();
};
let provider = NewTabUtils.activityStreamLinks;
// Force a cache invalidation
NewTabUtils.activityStreamLinks._pocketLastUpdated =
Date.now() - 70 * 60 * 1000;
NewTabUtils.activityStreamLinks._pocketLastLatest = -1;
let links = await provider.getHighlights();
Assert.equal(links.length, 0, "Return empty links if we reject the promise");
});
add_task(async function getHighlightsWithPocketNoData() {
await setUpActivityStreamTest();
NewTabUtils.activityStreamProvider.fetchSavedPocketItems = () => {};
let provider = NewTabUtils.activityStreamLinks;
// Force a cache invalidation
NewTabUtils.activityStreamLinks._pocketLastUpdated =
Date.now() - 70 * 60 * 1000;
NewTabUtils.activityStreamLinks._pocketLastLatest = -1;
let links = await provider.getHighlights();
Assert.equal(
links.length,
0,
"Return empty links if we got no data back from the response"
);
});
add_task(async function getTopFrecentSites() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
let links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 0, "empty history yields empty links");
// add a visit
let testURI = "http://mozilla.com/";
await PlacesTestUtils.addVisits(testURI);
links = await provider.getTopSites();
Assert.equal(
links.length,
0,
"adding a single visit doesn't exceed default threshold"
);
links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 1, "adding a visit yields a link");
Assert.equal(links[0].url, testURI, "added visit corresponds to added url");
});
add_task(
{
skip_if: () =>
AppConstants.MOZ_APP_NAME == "thunderbird" ||
Services.prefs.getBoolPref(
"browser.topsites.useRemoteSetting"
) /* see bug 1664502 */,
},
async function getTopFrecentSites_improveSearch() {
await setUpActivityStreamTest();
const SEARCH_SHORTCUTS_EXPERIMENT_PREF =
"browser.newtabpage.activity-stream.improvesearch.topSiteSearchShortcuts";
Services.prefs.setBoolPref(SEARCH_SHORTCUTS_EXPERIMENT_PREF, true);
let testURI = "https://www.amazon.com?search=tv";
await PlacesTestUtils.addVisits(testURI);
let provider = NewTabUtils.activityStreamLinks;
let links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(
links.length,
1,
"sanity check that we got the link from top sites"
);
Assert.equal(
links[0].url,
"https://amazon.com",
"the amazon site was converted to generic search shortcut site"
);
Services.prefs.setBoolPref(SEARCH_SHORTCUTS_EXPERIMENT_PREF, false);
}
);
add_task(async function getTopFrecentSites_no_dedup() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
let links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 0, "empty history yields empty links");
// Add a visits in reverse order they will be returned in when not deduped.
let testURIs = [
{ uri: "http://www.mozilla.com/" },
{ uri: "http://mozilla.com/" },
];
await PlacesTestUtils.addVisits(testURIs);
links = await provider.getTopSites();
Assert.equal(
links.length,
0,
"adding a single visit doesn't exceed default threshold"
);
links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 1, "adding a visit yields a link");
// Plain domain is returned when deduped.
Assert.equal(
links[0].url,
testURIs[1].uri,
"added visit corresponds to added url"
);
links = await provider.getTopSites({
topsiteFrecency: 100,
onePerDomain: false,
});
Assert.equal(links.length, 2, "adding a visit yields a link");
Assert.equal(
links[0].url,
testURIs[1].uri,
"added visit corresponds to added url"
);
Assert.equal(
links[1].url,
testURIs[0].uri,
"added visit corresponds to added url"
);
});
add_task(async function getTopFrecentSites_dedupeWWW() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
let links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 0, "empty history yields empty links");
// add a visit without www
let testURI = "http://mozilla.com";
await PlacesTestUtils.addVisits(testURI);
// add a visit with www
testURI = "http://www.mozilla.com";
await PlacesTestUtils.addVisits(testURI);
// Test combined frecency score
links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 1, "adding both www. and no-www. yields one link");
Assert.equal(links[0].frecency, 200, "frecency scores are combined");
// add another page visit with www and without www
let noWWW = "http://mozilla.com/page";
await PlacesTestUtils.addVisits(noWWW);
let withWWW = "http://www.mozilla.com/page";
await PlacesTestUtils.addVisits(withWWW);
links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 1, "adding both www. and no-www. yields one link");
Assert.equal(
links[0].frecency,
200,
"frecency scores are combined ignoring extra pages"
);
// add another visit with www
await PlacesTestUtils.addVisits(withWWW);
links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 1, "still yields one link");
Assert.equal(links[0].url, withWWW, "more frecent www link is used");
Assert.equal(
links[0].frecency,
300,
"frecency scores are combined ignoring extra pages"
);
// add a couple more visits to the no-www page
await PlacesTestUtils.addVisits(noWWW);
await PlacesTestUtils.addVisits(noWWW);
links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 1, "still yields one link");
Assert.equal(links[0].url, noWWW, "now more frecent no-www link is used");
Assert.equal(
links[0].frecency,
500,
"frecency scores are combined ignoring extra pages"
);
});
add_task(async function getTopFrencentSites_maxLimit() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
// add many visits
const MANY_LINKS = 20;
for (let i = 0; i < MANY_LINKS; i++) {
let testURI = `http://mozilla${i}.com`;
await PlacesTestUtils.addVisits(testURI);
}
let links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.ok(
links.length < MANY_LINKS,
"query default limited to less than many"
);
Assert.greater(links.length, 6, "query default to more than visible count");
});
add_task(async function getTopFrencentSites_allowedProtocols() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
// add a visit from a file:// site
let testURI = "file:///some/file/path.png";
await PlacesTestUtils.addVisits(testURI);
let links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 0, "don't get sites with the file:// protocol");
// now add a site with an allowed protocol
testURI = "http://www.mozilla.com";
await PlacesTestUtils.addVisits(testURI);
links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(links.length, 1, "http:// is an allowed protocol");
// and just to be sure, add a visit to a site with ftp:// protocol
testURI = "ftp://bad/example";
await PlacesTestUtils.addVisits(testURI);
links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(
links.length,
1,
"we still only accept http:// and https:// for top sites"
);
// add a different allowed protocol
testURI = "https://https";
await PlacesTestUtils.addVisits(testURI);
links = await provider.getTopSites({ topsiteFrecency: 100 });
Assert.equal(
links.length,
2,
"we now accept both http:// and https:// for top sites"
);
});
add_task(async function getTopFrecentSites_order() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
let { TRANSITION_TYPED } = PlacesUtils.history;
let timeEarlier = timeDaysAgo(0);
let timeLater = timeDaysAgo(2);
let visits = [
// frecency 200
{
uri: "https://mozilla1.com/0",
visitDate: timeEarlier,
transition: TRANSITION_TYPED,
},
// sort by url, frecency 200
{
uri: "https://mozilla2.com/1",
visitDate: timeEarlier,
transition: TRANSITION_TYPED,
},
// sort by last visit date, frecency 200
{
uri: "https://mozilla3.com/2",
visitDate: timeLater,
transition: TRANSITION_TYPED,
},
// sort by frecency, frecency 10
{ uri: "https://mozilla0.com/", visitDate: timeLater },
];
let links = await provider.getTopSites({ topsiteFrecency: 0 });
Assert.equal(links.length, 0, "empty history yields empty links");
// map of page url to favicon url
let faviconData = new Map();
faviconData.set("https://mozilla3.com/2", image1x1);
await PlacesTestUtils.addVisits(visits);
await PlacesTestUtils.addFavicons(faviconData);
links = await provider.getTopSites({ topsiteFrecency: 0 });
Assert.equal(
links.length,
visits.length,
"number of links added is the same as obtain by getTopFrecentSites"
);
// first link doesn't have a favicon
Assert.equal(
links[0].url,
visits[0].uri,
"links are obtained in the expected order"
);
Assert.equal(null, links[0].favicon, "favicon data is stored as expected");
Assert.ok(
isVisitDateOK(links[0].lastVisitDate),
"visit date within expected range"
);
// second link doesn't have a favicon
Assert.equal(
links[1].url,
visits[1].uri,
"links are obtained in the expected order"
);
Assert.equal(null, links[1].favicon, "favicon data is stored as expected");
Assert.ok(
isVisitDateOK(links[1].lastVisitDate),
"visit date within expected range"
);
// third link should have the favicon data that we added
Assert.equal(
links[2].url,
visits[2].uri,
"links are obtained in the expected order"
);
Assert.equal(
faviconData.get(links[2].url),
links[2].favicon,
"favicon data is stored as expected"
);
Assert.ok(
isVisitDateOK(links[2].lastVisitDate),
"visit date within expected range"
);
// fourth link doesn't have a favicon
Assert.equal(
links[3].url,
visits[3].uri,
"links are obtained in the expected order"
);
Assert.equal(null, links[3].favicon, "favicon data is stored as expected");
Assert.ok(
isVisitDateOK(links[3].lastVisitDate),
"visit date within expected range"
);
});
add_task(async function getTopFrecentSites_hideWithSearchParam() {
await setUpActivityStreamTest();
let pref = "browser.newtabpage.activity-stream.hideTopSitesWithSearchParam";
let provider = NewTabUtils.activityStreamLinks;
// This maps URL search params to objects describing whether a URL with those
// params is expected to be included in the returned links. Each object maps
// from effective hide-with search params to whether the URL is expected to be
// included.
let tests = {
"": {
"": true,
test: true,
"test=": true,
"test=hide": true,
nomatch: true,
"nomatch=": true,
"nomatch=hide": true,
},
test: {
"": true,
test: false,
"test=": false,
"test=hide": true,
nomatch: true,
"nomatch=": true,
"nomatch=hide": true,
},
"test=hide": {
"": true,
test: false,
"test=": true,
"test=hide": false,
nomatch: true,
"nomatch=": true,
"nomatch=hide": true,
},
"test=foo&test=hide": {
"": true,
test: false,
"test=": true,
"test=hide": false,
nomatch: true,
"nomatch=": true,
"nomatch=hide": true,
},
};
for (let [urlParams, expected] of Object.entries(tests)) {
for (let prefValue of Object.keys(expected)) {
info(
"Running test: " + JSON.stringify({ urlParams, prefValue, expected })
);
// Add a visit to a URL with search params `urlParams`.
let url = new URL("http://example.com/");
url.search = urlParams;
await PlacesTestUtils.addVisits(url);
// Set the pref to `prefValue`.
Services.prefs.setCharPref(pref, prefValue);
// Call `getTopSites()` with all the test values for `hideWithSearchParam`
// plus undefined. When `hideWithSearchParam` is undefined, the pref value
// should be used. Otherwise it should override the pref.
for (let hideWithSearchParam of [undefined, ...Object.keys(expected)]) {
info(
"Calling getTopSites() with hideWithSearchParam: " +
JSON.stringify(hideWithSearchParam)
);
let options = { topsiteFrecency: 100 };
if (hideWithSearchParam !== undefined) {
options = { ...options, hideWithSearchParam };
}
let links = await provider.getTopSites(options);
let effectiveHideWithParam =
hideWithSearchParam === undefined ? prefValue : hideWithSearchParam;
if (expected[effectiveHideWithParam]) {
Assert.equal(links.length, 1, "One link returned");
Assert.equal(links[0].url, url.toString(), "Expected link returned");
} else {
Assert.equal(links.length, 0, "No links returned");
}
}
await PlacesUtils.history.clear();
}
}
Services.prefs.clearUserPref(pref);
});
add_task(async function activitySteamProvider_deleteHistoryLink() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
let { TRANSITION_TYPED } = PlacesUtils.history;
let visits = [
// frecency 200
{
uri: "https://mozilla1.com/0",
visitDate: timeDaysAgo(1),
transition: TRANSITION_TYPED,
},
// sort by url, frecency 200
{ uri: "https://mozilla2.com/1", visitDate: timeDaysAgo(0) },
];
let size = await getHistorySize();
Assert.equal(size, 0, "empty history has size 0");
await PlacesTestUtils.addVisits(visits);
size = await getHistorySize();
Assert.equal(size, 2, "expected history size");
// delete a link
let deleted = await provider.deleteHistoryEntry("https://mozilla2.com/1");
Assert.equal(deleted, true, "link is deleted");
// ensure that there's only one link left
size = await getHistorySize();
Assert.equal(size, 1, "expected history size");
// pin the link and delete it
const linkToPin = { url: "https://mozilla1.com/0" };
NewTabUtils.pinnedLinks.pin(linkToPin, 0);
// sanity check that the correct link was pinned
Assert.equal(
NewTabUtils.pinnedLinks.links.length,
1,
"added a link to pinned sites"
);
Assert.equal(
NewTabUtils.pinnedLinks.isPinned(linkToPin),
true,
"pinned the correct link"
);
// delete the pinned link and ensure it was both deleted from history and unpinned
deleted = await provider.deleteHistoryEntry("https://mozilla1.com/0");
size = await getHistorySize();
Assert.equal(deleted, true, "link is deleted");
Assert.equal(size, 0, "expected history size");
Assert.equal(
NewTabUtils.pinnedLinks.links.length,
0,
"unpinned the deleted link"
);
});
add_task(async function activityStream_deleteBookmark() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
let bookmarks = [
{
url: "https://mozilla1.com/0",
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
},
{
url: "https://mozilla1.com/1",
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
},
];
let bookmarksSize = await getBookmarksSize();
Assert.equal(bookmarksSize, 0, "empty bookmarks yields 0 size");
for (let placeInfo of bookmarks) {
await PlacesUtils.bookmarks.insert(placeInfo);
}
bookmarksSize = await getBookmarksSize();
Assert.equal(bookmarksSize, 2, "size 2 for 2 bookmarks added");
let bookmarkGuid = await new Promise(resolve =>
PlacesUtils.bookmarks.fetch({ url: bookmarks[0].url }, bookmark =>
resolve(bookmark.guid)
)
);
await provider.deleteBookmark(bookmarkGuid);
Assert.strictEqual(
await PlacesUtils.bookmarks.fetch(bookmarkGuid),
null,
"the bookmark should no longer be found"
);
bookmarksSize = await getBookmarksSize();
Assert.equal(bookmarksSize, 1, "size 1 after deleting");
});
add_task(async function activityStream_blockedURLs() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamLinks;
NewTabUtils.blockedLinks.addObserver(provider);
let { TRANSITION_TYPED } = PlacesUtils.history;
let timeToday = timeDaysAgo(0);
let timeEarlier = timeDaysAgo(2);
let visits = [
{
uri: "https://example1.com/",
visitDate: timeToday,
transition: TRANSITION_TYPED,
},
{
uri: "https://example2.com/",
visitDate: timeToday,
transition: TRANSITION_TYPED,
},
{
uri: "https://example3.com/",
visitDate: timeEarlier,
transition: TRANSITION_TYPED,
},
{
uri: "https://example4.com/",
visitDate: timeEarlier,
transition: TRANSITION_TYPED,
},
];
await PlacesTestUtils.addVisits(visits);
await PlacesUtils.bookmarks.insert({
url: "https://example5.com/",
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
});
let sizeQueryResult;
// bookmarks
sizeQueryResult = await getBookmarksSize();
Assert.equal(sizeQueryResult, 1, "got the correct bookmark size");
});
add_task(async function activityStream_getTotalBookmarksCount() {
await setUpActivityStreamTest();
let provider = NewTabUtils.activityStreamProvider;
let bookmarks = [
{
url: "https://mozilla1.com/0",
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
},
{
url: "https://mozilla1.com/1",
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
},
];
let bookmarksSize = await provider.getTotalBookmarksCount();
Assert.equal(
bookmarksSize,
0,
".getTotalBookmarksCount() returns 0 for an empty bookmarks table"
);
for (const bookmark of bookmarks) {
await PlacesUtils.bookmarks.insert(bookmark);
}
bookmarksSize = await provider.getTotalBookmarksCount();
Assert.equal(
bookmarksSize,
2,
".getTotalBookmarksCount() returns 2 after 2 bookmarks are inserted"
);
});
function TestProvider(getLinksFn) {
this.getLinks = getLinksFn;
this._observers = new Set();
}
TestProvider.prototype = {
addObserver(observer) {
this._observers.add(observer);
},
notifyLinkChanged(link, index = -1, deleted = false) {
this._notifyObservers("onLinkChanged", link, index, deleted);
},
notifyManyLinksChanged() {
this._notifyObservers("onManyLinksChanged");
},
_notifyObservers() {
let observerMethodName = arguments[0];
let args = Array.prototype.slice.call(arguments, 1);
args.unshift(this);
for (let obs of this._observers) {
if (obs[observerMethodName]) {
obs[observerMethodName].apply(NewTabUtils.links, args);
}
}
},
};
|