summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/bookmarks/test_removeFolderTransaction_reinsert.js
blob: 2c9b5c44aa7ecc61ce65ce13bfa21c5433d25688 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
/**
 * This test ensures that reinserting a folder within a transaction gives it
 * the same GUID, and passes it to the observers.
 */

add_task(async function test_removeFolderTransaction_reinsert() {
  let folder = await PlacesUtils.bookmarks.insert({
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    parentGuid: PlacesUtils.bookmarks.menuGuid,
    title: "Test folder",
  });
  let fx = await PlacesUtils.bookmarks.insert({
    parentGuid: folder.guid,
    title: "Get Firefox!",
    url: "http://getfirefox.com",
  });
  let tb = await PlacesUtils.bookmarks.insert({
    parentGuid: folder.guid,
    title: "Get Thunderbird!",
    url: "http://getthunderbird.com",
  });

  let notifications = [];
  function checkNotifications(expected, message) {
    deepEqual(notifications, expected, message);
    notifications.length = 0;
  }

  let listener = events => {
    for (let event of events) {
      notifications.push([
        event.type,
        event.id,
        event.parentId,
        event.guid,
        event.parentGuid,
      ]);
    }
  };
  let observer = Object.create(NavBookmarkObserver.prototype);
  PlacesUtils.bookmarks.addObserver(observer);
  PlacesUtils.observers.addListener(
    ["bookmark-added", "bookmark-removed"],
    listener
  );
  PlacesUtils.registerShutdownFunction(function() {
    PlacesUtils.bookmarks.removeObserver(observer);
    PlacesUtils.observers.removeListener(
      ["bookmark-added", "bookmark-removed"],
      listener
    );
  });

  let transaction = PlacesTransactions.Remove({ guid: folder.guid });

  let folderId = await PlacesUtils.promiseItemId(folder.guid);
  let fxId = await PlacesUtils.promiseItemId(fx.guid);
  let tbId = await PlacesUtils.promiseItemId(tb.guid);

  await transaction.transact();
  let bookmarksMenuItemId = await PlacesUtils.promiseItemId(
    PlacesUtils.bookmarks.menuGuid
  );
  checkNotifications(
    [
      ["bookmark-removed", tbId, folderId, tb.guid, folder.guid],
      ["bookmark-removed", fxId, folderId, fx.guid, folder.guid],
      [
        "bookmark-removed",
        folderId,
        bookmarksMenuItemId,
        folder.guid,
        PlacesUtils.bookmarks.menuGuid,
      ],
    ],
    "Executing transaction should remove folder and its descendants"
  );

  await PlacesTransactions.undo();

  folderId = await PlacesUtils.promiseItemId(folder.guid);
  fxId = await PlacesUtils.promiseItemId(fx.guid);
  tbId = await PlacesUtils.promiseItemId(tb.guid);

  checkNotifications(
    [
      [
        "bookmark-added",
        folderId,
        bookmarksMenuItemId,
        folder.guid,
        PlacesUtils.bookmarks.menuGuid,
      ],
      ["bookmark-added", fxId, folderId, fx.guid, folder.guid],
      ["bookmark-added", tbId, folderId, tb.guid, folder.guid],
    ],
    "Undo should reinsert folder with different id but same GUID"
  );

  await PlacesTransactions.redo();

  checkNotifications(
    [
      ["bookmark-removed", tbId, folderId, tb.guid, folder.guid],
      ["bookmark-removed", fxId, folderId, fx.guid, folder.guid],
      [
        "bookmark-removed",
        folderId,
        bookmarksMenuItemId,
        folder.guid,
        PlacesUtils.bookmarks.menuGuid,
      ],
    ],
    "Redo should pass the GUID to observer"
  );
});