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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_bookmarks_restore_notification.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,362 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +Cu.import("resource://gre/modules/BookmarkHTMLUtils.jsm");
    1.11 +
    1.12 +/**
    1.13 + * Tests the bookmarks-restore-* nsIObserver notifications after restoring
    1.14 + * bookmarks from JSON and HTML.  See bug 470314.
    1.15 + */
    1.16 +
    1.17 +// The topics and data passed to nsIObserver.observe() on bookmarks restore
    1.18 +const NSIOBSERVER_TOPIC_BEGIN    = "bookmarks-restore-begin";
    1.19 +const NSIOBSERVER_TOPIC_SUCCESS  = "bookmarks-restore-success";
    1.20 +const NSIOBSERVER_TOPIC_FAILED   = "bookmarks-restore-failed";
    1.21 +const NSIOBSERVER_DATA_JSON      = "json";
    1.22 +const NSIOBSERVER_DATA_HTML      = "html";
    1.23 +const NSIOBSERVER_DATA_HTML_INIT = "html-initial";
    1.24 +
    1.25 +// Bookmarks are added for these URIs
    1.26 +var uris = [
    1.27 +  "http://example.com/1",
    1.28 +  "http://example.com/2",
    1.29 +  "http://example.com/3",
    1.30 +  "http://example.com/4",
    1.31 +  "http://example.com/5",
    1.32 +];
    1.33 +
    1.34 +// Add tests here.  Each is an object with these properties:
    1.35 +//   desc:       description printed before test is run
    1.36 +//   currTopic:  the next expected topic that should be observed for the test;
    1.37 +//               set to NSIOBSERVER_TOPIC_BEGIN to begin
    1.38 +//   finalTopic: the last expected topic that should be observed for the test,
    1.39 +//               which then causes the next test to be run
    1.40 +//   data:       the data passed to nsIObserver.observe() corresponding to the
    1.41 +//               test
    1.42 +//   file:       the nsILocalFile that the test creates
    1.43 +//   folderId:   for HTML restore into a folder, the folder ID to restore into;
    1.44 +//               otherwise, set it to null
    1.45 +//   run:        a method that actually runs the test
    1.46 +var tests = [
    1.47 +  {
    1.48 +    desc:       "JSON restore: normal restore should succeed",
    1.49 +    currTopic:  NSIOBSERVER_TOPIC_BEGIN,
    1.50 +    finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
    1.51 +    data:       NSIOBSERVER_DATA_JSON,
    1.52 +    folderId:   null,
    1.53 +    run:        function () {
    1.54 +      Task.spawn(function () {
    1.55 +        this.file = yield promiseFile("bookmarks-test_restoreNotification.json");
    1.56 +        addBookmarks();
    1.57 +
    1.58 +        yield BookmarkJSONUtils.exportToFile(this.file);
    1.59 +        remove_all_bookmarks();
    1.60 +        try {
    1.61 +          yield BookmarkJSONUtils.importFromFile(this.file, true);
    1.62 +        }
    1.63 +        catch (e) {
    1.64 +          do_throw("  Restore should not have failed");
    1.65 +        }
    1.66 +      }.bind(this));
    1.67 +    }
    1.68 +  },
    1.69 +
    1.70 +  {
    1.71 +    desc:       "JSON restore: empty file should succeed",
    1.72 +    currTopic:  NSIOBSERVER_TOPIC_BEGIN,
    1.73 +    finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
    1.74 +    data:       NSIOBSERVER_DATA_JSON,
    1.75 +    folderId:   null,
    1.76 +    run:        function () {
    1.77 +      Task.spawn(function() {
    1.78 +        this.file = yield promiseFile("bookmarks-test_restoreNotification.json");
    1.79 +        try {
    1.80 +          yield BookmarkJSONUtils.importFromFile(this.file, true);
    1.81 +        }
    1.82 +        catch (e) {
    1.83 +          do_throw("  Restore should not have failed" + e);
    1.84 +        }
    1.85 +      }.bind(this));
    1.86 +    }
    1.87 +  },
    1.88 +
    1.89 +  {
    1.90 +    desc:       "JSON restore: nonexistent file should fail",
    1.91 +    currTopic:  NSIOBSERVER_TOPIC_BEGIN,
    1.92 +    finalTopic: NSIOBSERVER_TOPIC_FAILED,
    1.93 +    data:       NSIOBSERVER_DATA_JSON,
    1.94 +    folderId:   null,
    1.95 +    run:        function () {
    1.96 +      this.file = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
    1.97 +      this.file.append("this file doesn't exist because nobody created it");
    1.98 +      Task.spawn(function() {
    1.99 +        try {
   1.100 +          yield BookmarkJSONUtils.importFromFile(this.file, true);
   1.101 +          do_throw("  Restore should have failed");
   1.102 +        }
   1.103 +        catch (e) {
   1.104 +        }
   1.105 +      }.bind(this));
   1.106 +    }
   1.107 +  },
   1.108 +
   1.109 +  {
   1.110 +    desc:       "HTML restore: normal restore should succeed",
   1.111 +    currTopic:  NSIOBSERVER_TOPIC_BEGIN,
   1.112 +    finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
   1.113 +    data:       NSIOBSERVER_DATA_HTML,
   1.114 +    folderId:   null,
   1.115 +    run:        function () {
   1.116 +      Task.spawn(function() {
   1.117 +        this.file = yield promiseFile("bookmarks-test_restoreNotification.html");
   1.118 +        addBookmarks();
   1.119 +        yield BookmarkHTMLUtils.exportToFile(this.file);
   1.120 +        remove_all_bookmarks();
   1.121 +        try {
   1.122 +          BookmarkHTMLUtils.importFromFile(this.file, false)
   1.123 +                           .then(null, do_report_unexpected_exception);
   1.124 +        }
   1.125 +        catch (e) {
   1.126 +          do_throw("  Restore should not have failed");
   1.127 +        }
   1.128 +      }.bind(this));
   1.129 +    }
   1.130 +  },
   1.131 +
   1.132 +  {
   1.133 +    desc:       "HTML restore: empty file should succeed",
   1.134 +    currTopic:  NSIOBSERVER_TOPIC_BEGIN,
   1.135 +    finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
   1.136 +    data:       NSIOBSERVER_DATA_HTML,
   1.137 +    folderId:   null,
   1.138 +    run:        function () {
   1.139 +      Task.spawn(function (){
   1.140 +        this.file = yield promiseFile("bookmarks-test_restoreNotification.init.html");
   1.141 +        try {
   1.142 +          BookmarkHTMLUtils.importFromFile(this.file, false)
   1.143 +                           .then(null, do_report_unexpected_exception);
   1.144 +        }
   1.145 +        catch (e) {
   1.146 +          do_throw("  Restore should not have failed");
   1.147 +        }
   1.148 +      }.bind(this));
   1.149 +    }
   1.150 +  },
   1.151 +
   1.152 +  {
   1.153 +    desc:       "HTML restore: nonexistent file should fail",
   1.154 +    currTopic:  NSIOBSERVER_TOPIC_BEGIN,
   1.155 +    finalTopic: NSIOBSERVER_TOPIC_FAILED,
   1.156 +    data:       NSIOBSERVER_DATA_HTML,
   1.157 +    folderId:   null,
   1.158 +    run:        function () {
   1.159 +      this.file = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
   1.160 +      this.file.append("this file doesn't exist because nobody created it");
   1.161 +      try {
   1.162 +        BookmarkHTMLUtils.importFromFile(this.file, false)
   1.163 +                         .then(function onSuccess() do_throw("Should fail!"),
   1.164 +                               null);
   1.165 +      }
   1.166 +      catch (e) {}
   1.167 +    }
   1.168 +  },
   1.169 +
   1.170 +  {
   1.171 +    desc:       "HTML initial restore: normal restore should succeed",
   1.172 +    currTopic:  NSIOBSERVER_TOPIC_BEGIN,
   1.173 +    finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
   1.174 +    data:       NSIOBSERVER_DATA_HTML_INIT,
   1.175 +    folderId:   null,
   1.176 +    run:        function () {
   1.177 +      Task.spawn(function () {
   1.178 +        this.file = yield promiseFile("bookmarks-test_restoreNotification.init.html");
   1.179 +        addBookmarks();
   1.180 +        yield BookmarkHTMLUtils.exportToFile(this.file);
   1.181 +        remove_all_bookmarks();
   1.182 +        try {
   1.183 +          BookmarkHTMLUtils.importFromFile(this.file, true)
   1.184 +                           .then(null, do_report_unexpected_exception);
   1.185 +        }
   1.186 +        catch (e) {
   1.187 +          do_throw("  Restore should not have failed");
   1.188 +        }
   1.189 +      }.bind(this));
   1.190 +    }
   1.191 +  },
   1.192 +
   1.193 +  {
   1.194 +    desc:       "HTML initial restore: empty file should succeed",
   1.195 +    currTopic:  NSIOBSERVER_TOPIC_BEGIN,
   1.196 +    finalTopic: NSIOBSERVER_TOPIC_SUCCESS,
   1.197 +    data:       NSIOBSERVER_DATA_HTML_INIT,
   1.198 +    folderId:   null,
   1.199 +    run:        function () {
   1.200 +      Task.spawn(function () {
   1.201 +        this.file = yield promiseFile("bookmarks-test_restoreNotification.init.html");
   1.202 +        try {
   1.203 +          BookmarkHTMLUtils.importFromFile(this.file, true)
   1.204 +                           .then(null, do_report_unexpected_exception);
   1.205 +        }
   1.206 +        catch (e) {
   1.207 +          do_throw("  Restore should not have failed");
   1.208 +        }
   1.209 +      }.bind(this));
   1.210 +    }
   1.211 +  },
   1.212 +
   1.213 +  {
   1.214 +    desc:       "HTML initial restore: nonexistent file should fail",
   1.215 +    currTopic:  NSIOBSERVER_TOPIC_BEGIN,
   1.216 +    finalTopic: NSIOBSERVER_TOPIC_FAILED,
   1.217 +    data:       NSIOBSERVER_DATA_HTML_INIT,
   1.218 +    folderId:   null,
   1.219 +    run:        function () {
   1.220 +      this.file = Services.dirsvc.get("ProfD", Ci.nsILocalFile);
   1.221 +      this.file.append("this file doesn't exist because nobody created it");
   1.222 +      try {
   1.223 +        BookmarkHTMLUtils.importFromFile(this.file, true)
   1.224 +                         .then(function onSuccess() do_throw("Should fail!"),
   1.225 +                               null);
   1.226 +      }
   1.227 +      catch (e) {}
   1.228 +    }
   1.229 +  }
   1.230 +];
   1.231 +
   1.232 +// nsIObserver that observes bookmarks-restore-begin.
   1.233 +var beginObserver = {
   1.234 +  observe: function _beginObserver(aSubject, aTopic, aData) {
   1.235 +    var test = tests[currTestIndex];
   1.236 +
   1.237 +    print("  Observed " + aTopic);
   1.238 +    print("  Topic for current test should be what is expected");
   1.239 +    do_check_eq(aTopic, test.currTopic);
   1.240 +
   1.241 +    print("  Data for current test should be what is expected");
   1.242 +    do_check_eq(aData, test.data);
   1.243 +
   1.244 +    // Update current expected topic to the next expected one.
   1.245 +    test.currTopic = test.finalTopic;
   1.246 +  }
   1.247 +};
   1.248 +
   1.249 +// nsIObserver that observes bookmarks-restore-success/failed.  This starts
   1.250 +// the next test.
   1.251 +var successAndFailedObserver = {
   1.252 +  observe: function _successAndFailedObserver(aSubject, aTopic, aData) {
   1.253 +    var test = tests[currTestIndex];
   1.254 +
   1.255 +    print("  Observed " + aTopic);
   1.256 +    print("  Topic for current test should be what is expected");
   1.257 +    do_check_eq(aTopic, test.currTopic);
   1.258 +
   1.259 +    print("  Data for current test should be what is expected");
   1.260 +    do_check_eq(aData, test.data);
   1.261 +
   1.262 +    // On restore failed, file may not exist, so wrap in try-catch.
   1.263 +    try {
   1.264 +      test.file.remove(false);
   1.265 +    }
   1.266 +    catch (exc) {}
   1.267 +
   1.268 +    // Make sure folder ID is what is expected.  For importing HTML into a
   1.269 +    // folder, this will be an integer, otherwise null.
   1.270 +    if (aSubject) {
   1.271 +      do_check_eq(aSubject.QueryInterface(Ci.nsISupportsPRInt64).data,
   1.272 +                  test.folderId);
   1.273 +    }
   1.274 +    else
   1.275 +      do_check_eq(test.folderId, null);
   1.276 +
   1.277 +    remove_all_bookmarks();
   1.278 +    do_execute_soon(doNextTest);
   1.279 +  }
   1.280 +};
   1.281 +
   1.282 +// Index of the currently running test.  See doNextTest().
   1.283 +var currTestIndex = -1;
   1.284 +
   1.285 +var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
   1.286 +            getService(Ci.nsINavBookmarksService);
   1.287 +
   1.288 +var obssvc = Cc["@mozilla.org/observer-service;1"].
   1.289 +             getService(Ci.nsIObserverService);
   1.290 +
   1.291 +///////////////////////////////////////////////////////////////////////////////
   1.292 +
   1.293 +/**
   1.294 + * Adds some bookmarks for the URIs in |uris|.
   1.295 + */
   1.296 +function addBookmarks() {
   1.297 +  uris.forEach(function (u) bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder,
   1.298 +                                                 uri(u),
   1.299 +                                                 bmsvc.DEFAULT_INDEX,
   1.300 +                                                 u));
   1.301 +  checkBookmarksExist();
   1.302 +}
   1.303 +
   1.304 +/**
   1.305 + * Checks that all of the bookmarks created for |uris| exist.  It works by
   1.306 + * creating one query per URI and then ORing all the queries.  The number of
   1.307 + * results returned should be uris.length.
   1.308 + */
   1.309 +function checkBookmarksExist() {
   1.310 +  var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
   1.311 +           getService(Ci.nsINavHistoryService);
   1.312 +  var queries = uris.map(function (u) {
   1.313 +    var q = hs.getNewQuery();
   1.314 +    q.uri = uri(u);
   1.315 +    return q;
   1.316 +  });
   1.317 +  var options = hs.getNewQueryOptions();
   1.318 +  options.queryType = options.QUERY_TYPE_BOOKMARKS;
   1.319 +  var root = hs.executeQueries(queries, uris.length, options).root;
   1.320 +  root.containerOpen = true;
   1.321 +  do_check_eq(root.childCount, uris.length);
   1.322 +  root.containerOpen = false;
   1.323 +}
   1.324 +
   1.325 +/**
   1.326 + * Creates an file in the profile directory.
   1.327 + *
   1.328 + * @param  aBasename
   1.329 + *         e.g., "foo.txt" in the path /some/long/path/foo.txt
   1.330 + * @return {Promise}
   1.331 + * @resolves to an OS.File path
   1.332 + */
   1.333 +function promiseFile(aBasename) {
   1.334 +  let path = OS.Path.join(OS.Constants.Path.profileDir, aBasename);
   1.335 +  dump("\n\nopening " + path + "\n\n");
   1.336 +  return OS.File.open(path, { truncate: true }).then(aFile => { aFile.close(); return path; });
   1.337 +}
   1.338 +
   1.339 +/**
   1.340 + * Runs the next test or if all tests have been run, finishes.
   1.341 + */
   1.342 +function doNextTest() {
   1.343 +  currTestIndex++;
   1.344 +  if (currTestIndex >= tests.length) {
   1.345 +    obssvc.removeObserver(beginObserver, NSIOBSERVER_TOPIC_BEGIN);
   1.346 +    obssvc.removeObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_SUCCESS);
   1.347 +    obssvc.removeObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_FAILED);
   1.348 +    do_test_finished();
   1.349 +  }
   1.350 +  else {
   1.351 +    var test = tests[currTestIndex];
   1.352 +    print("Running test: " + test.desc);
   1.353 +    test.run();
   1.354 +  }
   1.355 +}
   1.356 +
   1.357 +///////////////////////////////////////////////////////////////////////////////
   1.358 +
   1.359 +function run_test() {
   1.360 +  do_test_pending();
   1.361 +  obssvc.addObserver(beginObserver, NSIOBSERVER_TOPIC_BEGIN, false);
   1.362 +  obssvc.addObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_SUCCESS, false);
   1.363 +  obssvc.addObserver(successAndFailedObserver, NSIOBSERVER_TOPIC_FAILED, false);
   1.364 +  doNextTest();
   1.365 +}

mercurial