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

"use strict";

function ensurePosition(info, parentGuid, index) {
  print(`Checking ${info.guid}`);
  checkBookmarkObject(info);
  Assert.equal(
    info.parentGuid,
    parentGuid,
    "Should be in the correct parent folder"
  );
  Assert.equal(info.index, index, "Should have the correct index");
}

function insertChildren(folder, items) {
  if (!items.length) {
    return [];
  }

  let children = [];
  for (let i = 0; i < items.length; i++) {
    if (items[i].type === TYPE_BOOKMARK) {
      children.push({
        title: `${i}`,
        url: "http://example.com",
      });
    } else {
      throw new Error(`Type ${items[i].type} is not supported.`);
    }
  }
  return PlacesUtils.bookmarks.insertTree({
    guid: folder.guid,
    children,
  });
}

async function dumpFolderChildren(
  folder,
  details,
  folderA,
  folderB,
  originalAChildren,
  originalBChildren
) {
  info(`${folder} Details:`);
  info(`Input: ${JSON.stringify(details.initial[folder])}`);
  info(`Expected: ${JSON.stringify(details.expected[folder])}`);
  info("Index\tOriginal\tExpected\tResult");

  let originalChildren;
  let folderGuid;
  if (folder == "folderA") {
    originalChildren = originalAChildren;
    folderGuid = folderA.guid;
  } else {
    originalChildren = originalBChildren;
    folderGuid = folderB.guid;
  }

  let tree = await PlacesUtils.promiseBookmarksTree(folderGuid);
  let childrenCount = tree.children ? tree.children.length : 0;
  for (let i = 0; i < originalChildren.length || i < childrenCount; i++) {
    let originalGuid =
      i < originalChildren.length ? originalChildren[i].guid : "            ";
    let resultGuid = i < childrenCount ? tree.children[i].guid : "            ";
    let expectedGuid = "            ";
    if (i < details.expected[folder].length) {
      let expected = details.expected[folder][i];
      expectedGuid =
        expected.folder == "a"
          ? originalAChildren[expected.originalIndex].guid
          : originalBChildren[expected.originalIndex].guid;
    }
    info(`${i}\t${originalGuid}\t${expectedGuid}\t${resultGuid}\n`);
  }
}

async function checkExpectedResults(
  details,
  folder,
  folderGuid,
  lastModified,
  movedItems,
  folderAChildren,
  folderBChildren
) {
  let expectedResults = details.expected[folder];
  for (let i = 0; i < expectedResults.length; i++) {
    let expectedDetails = expectedResults[i];
    let originalItem =
      expectedDetails.folder == "a"
        ? folderAChildren[expectedDetails.originalIndex]
        : folderBChildren[expectedDetails.originalIndex];

    // Check the item got updated correctly in the database.
    let updatedItem = await PlacesUtils.bookmarks.fetch(originalItem.guid);

    ensurePosition(updatedItem, folderGuid, i);
    Assert.greaterOrEqual(
      updatedItem.lastModified.getTime(),
      lastModified.getTime(),
      "Last modified should be later or equal to before"
    );
  }

  if (details.expected.skipResultIndexChecks) {
    return;
  }

  // Check the items returned from the actual move() call are correct.
  let index = 0;
  for (let item of details.initial[folder]) {
    if (!("targetFolder" in item)) {
      // We weren't moving this item, so skip it and continue.
      continue;
    }

    let movedItem = movedItems[index];
    let updatedItem = await PlacesUtils.bookmarks.fetch(movedItem.guid);

    ensurePosition(movedItem, updatedItem.parentGuid, updatedItem.index);

    index++;
  }
}

