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: /* michael@0: michael@0: Backup/restore tests example: michael@0: michael@0: var myTest = { michael@0: populate: function () { ... add bookmarks ... }, michael@0: validate: function () { ... query for your bookmarks ... } michael@0: } michael@0: michael@0: this.push(myTest); michael@0: michael@0: */ michael@0: michael@0: tests.push({ michael@0: excludeItemsFromRestore: [], michael@0: populate: function populate() { michael@0: // check initial size michael@0: var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, michael@0: false, false).root; michael@0: do_check_eq(rootNode.childCount, 4); michael@0: michael@0: // create a test root michael@0: this._folderTitle = "test folder"; michael@0: this._folderId = michael@0: PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId, michael@0: this._folderTitle, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: do_check_eq(rootNode.childCount, 5); michael@0: michael@0: // add a tag michael@0: this._testURI = PlacesUtils._uri("http://test"); michael@0: this._tags = ["a", "b"]; michael@0: PlacesUtils.tagging.tagURI(this._testURI, this._tags); michael@0: michael@0: // add a child to each root, including our test root michael@0: this._roots = [PlacesUtils.bookmarksMenuFolderId, PlacesUtils.toolbarFolderId, michael@0: PlacesUtils.unfiledBookmarksFolderId, this._folderId]; michael@0: this._roots.forEach(function(aRootId) { michael@0: // clean slate michael@0: PlacesUtils.bookmarks.removeFolderChildren(aRootId); michael@0: // add a test bookmark michael@0: PlacesUtils.bookmarks.insertBookmark(aRootId, this._testURI, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, "test"); michael@0: }, this); michael@0: michael@0: // add a folder to exclude from replacing during restore michael@0: // this will still be present post-restore michael@0: var excludedFolderId = michael@0: PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId, michael@0: "excluded", michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: do_check_eq(rootNode.childCount, 6); michael@0: this.excludeItemsFromRestore.push(excludedFolderId); michael@0: michael@0: // add a test bookmark to it michael@0: PlacesUtils.bookmarks.insertBookmark(excludedFolderId, this._testURI, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, "test"); michael@0: }, michael@0: michael@0: inbetween: function inbetween() { michael@0: // add some items that should be removed by the restore michael@0: michael@0: // add a folder michael@0: this._litterTitle = "otter"; michael@0: PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId, michael@0: this._litterTitle, 0); michael@0: michael@0: // add some tags michael@0: PlacesUtils.tagging.tagURI(this._testURI, ["c", "d"]); michael@0: }, michael@0: michael@0: validate: function validate() { michael@0: // validate tags restored michael@0: var tags = PlacesUtils.tagging.getTagsForURI(this._testURI); michael@0: // also validates that litter tags are gone michael@0: do_check_eq(this._tags.toString(), tags.toString()); michael@0: michael@0: var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, michael@0: false, false).root; michael@0: michael@0: // validate litter is gone michael@0: do_check_neq(rootNode.getChild(0).title, this._litterTitle); michael@0: michael@0: // test root count is the same michael@0: do_check_eq(rootNode.childCount, 6); michael@0: michael@0: var foundTestFolder = 0; michael@0: for (var i = 0; i < rootNode.childCount; i++) { michael@0: var node = rootNode.getChild(i); michael@0: michael@0: LOG("validating " + node.title); michael@0: if (node.itemId != PlacesUtils.tagsFolderId) { michael@0: if (node.title == this._folderTitle) { michael@0: // check the test folder's properties michael@0: do_check_eq(node.type, node.RESULT_TYPE_FOLDER); michael@0: do_check_eq(node.title, this._folderTitle); michael@0: foundTestFolder++; michael@0: } michael@0: michael@0: // test contents michael@0: node.QueryInterface(Ci.nsINavHistoryContainerResultNode).containerOpen = true; michael@0: do_check_eq(node.childCount, 1); michael@0: var child = node.getChild(0); michael@0: do_check_true(PlacesUtils._uri(child.uri).equals(this._testURI)); michael@0: michael@0: // clean up michael@0: node.containerOpen = false; michael@0: } michael@0: } michael@0: do_check_eq(foundTestFolder, 1); michael@0: rootNode.containerOpen = false; michael@0: } michael@0: }); 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: // array of ids not to delete when restoring michael@0: var excludedItemsFromRestore = []; 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: if (aTest.excludedItemsFromRestore) michael@0: excludedItemsFromRestore = excludedItems.concat(aTest.excludedItemsFromRestore); michael@0: }); michael@0: michael@0: yield BookmarkJSONUtils.exportToFile(jsonFile); michael@0: michael@0: tests.forEach(function(aTest) { michael@0: aTest.inbetween(); 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: });