michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var tests = []; michael@0: michael@0: // Get database connection michael@0: try { michael@0: var mDBConn = PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase) michael@0: .DBConnection; michael@0: } michael@0: catch(ex) { michael@0: do_throw("Could not get database connection\n"); michael@0: } michael@0: michael@0: /* michael@0: This test is: michael@0: - don't try to add invalid uri nodes to a JSON backup michael@0: */ michael@0: michael@0: var invalidURITest = { michael@0: _itemTitle: "invalid uri", michael@0: _itemUrl: "http://test.mozilla.org/", michael@0: _itemId: null, michael@0: michael@0: populate: function () { michael@0: // add a valid bookmark michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId, michael@0: PlacesUtils._uri(this._itemUrl), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: this._itemTitle); michael@0: // this bookmark will go corrupt michael@0: this._itemId = michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId, michael@0: PlacesUtils._uri(this._itemUrl), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: this._itemTitle); michael@0: }, michael@0: michael@0: clean: function () { michael@0: PlacesUtils.bookmarks.removeItem(this._itemId); michael@0: }, michael@0: michael@0: validate: function (aExpectValidItemsCount) { michael@0: var query = PlacesUtils.history.getNewQuery(); michael@0: query.setFolders([PlacesUtils.bookmarks.toolbarFolder], 1); michael@0: var options = PlacesUtils.history.getNewQueryOptions(); michael@0: var result = PlacesUtils.history.executeQuery(query, options); michael@0: michael@0: var toolbar = result.root; michael@0: toolbar.containerOpen = true; michael@0: michael@0: // test for our bookmark michael@0: do_check_eq(toolbar.childCount, aExpectValidItemsCount); michael@0: for (var i = 0; i < toolbar.childCount; i++) { michael@0: var folderNode = toolbar.getChild(0); michael@0: do_check_eq(folderNode.type, folderNode.RESULT_TYPE_URI); michael@0: do_check_eq(folderNode.title, this._itemTitle); michael@0: } michael@0: michael@0: // clean up michael@0: toolbar.containerOpen = false; michael@0: } michael@0: } michael@0: tests.push(invalidURITest); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function() { michael@0: // make json file michael@0: let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json"); michael@0: michael@0: // populate db michael@0: tests.forEach(function(aTest) { michael@0: aTest.populate(); michael@0: // sanity michael@0: aTest.validate(2); michael@0: // Something in the code went wrong and we finish up losing the place, so michael@0: // the bookmark uri becomes null. michael@0: var sql = "UPDATE moz_bookmarks SET fk = 1337 WHERE id = ?1"; michael@0: var stmt = mDBConn.createStatement(sql); michael@0: stmt.bindByIndex(0, aTest._itemId); michael@0: try { michael@0: stmt.execute(); michael@0: } finally { michael@0: stmt.finalize(); michael@0: } michael@0: }); michael@0: michael@0: yield BookmarkJSONUtils.exportToFile(jsonFile); michael@0: michael@0: // clean michael@0: tests.forEach(function(aTest) { michael@0: aTest.clean(); michael@0: }); michael@0: michael@0: // restore json file michael@0: try { michael@0: yield BookmarkJSONUtils.importFromFile(jsonFile, true); michael@0: } catch(ex) { do_throw("couldn't import the exported file: " + ex); } michael@0: michael@0: // validate michael@0: tests.forEach(function(aTest) { michael@0: aTest.validate(1); michael@0: }); michael@0: michael@0: // clean up michael@0: yield OS.File.remove(jsonFile); michael@0: });