async function checkLastModifiedForFolders(details, folder, movedItems) {
  // For the tests, the moves always come from folderA.
  if (
    details.initial.folderA.some(
      item => "targetFolder" in item && item.targetFolder == folder.title
    )
  ) {
    let updatedFolder = await PlacesUtils.bookmarks.fetch(folder.guid);

    Assert.greaterOrEqual(
      updatedFolder.lastModified.getTime(),
      folder.lastModified.getTime(),
      "Should have updated the folder's last modified time."
    );
    print(JSON.stringify(movedItems[0]));
    Assert.deepEqual(
      updatedFolder.lastModified,
      movedItems[0].lastModified,
      "Should have the same last modified as the moved items."
    );
  }
}

async function testMoveToFolder(details) {
  await PlacesUtils.bookmarks.eraseEverything();

  // Always create two folders by default.
  let [folderA, folderB] = await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.unfiledGuid,
    children: [
      {
        title: "a",
        type: PlacesUtils.bookmarks.TYPE_FOLDER,
      },
      {
        title: "b",
        type: PlacesUtils.bookmarks.TYPE_FOLDER,
      },
    ],
  });

  checkBookmarkObject(folderA);
  let folderAChildren = await insertChildren(folderA, details.initial.folderA);
  checkBookmarkObject(folderB);
  let folderBChildren = await insertChildren(folderB, details.initial.folderB);

  const originalAChildren = folderAChildren.map(child => {
    return { ...child };
  });
  const originalBChildren = folderBChildren.map(child => {
    return { ...child };
  });

  let lastModified;
  if (folderAChildren.length) {
    lastModified = folderAChildren[0].lastModified;
  } else if (folderBChildren.length) {
    lastModified = folderBChildren[0].lastModified;
  } else {
    throw new Error("No children added, can't determine lastModified");
  }

  // Work out which children to move and to where.
  let childrenToUpdate = [];
  for (let i = 0; i < details.initial.folderA.length; i++) {
    if ("move" in details.initial.folderA[i]) {
      childrenToUpdate.push(folderAChildren[i].guid);
    }
  }

  let observer;
  if (details.notifications) {
    observer = expectPlacesObserverNotifications(["bookmark-moved"]);
  }

  let movedItems = await PlacesUtils.bookmarks.moveToFolder(
    childrenToUpdate,
    details.targetFolder == "a" ? folderA.guid : folderB.guid,
    details.targetIndex
  );

  await dumpFolderChildren(
    "folderA",
    details,
    folderA,
    folderB,
    originalAChildren,
    originalBChildren
  );
  await dumpFolderChildren(
    "folderB",
    details,
    folderA,
    folderB,
    originalAChildren,
    originalBChildren
  );

  Assert.equal(movedItems.length, childrenToUpdate.length);
  await checkExpectedResults(
    details,
    "folderA",
    folderA.guid,
    lastModified,
    movedItems,
    folderAChildren,
    folderBChildren
  );
  await checkExpectedResults(
    details,
    "folderB",
    folderB.guid,
    lastModified,
    movedItems,
    folderAChildren,
    folderBChildren
  );

  if (details.notifications) {
    let expectedNotifications = [];

    for (let notification of details.notifications) {
      let origItem =
        notification.originalFolder == "folderA"
          ? originalAChildren[notification.originalIndex]
          : originalBChildren[notification.originalIndex];
      let newFolder = notification.newFolder == "folderA" ? folderA : folderB;

      expectedNotifications.push({
        type: "bookmark-moved",
        id: await PlacesUtils.promiseItemId(origItem.guid),
        itemType: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        url: origItem.url,
        guid: origItem.guid,
        parentGuid: newFolder.guid,
        source: PlacesUtils.bookmarks.SOURCES.DEFAULT,
        index: notification.newIndex,
        oldParentGuid: origItem.parentGuid,
        oldIndex: notification.originalIndex,
        isTagging: false,
      });
    }
    observer.check(expectedNotifications);
  }

  await checkLastModifiedForFolders(details, folderA, movedItems);
  await checkLastModifiedForFolders(details, folderB, movedItems);
}

const TYPE_BOOKMARK = PlacesUtils.bookmarks.TYPE_BOOKMARK;

