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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 var tests = [];
michael@0 8
michael@0 9 // Get database connection
michael@0 10 try {
michael@0 11 var mDBConn = PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase)
michael@0 12 .DBConnection;
michael@0 13 }
michael@0 14 catch(ex) {
michael@0 15 do_throw("Could not get database connection\n");
michael@0 16 }
michael@0 17
michael@0 18 /*
michael@0 19 This test is:
michael@0 20 - don't try to add invalid uri nodes to a JSON backup
michael@0 21 */
michael@0 22
michael@0 23 var invalidURITest = {
michael@0 24 _itemTitle: "invalid uri",
michael@0 25 _itemUrl: "http://test.mozilla.org/",
michael@0 26 _itemId: null,
michael@0 27
michael@0 28 populate: function () {
michael@0 29 // add a valid bookmark
michael@0 30 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId,
michael@0 31 PlacesUtils._uri(this._itemUrl),
michael@0 32 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 33 this._itemTitle);
michael@0 34 // this bookmark will go corrupt
michael@0 35 this._itemId =
michael@0 36 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId,
michael@0 37 PlacesUtils._uri(this._itemUrl),
michael@0 38 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 39 this._itemTitle);
michael@0 40 },
michael@0 41
michael@0 42 clean: function () {
michael@0 43 PlacesUtils.bookmarks.removeItem(this._itemId);
michael@0 44 },
michael@0 45
michael@0 46 validate: function (aExpectValidItemsCount) {
michael@0 47 var query = PlacesUtils.history.getNewQuery();
michael@0 48 query.setFolders([PlacesUtils.bookmarks.toolbarFolder], 1);
michael@0 49 var options = PlacesUtils.history.getNewQueryOptions();
michael@0 50 var result = PlacesUtils.history.executeQuery(query, options);
michael@0 51
michael@0 52 var toolbar = result.root;
michael@0 53 toolbar.containerOpen = true;
michael@0 54
michael@0 55 // test for our bookmark
michael@0 56 do_check_eq(toolbar.childCount, aExpectValidItemsCount);
michael@0 57 for (var i = 0; i < toolbar.childCount; i++) {
michael@0 58 var folderNode = toolbar.getChild(0);
michael@0 59 do_check_eq(folderNode.type, folderNode.RESULT_TYPE_URI);
michael@0 60 do_check_eq(folderNode.title, this._itemTitle);
michael@0 61 }
michael@0 62
michael@0 63 // clean up
michael@0 64 toolbar.containerOpen = false;
michael@0 65 }
michael@0 66 }
michael@0 67 tests.push(invalidURITest);
michael@0 68
michael@0 69 function run_test() {
michael@0 70 run_next_test();
michael@0 71 }
michael@0 72
michael@0 73 add_task(function() {
michael@0 74 // make json file
michael@0 75 let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json");
michael@0 76
michael@0 77 // populate db
michael@0 78 tests.forEach(function(aTest) {
michael@0 79 aTest.populate();
michael@0 80 // sanity
michael@0 81 aTest.validate(2);
michael@0 82 // Something in the code went wrong and we finish up losing the place, so
michael@0 83 // the bookmark uri becomes null.
michael@0 84 var sql = "UPDATE moz_bookmarks SET fk = 1337 WHERE id = ?1";
michael@0 85 var stmt = mDBConn.createStatement(sql);
michael@0 86 stmt.bindByIndex(0, aTest._itemId);
michael@0 87 try {
michael@0 88 stmt.execute();
michael@0 89 } finally {
michael@0 90 stmt.finalize();
michael@0 91 }
michael@0 92 });
michael@0 93
michael@0 94 yield BookmarkJSONUtils.exportToFile(jsonFile);
michael@0 95
michael@0 96 // clean
michael@0 97 tests.forEach(function(aTest) {
michael@0 98 aTest.clean();
michael@0 99 });
michael@0 100
michael@0 101 // restore json file
michael@0 102 try {
michael@0 103 yield BookmarkJSONUtils.importFromFile(jsonFile, true);
michael@0 104 } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
michael@0 105
michael@0 106 // validate
michael@0 107 tests.forEach(function(aTest) {
michael@0 108 aTest.validate(1);
michael@0 109 });
michael@0 110
michael@0 111 // clean up
michael@0 112 yield OS.File.remove(jsonFile);
michael@0 113 });

mercurial