1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/bookmarks/test_405938_restore_queries.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,221 @@ 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 +var tests = []; 1.11 + 1.12 +/* 1.13 + 1.14 +Backup/restore tests example: 1.15 + 1.16 +var myTest = { 1.17 + populate: function () { ... add bookmarks ... }, 1.18 + validate: function () { ... query for your bookmarks ... } 1.19 +} 1.20 + 1.21 +this.push(myTest); 1.22 + 1.23 +*/ 1.24 + 1.25 +/* 1.26 + 1.27 +test summary: 1.28 +- create folders with content 1.29 +- create a query bookmark for those folders 1.30 +- backs up bookmarks 1.31 +- restores bookmarks 1.32 +- confirms that the query has the new ids for the same folders 1.33 + 1.34 +scenarios: 1.35 +- 1 folder (folder shortcut) 1.36 +- n folders (single query) 1.37 +- n folders (multiple queries) 1.38 + 1.39 +*/ 1.40 + 1.41 +const DEFAULT_INDEX = PlacesUtils.bookmarks.DEFAULT_INDEX; 1.42 + 1.43 +var test = { 1.44 + _testRootId: null, 1.45 + _testRootTitle: "test root", 1.46 + _folderIds: [], 1.47 + _bookmarkURIs: [], 1.48 + _count: 3, 1.49 + 1.50 + populate: function populate() { 1.51 + // folder to hold this test 1.52 + PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.toolbarFolderId); 1.53 + this._testRootId = 1.54 + PlacesUtils.bookmarks.createFolder(PlacesUtils.toolbarFolderId, 1.55 + this._testRootTitle, DEFAULT_INDEX); 1.56 + 1.57 + // create test folders each with a bookmark 1.58 + for (var i = 0; i < this._count; i++) { 1.59 + var folderId = 1.60 + PlacesUtils.bookmarks.createFolder(this._testRootId, "folder" + i, DEFAULT_INDEX); 1.61 + this._folderIds.push(folderId) 1.62 + 1.63 + var bookmarkURI = uri("http://" + i); 1.64 + PlacesUtils.bookmarks.insertBookmark(folderId, bookmarkURI, 1.65 + DEFAULT_INDEX, "bookmark" + i); 1.66 + this._bookmarkURIs.push(bookmarkURI); 1.67 + } 1.68 + 1.69 + // create a query URI with 1 folder (ie: folder shortcut) 1.70 + this._queryURI1 = uri("place:folder=" + this._folderIds[0] + "&queryType=1"); 1.71 + this._queryTitle1 = "query1"; 1.72 + PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI1, 1.73 + DEFAULT_INDEX, this._queryTitle1); 1.74 + 1.75 + // create a query URI with _count folders 1.76 + this._queryURI2 = uri("place:folder=" + this._folderIds.join("&folder=") + "&queryType=1"); 1.77 + this._queryTitle2 = "query2"; 1.78 + PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI2, 1.79 + DEFAULT_INDEX, this._queryTitle2); 1.80 + 1.81 + // create a query URI with _count queries (each with a folder) 1.82 + // first get a query object for each folder 1.83 + var queries = this._folderIds.map(function(aFolderId) { 1.84 + var query = PlacesUtils.history.getNewQuery(); 1.85 + query.setFolders([aFolderId], 1); 1.86 + return query; 1.87 + }); 1.88 + var options = PlacesUtils.history.getNewQueryOptions(); 1.89 + options.queryType = options.QUERY_TYPE_BOOKMARKS; 1.90 + this._queryURI3 = 1.91 + uri(PlacesUtils.history.queriesToQueryString(queries, queries.length, options)); 1.92 + this._queryTitle3 = "query3"; 1.93 + PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI3, 1.94 + DEFAULT_INDEX, this._queryTitle3); 1.95 + }, 1.96 + 1.97 + clean: function () {}, 1.98 + 1.99 + validate: function validate() { 1.100 + // Throw a wrench in the works by inserting some new bookmarks, 1.101 + // ensuring folder ids won't be the same, when restoring. 1.102 + for (var i = 0; i < 10; i++) { 1.103 + PlacesUtils.bookmarks. 1.104 + insertBookmark(PlacesUtils.bookmarksMenuFolderId, uri("http://aaaa"+i), DEFAULT_INDEX, ""); 1.105 + } 1.106 + 1.107 + var toolbar = 1.108 + PlacesUtils.getFolderContents(PlacesUtils.toolbarFolderId, 1.109 + false, true).root; 1.110 + do_check_true(toolbar.childCount, 1); 1.111 + 1.112 + var folderNode = toolbar.getChild(0); 1.113 + do_check_eq(folderNode.type, folderNode.RESULT_TYPE_FOLDER); 1.114 + do_check_eq(folderNode.title, this._testRootTitle); 1.115 + folderNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); 1.116 + folderNode.containerOpen = true; 1.117 + 1.118 + // |_count| folders + the query node 1.119 + do_check_eq(folderNode.childCount, this._count+3); 1.120 + 1.121 + for (var i = 0; i < this._count; i++) { 1.122 + var subFolder = folderNode.getChild(i); 1.123 + do_check_eq(subFolder.title, "folder"+i); 1.124 + subFolder.QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.125 + subFolder.containerOpen = true; 1.126 + do_check_eq(subFolder.childCount, 1); 1.127 + var child = subFolder.getChild(0); 1.128 + do_check_eq(child.title, "bookmark"+i); 1.129 + do_check_true(uri(child.uri).equals(uri("http://" + i))) 1.130 + } 1.131 + 1.132 + // validate folder shortcut 1.133 + this.validateQueryNode1(folderNode.getChild(this._count)); 1.134 + 1.135 + // validate folders query 1.136 + this.validateQueryNode2(folderNode.getChild(this._count + 1)); 1.137 + 1.138 + // validate multiple queries query 1.139 + this.validateQueryNode3(folderNode.getChild(this._count + 2)); 1.140 + 1.141 + // clean up 1.142 + folderNode.containerOpen = false; 1.143 + toolbar.containerOpen = false; 1.144 + }, 1.145 + 1.146 + validateQueryNode1: function validateQueryNode1(aNode) { 1.147 + do_check_eq(aNode.title, this._queryTitle1); 1.148 + do_check_true(PlacesUtils.nodeIsFolder(aNode)); 1.149 + 1.150 + aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.151 + aNode.containerOpen = true; 1.152 + do_check_eq(aNode.childCount, 1); 1.153 + var child = aNode.getChild(0); 1.154 + do_check_true(uri(child.uri).equals(uri("http://0"))) 1.155 + do_check_eq(child.title, "bookmark0") 1.156 + aNode.containerOpen = false; 1.157 + }, 1.158 + 1.159 + validateQueryNode2: function validateQueryNode2(aNode) { 1.160 + do_check_eq(aNode.title, this._queryTitle2); 1.161 + do_check_true(PlacesUtils.nodeIsQuery(aNode)); 1.162 + 1.163 + aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.164 + aNode.containerOpen = true; 1.165 + do_check_eq(aNode.childCount, this._count); 1.166 + for (var i = 0; i < aNode.childCount; i++) { 1.167 + var child = aNode.getChild(i); 1.168 + do_check_true(uri(child.uri).equals(uri("http://" + i))) 1.169 + do_check_eq(child.title, "bookmark" + i) 1.170 + } 1.171 + aNode.containerOpen = false; 1.172 + }, 1.173 + 1.174 + validateQueryNode3: function validateQueryNode3(aNode) { 1.175 + do_check_eq(aNode.title, this._queryTitle3); 1.176 + do_check_true(PlacesUtils.nodeIsQuery(aNode)); 1.177 + 1.178 + aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.179 + aNode.containerOpen = true; 1.180 + do_check_eq(aNode.childCount, this._count); 1.181 + for (var i = 0; i < aNode.childCount; i++) { 1.182 + var child = aNode.getChild(i); 1.183 + do_check_true(uri(child.uri).equals(uri("http://" + i))) 1.184 + do_check_eq(child.title, "bookmark" + i) 1.185 + } 1.186 + aNode.containerOpen = false; 1.187 + } 1.188 +} 1.189 +tests.push(test); 1.190 + 1.191 +function run_test() { 1.192 + run_next_test(); 1.193 +} 1.194 + 1.195 +add_task(function () { 1.196 + // make json file 1.197 + let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json"); 1.198 + 1.199 + // populate db 1.200 + tests.forEach(function(aTest) { 1.201 + aTest.populate(); 1.202 + // sanity 1.203 + aTest.validate(); 1.204 + }); 1.205 + 1.206 + // export json to file 1.207 + yield BookmarkJSONUtils.exportToFile(jsonFile); 1.208 + 1.209 + // clean 1.210 + tests.forEach(function(aTest) { 1.211 + aTest.clean(); 1.212 + }); 1.213 + 1.214 + // restore json file 1.215 + yield BookmarkJSONUtils.importFromFile(jsonFile, true); 1.216 + 1.217 + // validate 1.218 + tests.forEach(function(aTest) { 1.219 + aTest.validate(); 1.220 + }); 1.221 + 1.222 + // clean up 1.223 + yield OS.File.remove(jsonFile); 1.224 +});