add_task(async function invalid_input_throws() {
  Assert.throws(
    () => PlacesUtils.bookmarks.moveToFolder(),
    /guids should be an array/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.moveToFolder({}),
    /guids should be an array/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.moveToFolder([]),
    /guids should be an array of at least one item/
  );

  Assert.throws(
    () => PlacesUtils.bookmarks.moveToFolder(["test"]),
    /Expected only valid GUIDs to be passed/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.moveToFolder([null]),
    /Expected only valid GUIDs to be passed/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.moveToFolder([123]),
    /Expected only valid GUIDs to be passed/
  );

  Assert.throws(
    () => PlacesUtils.bookmarks.moveToFolder(["123456789012"], 123),
    /Error: parentGuid should be a valid GUID/
  );

  Assert.throws(
    () =>
      PlacesUtils.bookmarks.moveToFolder(
        ["123456789012"],
        PlacesUtils.bookmarks.rootGuid
      ),
    /Cannot move bookmarks into root/
  );

  Assert.throws(
    () =>
      PlacesUtils.bookmarks.moveToFolder(["123456789012"], "123456789012", -2),
    /index should be a number greater than/
  );
  Assert.throws(
    () =>
      PlacesUtils.bookmarks.moveToFolder(
        ["123456789012"],
        "123456789012",
        "sdffd"
      ),
    /index should be a number greater than/
  );
});

add_task(async function test_move_nonexisting_bookmark_rejects() {
  await Assert.rejects(
    PlacesUtils.bookmarks.moveToFolder(["123456789012"], "123456789012", -1),
    /No bookmarks found for the provided GUID/,
    "Should reject when moving a non-existing bookmark"
  );
});

add_task(async function test_move_folder_into_descendant_rejects() {
  let parent = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  });
  let descendant = await PlacesUtils.bookmarks.insert({
    parentGuid: parent.guid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  });

  await Assert.rejects(
    PlacesUtils.bookmarks.moveToFolder([parent.guid], parent.guid, 0),
    /Cannot insert a folder into itself or one of its descendants/,
    "Should reject when moving a folder into itself"
  );

  await Assert.rejects(
    PlacesUtils.bookmarks.moveToFolder([parent.guid], descendant.guid, 0),
    /Cannot insert a folder into itself or one of its descendants/,
    "Should reject when moving a folder into a descendant"
  );
});

add_task(async function test_move_from_differnt_with_no_target_rejects() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  });
  let bm2 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  });

  await Assert.rejects(
    PlacesUtils.bookmarks.moveToFolder([bm1.guid, bm2.guid], null, -1),
    /All bookmarks should be in the same folder if no parent is specified/,
    "Should reject when moving bookmarks from different folders with no target folder"
  );
});

add_task(async function test_move_append_same_folder() {
  await testMoveToFolder({
    initial: {
      folderA: [
        { type: TYPE_BOOKMARK },
        { type: TYPE_BOOKMARK, move: true },
        { type: TYPE_BOOKMARK },
      ],
      folderB: [],
    },
    targetFolder: "a",
    targetIndex: -1,
    expected: {
      folderA: [
        { folder: "a", originalIndex: 0 },
        { folder: "a", originalIndex: 2 },
        { folder: "a", originalIndex: 1 },
      ],
      folderB: [],
    },
    notifications: [
      {
        originalFolder: "folderA",
        originalIndex: 1,
        newFolder: "folderA",
        newIndex: 2,
      },
    ],
  });
});

