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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
/* -*- 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 { BookmarkHTMLUtils } = ChromeUtils.importESModule(
"resource://gre/modules/BookmarkHTMLUtils.sys.mjs"
);
/**
* Tests the bookmarks-restore-* nsIObserver notifications after restoring
* bookmarks from JSON and HTML. See bug 470314.
*/
// The topics and data passed to nsIObserver.observe() on bookmarks restore
const NSIOBSERVER_TOPIC_BEGIN = "bookmarks-restore-begin";
const NSIOBSERVER_TOPIC_SUCCESS = "bookmarks-restore-success";
const NSIOBSERVER_TOPIC_FAILED = "bookmarks-restore-failed";
const NSIOBSERVER_DATA_JSON = "json";
const NSIOBSERVER_DATA_HTML = "html";
const NSIOBSERVER_DATA_HTML_INIT = "html-initial";
// Bookmarks are added for these URIs
var uris = [
"http://example.com/1",
"http://example.com/2",
"http://example.com/3",
"http://example.com/4",
"http://example.com/5",
];
/**
* Adds some bookmarks for the URIs in |uris|.
*/
async function addBookmarks() {
for (let url of uris) {
await PlacesUtils.bookmarks.insert({
url,
parentGuid: PlacesUtils.bookmarks.menuGuid,
});
Assert.ok(await PlacesUtils.bookmarks.fetch({ url }), "Url is bookmarked");
}
}
/**
* Creates an file in the profile directory.
*
* @param aBasename
* e.g., "foo.txt" in the path /some/long/path/foo.txt
* @return {Promise}
* @resolves to an OS.File path
*/
async function promiseFile(aBasename) {
let path = PathUtils.join(PathUtils.profileDir, aBasename);
info("opening " + path);
await IOUtils.writeUTF8(path, "");
return path;
}
/**
* Register observers via promiseTopicObserved helper.
*
* @param {boolean} expectSuccess pass true when expect a success notification
* @return {Promise[]}
*/
function registerObservers(expectSuccess) {
let promiseBegin = promiseTopicObserved(NSIOBSERVER_TOPIC_BEGIN);
let promiseResult;
if (expectSuccess) {
promiseResult = promiseTopicObserved(NSIOBSERVER_TOPIC_SUCCESS);
} else {
promiseResult = promiseTopicObserved(NSIOBSERVER_TOPIC_FAILED);
}
return [promiseBegin, promiseResult];
}
/**
* Check notification results.
*
* @param {Promise[]} expectPromises array contain promiseBegin and promiseResult
* @param {object} expectedData contain data and folderId
*/
async function checkObservers(expectPromises, expectedData) {
let [promiseBegin, promiseResult] = expectPromises;
let beginData = (await promiseBegin)[1];
Assert.equal(
beginData,
expectedData.data,
"Data for current test should be what is expected"
);
let [resultSubject, resultData] = await promiseResult;
Assert.equal(
resultData,
expectedData.data,
"Data for current test should be what is expected"
);
// Make sure folder ID is what is expected. For importing HTML into a
// folder, this will be an integer, otherwise null.
if (resultSubject) {
Assert.equal(
resultSubject.QueryInterface(Ci.nsISupportsPRInt64).data,
expectedData.folderId
);
} else {
Assert.equal(expectedData.folderId, null);
}
}
/**
* Run after every test cases.
*/
async function teardown(file, begin, success, fail) {
// On restore failed, file may not exist, so wrap in try-catch.
await IOUtils.remove(file, { ignoreAbsent: true });
// clean up bookmarks
await PlacesUtils.bookmarks.eraseEverything();
}
add_task(async function test_json_restore_normal() {
// data: the data passed to nsIObserver.observe() corresponding to the test
// folderId: for HTML restore into a folder, the folder ID to restore into;
// otherwise, set it to null
let expectedData = {
data: NSIOBSERVER_DATA_JSON,
folderId: null,
};
let expectPromises = registerObservers(true);
info("JSON restore: normal restore should succeed");
let file = await promiseFile("bookmarks-test_restoreNotification.json");
await addBookmarks();
await BookmarkJSONUtils.exportToFile(file);
await PlacesUtils.bookmarks.eraseEverything();
try {
await BookmarkJSONUtils.importFromFile(file, { replace: true });
} catch (e) {
do_throw(" Restore should not have failed " + e);
}
await checkObservers(expectPromises, expectedData);
await teardown(file);
});
add_task(async function test_json_restore_empty() {
let expectedData = {
data: NSIOBSERVER_DATA_JSON,
folderId: null,
};
let expectPromises = registerObservers(false);
info("JSON restore: empty file should fail");
let file = await promiseFile("bookmarks-test_restoreNotification.json");
await Assert.rejects(
BookmarkJSONUtils.importFromFile(file, { replace: true }),
/SyntaxError/,
"Restore should reject for an empty file."
);
await checkObservers(expectPromises, expectedData);
await teardown(file);
});
add_task(async function test_json_restore_nonexist() {
let expectedData = {
data: NSIOBSERVER_DATA_JSON,
folderId: null,
};
let expectPromises = registerObservers(false);
info("JSON restore: nonexistent file should fail");
let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
file.append("this file doesn't exist because nobody created it 1");
await Assert.rejects(
BookmarkJSONUtils.importFromFile(file.path, { replace: true }),
/Cannot restore from nonexisting json file/,
"Restore should reject for a non-existent file."
);
await checkObservers(expectPromises, expectedData);
await teardown(file.path);
});
add_task(async function test_html_restore_normal() {
let expectedData = {
data: NSIOBSERVER_DATA_HTML,
folderId: null,
};
let expectPromises = registerObservers(true);
info("HTML restore: normal restore should succeed");
let file = await promiseFile("bookmarks-test_restoreNotification.html");
await addBookmarks();
await BookmarkHTMLUtils.exportToFile(file);
await PlacesUtils.bookmarks.eraseEverything();
try {
BookmarkHTMLUtils.importFromFile(file).catch(
do_report_unexpected_exception
);
} catch (e) {
do_throw(" Restore should not have failed");
}
await checkObservers(expectPromises, expectedData);
await teardown(file);
});
add_task(async function test_html_restore_empty() {
let expectedData = {
data: NSIOBSERVER_DATA_HTML,
folderId: null,
};
let expectPromises = registerObservers(true);
info("HTML restore: empty file should succeed");
let file = await promiseFile("bookmarks-test_restoreNotification.init.html");
try {
BookmarkHTMLUtils.importFromFile(file).catch(
do_report_unexpected_exception
);
} catch (e) {
do_throw(" Restore should not have failed");
}
await checkObservers(expectPromises, expectedData);
await teardown(file);
});
add_task(async function test_html_restore_nonexist() {
let expectedData = {
data: NSIOBSERVER_DATA_HTML,
folderId: null,
};
let expectPromises = registerObservers(false);
info("HTML restore: nonexistent file should fail");
let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
file.append("this file doesn't exist because nobody created it 2");
await Assert.rejects(
BookmarkHTMLUtils.importFromFile(file.path),
/Cannot import from nonexisting html file/,
"Restore should reject for a non-existent file."
);
await checkObservers(expectPromises, expectedData);
await teardown(file.path);
});
add_task(async function test_html_init_restore_normal() {
let expectedData = {
data: NSIOBSERVER_DATA_HTML_INIT,
folderId: null,
};
let expectPromises = registerObservers(true);
info("HTML initial restore: normal restore should succeed");
let file = await promiseFile("bookmarks-test_restoreNotification.init.html");
await addBookmarks();
await BookmarkHTMLUtils.exportToFile(file);
await PlacesUtils.bookmarks.eraseEverything();
try {
BookmarkHTMLUtils.importFromFile(file, { replace: true }).catch(
do_report_unexpected_exception
);
} catch (e) {
do_throw(" Restore should not have failed");
}
await checkObservers(expectPromises, expectedData);
await teardown(file);
});
add_task(async function test_html_init_restore_empty() {
let expectedData = {
data: NSIOBSERVER_DATA_HTML_INIT,
folderId: null,
};
let expectPromises = registerObservers(true);
info("HTML initial restore: empty file should succeed");
let file = await promiseFile("bookmarks-test_restoreNotification.init.html");
try {
BookmarkHTMLUtils.importFromFile(file, { replace: true }).catch(
do_report_unexpected_exception
);
} catch (e) {
do_throw(" Restore should not have failed");
}
await checkObservers(expectPromises, expectedData);
await teardown(file);
});
add_task(async function test_html_init_restore_nonexist() {
let expectedData = {
data: NSIOBSERVER_DATA_HTML_INIT,
folderId: null,
};
let expectPromises = registerObservers(false);
info("HTML initial restore: nonexistent file should fail");
let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
file.append("this file doesn't exist because nobody created it 3");
await Assert.rejects(
BookmarkHTMLUtils.importFromFile(file.path, { replace: true }),
/Cannot import from nonexisting html file/,
"Restore should reject for a non-existent file."
);
await checkObservers(expectPromises, expectedData);
await teardown(file.path);
});
|