Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | const EXCLUDE_FROM_BACKUP_ANNO = "places/excludeFromBackup"; |
michael@0 | 8 | // Menu, Toolbar, Unsorted, Tags |
michael@0 | 9 | const PLACES_ROOTS_COUNT = 4; |
michael@0 | 10 | var tests = []; |
michael@0 | 11 | |
michael@0 | 12 | /* |
michael@0 | 13 | |
michael@0 | 14 | Backup/restore tests example: |
michael@0 | 15 | |
michael@0 | 16 | var myTest = { |
michael@0 | 17 | populate: function () { ... add bookmarks ... }, |
michael@0 | 18 | validate: function () { ... query for your bookmarks ... } |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | this.push(myTest); |
michael@0 | 22 | |
michael@0 | 23 | */ |
michael@0 | 24 | |
michael@0 | 25 | var test = { |
michael@0 | 26 | populate: function populate() { |
michael@0 | 27 | // check initial size |
michael@0 | 28 | var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, |
michael@0 | 29 | false, false).root; |
michael@0 | 30 | do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT ); |
michael@0 | 31 | rootNode.containerOpen = false; |
michael@0 | 32 | |
michael@0 | 33 | var idx = PlacesUtils.bookmarks.DEFAULT_INDEX; |
michael@0 | 34 | |
michael@0 | 35 | // create a root to be restore |
michael@0 | 36 | this._restoreRootTitle = "restore root"; |
michael@0 | 37 | var restoreRootId = PlacesUtils.bookmarks |
michael@0 | 38 | .createFolder(PlacesUtils.placesRootId, |
michael@0 | 39 | this._restoreRootTitle, idx); |
michael@0 | 40 | // add a test bookmark |
michael@0 | 41 | this._restoreRootURI = uri("http://restore.uri"); |
michael@0 | 42 | PlacesUtils.bookmarks.insertBookmark(restoreRootId, this._restoreRootURI, |
michael@0 | 43 | idx, "restore uri"); |
michael@0 | 44 | // add a test bookmark to be exclude |
michael@0 | 45 | this._restoreRootExcludeURI = uri("http://exclude.uri"); |
michael@0 | 46 | var exItemId = PlacesUtils.bookmarks |
michael@0 | 47 | .insertBookmark(restoreRootId, |
michael@0 | 48 | this._restoreRootExcludeURI, |
michael@0 | 49 | idx, "exclude uri"); |
michael@0 | 50 | // Annotate the bookmark for exclusion. |
michael@0 | 51 | PlacesUtils.annotations.setItemAnnotation(exItemId, |
michael@0 | 52 | EXCLUDE_FROM_BACKUP_ANNO, 1, 0, |
michael@0 | 53 | PlacesUtils.annotations.EXPIRE_NEVER); |
michael@0 | 54 | |
michael@0 | 55 | // create a root to be exclude |
michael@0 | 56 | this._excludeRootTitle = "exclude root"; |
michael@0 | 57 | this._excludeRootId = PlacesUtils.bookmarks |
michael@0 | 58 | .createFolder(PlacesUtils.placesRootId, |
michael@0 | 59 | this._excludeRootTitle, idx); |
michael@0 | 60 | // Annotate the root for exclusion. |
michael@0 | 61 | PlacesUtils.annotations.setItemAnnotation(this._excludeRootId, |
michael@0 | 62 | EXCLUDE_FROM_BACKUP_ANNO, 1, 0, |
michael@0 | 63 | PlacesUtils.annotations.EXPIRE_NEVER); |
michael@0 | 64 | // add a test bookmark exclude by exclusion of its parent |
michael@0 | 65 | PlacesUtils.bookmarks.insertBookmark(this._excludeRootId, |
michael@0 | 66 | this._restoreRootExcludeURI, |
michael@0 | 67 | idx, "exclude uri"); |
michael@0 | 68 | }, |
michael@0 | 69 | |
michael@0 | 70 | validate: function validate(aEmptyBookmarks) { |
michael@0 | 71 | var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, |
michael@0 | 72 | false, false).root; |
michael@0 | 73 | |
michael@0 | 74 | if (!aEmptyBookmarks) { |
michael@0 | 75 | // since restore does not remove backup exclude items both |
michael@0 | 76 | // roots should still exist. |
michael@0 | 77 | do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT + 2); |
michael@0 | 78 | // open exclude root and check it still contains one item |
michael@0 | 79 | var restoreRootIndex = PLACES_ROOTS_COUNT; |
michael@0 | 80 | var excludeRootIndex = PLACES_ROOTS_COUNT+1; |
michael@0 | 81 | var excludeRootNode = rootNode.getChild(excludeRootIndex); |
michael@0 | 82 | do_check_eq(this._excludeRootTitle, excludeRootNode.title); |
michael@0 | 83 | excludeRootNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); |
michael@0 | 84 | excludeRootNode.containerOpen = true; |
michael@0 | 85 | do_check_eq(excludeRootNode.childCount, 1); |
michael@0 | 86 | var excludeRootChildNode = excludeRootNode.getChild(0); |
michael@0 | 87 | do_check_eq(excludeRootChildNode.uri, this._restoreRootExcludeURI.spec); |
michael@0 | 88 | excludeRootNode.containerOpen = false; |
michael@0 | 89 | } |
michael@0 | 90 | else { |
michael@0 | 91 | // exclude root should not exist anymore |
michael@0 | 92 | do_check_eq(rootNode.childCount, PLACES_ROOTS_COUNT + 1); |
michael@0 | 93 | var restoreRootIndex = PLACES_ROOTS_COUNT; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | var restoreRootNode = rootNode.getChild(restoreRootIndex); |
michael@0 | 97 | do_check_eq(this._restoreRootTitle, restoreRootNode.title); |
michael@0 | 98 | restoreRootNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); |
michael@0 | 99 | restoreRootNode.containerOpen = true; |
michael@0 | 100 | do_check_eq(restoreRootNode.childCount, 1); |
michael@0 | 101 | var restoreRootChildNode = restoreRootNode.getChild(0); |
michael@0 | 102 | do_check_eq(restoreRootChildNode.uri, this._restoreRootURI.spec); |
michael@0 | 103 | restoreRootNode.containerOpen = false; |
michael@0 | 104 | |
michael@0 | 105 | rootNode.containerOpen = false; |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | function run_test() { |
michael@0 | 110 | run_next_test(); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | add_task(function() { |
michael@0 | 114 | // make json file |
michael@0 | 115 | let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json"); |
michael@0 | 116 | |
michael@0 | 117 | // populate db |
michael@0 | 118 | test.populate(); |
michael@0 | 119 | |
michael@0 | 120 | yield BookmarkJSONUtils.exportToFile(jsonFile); |
michael@0 | 121 | |
michael@0 | 122 | // restore json file |
michael@0 | 123 | yield BookmarkJSONUtils.importFromFile(jsonFile, true); |
michael@0 | 124 | |
michael@0 | 125 | // validate without removing all bookmarks |
michael@0 | 126 | // restore do not remove backup exclude entries |
michael@0 | 127 | test.validate(false); |
michael@0 | 128 | |
michael@0 | 129 | // cleanup |
michael@0 | 130 | remove_all_bookmarks(); |
michael@0 | 131 | // manually remove the excluded root |
michael@0 | 132 | PlacesUtils.bookmarks.removeItem(test._excludeRootId); |
michael@0 | 133 | // restore json file |
michael@0 | 134 | yield BookmarkJSONUtils.importFromFile(jsonFile, true); |
michael@0 | 135 | |
michael@0 | 136 | // validate after a complete bookmarks cleanup |
michael@0 | 137 | test.validate(true); |
michael@0 | 138 | |
michael@0 | 139 | // clean up |
michael@0 | 140 | yield OS.File.remove(jsonFile); |
michael@0 | 141 | }); |