summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_bookmarks_restore_notification.js
blob: 2f8022c6b6a1e44f6d2d2683ff4685a8f39f1ed0 (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
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
320
321
322
323
324
325
/* -*- 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/. */

Cu.import("resource://gre/modules/BookmarkHTMLUtils.jsm");

/**
 * 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|.
 */
function* addBookmarks() {
  for (let url of uris) {
    yield PlacesUtils.bookmarks.insert({
      url: url, parentGuid: PlacesUtils.bookmarks.menuGuid
    })
  }
  checkBookmarksExist();
}

/**
 * Checks that all of the bookmarks created for |uris| exist.  It works by
 * creating one query per URI and then ORing all the queries.  The number of
 * results returned should be uris.length.
 */
function checkBookmarksExist() {
  let hs = PlacesUtils.history;
  let queries = uris.map(function (u) {
    let q = hs.getNewQuery();
    q.uri = uri(u);
    return q;
  });
  let options = hs.getNewQueryOptions();
  options.queryType = options.QUERY_TYPE_BOOKMARKS;
  let root = hs.executeQueries(queries, uris.length, options).root;
  root.containerOpen = true;
  Assert.equal(root.childCount, uris.length);
  root.containerOpen = false;
}

/**
 * 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
 */
function promiseFile(aBasename) {
  let path = OS.Path.join(OS.Constants.Path.profileDir, aBasename);
  do_print("opening " + path);
  return OS.File.open(path, { truncate: true })
                .then(aFile => {
                  aFile.close();
                  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
 */
function* checkObservers(expectPromises, expectedData) {
  let [promiseBegin, promiseResult] = expectPromises;

  let beginData = (yield promiseBegin)[1];
  Assert.equal(beginData, expectedData.data,
    "Data for current test should be what is expected");

  let [resultSubject, resultData] = yield 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(aSubject.QueryInterface(Ci.nsISupportsPRInt64).data,
                expectedData.folderId);
  } else {
    Assert.equal(expectedData.folderId, null);
  }
}

/**
 * Run after every test cases.
 */
function* teardown(file, begin, success, fail) {
  // On restore failed, file may not exist, so wrap in try-catch.
  try {
    yield OS.File.remove(file, {ignoreAbsent: true});
  } catch (e) {}

  // clean up bookmarks
  yield PlacesUtils.bookmarks.eraseEverything();
}

add_task(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);

  do_print("JSON restore: normal restore should succeed");
  let file = yield promiseFile("bookmarks-test_restoreNotification.json");
  yield addBookmarks();

  yield BookmarkJSONUtils.exportToFile(file);
  yield PlacesUtils.bookmarks.eraseEverything();
  try {
    yield BookmarkJSONUtils.importFromFile(file, true);
  } catch (e) {
    do_throw("  Restore should not have failed" + e);
  }

  yield checkObservers(expectPromises, expectedData);
  yield teardown(file);
});

add_task(function* test_json_restore_empty() {
  let expectedData = {
    data:       NSIOBSERVER_DATA_JSON,
    folderId:   null
  }
  let expectPromises = registerObservers(true);

  do_print("JSON restore: empty file should succeed");
  let file = yield promiseFile("bookmarks-test_restoreNotification.json");
  try {
    yield BookmarkJSONUtils.importFromFile(file, true);
  } catch (e) {
    do_throw("  Restore should not have failed" + e);
  }

  yield checkObservers(expectPromises, expectedData);
  yield teardown(file);
});

add_task(function* test_json_restore_nonexist() {
  let expectedData = {
    data:       NSIOBSERVER_DATA_JSON,
    folderId:   null
  }
  let expectPromises = registerObservers(false);

  do_print("JSON restore: nonexistent file should fail");
  let file = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
  file.append("this file doesn't exist because nobody created it 1");
  try {
    yield BookmarkJSONUtils.importFromFile(file, true);
    do_throw("  Restore should have failed");
  } catch (e) {}

  yield checkObservers(expectPromises, expectedData);
  yield teardown(file);
});

add_task(function* test_html_restore_normal() {
  let expectedData = {
    data:       NSIOBSERVER_DATA_HTML,
    folderId:   null
  }
  let expectPromises = registerObservers(true);

  do_print("HTML restore: normal restore should succeed");
  let file = yield promiseFile("bookmarks-test_restoreNotification.html");
  yield addBookmarks();
  yield BookmarkHTMLUtils.exportToFile(file);
  yield PlacesUtils.bookmarks.eraseEverything();
  try {
    BookmarkHTMLUtils.importFromFile(file, false)
                     .then(null, do_report_unexpected_exception);
  } catch (e) {
    do_throw("  Restore should not have failed");
  }

  yield checkObservers(expectPromises, expectedData);
  yield teardown(file);
});

add_task(function* test_html_restore_empty() {
  let expectedData = {
    data:       NSIOBSERVER_DATA_HTML,
    folderId:   null
  }
  let expectPromises = registerObservers(true);

  do_print("HTML restore: empty file should succeed");
  let file = yield promiseFile("bookmarks-test_restoreNotification.init.html");
  try {
    BookmarkHTMLUtils.importFromFile(file, false)
                     .then(null, do_report_unexpected_exception);
  } catch (e) {
    do_throw("  Restore should not have failed");
  }

  yield checkObservers(expectPromises, expectedData);
  yield teardown(file);
});

add_task(function* test_html_restore_nonexist() {
  let expectedData = {
    data:       NSIOBSERVER_DATA_HTML,
    folderId:   null
  }
  let expectPromises = registerObservers(false);

  do_print("HTML restore: nonexistent file should fail");
  let file = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
  file.append("this file doesn't exist because nobody created it 2");
  try {
    yield BookmarkHTMLUtils.importFromFile(file, false);
    do_throw("Should fail!");
  } catch (e) {}

  yield checkObservers(expectPromises, expectedData);
  yield teardown(file);
});

add_task(function* test_html_init_restore_normal() {
  let expectedData = {
    data:       NSIOBSERVER_DATA_HTML_INIT,
    folderId:   null
  }
  let expectPromises = registerObservers(true);

  do_print("HTML initial restore: normal restore should succeed");
  let file = yield promiseFile("bookmarks-test_restoreNotification.init.html");
  yield addBookmarks();
  yield BookmarkHTMLUtils.exportToFile(file);
  yield PlacesUtils.bookmarks.eraseEverything();
  try {
    BookmarkHTMLUtils.importFromFile(file, true)
                     .then(null, do_report_unexpected_exception);
  } catch (e) {
    do_throw("  Restore should not have failed");
  }

  yield checkObservers(expectPromises, expectedData);
  yield teardown(file);
});

add_task(function* test_html_init_restore_empty() {
  let expectedData = {
    data:       NSIOBSERVER_DATA_HTML_INIT,
    folderId:   null
  }
  let expectPromises = registerObservers(true);

  do_print("HTML initial restore: empty file should succeed");
  let file = yield promiseFile("bookmarks-test_restoreNotification.init.html");
  try {
    BookmarkHTMLUtils.importFromFile(file, true)
                     .then(null, do_report_unexpected_exception);
  } catch (e) {
    do_throw("  Restore should not have failed");
  }

  yield checkObservers(expectPromises, expectedData);
  yield teardown(file);
});

add_task(function* test_html_init_restore_nonexist() {
  let expectedData = {
    data:       NSIOBSERVER_DATA_HTML_INIT,
    folderId:   null
  }
  let expectPromises = registerObservers(false);

  do_print("HTML initial restore: nonexistent file should fail");
  let file = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
  file.append("this file doesn't exist because nobody created it 3");
  try {
    yield BookmarkHTMLUtils.importFromFile(file, true);
    do_throw("Should fail!");
  } catch (e) {}

  yield checkObservers(expectPromises, expectedData);
  yield teardown(file);
});