summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/sync/test_bookmark_observer_recorder.js
blob: b4f3d4bd0b3ada073e14481f7a24a8852d20b8f1 (plain)
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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

async function promiseAllURLFrecencies() {
  let frecencies = new Map();
  let db = await PlacesUtils.promiseDBConnection();
  let rows = await db.execute(`
    SELECT url, frecency, recalc_frecency
    FROM moz_places
    WHERE url_hash BETWEEN hash('http', 'prefix_lo') AND
                           hash('http', 'prefix_hi')`);
  for (let row of rows) {
    frecencies.set(row.getResultByName("url"), {
      frecency: row.getResultByName("frecency"),
      recalc: row.getResultByName("recalc_frecency"),
    });
  }
  return frecencies;
}

function mapFilterIterator(iter, fn) {
  let results = [];
  for (let value of iter) {
    let newValue = fn(value);
    if (newValue) {
      results.push(newValue);
    }
  }
  return results;
}

add_task(async function test_update_frecencies() {
  let buf = await openMirror("update_frecencies");

  info("Set up mirror");
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.menuGuid,
    children: [
      {
        // Not modified in mirror; shouldn't recalculate frecency.
        guid: "bookmarkAAAA",
        title: "A",
        url: "http://example.com/a",
      },
      {
        // URL changed to B1 in mirror; should recalculate frecency for B
        // and B1, using existing frecency to determine order.
        guid: "bookmarkBBBB",
        title: "B",
        url: "http://example.com/b",
      },
      {
        // URL changed to new URL in mirror, should recalculate frecency
        // for new URL first, before B1.
        guid: "bookmarkBBB1",
        title: "B1",
        url: "http://example.com/b1",
      },
    ],
  });
  await storeRecords(
    buf,
    [
      {
        id: "menu",
        parentid: "places",
        type: "folder",
        children: ["bookmarkAAAA", "bookmarkBBBB", "bookmarkBBB1"],
      },
      {
        id: "bookmarkAAAA",
        parentid: "menu",
        type: "bookmark",
        title: "A",
        bmkUri: "http://example.com/a",
      },
      {
        id: "bookmarkBBBB",
        parentid: "menu",
        type: "bookmark",
        title: "B",
        bmkUri: "http://example.com/b",
      },
      {
        id: "bookmarkBBB1",
        parentid: "menu",
        type: "bookmark",
        title: "B1",
        bmkUri: "http://example.com/b1",
      },
    ],
    { needsMerge: false }
  );
  await PlacesTestUtils.markBookmarksAsSynced();

  info("Make local changes");
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.menuGuid,
    children: [
      {
        // Query; shouldn't recalculate frecency.
        guid: "queryCCCCCCC",
        title: "C",
        url: "place:type=6&sort=14&maxResults=10",
      },
    ],
  });

  info("Calculate frecencies for all local URLs");
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();

  info("Make remote changes");
  await storeRecords(buf, [
    {
      id: "menu",
      parentid: "places",
      type: "folder",
      children: ["bookmarkAAAA", "bookmarkBBBB", "bookmarkBBB1"],
    },
    {
      id: "unfiled",
      parentid: "places",
      type: "folder",
      children: [
        "bookmarkBBB2",
        "bookmarkDDDD",
        "bookmarkEEEE",
        "queryFFFFFFF",
      ],
    },
    {
      // Existing bookmark changed to existing URL.
      id: "bookmarkBBBB",
      parentid: "menu",
      type: "bookmark",
      title: "B",
      bmkUri: "http://example.com/b1",
    },
    {
      // Existing bookmark with new URL; should recalculate frecency first.
      id: "bookmarkBBB1",
      parentid: "menu",
      type: "bookmark",
      title: "B1",
      bmkUri: "http://example.com/b11",
    },
    {
      id: "bookmarkBBB2",
      parentid: "unfiled",
      type: "bookmark",
      title: "B2",
      bmkUri: "http://example.com/b",
    },
    {
      // New bookmark with new URL; should recalculate frecency first.
      id: "bookmarkDDDD",
      parentid: "unfiled",
      type: "bookmark",
      title: null,
      bmkUri: "http://example.com/d",
    },
    {
      // New bookmark with new URL.
      id: "bookmarkEEEE",
      parentid: "unfiled",
      type: "bookmark",
      title: "E",
      bmkUri: "http://example.com/e",
    },
    {
      // New query; shouldn't count against limit.
      id: "queryFFFFFFF",
      parentid: "unfiled",
      type: "query",
      title: "F",
      bmkUri: `place:parent=${PlacesUtils.bookmarks.menuGuid}`,
    },
  ]);

  info("Apply new items and recalculate 3 frecencies");
  await buf.apply();
  await PlacesFrecencyRecalculator.recalculateSomeFrecencies({ chunkSize: 3 });

  {
    let frecencies = await promiseAllURLFrecencies();
    let urlsWithFrecency = mapFilterIterator(
      frecencies.entries(),
      ([href, { recalc }]) => (recalc == 0 ? href : null)
    );

    // A is unchanged, and we should recalculate frecency for three more
    // random URLs.
    equal(
      urlsWithFrecency.length,
      4,
      "Should keep unchanged frecency and recalculate 3"
    );
    let unexpectedURLs = CommonUtils.difference(
      urlsWithFrecency,
      new Set([
        // A is unchanged.
        "http://example.com/a",

        // B11, D, and E are new URLs.
        "http://example.com/b11",
        "http://example.com/d",
        "http://example.com/e",

        // B and B1 are existing, changed URLs.
        "http://example.com/b",
        "http://example.com/b1",
      ])
    );
    ok(
      !unexpectedURLs.size,
      "Should recalculate frecency for new and changed URLs only"
    );
  }

  info("Change non-URL property of D");
  await storeRecords(buf, [
    {
      id: "bookmarkDDDD",
      parentid: "unfiled",
      type: "bookmark",
      title: "D (remote)",
      bmkUri: "http://example.com/d",
    },
  ]);

  info("Apply new item and recalculate remaining frecencies");
  await buf.apply();
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();

  {
    let frecencies = await promiseAllURLFrecencies();
    let urlsWithoutFrecency = mapFilterIterator(
      frecencies.entries(),
      ([href, { recalc }]) => (recalc == 1 ? href : null)
    );
    deepEqual(
      urlsWithoutFrecency,
      [],
      "Should finish calculating remaining frecencies"
    );
  }

  await buf.finalize();
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesSyncUtils.bookmarks.reset();
});

