toolkit/components/places/tests/bookmarks/test_458683.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:5191ad5a5eb7
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/. */
6
7 var tests = [];
8
9 // Get database connection
10 try {
11 var mDBConn = PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase)
12 .DBConnection;
13 }
14 catch(ex) {
15 do_throw("Could not get database connection\n");
16 }
17
18 /*
19 This test is:
20 - don't block while doing backup and restore if tag containers contain
21 bogus items (separators, folders)
22 */
23
24 var invalidTagChildTest = {
25 _itemTitle: "invalid uri",
26 _itemUrl: "http://test.mozilla.org/",
27 _itemId: -1,
28 _tag: "testTag",
29 _tagItemId: -1,
30
31 populate: function () {
32 // add a valid bookmark
33 this._itemId = PlacesUtils.bookmarks
34 .insertBookmark(PlacesUtils.toolbarFolderId,
35 PlacesUtils._uri(this._itemUrl),
36 PlacesUtils.bookmarks.DEFAULT_INDEX,
37 this._itemTitle);
38
39 // create a tag
40 PlacesUtils.tagging.tagURI(PlacesUtils._uri(this._itemUrl), [this._tag]);
41 // get tag folder id
42 var options = PlacesUtils.history.getNewQueryOptions();
43 var query = PlacesUtils.history.getNewQuery();
44 query.setFolders([PlacesUtils.bookmarks.tagsFolder], 1);
45 var result = PlacesUtils.history.executeQuery(query, options);
46 var tagRoot = result.root;
47 tagRoot.containerOpen = true;
48 do_check_eq(tagRoot.childCount, 1);
49 var tagNode = tagRoot.getChild(0)
50 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
51 this._tagItemId = tagNode.itemId;
52 tagRoot.containerOpen = false;
53
54 // add a separator and a folder inside tag folder
55 PlacesUtils.bookmarks.insertSeparator(this._tagItemId,
56 PlacesUtils.bookmarks.DEFAULT_INDEX);
57 PlacesUtils.bookmarks.createFolder(this._tagItemId,
58 "test folder",
59 PlacesUtils.bookmarks.DEFAULT_INDEX);
60
61 // add a separator and a folder inside tag root
62 PlacesUtils.bookmarks.insertSeparator(PlacesUtils.bookmarks.tagsFolder,
63 PlacesUtils.bookmarks.DEFAULT_INDEX);
64 PlacesUtils.bookmarks.createFolder(PlacesUtils.bookmarks.tagsFolder,
65 "test tags root folder",
66 PlacesUtils.bookmarks.DEFAULT_INDEX);
67 },
68
69 clean: function () {
70 PlacesUtils.tagging.untagURI(PlacesUtils._uri(this._itemUrl), [this._tag]);
71 PlacesUtils.bookmarks.removeItem(this._itemId);
72 },
73
74 validate: function () {
75 var query = PlacesUtils.history.getNewQuery();
76 query.setFolders([PlacesUtils.bookmarks.toolbarFolder], 1);
77 var options = PlacesUtils.history.getNewQueryOptions();
78 var result = PlacesUtils.history.executeQuery(query, options);
79
80 var toolbar = result.root;
81 toolbar.containerOpen = true;
82
83 // test for our bookmark
84 do_check_eq(toolbar.childCount, 1);
85 for (var i = 0; i < toolbar.childCount; i++) {
86 var folderNode = toolbar.getChild(0);
87 do_check_eq(folderNode.type, folderNode.RESULT_TYPE_URI);
88 do_check_eq(folderNode.title, this._itemTitle);
89 }
90 toolbar.containerOpen = false;
91
92 // test for our tag
93 var tags = PlacesUtils.tagging.getTagsForURI(PlacesUtils._uri(this._itemUrl));
94 do_check_eq(tags.length, 1);
95 do_check_eq(tags[0], this._tag);
96 }
97 }
98 tests.push(invalidTagChildTest);
99
100 function run_test() {
101 run_next_test()
102 }
103
104 add_task(function () {
105 let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json");
106
107 // populate db
108 tests.forEach(function(aTest) {
109 aTest.populate();
110 // sanity
111 aTest.validate();
112 });
113
114 yield BookmarkJSONUtils.exportToFile(jsonFile);
115
116 // clean
117 tests.forEach(function(aTest) {
118 aTest.clean();
119 });
120
121 // restore json file
122 yield BookmarkJSONUtils.importFromFile(jsonFile, true);
123
124 // validate
125 tests.forEach(function(aTest) {
126 aTest.validate();
127 });
128
129 // clean up
130 yield OS.File.remove(jsonFile);
131 });

mercurial