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

// Test whether do batch removal if multiple bookmarks are removed at once.

add_task(async function test_remove_multiple_bookmarks() {
  info("Test for remove multiple bookmarks at once");

  info("Insert multiple bookmarks");
  const testBookmarks = [
    {
      parentGuid: PlacesUtils.bookmarks.unfiledGuid,
      url: "http://example.com/1",
    },
    {
      parentGuid: PlacesUtils.bookmarks.unfiledGuid,
      url: "http://example.com/2",
    },
    {
      parentGuid: PlacesUtils.bookmarks.unfiledGuid,
      url: "http://example.com/3",
    },
  ];
  const bookmarks = await Promise.all(
    testBookmarks.map(bookmark => PlacesUtils.bookmarks.insert(bookmark))
  );
  Assert.equal(
    bookmarks.length,
    testBookmarks.length,
    "All test data are insterted correctly"
  );

  info("Remove multiple bookmarks");
  const onRemoved = PlacesTestUtils.waitForNotification("bookmark-removed");
  await PlacesUtils.bookmarks.remove(bookmarks);
  const events = await onRemoved;

  info("Check whether all bookmark-removed events called at at once or not");
  assertBookmarkRemovedEvents(events, bookmarks);
});

add_task(async function test_remove_folder_with_bookmarks() {
  info("Test for remove a folder that has multiple bookmarks");

  info("Insert a folder");
  const testFolder = {
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  };
  const folder = await PlacesUtils.bookmarks.insert(testFolder);
  Assert.ok(folder, "A folder is inserted correctly");

  info("Insert multiple bookmarks to inserted folder");
  const testBookmarks = [
    {
      parentGuid: folder.guid,
      type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
      url: "http://example.com/1",
    },
    {
      parentGuid: folder.guid,
      url: "http://example.com/2",
    },
    {
      parentGuid: folder.guid,
      url: "http://example.com/3",
    },
  ];
  const bookmarks = await Promise.all(
    testBookmarks.map(bookmark => PlacesUtils.bookmarks.insert(bookmark))
  );
  Assert.equal(
    bookmarks.length,
    testBookmarks.length,
    "All test data are insterted correctly"
  );

  info("Remove the inserted folder");
  const notifiedEvents = [];
  const onRemoved = PlacesTestUtils.waitForNotification(
    "bookmark-removed",
    events => {
      notifiedEvents.push(events);
      return notifiedEvents.length === 2;
    }
  );
  await PlacesUtils.bookmarks.remove(folder);
  await onRemoved;

  info("Check whether all bookmark-removed events called at at once or not");
  const eventsForBookmarks = notifiedEvents[0];
  assertBookmarkRemovedEvents(eventsForBookmarks, bookmarks);

  info("Check whether a bookmark-removed event called for the folder");
  const eventsForFolder = notifiedEvents[1];
  Assert.equal(
    eventsForFolder.length,
    1,
    "The length of notified events is correct"
  );
  Assert.equal(
    eventsForFolder[0].guid,
    folder.guid,
    "The guid of event is correct"
  );
});

function assertBookmarkRemovedEvents(events, expectedBookmarks) {
  Assert.equal(
    events.length,
    expectedBookmarks.length,
    "The length of notified events is correct"
  );

  for (let i = 0; i < events.length; i++) {
    const event = events[i];
    const expectedBookmark = expectedBookmarks[i];
    Assert.equal(
      event.guid,
      expectedBookmark.guid,
      `The guid of events[${i}] is correct`
    );
    Assert.equal(
      event.url,
      expectedBookmark.url,
      `The url of events[${i}] is correct`
    );
  }
}