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: const EXCLUDE_FROM_BACKUP_ANNO = "places/excludeFromBackup"; michael@0: // Menu, Toolbar, Unsorted, Tags michael@0: const PLACES_ROOTS_COUNT = 4; 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: var test = { 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, PLACES_ROOTS_COUNT ); michael@0: rootNode.containerOpen = false; michael@0: michael@0: var idx = PlacesUtils.bookmarks.DEFAULT_INDEX; michael@0: michael@0: // create a root to be restore michael@0: this._restoreRootTitle = "restore root"; michael@0: var restoreRootId = PlacesUtils.bookmarks michael@0: .createFolder(PlacesUtils.placesRootId, michael@0: this._restoreRootTitle, idx); michael@0: // add a test bookmark michael@0: this._restoreRootURI = uri("http://restore.uri"); michael@0: PlacesUtils.bookmarks.insertBookmark(restoreRootId, this._restoreRootURI, michael@0: idx, "restore uri"); michael@0: // add a test bookmark to be exclude michael@0: this._restoreRootExcludeURI = uri("http://exclude.uri"); michael@0: var exItemId = PlacesUtils.bookmarks michael@0: .insertBookmark(restoreRootId, michael@0: this._restoreRootExcludeURI, michael@0: idx, "exclude uri"); michael@0: // Annotate the bookmark for exclusion. michael@0: PlacesUtils.annotations.setItemAnnotation(exItemId, michael@0: EXCLUDE_FROM_BACKUP_ANNO, 1, 0, michael@0: PlacesUtils.annotations.EXPIRE_NEVER); michael@0: michael@0: // create a root to be exclude michael@0: this._excludeRootTitle = "exclude root"; michael@0: this._excludeRootId = PlacesUtils.bookmarks michael@0: .createFolder(PlacesUtils.placesRootId, michael@0: this._excludeRootTitle, idx); michael@0: // Annotate the root for exclusion. michael@0: PlacesUtils.annotations.setItemAnnotation(this._excludeRootId, michael@0: EXCLUDE_FROM_BACKUP_ANNO, 1, 0, michael@0: PlacesUtils.annotations.EXPIRE_NEVER); michael@0: // add a test bookmark exclude by exclusion of its parent michael@0: PlacesUtils.bookmarks.insertBookmark(this._excludeRootId, michael@0: this._restoreRootExcludeURI, michael@0: idx, "exclude uri"); michael@0: }, michael@0: michael@0: validate: function validate(aEmptyBookmarks) { michael@0: var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, michael@0: false, false).root; michael@0: michael@0: if (!aEmptyBookmarks) { michael@0: // since restore does not remove backup exclude items both michael@0: // roots should still exist. michael@0: do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT + 2); michael@0: // open exclude root and check it still contains one item michael@0: var restoreRootIndex = PLACES_ROOTS_COUNT; michael@0: var excludeRootIndex = PLACES_ROOTS_COUNT+1; michael@0: var excludeRootNode = rootNode.getChild(excludeRootIndex); michael@0: do_check_eq(this._excludeRootTitle, excludeRootNode.title); michael@0: excludeRootNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); michael@0: excludeRootNode.containerOpen = true; michael@0: do_check_eq(excludeRootNode.childCount, 1); michael@0: var excludeRootChildNode = excludeRootNode.getChild(0); michael@0: do_check_eq(excludeRootChildNode.uri, this._restoreRootExcludeURI.spec); michael@0: excludeRootNode.containerOpen = false; michael@0: } michael@0: else { michael@0: // exclude root should not exist anymore michael@0: do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT + 1); michael@0: var restoreRootIndex = PLACES_ROOTS_COUNT; michael@0: } michael@0: michael@0: var restoreRootNode = rootNode.getChild(restoreRootIndex); michael@0: do_check_eq(this._restoreRootTitle, restoreRootNode.title); michael@0: restoreRootNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); michael@0: restoreRootNode.containerOpen = true; michael@0: do_check_eq(restoreRootNode.childCount, 1); michael@0: var restoreRootChildNode = restoreRootNode.getChild(0); michael@0: do_check_eq(restoreRootChildNode.uri, this._restoreRootURI.spec); michael@0: restoreRootNode.containerOpen = false; michael@0: 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: // populate db michael@0: test.populate(); michael@0: michael@0: yield BookmarkJSONUtils.exportToFile(jsonFile); michael@0: michael@0: // restore json file michael@0: yield BookmarkJSONUtils.importFromFile(jsonFile, true); michael@0: michael@0: // validate without removing all bookmarks michael@0: // restore do not remove backup exclude entries michael@0: test.validate(false); michael@0: michael@0: // cleanup michael@0: remove_all_bookmarks(); michael@0: // manually remove the excluded root michael@0: PlacesUtils.bookmarks.removeItem(test._excludeRootId); michael@0: // restore json file michael@0: yield BookmarkJSONUtils.importFromFile(jsonFile, true); michael@0: michael@0: // validate after a complete bookmarks cleanup michael@0: test.validate(true); michael@0: michael@0: // clean up michael@0: yield OS.File.remove(jsonFile); michael@0: });