toolkit/components/places/tests/unit/test_bookmarks_restore_notification.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 Cu.import("resource://gre/modules/BookmarkHTMLUtils.jsm");
michael@0 8
michael@0 9 /**
michael@0 10 * Tests the bookmarks-restore-* nsIObserver notifications after restoring
michael@0 11 * bookmarks from JSON and HTML. See bug 470314.
michael@0 12 */
michael@0 13
michael@0 14 // The topics and data passed to nsIObserver.observe() on bookmarks restore
michael@0 15 const NSIOBSERVER_TOPIC_BEGIN = "bookmarks-restore-begin";
michael@0 16 const NSIOBSERVER_TOPIC_SUCCESS = "bookmarks-restore-success";
michael@0 17 const NSIOBSERVER_TOPIC_FAILED = "bookmarks-restore-failed";
michael@0 18 const NSIOBSERVER_DATA_JSON = "json";
michael@0 19 const NSIOBSERVER_DATA_HTML = "html";
michael@0 20 const NSIOBSERVER_DATA_HTML_INIT = "html-initial";
michael@0 21
michael@0 22 // Bookmarks are added for these URIs
michael@0 23 var uris = [
michael@0 24 "http://example.com/1",
michael@0 25 "http://example.com/2",
michael@0 26 "http://example.com/3",
michael@0 27 "http://example.com/4",
michael@0 28 "http://example.com/5",
michael@0 29 ];
michael@0 30
michael@0 31 // Add tests here. Each is an object with these properties:
michael@0 32 // desc: description printed before test is run
michael@0 33 // currTopic: the next expected topic that should be observed for the test;
michael@0 34 // set to NSIOBSERVER_TOPIC_BEGIN to begin
michael@0 35 // finalTopic: the last expected topic that should be observed for the test,
michael@0 36 // which then causes the next test to be run
michael@0 37 // data: the data passed to nsIObserver.observe() corresponding to the
michael@0 38 // test
michael@0 39 // file: the nsILocalFile that the test creates
michael@0 40 // folderId: for HTML restore into a folder, the folder ID to restore into;
michael@0 41 // otherwise, set it to null
michael@0 42 // run: a method that actually runs the test
michael@0 43 var tests = [
michael@0 44 {
michael@0 45 desc: "JSON restore: normal restore should succeed",
michael@0 46 currTopic: NSIOBSERVER_TOPIC_BEGIN,
michael@0 47 finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
michael@0 48 data: NSIOBSERVER_DATA_JSON,
michael@0 49 folderId: null,
michael@0 50 run: function () {
michael@0 51 Task.spawn(function () {
michael@0 52 this.file = yield promiseFile("bookmarks-test_restoreNotification.json");
michael@0 53 addBookmarks();
michael@0 54
michael@0 55 yield BookmarkJSONUtils.exportToFile(this.file);
michael@0 56 remove_all_bookmarks();
michael@0 57 try {
michael@0 58 yield BookmarkJSONUtils.importFromFile(this.file, true);
michael@0 59 }
michael@0 60 catch (e) {
michael@0 61 do_throw(" Restore should not have failed");
michael@0 62 }
michael@0 63 }.bind(this));
michael@0 64 }
michael@0 65 },
michael@0 66
michael@0 67 {
michael@0 68 desc: "JSON restore: empty file should succeed",
michael@0 69 currTopic: NSIOBSERVER_TOPIC_BEGIN,
michael@0 70 finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
michael@0 71 data: NSIOBSERVER_DATA_JSON,
michael@0 72 folderId: null,
michael@0 73 run: function () {
michael@0 74 Task.spawn(function() {
michael@0 75 this.file = yield promiseFile("bookmarks-test_restoreNotification.json");
michael@0 76 try {
michael@0 77 yield BookmarkJSONUtils.importFromFile(this.file, true);
michael@0 78 }
michael@0 79 catch (e) {
michael@0 80 do_throw(" Restore should not have failed" + e);
michael@0 81 }
michael@0 82 }.bind(this));
michael@0 83 }
michael@0 84 },
michael@0 85
michael@0 86 {
michael@0 87 desc: "JSON restore: nonexistent file should fail",
michael@0 88 currTopic: NSIOBSERVER_TOPIC_BEGIN,
michael@0 89 finalTopic: NSIOBSERVER_TOPIC_FAILED,
michael@0 90 data: NSIOBSERVER_DATA_JSON,
michael@0 91 folderId: null,
michael@0 92 run: function () {
michael@0 93 this.file = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
michael@0 94 this.file.append("this file doesn't exist because nobody created it");
michael@0 95 Task.spawn(function() {
michael@0 96 try {
michael@0 97 yield BookmarkJSONUtils.importFromFile(this.file, true);
michael@0 98 do_throw(" Restore should have failed");
michael@0 99 }
michael@0 100 catch (e) {
michael@0 101 }
michael@0 102 }.bind(this));
michael@0 103 }
michael@0 104 },
michael@0 105
michael@0 106 {
michael@0 107 desc: "HTML restore: normal restore should succeed",
michael@0 108 currTopic: NSIOBSERVER_TOPIC_BEGIN,
michael@0 109 finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
michael@0 110 data: NSIOBSERVER_DATA_HTML,
michael@0 111 folderId: null,
michael@0 112 run: function () {
michael@0 113 Task.spawn(function() {
michael@0 114 this.file = yield promiseFile("bookmarks-test_restoreNotification.html");
michael@0 115 addBookmarks();
michael@0 116 yield BookmarkHTMLUtils.exportToFile(this.file);
michael@0 117 remove_all_bookmarks();
michael@0 118 try {
michael@0 119 BookmarkHTMLUtils.importFromFile(this.file, false)
michael@0 120 .then(null, do_report_unexpected_exception);
michael@0 121 }
michael@0 122 catch (e) {
michael@0 123 do_throw(" Restore should not have failed");
michael@0 124 }
michael@0 125 }.bind(this));
michael@0 126 }
michael@0 127 },
michael@0 128
michael@0 129 {
michael@0 130 desc: "HTML restore: empty file should succeed",
michael@0 131 currTopic: NSIOBSERVER_TOPIC_BEGIN,
michael@0 132 finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
michael@0 133 data: NSIOBSERVER_DATA_HTML,
michael@0 134 folderId: null,
michael@0 135 run: function () {
michael@0 136 Task.spawn(function (){
michael@0 137 this.file = yield promiseFile("bookmarks-test_restoreNotification.init.html");
michael@0 138 try {
michael@0 139 BookmarkHTMLUtils.importFromFile(this.file, false)
michael@0 140 .then(null, do_report_unexpected_exception);
michael@0 141 }
michael@0 142 catch (e) {
michael@0 143 do_throw(" Restore should not have failed");
michael@0 144 }
michael@0 145 }.bind(this));
michael@0 146 }
michael@0 147 },
michael@0 148
michael@0 149 {
michael@0 150 desc: "HTML restore: nonexistent file should fail",
michael@0 151 currTopic: NSIOBSERVER_TOPIC_BEGIN,
michael@0 152 finalTopic: NSIOBSERVER_TOPIC_FAILED,
michael@0 153 data: NSIOBSERVER_DATA_HTML,
michael@0 154 folderId: null,
michael@0 155 run: function () {
michael@0 156 this.file = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
michael@0 157 this.file.append("this file doesn't exist because nobody created it");
michael@0 158 try {
michael@0 159 BookmarkHTMLUtils.importFromFile(this.file, false)
michael@0 160 .then(function onSuccess() do_throw("Should fail!"),
michael@0 161 null);
michael@0 162 }
michael@0 163 catch (e) {}
michael@0 164 }
michael@0 165 },
michael@0 166
michael@0 167 {
michael@0 168 desc: "HTML initial restore: normal restore should succeed",
michael@0 169 currTopic: NSIOBSERVER_TOPIC_BEGIN,
michael@0 170 finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
michael@0 171 data: NSIOBSERVER_DATA_HTML_INIT,
michael@0 172 folderId: null,
michael@0 173 run: function () {
michael@0 174 Task.spawn(function () {
michael@0 175 this.file = yield promiseFile("bookmarks-test_restoreNotification.init.html");
michael@0 176 addBookmarks();
michael@0 177 yield BookmarkHTMLUtils.exportToFile(this.file);
michael@0 178 remove_all_bookmarks();
michael@0 179 try {
michael@0 180 BookmarkHTMLUtils.importFromFile(this.file, true)
michael@0 181 .then(null, do_report_unexpected_exception);
michael@0 182 }
michael@0 183 catch (e) {
michael@0 184 do_throw(" Restore should not have failed");
michael@0 185 }
michael@0 186 }.bind(this));
michael@0 187 }
michael@0 188 },
michael@0 189
michael@0 190 {
michael@0 191 desc: "HTML initial restore: empty file should succeed",
michael@0 192 currTopic: NSIOBSERVER_TOPIC_BEGIN,
michael@0 193 finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
michael@0 194 data: NSIOBSERVER_DATA_HTML_INIT,
michael@0 195 folderId: null,
michael@0 196 run: function () {
michael@0 197 Task.spawn(function () {
michael@0 198 this.file = yield promiseFile("bookmarks-test_restoreNotification.init.html");
michael@0 199 try {
michael@0 200 BookmarkHTMLUtils.importFromFile(this.file, true)
michael@0 201 .then(null, do_report_unexpected_exception);
michael@0 202 }
michael@0 203 catch (e) {
michael@0 204 do_throw(" Restore should not have failed");
michael@0 205 }
michael@0 206 }.bind(this));
michael@0 207 }
michael@0 208 },
michael@0 209
michael@0 210 {
michael@0 211 desc: "HTML initial restore: nonexistent file should fail",
michael@0 212 currTopic: NSIOBSERVER_TOPIC_BEGIN,
michael@0 213 finalTopic: NSIOBSERVER_TOPIC_FAILED,
michael@0 214 data: NSIOBSERVER_DATA_HTML_INIT,
michael@0 215 folderId: null,
michael@0 216 run: function () {
michael@0 217 this.file = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
michael@0 218 this.file.append("this file doesn't exist because nobody created it");
michael@0 219 try {
michael@0 220 BookmarkHTMLUtils.importFromFile(this.file, true)
michael@0 221 .then(function onSuccess() do_throw("Should fail!"),
michael@0 222 null);
michael@0 223 }
michael@0 224 catch (e) {}
michael@0 225 }
michael@0 226 }
michael@0 227 ];
michael@0 228
michael@0 229 // nsIObserver that observes bookmarks-restore-begin.
michael@0 230 var beginObserver = {
michael@0 231 observe: function _beginObserver(aSubject, aTopic, aData) {
michael@0 232 var test = tests[currTestIndex];
michael@0 233
michael@0 234 print(" Observed " + aTopic);
michael@0 235 print(" Topic for current test should be what is expected");
michael@0 236 do_check_eq(aTopic, test.currTopic);
michael@0 237
michael@0 238 print(" Data for current test should be what is expected");
michael@0 239 do_check_eq(aData, test.data);
michael@0 240
michael@0 241 // Update current expected topic to the next expected one.
michael@0 242 test.currTopic = test.finalTopic;
michael@0 243 }
michael@0 244 };
michael@0 245
michael@0 246 // nsIObserver that observes bookmarks-restore-success/failed. This starts
michael@0 247 // the next test.
michael@0 248 var successAndFailedObserver = {
michael@0 249 observe: function _successAndFailedObserver(aSubject, aTopic, aData) {
michael@0 250 var test = tests[currTestIndex];
michael@0 251
michael@0 252 print(" Observed " + aTopic);
michael@0 253 print(" Topic for current test should be what is expected");
michael@0 254 do_check_eq(aTopic, test.currTopic);
michael@0 255
michael@0 256 print(" Data for current test should be what is expected");
michael@0 257 do_check_eq(aData, test.data);
michael@0 258
michael@0 259 // On restore failed, file may not exist, so wrap in try-catch.
michael@0 260 try {
michael@0 261 test.file.remove(false);
michael@0 262 }
michael@0 263 catch (exc) {}
michael@0 264
michael@0 265 // Make sure folder ID is what is expected. For importing HTML into a
michael@0 266 // folder, this will be an integer, otherwise null.
michael@0 267 if (aSubject) {
michael@0 268 do_check_eq(aSubject.QueryInterface(Ci.nsISupportsPRInt64).data,
michael@0 269 test.folderId);
michael@0 270 }
michael@0 271 else
michael@0 272 do_check_eq(test.folderId, null);
michael@0 273
michael@0 274 remove_all_bookmarks();
michael@0 275 do_execute_soon(doNextTest);
michael@0 276 }
michael@0 277 };
michael@0 278
michael@0 279 // Index of the currently running test. See doNextTest().
michael@0 280 var currTestIndex = -1;
michael@0 281
michael@0 282 var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
michael@0 283 getService(Ci.nsINavBookmarksService);
michael@0 284
michael@0 285 var obssvc = Cc["@mozilla.org/observer-service;1"].
michael@0 286 getService(Ci.nsIObserverService);
michael@0 287
michael@0 288 ///////////////////////////////////////////////////////////////////////////////
michael@0 289
michael@0 290 /**
michael@0 291 * Adds some bookmarks for the URIs in |uris|.
michael@0 292 */
michael@0 293 function addBookmarks() {
michael@0 294 uris.forEach(function (u) bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder,
michael@0 295 uri(u),
michael@0 296 bmsvc.DEFAULT_INDEX,
michael@0 297 u));
michael@0 298 checkBookmarksExist();
michael@0 299 }
michael@0 300
michael@0 301 /**
michael@0 302 * Checks that all of the bookmarks created for |uris| exist. It works by
michael@0 303 * creating one query per URI and then ORing all the queries. The number of
michael@0 304 * results returned should be uris.length.
michael@0 305 */
michael@0 306 function checkBookmarksExist() {
michael@0 307 var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
michael@0 308 getService(Ci.nsINavHistoryService);
michael@0 309 var queries = uris.map(function (u) {
michael@0 310 var q = hs.getNewQuery();
michael@0 311 q.uri = uri(u);
michael@0 312 return q;
michael@0 313 });
michael@0 314 var options = hs.getNewQueryOptions();
michael@0 315 options.queryType = options.QUERY_TYPE_BOOKMARKS;
michael@0 316 var root = hs.executeQueries(queries, uris.length, options).root;
michael@0 317 root.containerOpen = true;
michael@0 318 do_check_eq(root.childCount, uris.length);
michael@0 319 root.containerOpen = false;
michael@0 320 }
michael@0 321
michael@0 322 /**
michael@0 323 * Creates an file in the profile directory.
michael@0 324 *
michael@0 325 * @param aBasename
michael@0 326 * e.g., "foo.txt" in the path /some/long/path/foo.txt
michael@0 327 * @return {Promise}
michael@0 328 * @resolves to an OS.File path
michael@0 329 */
michael@0 330 function promiseFile(aBasename) {
michael@0 331 let path = OS.Path.join(OS.Constants.Path.profileDir, aBasename);
michael@0 332 dump("\n\nopening " + path + "\n\n");
michael@0 333 return OS.File.open(path, { truncate: true }).then(aFile => { aFile.close(); return path; });
michael@0 334 }
michael@0 335
michael@0 336 /**
michael@0 337 * Runs the next test or if all tests have been run, finishes.
michael@0 338 */
michael@0 339 function doNextTest() {
michael@0 340 currTestIndex++;
michael@0 341 if (currTestIndex >= tests.length) {
michael@0 342 obssvc.removeObserver(beginObserver, NSIOBSERVER_TOPIC_BEGIN);
michael@0 343 obssvc.removeObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_SUCCESS);
michael@0 344 obssvc.removeObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_FAILED);
michael@0 345 do_test_finished();
michael@0 346 }
michael@0 347 else {
michael@0 348 var test = tests[currTestIndex];
michael@0 349 print("Running test: " + test.desc);
michael@0 350 test.run();
michael@0 351 }
michael@0 352 }
michael@0 353
michael@0 354 ///////////////////////////////////////////////////////////////////////////////
michael@0 355
michael@0 356 function run_test() {
michael@0 357 do_test_pending();
michael@0 358 obssvc.addObserver(beginObserver, NSIOBSERVER_TOPIC_BEGIN, false);
michael@0 359 obssvc.addObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_SUCCESS, false);
michael@0 360 obssvc.addObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_FAILED, false);
michael@0 361 doNextTest();
michael@0 362 }

mercurial