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: 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: /* michael@0: michael@0: test summary: michael@0: - create folders with content michael@0: - create a query bookmark for those folders michael@0: - backs up bookmarks michael@0: - restores bookmarks michael@0: - confirms that the query has the new ids for the same folders michael@0: michael@0: scenarios: michael@0: - 1 folder (folder shortcut) michael@0: - n folders (single query) michael@0: - n folders (multiple queries) michael@0: michael@0: */ michael@0: michael@0: const DEFAULT_INDEX = PlacesUtils.bookmarks.DEFAULT_INDEX; michael@0: michael@0: var test = { michael@0: _testRootId: null, michael@0: _testRootTitle: "test root", michael@0: _folderIds: [], michael@0: _bookmarkURIs: [], michael@0: _count: 3, michael@0: michael@0: populate: function populate() { michael@0: // folder to hold this test michael@0: PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.toolbarFolderId); michael@0: this._testRootId = michael@0: PlacesUtils.bookmarks.createFolder(PlacesUtils.toolbarFolderId, michael@0: this._testRootTitle, DEFAULT_INDEX); michael@0: michael@0: // create test folders each with a bookmark michael@0: for (var i = 0; i < this._count; i++) { michael@0: var folderId = michael@0: PlacesUtils.bookmarks.createFolder(this._testRootId, "folder" + i, DEFAULT_INDEX); michael@0: this._folderIds.push(folderId) michael@0: michael@0: var bookmarkURI = uri("http://" + i); michael@0: PlacesUtils.bookmarks.insertBookmark(folderId, bookmarkURI, michael@0: DEFAULT_INDEX, "bookmark" + i); michael@0: this._bookmarkURIs.push(bookmarkURI); michael@0: } michael@0: michael@0: // create a query URI with 1 folder (ie: folder shortcut) michael@0: this._queryURI1 = uri("place:folder=" + this._folderIds[0] + "&queryType=1"); michael@0: this._queryTitle1 = "query1"; michael@0: PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI1, michael@0: DEFAULT_INDEX, this._queryTitle1); michael@0: michael@0: // create a query URI with _count folders michael@0: this._queryURI2 = uri("place:folder=" + this._folderIds.join("&folder=") + "&queryType=1"); michael@0: this._queryTitle2 = "query2"; michael@0: PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI2, michael@0: DEFAULT_INDEX, this._queryTitle2); michael@0: michael@0: // create a query URI with _count queries (each with a folder) michael@0: // first get a query object for each folder michael@0: var queries = this._folderIds.map(function(aFolderId) { michael@0: var query = PlacesUtils.history.getNewQuery(); michael@0: query.setFolders([aFolderId], 1); michael@0: return query; michael@0: }); michael@0: var options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.queryType = options.QUERY_TYPE_BOOKMARKS; michael@0: this._queryURI3 = michael@0: uri(PlacesUtils.history.queriesToQueryString(queries, queries.length, options)); michael@0: this._queryTitle3 = "query3"; michael@0: PlacesUtils.bookmarks.insertBookmark(this._testRootId, this._queryURI3, michael@0: DEFAULT_INDEX, this._queryTitle3); michael@0: }, michael@0: michael@0: clean: function () {}, michael@0: michael@0: validate: function validate() { michael@0: // Throw a wrench in the works by inserting some new bookmarks, michael@0: // ensuring folder ids won't be the same, when restoring. michael@0: for (var i = 0; i < 10; i++) { michael@0: PlacesUtils.bookmarks. michael@0: insertBookmark(PlacesUtils.bookmarksMenuFolderId, uri("http://aaaa"+i), DEFAULT_INDEX, ""); michael@0: } michael@0: michael@0: var toolbar = michael@0: PlacesUtils.getFolderContents(PlacesUtils.toolbarFolderId, michael@0: false, true).root; michael@0: do_check_true(toolbar.childCount, 1); michael@0: michael@0: var folderNode = toolbar.getChild(0); michael@0: do_check_eq(folderNode.type, folderNode.RESULT_TYPE_FOLDER); michael@0: do_check_eq(folderNode.title, this._testRootTitle); michael@0: folderNode.QueryInterface(Ci.nsINavHistoryQueryResultNode); michael@0: folderNode.containerOpen = true; michael@0: michael@0: // |_count| folders + the query node michael@0: do_check_eq(folderNode.childCount, this._count+3); michael@0: michael@0: for (var i = 0; i < this._count; i++) { michael@0: var subFolder = folderNode.getChild(i); michael@0: do_check_eq(subFolder.title, "folder"+i); michael@0: subFolder.QueryInterface(Ci.nsINavHistoryContainerResultNode); michael@0: subFolder.containerOpen = true; michael@0: do_check_eq(subFolder.childCount, 1); michael@0: var child = subFolder.getChild(0); michael@0: do_check_eq(child.title, "bookmark"+i); michael@0: do_check_true(uri(child.uri).equals(uri("http://" + i))) michael@0: } michael@0: michael@0: // validate folder shortcut michael@0: this.validateQueryNode1(folderNode.getChild(this._count)); michael@0: michael@0: // validate folders query michael@0: this.validateQueryNode2(folderNode.getChild(this._count + 1)); michael@0: michael@0: // validate multiple queries query michael@0: this.validateQueryNode3(folderNode.getChild(this._count + 2)); michael@0: michael@0: // clean up michael@0: folderNode.containerOpen = false; michael@0: toolbar.containerOpen = false; michael@0: }, michael@0: michael@0: validateQueryNode1: function validateQueryNode1(aNode) { michael@0: do_check_eq(aNode.title, this._queryTitle1); michael@0: do_check_true(PlacesUtils.nodeIsFolder(aNode)); michael@0: michael@0: aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); michael@0: aNode.containerOpen = true; michael@0: do_check_eq(aNode.childCount, 1); michael@0: var child = aNode.getChild(0); michael@0: do_check_true(uri(child.uri).equals(uri("http://0"))) michael@0: do_check_eq(child.title, "bookmark0") michael@0: aNode.containerOpen = false; michael@0: }, michael@0: michael@0: validateQueryNode2: function validateQueryNode2(aNode) { michael@0: do_check_eq(aNode.title, this._queryTitle2); michael@0: do_check_true(PlacesUtils.nodeIsQuery(aNode)); michael@0: michael@0: aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); michael@0: aNode.containerOpen = true; michael@0: do_check_eq(aNode.childCount, this._count); michael@0: for (var i = 0; i < aNode.childCount; i++) { michael@0: var child = aNode.getChild(i); michael@0: do_check_true(uri(child.uri).equals(uri("http://" + i))) michael@0: do_check_eq(child.title, "bookmark" + i) michael@0: } michael@0: aNode.containerOpen = false; michael@0: }, michael@0: michael@0: validateQueryNode3: function validateQueryNode3(aNode) { michael@0: do_check_eq(aNode.title, this._queryTitle3); michael@0: do_check_true(PlacesUtils.nodeIsQuery(aNode)); michael@0: michael@0: aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode); michael@0: aNode.containerOpen = true; michael@0: do_check_eq(aNode.childCount, this._count); michael@0: for (var i = 0; i < aNode.childCount; i++) { michael@0: var child = aNode.getChild(i); michael@0: do_check_true(uri(child.uri).equals(uri("http://" + i))) michael@0: do_check_eq(child.title, "bookmark" + i) michael@0: } michael@0: aNode.containerOpen = false; michael@0: } michael@0: } michael@0: tests.push(test); 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: tests.forEach(function(aTest) { michael@0: aTest.populate(); michael@0: // sanity michael@0: aTest.validate(); michael@0: }); michael@0: michael@0: // export json to file michael@0: yield BookmarkJSONUtils.exportToFile(jsonFile); michael@0: michael@0: // clean michael@0: tests.forEach(function(aTest) { michael@0: aTest.clean(); michael@0: }); michael@0: michael@0: // restore json file michael@0: yield BookmarkJSONUtils.importFromFile(jsonFile, true); michael@0: michael@0: // validate michael@0: tests.forEach(function(aTest) { michael@0: aTest.validate(); michael@0: }); michael@0: michael@0: // clean up michael@0: yield OS.File.remove(jsonFile); michael@0: });