async function setupLocalTree(localTimeSeconds) {
  let dateAdded = new Date(localTimeSeconds * 1000);
  let lastModified = new Date(localTimeSeconds * 1000);
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.menuGuid,
    children: [
      {
        guid: "folderAAAAAA",
        type: PlacesUtils.bookmarks.TYPE_FOLDER,
        title: "A",
        dateAdded,
        lastModified,
        children: [
          {
            guid: "bookmarkBBBB",
            title: "B",
            url: "http://example.com/b",
            dateAdded,
            lastModified,
          },
          {
            guid: "bookmarkCCCC",
            title: "C",
            url: "http://example.com/c",
            dateAdded,
            lastModified,
          },
        ],
      },
      {
        guid: "bookmarkDDDD",
        title: null,
        url: "http://example.com/d",
        dateAdded,
        lastModified,
      },
    ],
  });
}

// This test ensures we clean up the temp tables between merges, and don't throw
// constraint errors recording observer notifications.
add_task(async function test_apply_then_revert() {
  let buf = await openMirror("apply_then_revert");

  let now = Date.now() / 1000;
  let localTimeSeconds = now - 180;

  info("Set up initial local tree and mirror");
  await setupLocalTree(localTimeSeconds);
  let recordsToUpload = await buf.apply({
    localTimeSeconds,
    remoteTimeSeconds: now,
  });
  await storeChangesInMirror(buf, recordsToUpload);
  await PlacesUtils.bookmarks.insert({
    guid: "bookmarkEEE1",
    parentGuid: PlacesUtils.bookmarks.menuGuid,
    title: "E",
    url: "http://example.com/e",
    dateAdded: new Date(localTimeSeconds * 1000),
    lastModified: new Date(localTimeSeconds * 1000),
  });

  info("Make remote changes");
  await storeRecords(buf, [
    {
      id: "menu",
      parentid: "places",
      type: "folder",
      children: ["bookmarkEEEE", "bookmarkFFFF"],
      modified: now,
    },
    {
      id: "toolbar",
      parentid: "places",
      type: "folder",
      children: ["folderAAAAAA"],
      modified: now,
    },
    {
      id: "folderAAAAAA",
      parentid: "toolbar",
      type: "folder",
      title: "A (remote)",
      children: ["bookmarkCCCC", "bookmarkBBBB"],
      modified: now,
    },
    {
      id: "bookmarkBBBB",
      parentid: "folderAAAAAA",
      type: "bookmark",
      title: "B",
      bmkUri: "http://example.com/b-remote",
      modified: now,
    },
    {
      id: "bookmarkDDDD",
      deleted: true,
      modified: now,
    },
    {
      id: "bookmarkEEEE",
      parentid: "menu",
      type: "bookmark",
      title: "E",
      bmkUri: "http://example.com/e",
      modified: now,
    },
    {
      id: "bookmarkFFFF",
      parentid: "menu",
      type: "bookmark",
      title: "F",
      bmkUri: "http://example.com/f",
      modified: now,
    },
  ]);

  info("Apply remote changes, first time");
  let firstTimeRecords = await buf.apply({
    localTimeSeconds,
    remoteTimeSeconds: now,
  });
  deepEqual(
    await buf.fetchUnmergedGuids(),
    [PlacesUtils.bookmarks.menuGuid],
    "Should leave menu with new remote structure unmerged after first time"
  );

  info("Revert local tree");
  let dateAdded = new Date(localTimeSeconds * 1000);
  await PlacesSyncUtils.bookmarks.wipe();
  await setupLocalTree(localTimeSeconds);
  await PlacesTestUtils.markBookmarksAsSynced();
  await PlacesUtils.bookmarks.insert({
    guid: "bookmarkEEE1",
    parentGuid: PlacesUtils.bookmarks.menuGuid,
    title: "E",
    url: "http://example.com/e",
    dateAdded,
    lastModified: new Date(localTimeSeconds * 1000),
  });
  let localIdForD = await PlacesTestUtils.promiseItemId("bookmarkDDDD");

  info("Apply remote changes, second time");
  await buf.db.execute(
    `
    UPDATE items SET
      needsMerge = 1
    WHERE guid <> :rootGuid`,
    { rootGuid: PlacesUtils.bookmarks.rootGuid }
  );
  let observer = expectBookmarkChangeNotifications();
  let secondTimeRecords = await buf.apply({
    localTimeSeconds,
    remoteTimeSeconds: now,
    notifyInStableOrder: true,
  });
  deepEqual(
    await buf.fetchUnmergedGuids(),
    [PlacesUtils.bookmarks.menuGuid],
    "Should leave menu with new remote structure unmerged after second time"
  );
  deepEqual(
    secondTimeRecords,
    firstTimeRecords,
    "Should stage identical records to upload, first and second time"
  );

  let localItemIds = await PlacesTestUtils.promiseManyItemIds([
    "bookmarkFFFF",
    "bookmarkEEEE",
    "folderAAAAAA",
    "bookmarkCCCC",
    "bookmarkBBBB",
    PlacesUtils.bookmarks.menuGuid,
  ]);
  observer.check([
    {
      name: "bookmark-removed",
      params: {
        itemId: localIdForD,
        parentId: localItemIds.get(PlacesUtils.bookmarks.menuGuid),
        index: 1,
        type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        urlHref: "http://example.com/d",
        title: "", // null titles get turned into empty strings.
        guid: "bookmarkDDDD",
        parentGuid: PlacesUtils.bookmarks.menuGuid,
        source: PlacesUtils.bookmarks.SOURCES.SYNC,
      },
    },
    {
      name: "bookmark-guid-changed",
      params: {
        itemId: localItemIds.get("bookmarkEEEE"),
        type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        urlHref: "",
        guid: "bookmarkEEEE",
        parentGuid: PlacesUtils.bookmarks.menuGuid,
        source: PlacesUtils.bookmarks.SOURCES.SYNC,
        isTagging: false,
      },
    },
    {
      name: "bookmark-added",
      params: {
        itemId: localItemIds.get("bookmarkFFFF"),
        parentId: localItemIds.get(PlacesUtils.bookmarks.menuGuid),
        index: 1,
        type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        urlHref: "http://example.com/f",
        title: "F",
        guid: "bookmarkFFFF",
        parentGuid: PlacesUtils.bookmarks.menuGuid,
        source: PlacesUtils.bookmarks.SOURCES.SYNC,
        tags: "",
        frecency: 1,
        hidden: false,
        visitCount: 0,
      },
    },
    {
      name: "bookmark-moved",
      params: {
        itemId: localItemIds.get("bookmarkEEEE"),
        oldIndex: 2,
        newIndex: 0,
        type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        guid: "bookmarkEEEE",
        oldParentGuid: PlacesUtils.bookmarks.menuGuid,
        newParentGuid: PlacesUtils.bookmarks.menuGuid,
        source: PlacesUtils.bookmarks.SOURCES.SYNC,
        urlHref: "http://example.com/e",
        isTagging: false,
        title: "E",
        tags: "",
        frecency: 0,
        hidden: false,
        visitCount: 0,
        dateAdded: dateAdded.getTime(),
        lastVisitDate: null,
      },
    },
    {
      name: "bookmark-moved",
      params: {
        itemId: localItemIds.get("folderAAAAAA"),
        oldIndex: 0,
        newIndex: 0,
        type: PlacesUtils.bookmarks.TYPE_FOLDER,
        guid: "folderAAAAAA",
        oldParentGuid: PlacesUtils.bookmarks.menuGuid,
        newParentGuid: PlacesUtils.bookmarks.toolbarGuid,
        source: PlacesUtils.bookmarks.SOURCES.SYNC,
        urlHref: "",
        isTagging: false,
        title: "A (remote)",
        tags: "",
        frecency: 0,
        hidden: false,
        visitCount: 0,
        dateAdded: dateAdded.getTime(),
        lastVisitDate: null,
      },
    },
    {
      name: "bookmark-moved",
      params: {
        itemId: localItemIds.get("bookmarkCCCC"),
        oldIndex: 1,
        newIndex: 0,
        type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        guid: "bookmarkCCCC",
        oldParentGuid: "folderAAAAAA",
        newParentGuid: "folderAAAAAA",
        source: PlacesUtils.bookmarks.SOURCES.SYNC,
        urlHref: "http://example.com/c",
        isTagging: false,
        title: "C",
        tags: "",
        frecency: 1,
        hidden: false,
        visitCount: 0,
        dateAdded: dateAdded.getTime(),
        lastVisitDate: null,
      },
    },
    {
      name: "bookmark-moved",
      params: {
        itemId: localItemIds.get("bookmarkBBBB"),
        oldIndex: 0,
        newIndex: 1,
        type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        guid: "bookmarkBBBB",
        oldParentGuid: "folderAAAAAA",
        newParentGuid: "folderAAAAAA",
        source: PlacesUtils.bookmarks.SOURCES.SYNC,
        urlHref: "http://example.com/b-remote",
        isTagging: false,
        title: "B",
        tags: "",
        frecency: -1,
        hidden: false,
        visitCount: 0,
        dateAdded: dateAdded.getTime(),
        lastVisitDate: null,
      },
    },
    {
      name: "bookmark-title-changed",
      params: {
        itemId: localItemIds.get("folderAAAAAA"),
        title: "A (remote)",
        guid: "folderAAAAAA",
        parentGuid: PlacesUtils.bookmarks.toolbarGuid,
      },
    },
    {
      name: "bookmark-url-changed",
      params: {
        itemId: localItemIds.get("bookmarkBBBB"),
        type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        urlHref: "http://example.com/b-remote",
        guid: "bookmarkBBBB",
        parentGuid: "folderAAAAAA",
        source: PlacesUtils.bookmarks.SOURCES.SYNC,
        isTagging: false,
      },
    },
  ]);

  await assertLocalTree(
    PlacesUtils.bookmarks.rootGuid,
    {
      guid: PlacesUtils.bookmarks.rootGuid,
      type: PlacesUtils.bookmarks.TYPE_FOLDER,
      index: 0,
      title: "",
      children: [
        {
          guid: PlacesUtils.bookmarks.menuGuid,
          type: PlacesUtils.bookmarks.TYPE_FOLDER,
          index: 0,
          title: BookmarksMenuTitle,
          children: [
            {
              guid: "bookmarkEEEE",
              type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
              index: 0,
              title: "E",
              url: "http://example.com/e",
            },
            {
              guid: "bookmarkFFFF",
              type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
              index: 1,
              title: "F",
              url: "http://example.com/f",
            },
          ],
        },
        {
          guid: PlacesUtils.bookmarks.toolbarGuid,
          type: PlacesUtils.bookmarks.TYPE_FOLDER,
          index: 1,
          title: BookmarksToolbarTitle,
          children: [
            {
              guid: "folderAAAAAA",
              type: PlacesUtils.bookmarks.TYPE_FOLDER,
              index: 0,
              title: "A (remote)",
              children: [
                {
                  guid: "bookmarkCCCC",
                  type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                  index: 0,
                  title: "C",
                  url: "http://example.com/c",
                },
                {
                  guid: "bookmarkBBBB",
                  type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                  index: 1,
                  title: "B",
                  url: "http://example.com/b-remote",
                },
              ],
            },
          ],
        },
        {
          guid: PlacesUtils.bookmarks.unfiledGuid,
          type: PlacesUtils.bookmarks.TYPE_FOLDER,
          index: 3,
          title: UnfiledBookmarksTitle,
        },
        {
          guid: PlacesUtils.bookmarks.mobileGuid,
          type: PlacesUtils.bookmarks.TYPE_FOLDER,
          index: 4,
          title: MobileBookmarksTitle,
        },
      ],
    },
    "Should apply new structure, second time"
  );

  await storeChangesInMirror(buf, secondTimeRecords);
  deepEqual(await buf.fetchUnmergedGuids(), [], "Should merge all items");

  await buf.finalize();
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesSyncUtils.bookmarks.reset();
});