-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_result_sort.js
128 lines (109 loc) · 3.88 KB
/
test_result_sort.js
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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* 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/. */
const NHQO = Ci.nsINavHistoryQueryOptions;
/**
* Waits for onItemVisited notifications to be received.
*/
function promiseOnItemVisited() {
return new Promise(resolve => {
let bookmarksObserver = {
__proto__: NavBookmarkObserver.prototype,
onItemVisited: function BO_onItemVisited() {
PlacesUtils.bookmarks.removeObserver(this);
// Enqueue to be sure that all onItemVisited notifications ran.
executeSoon(resolve);
},
};
PlacesUtils.bookmarks.addObserver(bookmarksObserver);
});
}
add_task(async function test() {
const uri1 = "http://foo.tld/a";
const uri2 = "http://foo.tld/b";
let bookmarks = await PlacesUtils.bookmarks.insertTree({
guid: PlacesUtils.bookmarks.menuGuid,
children: [{
title: "Result-sort functionality tests root",
type: PlacesUtils.bookmarks.TYPE_FOLDER,
children: [{
title: "b",
url: uri1,
}, {
title: "a",
url: uri2,
}, {
// url of the first child, title of second
title: "a",
url: uri1,
} ],
}],
});
let guid1 = bookmarks[1].guid;
let guid2 = bookmarks[2].guid;
let guid3 = bookmarks[3].guid;
// query with natural order
let result = PlacesUtils.getFolderContents(bookmarks[0].guid);
let root = result.root;
Assert.equal(root.childCount, 3);
function checkOrder(a, b, c) {
Assert.equal(root.getChild(0).bookmarkGuid, a);
Assert.equal(root.getChild(1).bookmarkGuid, b);
Assert.equal(root.getChild(2).bookmarkGuid, c);
}
// natural order
info("Natural order");
checkOrder(guid1, guid2, guid3);
// title: guid3 should precede guid2 since we fall-back to URI-based sorting
info("Sort by title asc");
result.sortingMode = NHQO.SORT_BY_TITLE_ASCENDING;
checkOrder(guid3, guid2, guid1);
// In reverse
info("Sort by title desc");
result.sortingMode = NHQO.SORT_BY_TITLE_DESCENDING;
checkOrder(guid1, guid2, guid3);
// uri sort: guid1 should precede guid3 since we fall-back to natural order
info("Sort by uri asc");
result.sortingMode = NHQO.SORT_BY_URI_ASCENDING;
checkOrder(guid1, guid3, guid2);
// test live update
info("Change bookmark uri liveupdate");
await PlacesUtils.bookmarks.update({
guid: guid1,
url: uri2,
});
checkOrder(guid3, guid1, guid2);
await PlacesUtils.bookmarks.update({
guid: guid1,
url: uri1,
});
checkOrder(guid1, guid3, guid2);
// XXXtodo: test history sortings (visit count, visit date)
// XXXtodo: test different item types once folderId and bookmarkId are merged.
// XXXtodo: test dateAdded sort
// XXXtodo: test lastModified sort
// Add a visit, then check frecency ordering.
// When the bookmarks service gets onVisit, it asynchronously fetches all
// items for that visit, and then notifies onItemVisited. Thus we must
// explicitly wait for that.
let waitForVisited = promiseOnItemVisited();
await PlacesTestUtils.addVisits({ uri: uri2, transition: TRANSITION_TYPED });
await waitForVisited;
info("Sort by frecency desc");
result.sortingMode = NHQO.SORT_BY_FRECENCY_DESCENDING;
for (let i = 0; i < root.childCount; ++i) {
print(root.getChild(i).uri + " " + root.getChild(i).title);
}
// For guid1 and guid3, since they have same frecency and no visits, fallback
// to sort by the newest bookmark.
checkOrder(guid2, guid3, guid1);
info("Sort by frecency asc");
result.sortingMode = NHQO.SORT_BY_FRECENCY_ASCENDING;
for (let i = 0; i < root.childCount; ++i) {
print(root.getChild(i).uri + " " + root.getChild(i).title);
}
checkOrder(guid1, guid3, guid2);
root.containerOpen = false;
});