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
|
/* -*- 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 bmsvc = PlacesUtils.bookmarks;
const histsvc = PlacesUtils.history;
function run_test() {
run_next_test();
}
add_task(function* test_addBookmarksAndCheckGuids() {
let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder", bmsvc.DEFAULT_INDEX);
bmsvc.insertBookmark(folder, uri("http://test1.com/"),
bmsvc.DEFAULT_INDEX, "1 title");
bmsvc.insertBookmark(folder, uri("http://test2.com/"),
bmsvc.DEFAULT_INDEX, "2 title");
bmsvc.insertBookmark(folder, uri("http://test3.com/"),
bmsvc.DEFAULT_INDEX, "3 title");
bmsvc.insertSeparator(folder, bmsvc.DEFAULT_INDEX);
bmsvc.createFolder(folder, "test folder 2", bmsvc.DEFAULT_INDEX);
let root = PlacesUtils.getFolderContents(folder).root;
do_check_eq(root.childCount, 5);
// check bookmark guids
let bookmarkGuidZero = root.getChild(0).bookmarkGuid;
do_check_eq(bookmarkGuidZero.length, 12);
// bookmarks have bookmark guids
do_check_eq(root.getChild(1).bookmarkGuid.length, 12);
do_check_eq(root.getChild(2).bookmarkGuid.length, 12);
// separator has bookmark guid
do_check_eq(root.getChild(3).bookmarkGuid.length, 12);
// folder has bookmark guid
do_check_eq(root.getChild(4).bookmarkGuid.length, 12);
// all bookmark guids are different.
do_check_neq(bookmarkGuidZero, root.getChild(1).bookmarkGuid);
do_check_neq(root.getChild(1).bookmarkGuid, root.getChild(2).bookmarkGuid);
do_check_neq(root.getChild(2).bookmarkGuid, root.getChild(3).bookmarkGuid);
do_check_neq(root.getChild(3).bookmarkGuid, root.getChild(4).bookmarkGuid);
// check page guids
let pageGuidZero = root.getChild(0).pageGuid;
do_check_eq(pageGuidZero.length, 12);
// bookmarks have page guids
do_check_eq(root.getChild(1).pageGuid.length, 12);
do_check_eq(root.getChild(2).pageGuid.length, 12);
// folder and separator don't have page guids
do_check_eq(root.getChild(3).pageGuid, "");
do_check_eq(root.getChild(4).pageGuid, "");
do_check_neq(pageGuidZero, root.getChild(1).pageGuid);
do_check_neq(root.getChild(1).pageGuid, root.getChild(2).pageGuid);
root.containerOpen = false;
yield PlacesUtils.bookmarks.eraseEverything();
});
add_task(function* test_updateBookmarksAndCheckGuids() {
let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder", bmsvc.DEFAULT_INDEX);
let b1 = bmsvc.insertBookmark(folder, uri("http://test1.com/"),
bmsvc.DEFAULT_INDEX, "1 title");
let f1 = bmsvc.createFolder(folder, "test folder 2", bmsvc.DEFAULT_INDEX);
let root = PlacesUtils.getFolderContents(folder).root;
do_check_eq(root.childCount, 2);
// ensure the bookmark and page guids remain the same after modifing other property.
let bookmarkGuidZero = root.getChild(0).bookmarkGuid;
let pageGuidZero = root.getChild(0).pageGuid;
bmsvc.setItemTitle(b1, "1 title mod");
do_check_eq(root.getChild(0).title, "1 title mod");
do_check_eq(root.getChild(0).bookmarkGuid, bookmarkGuidZero);
do_check_eq(root.getChild(0).pageGuid, pageGuidZero);
let bookmarkGuidOne = root.getChild(1).bookmarkGuid;
let pageGuidOne = root.getChild(1).pageGuid;
bmsvc.setItemTitle(f1, "test foolder 234");
do_check_eq(root.getChild(1).title, "test foolder 234");
do_check_eq(root.getChild(1).bookmarkGuid, bookmarkGuidOne);
do_check_eq(root.getChild(1).pageGuid, pageGuidOne);
root.containerOpen = false;
yield PlacesUtils.bookmarks.eraseEverything();
});
add_task(function* test_addVisitAndCheckGuid() {
// add a visit and test page guid and non-existing bookmark guids.
let sourceURI = uri("http://test4.com/");
yield PlacesTestUtils.addVisits({ uri: sourceURI });
do_check_eq(bmsvc.getBookmarkedURIFor(sourceURI), null);
let options = histsvc.getNewQueryOptions();
let query = histsvc.getNewQuery();
query.uri = sourceURI;
let root = histsvc.executeQuery(query, options).root;
root.containerOpen = true;
do_check_eq(root.childCount, 1);
do_check_valid_places_guid(root.getChild(0).pageGuid);
do_check_eq(root.getChild(0).bookmarkGuid, "");
root.containerOpen = false;
yield PlacesTestUtils.clearHistory();
});
add_task(function* test_addItemsWithInvalidGUIDsFails() {
const INVALID_GUID = "XYZ";
try {
bmsvc.createFolder(bmsvc.placesRoot, "XYZ folder",
bmsvc.DEFAULT_INDEX, INVALID_GUID);
do_throw("Adding a folder with an invalid guid should fail");
}
catch (ex) { }
let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder",
bmsvc.DEFAULT_INDEX);
try {
bmsvc.insertBookmark(folder, uri("http://test.tld"), bmsvc.DEFAULT_INDEX,
"title", INVALID_GUID);
do_throw("Adding a bookmark with an invalid guid should fail");
}
catch (ex) { }
try {
bmsvc.insertSeparator(folder, bmsvc.DEFAULT_INDEX, INVALID_GUID);
do_throw("Adding a separator with an invalid guid should fail");
}
catch (ex) { }
yield PlacesUtils.bookmarks.eraseEverything();
});
add_task(function* test_addItemsWithGUIDs() {
const FOLDER_GUID = "FOLDER--GUID";
const BOOKMARK_GUID = "BM------GUID";
const SEPARATOR_GUID = "SEP-----GUID";
let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder",
bmsvc.DEFAULT_INDEX, FOLDER_GUID);
bmsvc.insertBookmark(folder, uri("http://test1.com/"), bmsvc.DEFAULT_INDEX,
"1 title", BOOKMARK_GUID);
bmsvc.insertSeparator(folder, bmsvc.DEFAULT_INDEX, SEPARATOR_GUID);
let root = PlacesUtils.getFolderContents(folder).root;
do_check_eq(root.childCount, 2);
do_check_eq(root.bookmarkGuid, FOLDER_GUID);
do_check_eq(root.getChild(0).bookmarkGuid, BOOKMARK_GUID);
do_check_eq(root.getChild(1).bookmarkGuid, SEPARATOR_GUID);
root.containerOpen = false;
yield PlacesUtils.bookmarks.eraseEverything();
});
add_task(function* test_emptyGUIDIgnored() {
let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder",
bmsvc.DEFAULT_INDEX, "");
do_check_valid_places_guid(PlacesUtils.getFolderContents(folder)
.root.bookmarkGuid);
yield PlacesUtils.bookmarks.eraseEverything();
});
add_task(function* test_usingSameGUIDFails() {
const GUID = "XYZXYZXYZXYZ";
bmsvc.createFolder(bmsvc.placesRoot, "test folder",
bmsvc.DEFAULT_INDEX, GUID);
try {
bmsvc.createFolder(bmsvc.placesRoot, "test folder 2",
bmsvc.DEFAULT_INDEX, GUID);
do_throw("Using the same guid twice should fail");
}
catch (ex) { }
yield PlacesUtils.bookmarks.eraseEverything();
});
|