add_task(async function test_move_append_multiple_same_folder() {
  await testMoveToFolder({
    initial: {
      folderA: [
        { type: TYPE_BOOKMARK, move: true },
        { type: TYPE_BOOKMARK, move: true },
        { type: TYPE_BOOKMARK, move: true },
        { type: TYPE_BOOKMARK },
      ],
      folderB: [],
    },
    targetFolder: "a",
    targetIndex: -1,
    expected: {
      folderA: [
        { folder: "a", originalIndex: 3 },
        { folder: "a", originalIndex: 0 },
        { folder: "a", originalIndex: 1 },
        { folder: "a", originalIndex: 2 },
      ],
      folderB: [],
    },
    // These are all inserted at position 3 as that's what the views require
    // to be notified, to ensure the new items are displayed in their correct
    // positions.
    notifications: [
      {
        originalFolder: "folderA",
        originalIndex: 0,
        newFolder: "folderA",
        newIndex: 3,
      },
      {
        originalFolder: "folderA",
        originalIndex: 1,
        newFolder: "folderA",
        newIndex: 3,
      },
      {
        originalFolder: "folderA",
        originalIndex: 2,
        newFolder: "folderA",
        newIndex: 3,
      },
    ],
  });
});

add_task(async function test_move_append_multiple_new_folder() {
  await testMoveToFolder({
    initial: {
      folderA: [
        { type: TYPE_BOOKMARK, move: true },
        { type: TYPE_BOOKMARK, move: true },
        { type: TYPE_BOOKMARK, move: true },
      ],
      folderB: [],
    },
    targetFolder: "b",
    targetIndex: -1,
    expected: {
      folderA: [],
      folderB: [
        { folder: "a", originalIndex: 0 },
        { folder: "a", originalIndex: 1 },
        { folder: "a", originalIndex: 2 },
      ],
    },
    notifications: [
      {
        originalFolder: "folderA",
        originalIndex: 0,
        newFolder: "folderB",
        newIndex: 0,
      },
      {
        originalFolder: "folderA",
        originalIndex: 1,
        newFolder: "folderB",
        newIndex: 1,
      },
      {
        originalFolder: "folderA",
        originalIndex: 2,
        newFolder: "folderB",
        newIndex: 2,
      },
    ],
  });
});

add_task(async function test_move_append_multiple_new_folder_with_existing() {
  await testMoveToFolder({
    initial: {
      folderA: [
        { type: TYPE_BOOKMARK, move: true },
        { type: TYPE_BOOKMARK, move: true },
        { type: TYPE_BOOKMARK, move: true },
      ],
      folderB: [{ type: TYPE_BOOKMARK }, { type: TYPE_BOOKMARK }],
    },
    targetFolder: "b",
    targetIndex: -1,
    expected: {
      folderA: [],
      folderB: [
        { folder: "b", originalIndex: 0 },
        { folder: "b", originalIndex: 1 },
        { folder: "a", originalIndex: 0 },
        { folder: "a", originalIndex: 1 },
        { folder: "a", originalIndex: 2 },
      ],
    },
    notifications: [
      {
        originalFolder: "folderA",
        originalIndex: 0,
        newFolder: "folderB",
        newIndex: 2,
      },
      {
        originalFolder: "folderA",
        originalIndex: 1,
        newFolder: "folderB",
        newIndex: 3,
      },
      {
        originalFolder: "folderA",
        originalIndex: 2,
        newFolder: "folderB",
        newIndex: 4,
      },
    ],
  });
});

add_task(async function test_move_insert_same_folder_up() {
  await testMoveToFolder({
    initial: {
      folderA: [
        { type: TYPE_BOOKMARK },
        { type: TYPE_BOOKMARK },
        { type: TYPE_BOOKMARK, move: true },
      ],
      folderB: [],
    },
    targetFolder: "a",
    targetIndex: 0,
    expected: {
      folderA: [
        { folder: "a", originalIndex: 2 },
        { folder: "a", originalIndex: 0 },
        { folder: "a", originalIndex: 1 },
      ],
      folderB: [],
    },
    notifications: [
      {
        originalFolder: "folderA",
        originalIndex: 2,
        newFolder: "folderA",
        newIndex: 0,
      },
    ],
  });
});

