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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // This test performs checks on the history testing area as outlined |
michael@0 | 6 | // https://wiki.mozilla.org/Firefox3.1/PrivateBrowsing/TestPlan#History |
michael@0 | 7 | // http://developer.mozilla.org/en/Using_the_Places_history_service |
michael@0 | 8 | |
michael@0 | 9 | let visitedURIs = [ |
michael@0 | 10 | "http://www.test-link.com/", |
michael@0 | 11 | "http://www.test-typed.com/", |
michael@0 | 12 | "http://www.test-bookmark.com/", |
michael@0 | 13 | "http://www.test-redirect-permanent.com/", |
michael@0 | 14 | "http://www.test-redirect-temporary.com/", |
michael@0 | 15 | "http://www.test-embed.com/", |
michael@0 | 16 | "http://www.test-framed.com/", |
michael@0 | 17 | "http://www.test-download.com/" |
michael@0 | 18 | ].map(NetUtil.newURI.bind(NetUtil)); |
michael@0 | 19 | |
michael@0 | 20 | add_task(function () { |
michael@0 | 21 | let windowsToClose = []; |
michael@0 | 22 | let placeItemsCount = 0; |
michael@0 | 23 | |
michael@0 | 24 | registerCleanupFunction(function() { |
michael@0 | 25 | windowsToClose.forEach(function(win) { |
michael@0 | 26 | win.close(); |
michael@0 | 27 | }); |
michael@0 | 28 | }); |
michael@0 | 29 | |
michael@0 | 30 | yield promiseClearHistory(); |
michael@0 | 31 | |
michael@0 | 32 | // History database should be empty |
michael@0 | 33 | is(PlacesUtils.history.hasHistoryEntries, false, |
michael@0 | 34 | "History database should be empty"); |
michael@0 | 35 | |
michael@0 | 36 | // Ensure we wait for the default bookmarks import. |
michael@0 | 37 | let bookmarksDeferred = Promise.defer(); |
michael@0 | 38 | waitForCondition(() => { |
michael@0 | 39 | placeItemsCount = getPlacesItemsCount(); |
michael@0 | 40 | return placeItemsCount > 0 |
michael@0 | 41 | }, bookmarksDeferred.resolve, "Should have default bookmarks"); |
michael@0 | 42 | yield bookmarksDeferred.promise; |
michael@0 | 43 | |
michael@0 | 44 | // Create a handful of history items with various visit types |
michael@0 | 45 | yield promiseAddVisits([ |
michael@0 | 46 | { uri: visitedURIs[0], transition: TRANSITION_LINK }, |
michael@0 | 47 | { uri: visitedURIs[1], transition: TRANSITION_TYPED }, |
michael@0 | 48 | { uri: visitedURIs[2], transition: TRANSITION_BOOKMARK }, |
michael@0 | 49 | { uri: visitedURIs[3], transition: TRANSITION_REDIRECT_PERMANENT }, |
michael@0 | 50 | { uri: visitedURIs[4], transition: TRANSITION_REDIRECT_TEMPORARY }, |
michael@0 | 51 | { uri: visitedURIs[5], transition: TRANSITION_EMBED }, |
michael@0 | 52 | { uri: visitedURIs[6], transition: TRANSITION_FRAMED_LINK }, |
michael@0 | 53 | { uri: visitedURIs[7], transition: TRANSITION_DOWNLOAD } |
michael@0 | 54 | ]); |
michael@0 | 55 | |
michael@0 | 56 | // History database should have entries |
michael@0 | 57 | is(PlacesUtils.history.hasHistoryEntries, true, |
michael@0 | 58 | "History database should have entries"); |
michael@0 | 59 | |
michael@0 | 60 | placeItemsCount += 7; |
michael@0 | 61 | // We added 7 new items to history. |
michael@0 | 62 | is(getPlacesItemsCount(), placeItemsCount, |
michael@0 | 63 | "Check the total items count"); |
michael@0 | 64 | |
michael@0 | 65 | function* testOnWindow(aIsPrivate, aCount) { |
michael@0 | 66 | let deferred = Promise.defer(); |
michael@0 | 67 | whenNewWindowLoaded({ private: aIsPrivate }, deferred.resolve); |
michael@0 | 68 | let win = yield deferred.promise; |
michael@0 | 69 | windowsToClose.push(win); |
michael@0 | 70 | |
michael@0 | 71 | // History items should be retrievable by query |
michael@0 | 72 | yield checkHistoryItems(); |
michael@0 | 73 | |
michael@0 | 74 | // Updates the place items count |
michael@0 | 75 | let count = getPlacesItemsCount(); |
michael@0 | 76 | |
michael@0 | 77 | // Create Bookmark |
michael@0 | 78 | let bookmarkTitle = "title " + windowsToClose.length; |
michael@0 | 79 | let bookmarkKeyword = "keyword " + windowsToClose.length; |
michael@0 | 80 | let bookmarkUri = NetUtil.newURI("http://test-a-" + windowsToClose.length + ".com/"); |
michael@0 | 81 | |
michael@0 | 82 | let id = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarksMenuFolderId, |
michael@0 | 83 | bookmarkUri, |
michael@0 | 84 | PlacesUtils.bookmarks.DEFAULT_INDEX, |
michael@0 | 85 | bookmarkTitle); |
michael@0 | 86 | PlacesUtils.bookmarks.setKeywordForBookmark(id, bookmarkKeyword); |
michael@0 | 87 | count++; |
michael@0 | 88 | |
michael@0 | 89 | ok(PlacesUtils.bookmarks.isBookmarked(bookmarkUri), |
michael@0 | 90 | "Bookmark should be bookmarked, data should be retrievable"); |
michael@0 | 91 | is(bookmarkKeyword, PlacesUtils.bookmarks.getKeywordForURI(bookmarkUri), |
michael@0 | 92 | "Check bookmark uri keyword"); |
michael@0 | 93 | is(getPlacesItemsCount(), count, |
michael@0 | 94 | "Check the new bookmark items count"); |
michael@0 | 95 | is(isBookmarkAltered(), false, "Check if bookmark has been visited"); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | // Test on windows. |
michael@0 | 99 | yield testOnWindow(false); |
michael@0 | 100 | yield testOnWindow(true); |
michael@0 | 101 | yield testOnWindow(false); |
michael@0 | 102 | }); |
michael@0 | 103 | |
michael@0 | 104 | /** |
michael@0 | 105 | * Function performs a really simple query on our places entries, |
michael@0 | 106 | * and makes sure that the number of entries equal num_places_entries. |
michael@0 | 107 | */ |
michael@0 | 108 | function getPlacesItemsCount() { |
michael@0 | 109 | // Get bookmarks count |
michael@0 | 110 | let options = PlacesUtils.history.getNewQueryOptions(); |
michael@0 | 111 | options.includeHidden = true; |
michael@0 | 112 | options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS; |
michael@0 | 113 | let root = PlacesUtils.history.executeQuery( |
michael@0 | 114 | PlacesUtils.history.getNewQuery(), options).root; |
michael@0 | 115 | root.containerOpen = true; |
michael@0 | 116 | let cc = root.childCount; |
michael@0 | 117 | root.containerOpen = false; |
michael@0 | 118 | |
michael@0 | 119 | // Get history item count |
michael@0 | 120 | options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY; |
michael@0 | 121 | let root = PlacesUtils.history.executeQuery( |
michael@0 | 122 | PlacesUtils.history.getNewQuery(), options).root; |
michael@0 | 123 | root.containerOpen = true; |
michael@0 | 124 | cc += root.childCount; |
michael@0 | 125 | root.containerOpen = false; |
michael@0 | 126 | |
michael@0 | 127 | return cc; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | function* checkHistoryItems() { |
michael@0 | 131 | for (let i = 0; i < visitedURIs.length; i++) { |
michael@0 | 132 | let visitedUri = visitedURIs[i]; |
michael@0 | 133 | ok((yield promiseIsURIVisited(visitedUri)), ""); |
michael@0 | 134 | if (/embed/.test(visitedUri.spec)) { |
michael@0 | 135 | is(!!pageInDatabase(visitedUri), false, "Check if URI is in database"); |
michael@0 | 136 | } else { |
michael@0 | 137 | ok(!!pageInDatabase(visitedUri), "Check if URI is in database"); |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | /** |
michael@0 | 143 | * Checks if an address is found in the database. |
michael@0 | 144 | * @param aURI |
michael@0 | 145 | * nsIURI or address to look for. |
michael@0 | 146 | * @return place id of the page or 0 if not found |
michael@0 | 147 | */ |
michael@0 | 148 | function pageInDatabase(aURI) { |
michael@0 | 149 | let url = (aURI instanceof Ci.nsIURI ? aURI.spec : aURI); |
michael@0 | 150 | let stmt = DBConn().createStatement( |
michael@0 | 151 | "SELECT id FROM moz_places WHERE url = :url" |
michael@0 | 152 | ); |
michael@0 | 153 | stmt.params.url = url; |
michael@0 | 154 | try { |
michael@0 | 155 | if (!stmt.executeStep()) |
michael@0 | 156 | return 0; |
michael@0 | 157 | return stmt.getInt64(0); |
michael@0 | 158 | } finally { |
michael@0 | 159 | stmt.finalize(); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | /** |
michael@0 | 164 | * Function attempts to check if Bookmark-A has been visited |
michael@0 | 165 | * during private browsing mode, function should return false |
michael@0 | 166 | * |
michael@0 | 167 | * @returns false if the accessCount has not changed |
michael@0 | 168 | * true if the accessCount has changed |
michael@0 | 169 | */ |
michael@0 | 170 | function isBookmarkAltered(){ |
michael@0 | 171 | let options = PlacesUtils.history.getNewQueryOptions(); |
michael@0 | 172 | options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS; |
michael@0 | 173 | options.maxResults = 1; // should only expect a new bookmark |
michael@0 | 174 | |
michael@0 | 175 | let query = PlacesUtils.history.getNewQuery(); |
michael@0 | 176 | query.setFolders([PlacesUtils.bookmarks.bookmarksMenuFolder], 1); |
michael@0 | 177 | |
michael@0 | 178 | let root = PlacesUtils.history.executeQuery(query, options).root; |
michael@0 | 179 | root.containerOpen = true; |
michael@0 | 180 | is(root.childCount, options.maxResults, "Check new bookmarks results"); |
michael@0 | 181 | let node = root.getChild(0); |
michael@0 | 182 | root.containerOpen = false; |
michael@0 | 183 | |
michael@0 | 184 | return (node.accessCount != 0); |
michael@0 | 185 | } |