toolkit/components/places/tests/bookmarks/test_448584.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/bookmarks/test_448584.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     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 +var tests = [];
    1.11 +
    1.12 +// Get database connection
    1.13 +try {
    1.14 +  var mDBConn = PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase)
    1.15 +                                   .DBConnection;
    1.16 +}
    1.17 +catch(ex) {
    1.18 +  do_throw("Could not get database connection\n");
    1.19 +}
    1.20 +
    1.21 +/*
    1.22 +  This test is:
    1.23 +    - don't try to add invalid uri nodes to a JSON backup
    1.24 +*/
    1.25 +
    1.26 +var invalidURITest = {
    1.27 +  _itemTitle: "invalid uri",
    1.28 +  _itemUrl: "http://test.mozilla.org/",
    1.29 +  _itemId: null,
    1.30 +
    1.31 +  populate: function () {
    1.32 +    // add a valid bookmark
    1.33 +    PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId,
    1.34 +                                         PlacesUtils._uri(this._itemUrl),
    1.35 +                                         PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.36 +                                         this._itemTitle);
    1.37 +    // this bookmark will go corrupt
    1.38 +    this._itemId = 
    1.39 +      PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId,
    1.40 +                                           PlacesUtils._uri(this._itemUrl),
    1.41 +                                           PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.42 +                                           this._itemTitle);
    1.43 +  },
    1.44 +
    1.45 +  clean: function () {
    1.46 +    PlacesUtils.bookmarks.removeItem(this._itemId);
    1.47 +  },
    1.48 +
    1.49 +  validate: function (aExpectValidItemsCount) {
    1.50 +    var query = PlacesUtils.history.getNewQuery();
    1.51 +    query.setFolders([PlacesUtils.bookmarks.toolbarFolder], 1);
    1.52 +    var options = PlacesUtils.history.getNewQueryOptions();
    1.53 +    var result = PlacesUtils.history.executeQuery(query, options);
    1.54 +
    1.55 +    var toolbar = result.root;
    1.56 +    toolbar.containerOpen = true;
    1.57 +
    1.58 +    // test for our bookmark
    1.59 +    do_check_eq(toolbar.childCount, aExpectValidItemsCount);
    1.60 +    for (var i = 0; i < toolbar.childCount; i++) {
    1.61 +      var folderNode = toolbar.getChild(0);
    1.62 +      do_check_eq(folderNode.type, folderNode.RESULT_TYPE_URI);
    1.63 +      do_check_eq(folderNode.title, this._itemTitle);
    1.64 +    }
    1.65 +
    1.66 +    // clean up
    1.67 +    toolbar.containerOpen = false;
    1.68 +  }
    1.69 +}
    1.70 +tests.push(invalidURITest);
    1.71 +
    1.72 +function run_test() {
    1.73 +  run_next_test();
    1.74 +}
    1.75 +
    1.76 +add_task(function() {
    1.77 +  // make json file
    1.78 +  let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json");
    1.79 +
    1.80 +  // populate db
    1.81 +  tests.forEach(function(aTest) {
    1.82 +    aTest.populate();
    1.83 +    // sanity
    1.84 +    aTest.validate(2);
    1.85 +    // Something in the code went wrong and we finish up losing the place, so
    1.86 +    // the bookmark uri becomes null.
    1.87 +    var sql = "UPDATE moz_bookmarks SET fk = 1337 WHERE id = ?1";
    1.88 +    var stmt = mDBConn.createStatement(sql);
    1.89 +    stmt.bindByIndex(0, aTest._itemId);
    1.90 +    try {
    1.91 +      stmt.execute();
    1.92 +    } finally {
    1.93 +      stmt.finalize();
    1.94 +    }
    1.95 +  });
    1.96 +
    1.97 +  yield BookmarkJSONUtils.exportToFile(jsonFile);
    1.98 +
    1.99 +  // clean
   1.100 +  tests.forEach(function(aTest) {
   1.101 +    aTest.clean();
   1.102 +  });
   1.103 +
   1.104 +  // restore json file
   1.105 +  try {
   1.106 +    yield BookmarkJSONUtils.importFromFile(jsonFile, true);
   1.107 +  } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
   1.108 +
   1.109 +  // validate
   1.110 +  tests.forEach(function(aTest) {
   1.111 +    aTest.validate(1);
   1.112 +  });
   1.113 +
   1.114 +  // clean up
   1.115 +  yield OS.File.remove(jsonFile);
   1.116 +});

mercurial