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
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 var tests = [];
9 /*
11 Backup/restore tests example:
13 var myTest = {
14 populate: function () { ... add bookmarks ... },
15 validate: function () { ... query for your bookmarks ... }
16 }
18 this.push(myTest);
20 */
22 tests.push({
23 excludeItemsFromRestore: [],
24 populate: function populate() {
25 // check initial size
26 var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId,
27 false, false).root;
28 do_check_eq(rootNode.childCount, 4);
30 // create a test root
31 this._folderTitle = "test folder";
32 this._folderId =
33 PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId,
34 this._folderTitle,
35 PlacesUtils.bookmarks.DEFAULT_INDEX);
36 do_check_eq(rootNode.childCount, 5);
38 // add a tag
39 this._testURI = PlacesUtils._uri("http://test");
40 this._tags = ["a", "b"];
41 PlacesUtils.tagging.tagURI(this._testURI, this._tags);
43 // add a child to each root, including our test root
44 this._roots = [PlacesUtils.bookmarksMenuFolderId, PlacesUtils.toolbarFolderId,
45 PlacesUtils.unfiledBookmarksFolderId, this._folderId];
46 this._roots.forEach(function(aRootId) {
47 // clean slate
48 PlacesUtils.bookmarks.removeFolderChildren(aRootId);
49 // add a test bookmark
50 PlacesUtils.bookmarks.insertBookmark(aRootId, this._testURI,
51 PlacesUtils.bookmarks.DEFAULT_INDEX, "test");
52 }, this);
54 // add a folder to exclude from replacing during restore
55 // this will still be present post-restore
56 var excludedFolderId =
57 PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId,
58 "excluded",
59 PlacesUtils.bookmarks.DEFAULT_INDEX);
60 do_check_eq(rootNode.childCount, 6);
61 this.excludeItemsFromRestore.push(excludedFolderId);
63 // add a test bookmark to it
64 PlacesUtils.bookmarks.insertBookmark(excludedFolderId, this._testURI,
65 PlacesUtils.bookmarks.DEFAULT_INDEX, "test");
66 },
68 inbetween: function inbetween() {
69 // add some items that should be removed by the restore
71 // add a folder
72 this._litterTitle = "otter";
73 PlacesUtils.bookmarks.createFolder(PlacesUtils.placesRootId,
74 this._litterTitle, 0);
76 // add some tags
77 PlacesUtils.tagging.tagURI(this._testURI, ["c", "d"]);
78 },
80 validate: function validate() {
81 // validate tags restored
82 var tags = PlacesUtils.tagging.getTagsForURI(this._testURI);
83 // also validates that litter tags are gone
84 do_check_eq(this._tags.toString(), tags.toString());
86 var rootNode = PlacesUtils.getFolderContents(PlacesUtils.placesRootId,
87 false, false).root;
89 // validate litter is gone
90 do_check_neq(rootNode.getChild(0).title, this._litterTitle);
92 // test root count is the same
93 do_check_eq(rootNode.childCount, 6);
95 var foundTestFolder = 0;
96 for (var i = 0; i < rootNode.childCount; i++) {
97 var node = rootNode.getChild(i);
99 LOG("validating " + node.title);
100 if (node.itemId != PlacesUtils.tagsFolderId) {
101 if (node.title == this._folderTitle) {
102 // check the test folder's properties
103 do_check_eq(node.type, node.RESULT_TYPE_FOLDER);
104 do_check_eq(node.title, this._folderTitle);
105 foundTestFolder++;
106 }
108 // test contents
109 node.QueryInterface(Ci.nsINavHistoryContainerResultNode).containerOpen = true;
110 do_check_eq(node.childCount, 1);
111 var child = node.getChild(0);
112 do_check_true(PlacesUtils._uri(child.uri).equals(this._testURI));
114 // clean up
115 node.containerOpen = false;
116 }
117 }
118 do_check_eq(foundTestFolder, 1);
119 rootNode.containerOpen = false;
120 }
121 });
123 function run_test() {
124 run_next_test();
125 }
127 add_task(function () {
128 // make json file
129 let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json");
131 // array of ids not to delete when restoring
132 var excludedItemsFromRestore = [];
134 // populate db
135 tests.forEach(function(aTest) {
136 aTest.populate();
137 // sanity
138 aTest.validate();
140 if (aTest.excludedItemsFromRestore)
141 excludedItemsFromRestore = excludedItems.concat(aTest.excludedItemsFromRestore);
142 });
144 yield BookmarkJSONUtils.exportToFile(jsonFile);
146 tests.forEach(function(aTest) {
147 aTest.inbetween();
148 });
150 // restore json file
151 yield BookmarkJSONUtils.importFromFile(jsonFile, true);
153 // validate
154 tests.forEach(function(aTest) {
155 aTest.validate();
156 });
158 // clean up
159 yield OS.File.remove(jsonFile);
160 });