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 // 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 }
18 /*
19 This test is:
20 - don't try to add invalid uri nodes to a JSON backup
21 */
23 var invalidURITest = {
24 _itemTitle: "invalid uri",
25 _itemUrl: "http://test.mozilla.org/",
26 _itemId: null,
28 populate: function () {
29 // add a valid bookmark
30 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId,
31 PlacesUtils._uri(this._itemUrl),
32 PlacesUtils.bookmarks.DEFAULT_INDEX,
33 this._itemTitle);
34 // this bookmark will go corrupt
35 this._itemId =
36 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId,
37 PlacesUtils._uri(this._itemUrl),
38 PlacesUtils.bookmarks.DEFAULT_INDEX,
39 this._itemTitle);
40 },
42 clean: function () {
43 PlacesUtils.bookmarks.removeItem(this._itemId);
44 },
46 validate: function (aExpectValidItemsCount) {
47 var query = PlacesUtils.history.getNewQuery();
48 query.setFolders([PlacesUtils.bookmarks.toolbarFolder], 1);
49 var options = PlacesUtils.history.getNewQueryOptions();
50 var result = PlacesUtils.history.executeQuery(query, options);
52 var toolbar = result.root;
53 toolbar.containerOpen = true;
55 // test for our bookmark
56 do_check_eq(toolbar.childCount, aExpectValidItemsCount);
57 for (var i = 0; i < toolbar.childCount; i++) {
58 var folderNode = toolbar.getChild(0);
59 do_check_eq(folderNode.type, folderNode.RESULT_TYPE_URI);
60 do_check_eq(folderNode.title, this._itemTitle);
61 }
63 // clean up
64 toolbar.containerOpen = false;
65 }
66 }
67 tests.push(invalidURITest);
69 function run_test() {
70 run_next_test();
71 }
73 add_task(function() {
74 // make json file
75 let jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.json");
77 // populate db
78 tests.forEach(function(aTest) {
79 aTest.populate();
80 // sanity
81 aTest.validate(2);
82 // Something in the code went wrong and we finish up losing the place, so
83 // the bookmark uri becomes null.
84 var sql = "UPDATE moz_bookmarks SET fk = 1337 WHERE id = ?1";
85 var stmt = mDBConn.createStatement(sql);
86 stmt.bindByIndex(0, aTest._itemId);
87 try {
88 stmt.execute();
89 } finally {
90 stmt.finalize();
91 }
92 });
94 yield BookmarkJSONUtils.exportToFile(jsonFile);
96 // clean
97 tests.forEach(function(aTest) {
98 aTest.clean();
99 });
101 // restore json file
102 try {
103 yield BookmarkJSONUtils.importFromFile(jsonFile, true);
104 } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
106 // validate
107 tests.forEach(function(aTest) {
108 aTest.validate(1);
109 });
111 // clean up
112 yield OS.File.remove(jsonFile);
113 });