test_bookmarksRestoreNotification.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Places test code.
17  *
18  * The Initial Developer of the Original Code is Mozilla Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2009
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Drew Willcoxon <adw@mozilla.com> (Original Author)
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
44 // The topics and data passed to nsIObserver.observe() on bookmarks restore
45 const NSIOBSERVER_TOPIC_BEGIN = "bookmarks-restore-begin";
46 const NSIOBSERVER_TOPIC_SUCCESS = "bookmarks-restore-success";
47 const NSIOBSERVER_TOPIC_FAILED = "bookmarks-restore-failed";
48 const NSIOBSERVER_DATA_JSON = "json";
49 const NSIOBSERVER_DATA_HTML = "html";
50 const NSIOBSERVER_DATA_HTML_INIT = "html-initial";
51 
52 // Bookmarks are added for these URIs
53 var uris = [
54  "http://example.com/1",
55  "http://example.com/2",
56  "http://example.com/3",
57  "http://example.com/4",
58  "http://example.com/5",
59 ];
60 
61 // Add tests here. Each is an object with these properties:
62 // desc: description printed before test is run
63 // currTopic: the next expected topic that should be observed for the test;
64 // set to NSIOBSERVER_TOPIC_BEGIN to begin
65 // finalTopic: the last expected topic that should be observed for the test,
66 // which then causes the next test to be run
67 // data: the data passed to nsIObserver.observe() corresponding to the
68 // test
69 // file: the nsILocalFile that the test creates
70 // folderId: for HTML restore into a folder, the folder ID to restore into;
71 // otherwise, set it to null
72 // run: a method that actually runs the test
73 var tests = [
74  {
75  desc: "JSON restore: normal restore should succeed",
76  currTopic: NSIOBSERVER_TOPIC_BEGIN,
77  finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
79  folderId: null,
80  run: function () {
81  this.file = createFile("bookmarks-test_restoreNotification.json");
82  addBookmarks();
83  PlacesUtils.backupBookmarksToFile(this.file);
85  try {
86  PlacesUtils.restoreBookmarksFromJSONFile(this.file);
87  }
88  catch (e) {
89  do_throw(" Restore should not have failed");
90  }
91  }
92  },
93 
94  {
95  desc: "JSON restore: empty file should succeed",
96  currTopic: NSIOBSERVER_TOPIC_BEGIN,
97  finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
99  folderId: null,
100  run: function () {
101  this.file = createFile("bookmarks-test_restoreNotification.json");
102  try {
103  PlacesUtils.restoreBookmarksFromJSONFile(this.file);
104  }
105  catch (e) {
106  do_throw(" Restore should not have failed");
107  }
108  }
109  },
110 
111  {
112  desc: "JSON restore: nonexisting file should fail",
113  currTopic: NSIOBSERVER_TOPIC_BEGIN,
114  finalTopic: NSIOBSERVER_TOPIC_FAILED,
116  folderId: null,
117  run: function () {
118  this.file = dirSvc.get("ProfD", Ci.nsILocalFile);
119  this.file.append("this file doesn't exist because nobody created it");
120  try {
121  PlacesUtils.restoreBookmarksFromJSONFile(this.file);
122  do_throw(" Restore should have failed");
123  }
124  catch (e) {}
125  }
126  },
127 
128  {
129  desc: "HTML restore: normal restore should succeed",
130  currTopic: NSIOBSERVER_TOPIC_BEGIN,
131  finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
133  folderId: null,
134  run: function () {
135  this.file = createFile("bookmarks-test_restoreNotification.html");
136  addBookmarks();
137  importer.exportHTMLToFile(this.file);
139  try {
140  importer.importHTMLFromFile(this.file, false);
141  }
142  catch (e) {
143  do_throw(" Restore should not have failed");
144  }
145  }
146  },
147 
148  {
149  desc: "HTML restore: empty file should succeed",
150  currTopic: NSIOBSERVER_TOPIC_BEGIN,
151  finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
153  folderId: null,
154  run: function () {
155  this.file = createFile("bookmarks-test_restoreNotification.init.html");
156  try {
157  importer.importHTMLFromFile(this.file, false);
158  }
159  catch (e) {
160  do_throw(" Restore should not have failed");
161  }
162  }
163  },
164 
165  {
166  desc: "HTML restore: nonexisting file should fail",
167  currTopic: NSIOBSERVER_TOPIC_BEGIN,
168  finalTopic: NSIOBSERVER_TOPIC_FAILED,
170  folderId: null,
171  run: function () {
172  this.file = dirSvc.get("ProfD", Ci.nsILocalFile);
173  this.file.append("this file doesn't exist because nobody created it");
174  try {
175  importer.importHTMLFromFile(this.file, false);
176  do_throw(" Restore should have failed");
177  }
178  catch (e) {}
179  }
180  },
181 
182  {
183  desc: "HTML initial restore: normal restore should succeed",
184  currTopic: NSIOBSERVER_TOPIC_BEGIN,
185  finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
187  folderId: null,
188  run: function () {
189  this.file = createFile("bookmarks-test_restoreNotification.init.html");
190  addBookmarks();
191  importer.exportHTMLToFile(this.file);
193  try {
194  importer.importHTMLFromFile(this.file, true);
195  }
196  catch (e) {
197  do_throw(" Restore should not have failed");
198  }
199  }
200  },
201 
202  {
203  desc: "HTML initial restore: empty file should succeed",
204  currTopic: NSIOBSERVER_TOPIC_BEGIN,
205  finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
207  folderId: null,
208  run: function () {
209  this.file = createFile("bookmarks-test_restoreNotification.init.html");
210  try {
211  importer.importHTMLFromFile(this.file, true);
212  }
213  catch (e) {
214  do_throw(" Restore should not have failed");
215  }
216  }
217  },
218 
219  {
220  desc: "HTML initial restore: nonexisting file should fail",
221  currTopic: NSIOBSERVER_TOPIC_BEGIN,
222  finalTopic: NSIOBSERVER_TOPIC_FAILED,
224  folderId: null,
225  run: function () {
226  this.file = dirSvc.get("ProfD", Ci.nsILocalFile);
227  this.file.append("this file doesn't exist because nobody created it");
228  try {
229  importer.importHTMLFromFile(this.file, true);
230  do_throw(" Restore should have failed");
231  }
232  catch (e) {}
233  }
234  },
235 
236  {
237  desc: "HTML restore into folder: normal restore should succeed",
238  currTopic: NSIOBSERVER_TOPIC_BEGIN,
239  finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
241  run: function () {
242  this.file = createFile("bookmarks-test_restoreNotification.html");
243  addBookmarks();
244  importer.exportHTMLToFile(this.file);
246  this.folderId = bmsvc.createFolder(bmsvc.unfiledBookmarksFolder,
247  "test folder",
248  bmsvc.DEFAULT_INDEX);
249  print(" Sanity check: createFolder() should have succeeded");
250  do_check_true(this.folderId > 0);
251  try {
252  importer.importHTMLFromFileToFolder(this.file, this.folderId, false);
253  }
254  catch (e) {
255  do_throw(" Restore should not have failed");
256  }
257  }
258  },
259 
260  {
261  desc: "HTML restore into folder: empty file should succeed",
262  currTopic: NSIOBSERVER_TOPIC_BEGIN,
263  finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
265  run: function () {
266  this.file = createFile("bookmarks-test_restoreNotification.init.html");
267  this.folderId = bmsvc.createFolder(bmsvc.unfiledBookmarksFolder,
268  "test folder",
269  bmsvc.DEFAULT_INDEX);
270  print(" Sanity check: createFolder() should have succeeded");
271  do_check_true(this.folderId > 0);
272  try {
273  importer.importHTMLFromFileToFolder(this.file, this.folderId, false);
274  }
275  catch (e) {
276  do_throw(" Restore should not have failed");
277  }
278  }
279  },
280 
281  {
282  desc: "HTML restore into folder: nonexisting file should fail",
283  currTopic: NSIOBSERVER_TOPIC_BEGIN,
284  finalTopic: NSIOBSERVER_TOPIC_FAILED,
286  run: function () {
287  this.file = dirSvc.get("ProfD", Ci.nsILocalFile);
288  this.file.append("this file doesn't exist because nobody created it");
289  this.folderId = bmsvc.createFolder(bmsvc.unfiledBookmarksFolder,
290  "test folder",
291  bmsvc.DEFAULT_INDEX);
292  print(" Sanity check: createFolder() should have succeeded");
293  do_check_true(this.folderId > 0);
294  try {
295  importer.importHTMLFromFileToFolder(this.file, this.folderId, false);
296  do_throw(" Restore should have failed");
297  }
298  catch (e) {}
299  }
300  }
301 ];
302 
303 // nsIObserver that observes bookmarks-restore-begin.
305  observe: function _beginObserver(aSubject, aTopic, aData) {
306  var test = tests[currTestIndex];
307 
308  print(" Observed " + aTopic);
309  print(" Topic for current test should be what is expected");
310  do_check_eq(aTopic, test.currTopic);
311 
312  print(" Data for current test should be what is expected");
313  do_check_eq(aData, test.data);
314 
315  // Update current expected topic to the next expected one.
316  test.currTopic = test.finalTopic;
317  }
318 };
319 
320 // nsIObserver that observes bookmarks-restore-success/failed. This starts
321 // the next test.
323  observe: function _successAndFailedObserver(aSubject, aTopic, aData) {
324  var test = tests[currTestIndex];
325 
326  print(" Observed " + aTopic);
327  print(" Topic for current test should be what is expected");
328  do_check_eq(aTopic, test.currTopic);
329 
330  print(" Data for current test should be what is expected");
331  do_check_eq(aData, test.data);
332 
333  // On restore failed, file may not exist, so wrap in try-catch.
334  try {
335  test.file.remove(false);
336  }
337  catch (exc) {}
338 
339  // Make sure folder ID is what is expected. For importing HTML into a
340  // folder, this will be an integer, otherwise null.
341  if (aSubject) {
342  do_check_eq(aSubject.QueryInterface(Ci.nsISupportsPRInt64).data,
343  test.folderId);
344  }
345  else
346  do_check_eq(test.folderId, null);
347 
349  doNextTest();
350  }
351 };
352 
353 // Index of the currently running test. See doNextTest().
354 var currTestIndex = -1;
355 
356 // Need JSON import/export in toolkit/components/places/src/utils.js
357 Components.utils.import("resource://gre/modules/utils.js");
358 
359 var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
360  getService(Ci.nsINavBookmarksService);
361 
362 var obssvc = Cc["@mozilla.org/observer-service;1"].
363  getService(Ci.nsIObserverService);
364 
365 var importer = Cc["@mozilla.org/browser/places/import-export-service;1"].
366  getService(Ci.nsIPlacesImportExportService);
367 
369 
373 function addBookmarks() {
374  uris.forEach(function (u) bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder,
375  uri(u),
376  bmsvc.DEFAULT_INDEX,
377  u));
379 }
380 
387  var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
388  getService(Ci.nsINavHistoryService);
389  var queries = uris.map(function (u) {
390  var q = hs.getNewQuery();
391  q.uri = uri(u);
392  return q;
393  });
394  var options = hs.getNewQueryOptions();
395  options.queryType = options.QUERY_TYPE_BOOKMARKS;
396  var root = hs.executeQueries(queries, uris.length, options).root;
397  root.containerOpen = true;
398  do_check_eq(root.childCount, uris.length);
399  root.containerOpen = false;
400 }
401 
409 function createFile(aBasename) {
410  var file = dirSvc.get("ProfD", Ci.nsILocalFile);
411  file.append(aBasename);
412  if (file.exists())
413  file.remove(false);
414  file.create(file.NORMAL_FILE_TYPE, 0666);
415  if (!file.exists())
416  do_throw("Couldn't create file: " + aBasename);
417  return file;
418 }
419 
423 function doNextTest() {
424  currTestIndex++;
425  if (currTestIndex >= tests.length) {
426  obssvc.removeObserver(beginObserver, NSIOBSERVER_TOPIC_BEGIN);
427  obssvc.removeObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_SUCCESS);
428  obssvc.removeObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_FAILED);
429  do_test_finished();
430  }
431  else {
432  var test = tests[currTestIndex];
433  print("Running test: " + test.desc);
434  test.run();
435  }
436 }
437 
439 
440 function run_test() {
441  do_test_pending();
442  obssvc.addObserver(beginObserver, NSIOBSERVER_TOPIC_BEGIN, false);
443  obssvc.addObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_SUCCESS, false);
444  obssvc.addObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_FAILED, false);
445  doNextTest();
446 }
const Cc
do_check_eq(typeof PlacesUtils,"object")
var dirSvc
const NSIOBSERVER_TOPIC_FAILED
function remove_all_bookmarks()
const NSIOBSERVER_DATA_HTML_INIT
getService(Ci.sbIFaceplateManager)
function createFile(aBasename)
function checkBookmarksExist()
return null
Definition: FeedWriter.js:1143
var uri
Definition: FeedWriter.js:1135
foldersync options
Definition: options.js:13
const Ci
function addBookmarks()
observe data
Definition: FeedWriter.js:1329
const NSIOBSERVER_TOPIC_SUCCESS
_updateTextAndScrollDataForFrame aData
var file
sbDeviceFirmwareAutoCheckForUpdate prototype observe