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 | var tests = []; |
michael@0 | 8 | |
michael@0 | 9 | /* |
michael@0 | 10 | |
michael@0 | 11 | Backup/restore tests example: |
michael@0 | 12 | |
michael@0 | 13 | var myTest = { |
michael@0 | 14 | populate: function () { ... add bookmarks ... }, |
michael@0 | 15 | validate: function () { ... query for your bookmarks ... } |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | this.push(myTest); |
michael@0 | 19 | |
michael@0 | 20 | */ |
michael@0 | 21 | |
michael@0 | 22 | /* |
michael@0 | 23 | |
michael@0 | 24 | test summary: |
michael@0 | 25 | - create folders with content |
michael@0 | 26 | - create a query bookmark for those folders |
michael@0 | 27 | - backs up bookmarks |
michael@0 | 28 | - restores bookmarks |
michael@0 | 29 | - confirms that the query has the new ids for the same folders |
michael@0 | 30 | |
michael@0 | 31 | scenarios: |
michael@0 | 32 | - 1 folder (folder shortcut) |
michael@0 | 33 | - n folders (single query) |
michael@0 | 34 | - n folders (multiple queries) |
michael@0 | 35 | |
michael@0 | 36 | */ |
michael@0 | 37 | |
michael@0 | 38 | const DEFAULT_INDEX = PlacesUtils.bookmarks.DEFAULT_INDEX; |
michael@0 | 39 | |
michael@0 | 40 | var test = { |
michael@0 | 41 | _testRootId: null, |
michael@0 | 42 | _testRootTitle: "test root", |
michael@0 | 43 | _folderIds: [], |
michael@0 | 44 | _bookmarkURIs: [], |
michael@0 | 45 | _count: 3, |
michael@0 | 46 | |
michael@0 | 47 | populate: function populate() { |
michael@0 | 48 | // folder to hold this test |
michael@0 | 49 | PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.toolbarFolderId); |
michael@0 | 50 | this._testRootId = |
michael@0 | 51 | PlacesUtils.bookmarks.createFolder(PlacesUtils.toolbarFolderId, |
michael@0 | 52 | this._testRootTitle, DEFAULT_INDEX); |
michael@0 | 53 | |
michael@0 | 54 | // create test folders each with a bookmark |
michael@0 | 55 | for (var i = 0; i < this._count; i++) { |
michael@0 | 56 | var folderId = |
michael@0 | 57 | PlacesUtils.bookmarks.createFolder(this._testRootId, "folder" + i, DEFAULT_INDEX); |
michael@0 | 58 | this._folderIds.push(folderId) |
michael@0 | 59 | |
michael@0 | 60 | var bookmarkURI = uri("http://" + i); |
michael@0 | 61 | PlacesUtils.bookmarks.insertBookmark(folderId, bookmarkURI, |
michael@0 | 62 | DEFAULT_INDEX, "bookmark" + i); |
michael@0 | 63 | this._bookmarkURIs.push(bookmarkURI); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // create a query URI with 1 folder (ie: folder shortcut) |
michael@0 | 67 | this._queryURI1 = uri("place:folder=" + this._folderIds[0] + "&queryType=1"); |
michael@0 | 68 | this._queryTitle1 = "query1"; |
michael@0 | 69 | PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI1, |
michael@0 | 70 | DEFAULT_INDEX, this._queryTitle1); |
michael@0 | 71 | |
michael@0 | 72 | // create a query URI with _count folders |
michael@0 | 73 | this._queryURI2 = uri("place:folder=" + this._folderIds.join("&folder=") + "&queryType=1"); |
michael@0 | 74 | this._queryTitle2 = "query2"; |
michael@0 | 75 | PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI2, |
michael@0 | 76 | DEFAULT_INDEX, this._queryTitle2); |
michael@0 | 77 | |
michael@0 | 78 | // create a query URI with _count queries (each with a folder) |
michael@0 | 79 | // first get a query object for each folder |
michael@0 | 80 | var queries = this._folderIds.map(function(aFolderId) { |
michael@0 | 81 | var query = PlacesUtils.history.getNewQuery(); |
michael@0 | 82 | query.setFolders([aFolderId], 1); |
michael@0 | 83 | return query; |
michael@0 | 84 | }); |
michael@0 | 85 | var options = PlacesUtils.history.getNewQueryOptions(); |
michael@0 | 86 | options.queryType = options.QUERY_TYPE_BOOKMARKS; |
michael@0 | 87 | this._queryURI3 = |
michael@0 | 88 | uri(PlacesUtils.history.queriesToQueryString(queries, queries.length, options)); |
michael@0 | 89 | this._queryTitle3 = "query3"; |
michael@0 | 90 | PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI3, |
michael@0 | 91 | DEFAULT_INDEX, this._queryTitle3); |
michael@0 | 92 | }, |
michael@0 | 93 | |
michael@0 | 94 | clean: function () {}, |
michael@0 | 95 | |
michael@0 | 96 | validate: function validate() { |
michael@0 | 97 | // Throw a wrench in the works by inserting some new bookmarks, |
michael@0 | 98 | // ensuring folder ids won't be the same, when restoring. |
michael@0 | 99 | for (var i = 0; i < 10; i++) { |
michael@0 | 100 | PlacesUtils.bookmarks. |
michael@0 | 101 | insertBookmark(PlacesUtils.bookmarksMenuFolderId, uri("http://aaaa"+i), DEFAULT_INDEX, ""); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | var toolbar = |
michael@0 | 105 | PlacesUtils.getFolderContents(PlacesUtils.toolbarFolderId, |
michael@0 | 106 | false, true).root; |
michael@0 | 107 | do_check_true(toolbar.childCount, 1); |
michael@0 | 108 | |
michael@0 | 109 | var folderNode = toolbar.getChild(0); |
michael@0 | 110 | do_check_eq(folderNode.type, folderNode.RESULT_TYPE_FOLDER); |
michael@0 | 111 | do_check_eq(folderNode.title, this._testRootTitle); |
michael@0 | 112 | folderNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); |
michael@0 | 113 | folderNode.containerOpen = true; |
michael@0 | 114 | |
michael@0 | 115 | // |_count| folders + the query node |
michael@0 | 116 | do_check_eq(folderNode.childCount, this._count+3); |
michael@0 | 117 | |
michael@0 | 118 | for (var i = 0; i < this._count; i++) { |
michael@0 | 119 | var subFolder = folderNode.getChild(i); |
michael@0 | 120 | do_check_eq(subFolder.title, "folder"+i); |
michael@0 | 121 | subFolder.QueryInterface(Ci.nsINavHistoryContainerResultNode); |
michael@0 | 122 | subFolder.containerOpen = true; |
michael@0 | 123 | do_check_eq(subFolder.childCount, 1); |
michael@0 | 124 | var child = subFolder.getChild(0); |
michael@0 | 125 | do_check_eq(child.title, "bookmark"+i); |
michael@0 | 126 | do_check_true(uri(child.uri).equals(uri("http://" + i))) |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | // validate folder shortcut |
michael@0 | 130 | this.validateQueryNode1(folderNode.getChild(this._count)); |
michael@0 | 131 | |
michael@0 | 132 | // validate folders query |
michael@0 | 133 | this.validateQueryNode2(folderNode.getChild(this._count + 1)); |
michael@0 | 134 | |
michael@0 | 135 | // validate multiple queries query |
michael@0 | 136 | this.validateQueryNode3(folderNode.getChild(this._count + 2)); |
michael@0 | 137 | |
michael@0 | 138 | // clean up |
michael@0 | 139 | folderNode.containerOpen = false; |
michael@0 | 140 | toolbar.containerOpen = false; |
michael@0 | 141 | }, |
michael@0 | 142 | |
michael@0 | 143 | validateQueryNode1: function validateQueryNode1(aNode) { |
michael@0 | 144 | do_check_eq(aNode.title, this._queryTitle1); |
michael@0 | 145 | do_check_true(PlacesUtils.nodeIsFolder(aNode)); |
michael@0 | 146 | |
michael@0 | 147 | aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); |
michael@0 | 148 | aNode.containerOpen = true; |
michael@0 | 149 | do_check_eq(aNode.childCount, 1); |
michael@0 | 150 | var child = aNode.getChild(0); |
michael@0 | 151 | do_check_true(uri(child.uri).equals(uri("http://0"))) |
michael@0 | 152 | do_check_eq(child.title, "bookmark0") |
michael@0 | 153 | aNode.containerOpen = false; |
michael@0 | 154 | }, |
michael@0 | 155 | |
michael@0 | 156 | validateQueryNode2: function validateQueryNode2(aNode) { |
michael@0 | 157 | do_check_eq(aNode.title, this._queryTitle2); |
michael@0 | 158 | do_check_true(PlacesUtils.nodeIsQuery(aNode)); |
michael@0 | 159 | |
michael@0 | 160 | aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); |
michael@0 | 161 | aNode.containerOpen = true; |
michael@0 | 162 | do_check_eq(aNode.childCount, this._count); |
michael@0 | 163 | for (var i = 0; i < aNode.childCount; i++) { |
michael@0 | 164 | var child = aNode.getChild(i); |
michael@0 | 165 | do_check_true(uri(child.uri).equals(uri("http://" + i))) |
michael@0 | 166 | do_check_eq(child.title, "bookmark" + i) |
michael@0 | 167 | } |
michael@0 | 168 | aNode.containerOpen = false; |
michael@0 | 169 | }, |
michael@0 | 170 | |
michael@0 | 171 | validateQueryNode3: function validateQueryNode3(aNode) { |
michael@0 | 172 | do_check_eq(aNode.title, this._queryTitle3); |
michael@0 | 173 | do_check_true(PlacesUtils.nodeIsQuery(aNode)); |
michael@0 | 174 | |
michael@0 | 175 | aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); |
michael@0 | 176 | aNode.containerOpen = true; |
michael@0 | 177 | do_check_eq(aNode.childCount, this._count); |
michael@0 | 178 | for (var i = 0; i < aNode.childCount; i++) { |
michael@0 | 179 | var child = aNode.getChild(i); |
michael@0 | 180 | do_check_true(uri(child.uri).equals(uri("http://" + i))) |
michael@0 | 181 | do_check_eq(child.title, "bookmark" + i) |
michael@0 | 182 | } |
michael@0 | 183 | aNode.containerOpen = false; |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | tests.push(test); |
michael@0 | 187 | |
michael@0 | 188 | function run_test() { |
michael@0 | 189 | run_next_test(); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | add_task(function () { |
michael@0 | 193 | // make json file |
michael@0 | 194 | let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json"); |
michael@0 | 195 | |
michael@0 | 196 | // populate db |
michael@0 | 197 | tests.forEach(function(aTest) { |
michael@0 | 198 | aTest.populate(); |
michael@0 | 199 | // sanity |
michael@0 | 200 | aTest.validate(); |
michael@0 | 201 | }); |
michael@0 | 202 | |
michael@0 | 203 | // export json to file |
michael@0 | 204 | yield BookmarkJSONUtils.exportToFile(jsonFile); |
michael@0 | 205 | |
michael@0 | 206 | // clean |
michael@0 | 207 | tests.forEach(function(aTest) { |
michael@0 | 208 | aTest.clean(); |
michael@0 | 209 | }); |
michael@0 | 210 | |
michael@0 | 211 | // restore json file |
michael@0 | 212 | yield BookmarkJSONUtils.importFromFile(jsonFile, true); |
michael@0 | 213 | |
michael@0 | 214 | // validate |
michael@0 | 215 | tests.forEach(function(aTest) { |
michael@0 | 216 | aTest.validate(); |
michael@0 | 217 | }); |
michael@0 | 218 | |
michael@0 | 219 | // clean up |
michael@0 | 220 | yield OS.File.remove(jsonFile); |
michael@0 | 221 | }); |