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