add_task(async function test_move_insert_same_folder_down() {
  await testMoveToFolder({
    initial: {
      folderA: [
        { type: TYPE_BOOKMARK, move: true },
        { type: TYPE_BOOKMARK },
        { type: TYPE_BOOKMARK },
      ],
      folderB: [],
    },
    targetFolder: "a",
    targetIndex: 2,
    expected: {
      folderA: [
        { folder: "a", originalIndex: 1 },
        { folder: "a", originalIndex: 0 },
        { folder: "a", originalIndex: 2 },
      ],
      folderB: [],
    },
    notifications: [
      {
        originalFolder: "folderA",
        originalIndex: 0,
        newFolder: "folderA",
        newIndex: 1,
      },
    ],
  });
});

add_task(
  async function test_move_insert_multiple_same_folder_split_locations() {
    await testMoveToFolder({
      initial: {
        folderA: [
          { type: TYPE_BOOKMARK, move: true },
          { type: TYPE_BOOKMARK },
          { type: TYPE_BOOKMARK },
          { type: TYPE_BOOKMARK, move: true },
          { type: TYPE_BOOKMARK },
          { type: TYPE_BOOKMARK },
          { type: TYPE_BOOKMARK, move: true },
          { type: TYPE_BOOKMARK },
          { type: TYPE_BOOKMARK },
          { type: TYPE_BOOKMARK, move: true },
        ],
        folderB: [],
      },
      targetFolder: "a",
      targetIndex: 2,
      expected: {
        folderA: [
          { folder: "a", originalIndex: 1 },
          { folder: "a", originalIndex: 0 },
          { folder: "a", originalIndex: 3 },
          { folder: "a", originalIndex: 6 },
          { folder: "a", originalIndex: 9 },
          { folder: "a", originalIndex: 2 },
          { folder: "a", originalIndex: 4 },
          { folder: "a", originalIndex: 5 },
          { folder: "a", originalIndex: 7 },
          { folder: "a", originalIndex: 8 },
        ],
        folderB: [],
      },
      notifications: [
        {
          originalFolder: "folderA",
          originalIndex: 0,
          newFolder: "folderA",
          newIndex: 1,
        },
        {
          originalFolder: "folderA",
          originalIndex: 3,
          newFolder: "folderA",
          newIndex: 2,
        },
        {
          originalFolder: "folderA",
          originalIndex: 6,
          newFolder: "folderA",
          newIndex: 3,
        },
        {
          originalFolder: "folderA",
          originalIndex: 9,
          newFolder: "folderA",
          newIndex: 4,
        },
      ],
    });
  }
);

add_task(async function test_move_folder_with_descendant() {
  let parent = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  });
  let bm = await PlacesUtils.bookmarks.insert({
    parentGuid: parent.guid,
    url: "http://example.com/",
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
  });
  let descendant = await PlacesUtils.bookmarks.insert({
    parentGuid: parent.guid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  });
  Assert.equal(descendant.index, 1);
  let lastModified = bm.lastModified;

  // This is moving to a nonexisting index by purpose, it will be appended.
  [bm] = await PlacesUtils.bookmarks.moveToFolder(
    [bm.guid],
    descendant.guid,
    1
  );
  checkBookmarkObject(bm);
  Assert.equal(bm.parentGuid, descendant.guid);
  Assert.equal(bm.index, 0);
  Assert.ok(bm.lastModified >= lastModified);

  parent = await PlacesUtils.bookmarks.fetch(parent.guid);
  descendant = await PlacesUtils.bookmarks.fetch(descendant.guid);
  Assert.deepEqual(parent.lastModified, bm.lastModified);
  Assert.deepEqual(descendant.lastModified, bm.lastModified);
  Assert.equal(descendant.index, 0);

  bm = await PlacesUtils.bookmarks.fetch(bm.guid);
  Assert.equal(bm.parentGuid, descendant.guid);
  Assert.equal(bm.index, 0);

  [bm] = await PlacesUtils.bookmarks.moveToFolder([bm.guid], parent.guid, 0);
  Assert.equal(bm.parentGuid, parent.guid);
  Assert.equal(bm.index, 0);

  descendant = await PlacesUtils.bookmarks.fetch(descendant.guid);
  Assert.equal(descendant.index, 1);
});