1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/bookmarks/test_417228-exclude-from-backup.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,141 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +const EXCLUDE_FROM_BACKUP_ANNO = "places/excludeFromBackup"; 1.11 +// Menu, Toolbar, Unsorted, Tags 1.12 +const PLACES_ROOTS_COUNT = 4; 1.13 +var tests = []; 1.14 + 1.15 +/* 1.16 + 1.17 +Backup/restore tests example: 1.18 + 1.19 +var myTest = { 1.20 + populate: function () { ... add bookmarks ... }, 1.21 + validate: function () { ... query for your bookmarks ... } 1.22 +} 1.23 + 1.24 +this.push(myTest); 1.25 + 1.26 +*/ 1.27 + 1.28 +var test = { 1.29 + populate: function populate() { 1.30 + // check initial size 1.31 + var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, 1.32 + false, false).root; 1.33 + do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT ); 1.34 + rootNode.containerOpen = false; 1.35 + 1.36 + var idx = PlacesUtils.bookmarks.DEFAULT_INDEX; 1.37 + 1.38 + // create a root to be restore 1.39 + this._restoreRootTitle = "restore root"; 1.40 + var restoreRootId = PlacesUtils.bookmarks 1.41 + .createFolder(PlacesUtils.placesRootId, 1.42 + this._restoreRootTitle, idx); 1.43 + // add a test bookmark 1.44 + this._restoreRootURI = uri("http://restore.uri"); 1.45 + PlacesUtils.bookmarks.insertBookmark(restoreRootId, this._restoreRootURI, 1.46 + idx, "restore uri"); 1.47 + // add a test bookmark to be exclude 1.48 + this._restoreRootExcludeURI = uri("http://exclude.uri"); 1.49 + var exItemId = PlacesUtils.bookmarks 1.50 + .insertBookmark(restoreRootId, 1.51 + this._restoreRootExcludeURI, 1.52 + idx, "exclude uri"); 1.53 + // Annotate the bookmark for exclusion. 1.54 + PlacesUtils.annotations.setItemAnnotation(exItemId, 1.55 + EXCLUDE_FROM_BACKUP_ANNO, 1, 0, 1.56 + PlacesUtils.annotations.EXPIRE_NEVER); 1.57 + 1.58 + // create a root to be exclude 1.59 + this._excludeRootTitle = "exclude root"; 1.60 + this._excludeRootId = PlacesUtils.bookmarks 1.61 + .createFolder(PlacesUtils.placesRootId, 1.62 + this._excludeRootTitle, idx); 1.63 + // Annotate the root for exclusion. 1.64 + PlacesUtils.annotations.setItemAnnotation(this._excludeRootId, 1.65 + EXCLUDE_FROM_BACKUP_ANNO, 1, 0, 1.66 + PlacesUtils.annotations.EXPIRE_NEVER); 1.67 + // add a test bookmark exclude by exclusion of its parent 1.68 + PlacesUtils.bookmarks.insertBookmark(this._excludeRootId, 1.69 + this._restoreRootExcludeURI, 1.70 + idx, "exclude uri"); 1.71 + }, 1.72 + 1.73 + validate: function validate(aEmptyBookmarks) { 1.74 + var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, 1.75 + false, false).root; 1.76 + 1.77 + if (!aEmptyBookmarks) { 1.78 + // since restore does not remove backup exclude items both 1.79 + // roots should still exist. 1.80 + do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT + 2); 1.81 + // open exclude root and check it still contains one item 1.82 + var restoreRootIndex = PLACES_ROOTS_COUNT; 1.83 + var excludeRootIndex = PLACES_ROOTS_COUNT+1; 1.84 + var excludeRootNode = rootNode.getChild(excludeRootIndex); 1.85 + do_check_eq(this._excludeRootTitle, excludeRootNode.title); 1.86 + excludeRootNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); 1.87 + excludeRootNode.containerOpen = true; 1.88 + do_check_eq(excludeRootNode.childCount, 1); 1.89 + var excludeRootChildNode = excludeRootNode.getChild(0); 1.90 + do_check_eq(excludeRootChildNode.uri, this._restoreRootExcludeURI.spec); 1.91 + excludeRootNode.containerOpen = false; 1.92 + } 1.93 + else { 1.94 + // exclude root should not exist anymore 1.95 + do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT + 1); 1.96 + var restoreRootIndex = PLACES_ROOTS_COUNT; 1.97 + } 1.98 + 1.99 + var restoreRootNode = rootNode.getChild(restoreRootIndex); 1.100 + do_check_eq(this._restoreRootTitle, restoreRootNode.title); 1.101 + restoreRootNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); 1.102 + restoreRootNode.containerOpen = true; 1.103 + do_check_eq(restoreRootNode.childCount, 1); 1.104 + var restoreRootChildNode = restoreRootNode.getChild(0); 1.105 + do_check_eq(restoreRootChildNode.uri, this._restoreRootURI.spec); 1.106 + restoreRootNode.containerOpen = false; 1.107 + 1.108 + rootNode.containerOpen = false; 1.109 + } 1.110 +} 1.111 + 1.112 +function run_test() { 1.113 + run_next_test(); 1.114 +} 1.115 + 1.116 +add_task(function() { 1.117 + // make json file 1.118 + let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json"); 1.119 + 1.120 + // populate db 1.121 + test.populate(); 1.122 + 1.123 + yield BookmarkJSONUtils.exportToFile(jsonFile); 1.124 + 1.125 + // restore json file 1.126 + yield BookmarkJSONUtils.importFromFile(jsonFile, true); 1.127 + 1.128 + // validate without removing all bookmarks 1.129 + // restore do not remove backup exclude entries 1.130 + test.validate(false); 1.131 + 1.132 + // cleanup 1.133 + remove_all_bookmarks(); 1.134 + // manually remove the excluded root 1.135 + PlacesUtils.bookmarks.removeItem(test._excludeRootId); 1.136 + // restore json file 1.137 + yield BookmarkJSONUtils.importFromFile(jsonFile, true); 1.138 + 1.139 + // validate after a complete bookmarks cleanup 1.140 + test.validate(true); 1.141 + 1.142 + // clean up 1.143 + yield OS.File.remove(jsonFile); 1.144 +});