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 block while doing backup and restore if tag containers contain michael@0: bogus items (separators, folders) michael@0: */ michael@0: michael@0: var invalidTagChildTest = { michael@0: _itemTitle: "invalid uri", michael@0: _itemUrl: "http://test.mozilla.org/", michael@0: _itemId: -1, michael@0: _tag: "testTag", michael@0: _tagItemId: -1, michael@0: michael@0: populate: function () { michael@0: // add a valid bookmark michael@0: this._itemId = PlacesUtils.bookmarks michael@0: .insertBookmark(PlacesUtils.toolbarFolderId, michael@0: PlacesUtils._uri(this._itemUrl), michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: this._itemTitle); michael@0: michael@0: // create a tag michael@0: PlacesUtils.tagging.tagURI(PlacesUtils._uri(this._itemUrl), [this._tag]); michael@0: // get tag folder id michael@0: var options = PlacesUtils.history.getNewQueryOptions(); michael@0: var query = PlacesUtils.history.getNewQuery(); michael@0: query.setFolders([PlacesUtils.bookmarks.tagsFolder], 1); michael@0: var result = PlacesUtils.history.executeQuery(query, options); michael@0: var tagRoot = result.root; michael@0: tagRoot.containerOpen = true; michael@0: do_check_eq(tagRoot.childCount, 1); michael@0: var tagNode = tagRoot.getChild(0) michael@0: .QueryInterface(Ci.nsINavHistoryContainerResultNode); michael@0: this._tagItemId = tagNode.itemId; michael@0: tagRoot.containerOpen = false; michael@0: michael@0: // add a separator and a folder inside tag folder michael@0: PlacesUtils.bookmarks.insertSeparator(this._tagItemId, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: PlacesUtils.bookmarks.createFolder(this._tagItemId, michael@0: "test folder", michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: michael@0: // add a separator and a folder inside tag root michael@0: PlacesUtils.bookmarks.insertSeparator(PlacesUtils.bookmarks.tagsFolder, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: PlacesUtils.bookmarks.createFolder(PlacesUtils.bookmarks.tagsFolder, michael@0: "test tags root folder", michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: }, michael@0: michael@0: clean: function () { michael@0: PlacesUtils.tagging.untagURI(PlacesUtils._uri(this._itemUrl), [this._tag]); michael@0: PlacesUtils.bookmarks.removeItem(this._itemId); michael@0: }, michael@0: michael@0: validate: function () { 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, 1); 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: toolbar.containerOpen = false; michael@0: michael@0: // test for our tag michael@0: var tags = PlacesUtils.tagging.getTagsForURI(PlacesUtils._uri(this._itemUrl)); michael@0: do_check_eq(tags.length, 1); michael@0: do_check_eq(tags[0], this._tag); michael@0: } michael@0: } michael@0: tests.push(invalidTagChildTest); michael@0: michael@0: function run_test() { michael@0: run_next_test() michael@0: } michael@0: michael@0: add_task(function () { 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(); 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: yield BookmarkJSONUtils.importFromFile(jsonFile, true); michael@0: michael@0: // validate michael@0: tests.forEach(function(aTest) { michael@0: aTest.validate(); michael@0: }); michael@0: michael@0: // clean up michael@0: yield OS.File.remove(jsonFile); michael@0: });