Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | /** |
michael@0 | 6 | * This file tests setAndFetchFaviconForPage when it is called with invalid |
michael@0 | 7 | * arguments, and when no favicon is stored for the given arguments. |
michael@0 | 8 | */ |
michael@0 | 9 | function test() { |
michael@0 | 10 | // Initialization |
michael@0 | 11 | waitForExplicitFinish(); |
michael@0 | 12 | let windowsToClose = []; |
michael@0 | 13 | let favIcon16Location = |
michael@0 | 14 | "http://example.org/tests/toolkit/components/places/tests/browser/favicon-normal16.png"; |
michael@0 | 15 | let favIcon32Location = |
michael@0 | 16 | "http://example.org/tests/toolkit/components/places/tests/browser/favicon-normal32.png"; |
michael@0 | 17 | let favIcon16URI = NetUtil.newURI(favIcon16Location); |
michael@0 | 18 | let favIcon32URI = NetUtil.newURI(favIcon32Location); |
michael@0 | 19 | let lastPageURI = NetUtil.newURI("http://example.com/verification"); |
michael@0 | 20 | // This error icon must stay in sync with FAVICON_ERRORPAGE_URL in |
michael@0 | 21 | // nsIFaviconService.idl, aboutCertError.xhtml and netError.xhtml. |
michael@0 | 22 | let favIconErrorPageURI = |
michael@0 | 23 | NetUtil.newURI("chrome://global/skin/icons/warning-16.png"); |
michael@0 | 24 | let favIconsResultCount = 0; |
michael@0 | 25 | let pageURI; |
michael@0 | 26 | |
michael@0 | 27 | function testOnWindow(aOptions, aCallback) { |
michael@0 | 28 | whenNewWindowLoaded(aOptions, function(aWin) { |
michael@0 | 29 | windowsToClose.push(aWin); |
michael@0 | 30 | executeSoon(function() aCallback(aWin)); |
michael@0 | 31 | }); |
michael@0 | 32 | }; |
michael@0 | 33 | |
michael@0 | 34 | // This function is called after calling finish() on the test. |
michael@0 | 35 | registerCleanupFunction(function() { |
michael@0 | 36 | windowsToClose.forEach(function(aWin) { |
michael@0 | 37 | aWin.close(); |
michael@0 | 38 | }); |
michael@0 | 39 | }); |
michael@0 | 40 | |
michael@0 | 41 | function checkFavIconsDBCount(aCallback) { |
michael@0 | 42 | let stmt = DBConn().createAsyncStatement("SELECT url FROM moz_favicons"); |
michael@0 | 43 | stmt.executeAsync({ |
michael@0 | 44 | handleResult: function final_handleResult(aResultSet) { |
michael@0 | 45 | for (let row; (row = aResultSet.getNextRow()); ) { |
michael@0 | 46 | favIconsResultCount++; |
michael@0 | 47 | } |
michael@0 | 48 | }, |
michael@0 | 49 | handleError: function final_handleError(aError) { |
michael@0 | 50 | throw("Unexpected error (" + aError.result + "): " + aError.message); |
michael@0 | 51 | }, |
michael@0 | 52 | handleCompletion: function final_handleCompletion(aReason) { |
michael@0 | 53 | //begin testing |
michael@0 | 54 | info("Previous records in moz_favicons: " + favIconsResultCount); |
michael@0 | 55 | if (aCallback) { |
michael@0 | 56 | aCallback(); |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | }); |
michael@0 | 60 | stmt.finalize(); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | function testNullPageURI(aWindow, aCallback) { |
michael@0 | 64 | try { |
michael@0 | 65 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(null, favIcon16URI, |
michael@0 | 66 | true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 67 | throw("Exception expected because aPageURI is null."); |
michael@0 | 68 | } catch (ex) { |
michael@0 | 69 | // We expected an exception. |
michael@0 | 70 | ok(true, "Exception expected because aPageURI is null"); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | if (aCallback) { |
michael@0 | 74 | aCallback(); |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | function testNullFavIconURI(aWindow, aCallback) { |
michael@0 | 79 | try { |
michael@0 | 80 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage( |
michael@0 | 81 | NetUtil.newURI("http://example.com/null_faviconURI"), null, true, |
michael@0 | 82 | aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 83 | throw("Exception expected because aFaviconURI is null."); |
michael@0 | 84 | } catch (ex) { |
michael@0 | 85 | // We expected an exception. |
michael@0 | 86 | ok(true, "Exception expected because aFaviconURI is null."); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | if (aCallback) { |
michael@0 | 90 | aCallback(); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | function testAboutURI(aWindow, aCallback) { |
michael@0 | 95 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage( |
michael@0 | 96 | NetUtil.newURI("about:testAboutURI"), favIcon16URI, true, |
michael@0 | 97 | aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 98 | |
michael@0 | 99 | if (aCallback) { |
michael@0 | 100 | aCallback(); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | function testPrivateBrowsingNonBookmarkedURI(aWindow, aCallback) { |
michael@0 | 105 | let pageURI = NetUtil.newURI("http://example.com/privateBrowsing"); |
michael@0 | 106 | addVisits({ uri: pageURI, transitionType: TRANSITION_TYPED }, aWindow, |
michael@0 | 107 | function () { |
michael@0 | 108 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, |
michael@0 | 109 | favIcon16URI, true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_PRIVATE); |
michael@0 | 110 | |
michael@0 | 111 | if (aCallback) { |
michael@0 | 112 | aCallback(); |
michael@0 | 113 | } |
michael@0 | 114 | }); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | function testDisabledHistory(aWindow, aCallback) { |
michael@0 | 118 | let pageURI = NetUtil.newURI("http://example.com/disabledHistory"); |
michael@0 | 119 | addVisits({ uri: pageURI, transition: TRANSITION_TYPED }, aWindow, |
michael@0 | 120 | function () { |
michael@0 | 121 | aWindow.Services.prefs.setBoolPref("places.history.enabled", false); |
michael@0 | 122 | |
michael@0 | 123 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, |
michael@0 | 124 | favIcon16URI, true, |
michael@0 | 125 | aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 126 | |
michael@0 | 127 | // The setAndFetchFaviconForPage function calls CanAddURI synchronously, thus |
michael@0 | 128 | // we can set the preference back to true immediately . We don't clear the |
michael@0 | 129 | // preference because not all products enable Places by default. |
michael@0 | 130 | aWindow.Services.prefs.setBoolPref("places.history.enabled", true); |
michael@0 | 131 | |
michael@0 | 132 | if (aCallback) { |
michael@0 | 133 | aCallback(); |
michael@0 | 134 | } |
michael@0 | 135 | }); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | function testErrorIcon(aWindow, aCallback) { |
michael@0 | 139 | let pageURI = NetUtil.newURI("http://example.com/errorIcon"); |
michael@0 | 140 | let places = [{ uri: pageURI, transition: TRANSITION_TYPED }]; |
michael@0 | 141 | addVisits({ uri: pageURI, transition: TRANSITION_TYPED }, aWindow, |
michael@0 | 142 | function () { |
michael@0 | 143 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, |
michael@0 | 144 | favIconErrorPageURI, true, |
michael@0 | 145 | aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 146 | |
michael@0 | 147 | if (aCallback) { |
michael@0 | 148 | aCallback(); |
michael@0 | 149 | } |
michael@0 | 150 | }); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | function testNonExistingPage(aWindow, aCallback) { |
michael@0 | 154 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage( |
michael@0 | 155 | NetUtil.newURI("http://example.com/nonexistingPage"), favIcon16URI, true, |
michael@0 | 156 | aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 157 | |
michael@0 | 158 | if (aCallback) { |
michael@0 | 159 | aCallback(); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | function testFinalVerification(aWindow, aCallback) { |
michael@0 | 164 | // Only the last test should raise the onPageChanged notification, |
michael@0 | 165 | // executing the waitForFaviconChanged callback. |
michael@0 | 166 | waitForFaviconChanged(lastPageURI, favIcon32URI, aWindow, |
michael@0 | 167 | function final_callback() { |
michael@0 | 168 | // Check that only one record corresponding to the last favicon is present. |
michael@0 | 169 | let resultCount = 0; |
michael@0 | 170 | let stmt = DBConn().createAsyncStatement("SELECT url FROM moz_favicons"); |
michael@0 | 171 | stmt.executeAsync({ |
michael@0 | 172 | handleResult: function final_handleResult(aResultSet) { |
michael@0 | 173 | |
michael@0 | 174 | // If the moz_favicons DB had been previously loaded (before our |
michael@0 | 175 | // test began), we should focus only in the URI we are testing and |
michael@0 | 176 | // skip the URIs not related to our test. |
michael@0 | 177 | if (favIconsResultCount > 0) { |
michael@0 | 178 | for (let row; (row = aResultSet.getNextRow()); ) { |
michael@0 | 179 | if (favIcon32URI.spec === row.getResultByIndex(0)) { |
michael@0 | 180 | is(favIcon32URI.spec, row.getResultByIndex(0), |
michael@0 | 181 | "Check equal favicons"); |
michael@0 | 182 | resultCount++; |
michael@0 | 183 | } |
michael@0 | 184 | } |
michael@0 | 185 | } else { |
michael@0 | 186 | for (let row; (row = aResultSet.getNextRow()); ) { |
michael@0 | 187 | is(favIcon32URI.spec, row.getResultByIndex(0), |
michael@0 | 188 | "Check equal favicons"); |
michael@0 | 189 | resultCount++; |
michael@0 | 190 | } |
michael@0 | 191 | } |
michael@0 | 192 | }, |
michael@0 | 193 | handleError: function final_handleError(aError) { |
michael@0 | 194 | throw("Unexpected error (" + aError.result + "): " + aError.message); |
michael@0 | 195 | }, |
michael@0 | 196 | handleCompletion: function final_handleCompletion(aReason) { |
michael@0 | 197 | is(Ci.mozIStorageStatementCallback.REASON_FINISHED, aReason, |
michael@0 | 198 | "Check reasons are equal"); |
michael@0 | 199 | is(1, resultCount, "Check result count"); |
michael@0 | 200 | if (aCallback) { |
michael@0 | 201 | aCallback(); |
michael@0 | 202 | } |
michael@0 | 203 | } |
michael@0 | 204 | }); |
michael@0 | 205 | stmt.finalize(); |
michael@0 | 206 | }); |
michael@0 | 207 | |
michael@0 | 208 | // This is the only test that should cause the waitForFaviconChanged |
michael@0 | 209 | // callback to be invoked. In turn, the callback will invoke |
michael@0 | 210 | // finish() causing the tests to finish. |
michael@0 | 211 | addVisits({ uri: lastPageURI, transition: TRANSITION_TYPED }, aWindow, |
michael@0 | 212 | function () { |
michael@0 | 213 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(lastPageURI, |
michael@0 | 214 | favIcon32URI, true, |
michael@0 | 215 | aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 216 | }); |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | checkFavIconsDBCount(function () { |
michael@0 | 220 | testOnWindow({}, function(aWin) { |
michael@0 | 221 | testNullPageURI(aWin, function () { |
michael@0 | 222 | testOnWindow({}, function(aWin) { |
michael@0 | 223 | testNullFavIconURI(aWin, function() { |
michael@0 | 224 | testOnWindow({}, function(aWin) { |
michael@0 | 225 | testAboutURI(aWin, function() { |
michael@0 | 226 | testOnWindow({private: true}, function(aWin) { |
michael@0 | 227 | testPrivateBrowsingNonBookmarkedURI(aWin, function () { |
michael@0 | 228 | testOnWindow({}, function(aWin) { |
michael@0 | 229 | testDisabledHistory(aWin, function () { |
michael@0 | 230 | testOnWindow({}, function(aWin) { |
michael@0 | 231 | testErrorIcon(aWin, function() { |
michael@0 | 232 | testOnWindow({}, function(aWin) { |
michael@0 | 233 | testNonExistingPage(aWin, function() { |
michael@0 | 234 | testOnWindow({}, function(aWin) { |
michael@0 | 235 | testFinalVerification(aWin, function() { |
michael@0 | 236 | finish(); |
michael@0 | 237 | }); |
michael@0 | 238 | }); |
michael@0 | 239 | }); |
michael@0 | 240 | }); |
michael@0 | 241 | }); |
michael@0 | 242 | }); |
michael@0 | 243 | }); |
michael@0 | 244 | }); |
michael@0 | 245 | }); |
michael@0 | 246 | }); |
michael@0 | 247 | }); |
michael@0 | 248 | }); |
michael@0 | 249 | }); |
michael@0 | 250 | }); |
michael@0 | 251 | }); |
michael@0 | 252 | }); |
michael@0 | 253 | }); |
michael@0 | 254 | } |