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
|
/* 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/. */
function* check_has_child(aParentGuid, aChildGuid) {
let parentTree = yield PlacesUtils.promiseBookmarksTree(aParentGuid);
do_check_true("children" in parentTree);
do_check_true(parentTree.children.find( e => e.guid == aChildGuid ) != null);
}
function* compareToNode(aItem, aNode, aIsRootItem, aExcludedGuids = []) {
// itemId==-1 indicates a non-bookmark node, which is unexpected.
do_check_neq(aNode.itemId, -1);
function check_unset(...aProps) {
aProps.forEach( p => { do_check_false(p in aItem); } );
}
function strict_eq_check(v1, v2) {
dump("v1: " + v1 + " v2: " + v2 + "\n");
do_check_eq(typeof v1, typeof v2);
do_check_eq(v1, v2);
}
function compare_prop(aItemProp, aNodeProp = aItemProp, aOptional = false) {
if (aOptional && aNode[aNodeProp] === null)
check_unset(aItemProp);
else
strict_eq_check(aItem[aItemProp], aNode[aNodeProp]);
}
function compare_prop_to_value(aItemProp, aValue, aOptional = true) {
if (aOptional && aValue === null)
check_unset(aItemProp);
else
strict_eq_check(aItem[aItemProp], aValue);
}
// Bug 1013053 - bookmarkIndex is unavailable for the query's root
if (aNode.bookmarkIndex == -1) {
compare_prop_to_value("index",
PlacesUtils.bookmarks.getItemIndex(aNode.itemId),
false);
}
else {
compare_prop("index", "bookmarkIndex");
}
compare_prop("dateAdded");
compare_prop("lastModified");
if (aIsRootItem && aNode.itemId != PlacesUtils.placesRootId) {
do_check_true("parentGuid" in aItem);
yield check_has_child(aItem.parentGuid, aItem.guid)
}
else {
check_unset("parentGuid");
}
let expectedAnnos = PlacesUtils.getAnnotationsForItem(aItem.id);
if (expectedAnnos.length > 0) {
let annosToString = annos => {
return annos.map(a => a.name + ":" + a.value).sort().join(",");
};
do_check_true(Array.isArray(aItem.annos))
do_check_eq(annosToString(aItem.annos), annosToString(expectedAnnos));
}
else {
check_unset("annos");
}
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:
do_check_eq(aItem.type, PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER);
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 > 0) {
do_check_true(Array.isArray(aItem.children));
do_check_eq(aItem.children.length, expectedChildrenNodes.length);
for (let i = 0; i < aItem.children.length; i++) {
nodesCount +=
yield compareToNode(aItem.children[i], expectedChildrenNodes[i],
false, aExcludedGuids);
}
}
else {
check_unset("children");
}
switch (aItem.id) {
case PlacesUtils.placesRootId:
compare_prop_to_value("root", "placesRoot");
break;
case PlacesUtils.bookmarksMenuFolderId:
compare_prop_to_value("root", "bookmarksMenuFolder");
break;
case PlacesUtils.toolbarFolderId:
compare_prop_to_value("root", "toolbarFolder");
break;
case PlacesUtils.unfiledBookmarksFolderId:
compare_prop_to_value("root", "unfiledBookmarksFolder");
break;
case PlacesUtils.mobileFolderId:
compare_prop_to_value("root", "mobileFolder");
break;
default:
check_unset("root");
}
break;
case Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR:
do_check_eq(aItem.type, PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR);
check_unset(...BOOKMARK_ONLY_PROPS, ...FOLDER_ONLY_PROPS);
break;
default:
do_check_eq(aItem.type, PlacesUtils.TYPE_X_MOZ_PLACE);
compare_prop("uri");
// node.tags's format is "a, b" whilst promiseBoookmarksTree is "a,b"
if (aNode.tags === null)
check_unset("tags");
else
compare_prop_to_value("tags", aNode.tags.replace(/, /g, ","), false);
if (aNode.icon) {
let nodeIconData = aNode.icon.replace("moz-anno:favicon:", "");
compare_prop_to_value("iconuri", nodeIconData);
}
else {
check_unset(aItem.iconuri);
}
check_unset(...FOLDER_ONLY_PROPS);
let itemURI = uri(aNode.uri);
compare_prop_to_value("charset",
yield PlacesUtils.getCharsetForURI(itemURI));
let entry = yield PlacesUtils.keywords.fetch({ url: aNode.uri });
compare_prop_to_value("keyword", entry ? entry.keyword : null);
if ("title" in aItem)
compare_prop("title");
else
do_check_null(aNode.title);
}
if (aIsRootItem)
do_check_eq(aItem.itemsCount, nodesCount);
return nodesCount;
}
var itemsCount = 0;
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;
yield PlacesTransactions.NewBookmark(aInfo).transact();
}
function* new_folder(aInfo) {
if (!("title" in aInfo))
aInfo.title = "Test Item (folder) " + itemsCount;
return yield 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).
function* test_promiseBookmarksTreeForEachNode(aNode, aOptions, aExcludedGuids) {
do_check_true(aNode.bookmarkGuid && aNode.bookmarkGuid.length > 0);
let item = yield PlacesUtils.promiseBookmarksTree(aNode.bookmarkGuid, aOptions);
yield* compareToNode(item, aNode, true, aExcludedGuids);
for (let i = 0; i < aNode.childCount; i++) {
let child = aNode.getChild(i);
if (child.itemId != PlacesUtils.tagsFolderId)
yield test_promiseBookmarksTreeForEachNode(child,
{ includeItemIds: true },
aExcludedGuids);
}
return item;
}
function* test_promiseBookmarksTreeAgainstResult(aItemGuid = "",
aOptions = { includeItemIds: true },
aExcludedGuids) {
let itemId = aItemGuid ?
yield PlacesUtils.promiseItemId(aItemGuid) : PlacesUtils.placesRootId;
let node = PlacesUtils.getFolderContents(itemId).root;
return yield test_promiseBookmarksTreeForEachNode(node, aOptions, aExcludedGuids);
}
add_task(function* () {
// Add some bookmarks to cover various use cases.
yield new_bookmark({ parentGuid: PlacesUtils.bookmarks.toolbarGuid });
yield new_folder({ parentGuid: PlacesUtils.bookmarks.menuGuid,
annotations: [{ name: "TestAnnoA", value: "TestVal" },
{ name: "TestAnnoB", value: 0 }]});
let sepInfo = { parentGuid: PlacesUtils.bookmarks.menuGuid };
yield PlacesTransactions.NewSeparator(sepInfo).transact();
let folderGuid = yield new_folder({ parentGuid: PlacesUtils.bookmarks.menuGuid });
yield new_bookmark({ title: null,
parentGuid: folderGuid,
keyword: "test_keyword",
tags: ["TestTagA", "TestTagB"],
annotations: [{ name: "TestAnnoA", value: "TestVal2"}]});
let urlWithCharsetAndFavicon = uri("http://charset.and.favicon");
yield new_bookmark({ parentGuid: folderGuid, url: urlWithCharsetAndFavicon });
yield PlacesUtils.setCharsetForURI(urlWithCharsetAndFavicon, "UTF-8");
yield promiseSetIconForPage(urlWithCharsetAndFavicon, SMALLPNG_DATA_URI);
// Test the default places root without specifying it.
yield test_promiseBookmarksTreeAgainstResult();
// Do specify it
yield 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 =
yield test_promiseBookmarksTreeAgainstResult(PlacesUtils.bookmarks.rootGuid, {
excludeItemsCallback: aItem => {
guidsPassedToExcludeCallback.add(aItem.guid);
return aItem.root == "bookmarksMenuFolder";
},
includeItemIds: true
}, [PlacesUtils.bookmarks.menuGuid]);
do_check_eq(guidsPassedToExcludeCallback.size, 5);
do_check_eq(placesRootWithoutTheMenu.children.length, 3);
});
|