summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_promiseBookmarksTree.js
blob: 9824908cef5ef915ce6c7c311018fbd7c8aca2bc (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

async function check_has_child(aParentGuid, aChildGuid) {
  let parentTree = await PlacesUtils.promiseBookmarksTree(aParentGuid);
  Assert.ok("children" in parentTree);
  Assert.notEqual(
    parentTree.children.find(e => e.guid == aChildGuid),
    null
  );
}

async function compareToNode(aItem, aNode, aIsRootItem, aExcludedGuids = []) {
  // itemId==-1 indicates a non-bookmark node, which is unexpected.
  Assert.notEqual(aNode.itemId, -1);

  function check_unset(...aProps) {
    for (let p of aProps) {
      if (p in aItem) {
        Assert.ok(false, `Unexpected property ${p} with value ${aItem[p]}`);
      }
    }
  }

  function compare_prop(aItemProp, aNodeProp = aItemProp, aOptional = false) {
    if (aOptional && aNode[aNodeProp] === null) {
      check_unset(aItemProp);
    } else {
      Assert.strictEqual(aItem[aItemProp], aNode[aNodeProp]);
    }
  }

  // Bug 1013053 - bookmarkIndex is unavailable for the query's root
  if (aNode.bookmarkIndex == -1) {
    let bookmark = await PlacesUtils.bookmarks.fetch(aNode.bookmarkGuid);
    Assert.strictEqual(aItem.index, bookmark.index);
  } else {
    compare_prop("index", "bookmarkIndex");
  }

  compare_prop("dateAdded");
  compare_prop("lastModified");

  if (aIsRootItem && aNode.bookmarkGuid != PlacesUtils.bookmarks.rootGuid) {
    Assert.ok("parentGuid" in aItem);
    await check_has_child(aItem.parentGuid, aItem.guid);
  } else {
    check_unset("parentGuid");
  }

  const BOOKMARK_ONLY_PROPS = ["uri", "iconUri", "tags", "charset", "keyword"];
  const FOLDER_ONLY_PROPS = ["children", "root"];

  let nodesCount = 1;

  switch (aNode.type) {
    case Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER:
      Assert.equal(aItem.type, PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER);
      Assert.equal(aItem.typeCode, PlacesUtils.bookmarks.TYPE_FOLDER);
      compare_prop("title", "title", true);
      check_unset(...BOOKMARK_ONLY_PROPS);

      let expectedChildrenNodes = [];

      PlacesUtils.asContainer(aNode);
      if (!aNode.containerOpen) {
        aNode.containerOpen = true;
      }

      for (let i = 0; i < aNode.childCount; i++) {
        let childNode = aNode.getChild(i);
        if (
          childNode.itemId == PlacesUtils.tagsFolderId ||
          aExcludedGuids.includes(childNode.bookmarkGuid)
        ) {
          continue;
        }
        expectedChildrenNodes.push(childNode);
      }

      if (expectedChildrenNodes.length) {
        Assert.ok(Array.isArray(aItem.children));
        Assert.equal(aItem.children.length, expectedChildrenNodes.length);
        for (let i = 0; i < aItem.children.length; i++) {
          nodesCount += await compareToNode(
            aItem.children[i],
            expectedChildrenNodes[i],
            false,
            aExcludedGuids
          );
        }
      } else {
        check_unset("children");
      }

      let rootName = mapItemGuidToInternalRootName(aItem.guid);
      if (rootName) {
        Assert.equal(aItem.root, rootName);
      } else {
        check_unset("root");
      }
      break;
    case Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR:
      Assert.equal(aItem.type, PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR);
      Assert.equal(aItem.typeCode, PlacesUtils.bookmarks.TYPE_SEPARATOR);
      check_unset(...BOOKMARK_ONLY_PROPS, ...FOLDER_ONLY_PROPS);
      break;
    default:
      Assert.equal(aItem.type, PlacesUtils.TYPE_X_MOZ_PLACE);
      Assert.equal(aItem.typeCode, PlacesUtils.bookmarks.TYPE_BOOKMARK);
      compare_prop("uri");
      // node.tags's format is "a, b" whilst promiseBoookmarksTree is "a,b"
      if (aNode.tags === null) {
        check_unset("tags");
      } else {
        Assert.equal(aItem.tags, aNode.tags.replace(/, /g, ","));
      }

      if (aNode.icon) {
        try {
          await compareFavicons(aNode.icon, aItem.iconUri);
        } catch (ex) {
          info(ex);
          todo_check_true(false);
        }
      } else {
        check_unset(aItem.iconUri);
      }

      check_unset(...FOLDER_ONLY_PROPS);

      let pageInfo = await PlacesUtils.history.fetch(aNode.uri, {
        includeAnnotations: true,
      });
      let expectedCharset = pageInfo.annotations.get(PlacesUtils.CHARSET_ANNO);
      if (expectedCharset) {
        Assert.equal(aItem.charset, expectedCharset);
      } else {
        check_unset("charset");
      }

      let entry = await PlacesUtils.keywords.fetch({ url: aNode.uri });
      if (entry) {
        Assert.equal(aItem.keyword, entry.keyword);
      } else {
        check_unset("keyword");
      }

      if ("title" in aItem) {
        compare_prop("title");
      } else {
        Assert.equal(null, aNode.title);
      }
  }

  if (aIsRootItem) {
    Assert.strictEqual(aItem.itemsCount, nodesCount);
  }

  return nodesCount;
}

var itemsCount = 0;
async function new_bookmark(aInfo) {
  ++itemsCount;
  if (!("url" in aInfo)) {
    aInfo.url = uri("http://test.item." + itemsCount);
  }

  if (!("title" in aInfo)) {
    aInfo.title = "Test Item (bookmark) " + itemsCount;
  }

  await PlacesTransactions.NewBookmark(aInfo).transact();
}

function new_folder(aInfo) {
  if (!("title" in aInfo)) {
    aInfo.title = "Test Item (folder) " + itemsCount;
  }
  return PlacesTransactions.NewFolder(aInfo).transact();
}

// Walks a result nodes tree and test promiseBookmarksTree for each node.
// DO NOT COPY THIS LOGIC:  It is done here to accomplish a more comprehensive
// test of the API (the entire hierarchy data is available in the very test).
async function test_promiseBookmarksTreeForEachNode(
  aNode,
  aOptions,
  aExcludedGuids
) {
  Assert.ok(aNode.bookmarkGuid && !!aNode.bookmarkGuid.length);
  let item = await PlacesUtils.promiseBookmarksTree(
    aNode.bookmarkGuid,
    aOptions
  );
  await compareToNode(item, aNode, true, aExcludedGuids);

  if (!PlacesUtils.nodeIsContainer(aNode)) {
    return item;
  }

  for (let i = 0; i < aNode.childCount; i++) {
    let child = aNode.getChild(i);
    if (child.itemId != PlacesUtils.tagsFolderId) {
      await test_promiseBookmarksTreeForEachNode(
        child,
        { includeItemIds: true },
        aExcludedGuids
      );
    }
  }
  return item;
}

async function test_promiseBookmarksTreeAgainstResult(
  aItemGuid = PlacesUtils.bookmarks.rootGuid,
  aOptions = { includeItemIds: true },
  aExcludedGuids
) {
  let node = PlacesUtils.getFolderContents(aItemGuid).root;
  return test_promiseBookmarksTreeForEachNode(node, aOptions, aExcludedGuids);
}

add_task(async function () {
  // Add some bookmarks to cover various use cases.
  await new_bookmark({ parentGuid: PlacesUtils.bookmarks.toolbarGuid });
  await new_folder({
    parentGuid: PlacesUtils.bookmarks.menuGuid,
    annotations: [
      { name: "TestAnnoA", value: "TestVal" },
      { name: "TestAnnoB", value: 0 },
    ],
  });
  let sepInfo = { parentGuid: PlacesUtils.bookmarks.menuGuid };
  await PlacesTransactions.NewSeparator(sepInfo).transact();
  let folderGuid = await new_folder({
    parentGuid: PlacesUtils.bookmarks.menuGuid,
  });
  await new_bookmark({
    title: null,
    parentGuid: folderGuid,
    keyword: "test_keyword",
    tags: ["TestTagA", "TestTagB"],
    annotations: [{ name: "TestAnnoA", value: "TestVal2" }],
  });
  let urlWithCharsetAndFavicon = uri("http://charset.and.favicon");
  await new_bookmark({ parentGuid: folderGuid, url: urlWithCharsetAndFavicon });
  await PlacesUtils.history.update({
    url: urlWithCharsetAndFavicon,
    annotations: new Map([[PlacesUtils.CHARSET_ANNO, "UTF-16"]]),
  });
  await setFaviconForPage(urlWithCharsetAndFavicon, SMALLPNG_DATA_URI);
  // Test the default places root without specifying it.
  await test_promiseBookmarksTreeAgainstResult();

  // Do specify it
  await test_promiseBookmarksTreeAgainstResult(PlacesUtils.bookmarks.rootGuid);

  // Exclude the bookmarks menu.
  // The calllback should be four times - once for the toolbar, once for
  // the bookmark we inserted under, and once for the menu (and not
  // at all for any of its descendants) and once for the unsorted bookmarks
  // folder.  However, promiseBookmarksTree is called multiple times, so
  // rather than counting the calls, we count the number of unique items
  // passed in.
  let guidsPassedToExcludeCallback = new Set();
  let placesRootWithoutTheMenu = await test_promiseBookmarksTreeAgainstResult(
    PlacesUtils.bookmarks.rootGuid,
    {
      excludeItemsCallback: aItem => {
        guidsPassedToExcludeCallback.add(aItem.guid);
        return aItem.root == "bookmarksMenuFolder";
      },
      includeItemIds: true,
    },
    [PlacesUtils.bookmarks.menuGuid]
  );
  Assert.equal(guidsPassedToExcludeCallback.size, 5);
  Assert.equal(placesRootWithoutTheMenu.children.length, 3);
});