1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/browser/browser_favicon_setAndFetchFaviconForPage_failures.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,254 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/** 1.9 + * This file tests setAndFetchFaviconForPage when it is called with invalid 1.10 + * arguments, and when no favicon is stored for the given arguments. 1.11 + */ 1.12 +function test() { 1.13 + // Initialization 1.14 + waitForExplicitFinish(); 1.15 + let windowsToClose = []; 1.16 + let favIcon16Location = 1.17 + "http://example.org/tests/toolkit/components/places/tests/browser/favicon-normal16.png"; 1.18 + let favIcon32Location = 1.19 + "http://example.org/tests/toolkit/components/places/tests/browser/favicon-normal32.png"; 1.20 + let favIcon16URI = NetUtil.newURI(favIcon16Location); 1.21 + let favIcon32URI = NetUtil.newURI(favIcon32Location); 1.22 + let lastPageURI = NetUtil.newURI("http://example.com/verification"); 1.23 + // This error icon must stay in sync with FAVICON_ERRORPAGE_URL in 1.24 + // nsIFaviconService.idl, aboutCertError.xhtml and netError.xhtml. 1.25 + let favIconErrorPageURI = 1.26 + NetUtil.newURI("chrome://global/skin/icons/warning-16.png"); 1.27 + let favIconsResultCount = 0; 1.28 + let pageURI; 1.29 + 1.30 + function testOnWindow(aOptions, aCallback) { 1.31 + whenNewWindowLoaded(aOptions, function(aWin) { 1.32 + windowsToClose.push(aWin); 1.33 + executeSoon(function() aCallback(aWin)); 1.34 + }); 1.35 + }; 1.36 + 1.37 + // This function is called after calling finish() on the test. 1.38 + registerCleanupFunction(function() { 1.39 + windowsToClose.forEach(function(aWin) { 1.40 + aWin.close(); 1.41 + }); 1.42 + }); 1.43 + 1.44 + function checkFavIconsDBCount(aCallback) { 1.45 + let stmt = DBConn().createAsyncStatement("SELECT url FROM moz_favicons"); 1.46 + stmt.executeAsync({ 1.47 + handleResult: function final_handleResult(aResultSet) { 1.48 + for (let row; (row = aResultSet.getNextRow()); ) { 1.49 + favIconsResultCount++; 1.50 + } 1.51 + }, 1.52 + handleError: function final_handleError(aError) { 1.53 + throw("Unexpected error (" + aError.result + "): " + aError.message); 1.54 + }, 1.55 + handleCompletion: function final_handleCompletion(aReason) { 1.56 + //begin testing 1.57 + info("Previous records in moz_favicons: " + favIconsResultCount); 1.58 + if (aCallback) { 1.59 + aCallback(); 1.60 + } 1.61 + } 1.62 + }); 1.63 + stmt.finalize(); 1.64 + } 1.65 + 1.66 + function testNullPageURI(aWindow, aCallback) { 1.67 + try { 1.68 + aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(null, favIcon16URI, 1.69 + true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); 1.70 + throw("Exception expected because aPageURI is null."); 1.71 + } catch (ex) { 1.72 + // We expected an exception. 1.73 + ok(true, "Exception expected because aPageURI is null"); 1.74 + } 1.75 + 1.76 + if (aCallback) { 1.77 + aCallback(); 1.78 + } 1.79 + } 1.80 + 1.81 + function testNullFavIconURI(aWindow, aCallback) { 1.82 + try { 1.83 + aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage( 1.84 + NetUtil.newURI("http://example.com/null_faviconURI"), null, true, 1.85 + aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); 1.86 + throw("Exception expected because aFaviconURI is null."); 1.87 + } catch (ex) { 1.88 + // We expected an exception. 1.89 + ok(true, "Exception expected because aFaviconURI is null."); 1.90 + } 1.91 + 1.92 + if (aCallback) { 1.93 + aCallback(); 1.94 + } 1.95 + } 1.96 + 1.97 + function testAboutURI(aWindow, aCallback) { 1.98 + aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage( 1.99 + NetUtil.newURI("about:testAboutURI"), favIcon16URI, true, 1.100 + aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); 1.101 + 1.102 + if (aCallback) { 1.103 + aCallback(); 1.104 + } 1.105 + } 1.106 + 1.107 + function testPrivateBrowsingNonBookmarkedURI(aWindow, aCallback) { 1.108 + let pageURI = NetUtil.newURI("http://example.com/privateBrowsing"); 1.109 + addVisits({ uri: pageURI, transitionType: TRANSITION_TYPED }, aWindow, 1.110 + function () { 1.111 + aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, 1.112 + favIcon16URI, true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_PRIVATE); 1.113 + 1.114 + if (aCallback) { 1.115 + aCallback(); 1.116 + } 1.117 + }); 1.118 + } 1.119 + 1.120 + function testDisabledHistory(aWindow, aCallback) { 1.121 + let pageURI = NetUtil.newURI("http://example.com/disabledHistory"); 1.122 + addVisits({ uri: pageURI, transition: TRANSITION_TYPED }, aWindow, 1.123 + function () { 1.124 + aWindow.Services.prefs.setBoolPref("places.history.enabled", false); 1.125 + 1.126 + aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, 1.127 + favIcon16URI, true, 1.128 + aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); 1.129 + 1.130 + // The setAndFetchFaviconForPage function calls CanAddURI synchronously, thus 1.131 + // we can set the preference back to true immediately . We don't clear the 1.132 + // preference because not all products enable Places by default. 1.133 + aWindow.Services.prefs.setBoolPref("places.history.enabled", true); 1.134 + 1.135 + if (aCallback) { 1.136 + aCallback(); 1.137 + } 1.138 + }); 1.139 + } 1.140 + 1.141 + function testErrorIcon(aWindow, aCallback) { 1.142 + let pageURI = NetUtil.newURI("http://example.com/errorIcon"); 1.143 + let places = [{ uri: pageURI, transition: TRANSITION_TYPED }]; 1.144 + addVisits({ uri: pageURI, transition: TRANSITION_TYPED }, aWindow, 1.145 + function () { 1.146 + aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, 1.147 + favIconErrorPageURI, true, 1.148 + aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); 1.149 + 1.150 + if (aCallback) { 1.151 + aCallback(); 1.152 + } 1.153 + }); 1.154 + } 1.155 + 1.156 + function testNonExistingPage(aWindow, aCallback) { 1.157 + aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage( 1.158 + NetUtil.newURI("http://example.com/nonexistingPage"), favIcon16URI, true, 1.159 + aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); 1.160 + 1.161 + if (aCallback) { 1.162 + aCallback(); 1.163 + } 1.164 + } 1.165 + 1.166 + function testFinalVerification(aWindow, aCallback) { 1.167 + // Only the last test should raise the onPageChanged notification, 1.168 + // executing the waitForFaviconChanged callback. 1.169 + waitForFaviconChanged(lastPageURI, favIcon32URI, aWindow, 1.170 + function final_callback() { 1.171 + // Check that only one record corresponding to the last favicon is present. 1.172 + let resultCount = 0; 1.173 + let stmt = DBConn().createAsyncStatement("SELECT url FROM moz_favicons"); 1.174 + stmt.executeAsync({ 1.175 + handleResult: function final_handleResult(aResultSet) { 1.176 + 1.177 + // If the moz_favicons DB had been previously loaded (before our 1.178 + // test began), we should focus only in the URI we are testing and 1.179 + // skip the URIs not related to our test. 1.180 + if (favIconsResultCount > 0) { 1.181 + for (let row; (row = aResultSet.getNextRow()); ) { 1.182 + if (favIcon32URI.spec === row.getResultByIndex(0)) { 1.183 + is(favIcon32URI.spec, row.getResultByIndex(0), 1.184 + "Check equal favicons"); 1.185 + resultCount++; 1.186 + } 1.187 + } 1.188 + } else { 1.189 + for (let row; (row = aResultSet.getNextRow()); ) { 1.190 + is(favIcon32URI.spec, row.getResultByIndex(0), 1.191 + "Check equal favicons"); 1.192 + resultCount++; 1.193 + } 1.194 + } 1.195 + }, 1.196 + handleError: function final_handleError(aError) { 1.197 + throw("Unexpected error (" + aError.result + "): " + aError.message); 1.198 + }, 1.199 + handleCompletion: function final_handleCompletion(aReason) { 1.200 + is(Ci.mozIStorageStatementCallback.REASON_FINISHED, aReason, 1.201 + "Check reasons are equal"); 1.202 + is(1, resultCount, "Check result count"); 1.203 + if (aCallback) { 1.204 + aCallback(); 1.205 + } 1.206 + } 1.207 + }); 1.208 + stmt.finalize(); 1.209 + }); 1.210 + 1.211 + // This is the only test that should cause the waitForFaviconChanged 1.212 + // callback to be invoked. In turn, the callback will invoke 1.213 + // finish() causing the tests to finish. 1.214 + addVisits({ uri: lastPageURI, transition: TRANSITION_TYPED }, aWindow, 1.215 + function () { 1.216 + aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(lastPageURI, 1.217 + favIcon32URI, true, 1.218 + aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); 1.219 + }); 1.220 + } 1.221 + 1.222 + checkFavIconsDBCount(function () { 1.223 + testOnWindow({}, function(aWin) { 1.224 + testNullPageURI(aWin, function () { 1.225 + testOnWindow({}, function(aWin) { 1.226 + testNullFavIconURI(aWin, function() { 1.227 + testOnWindow({}, function(aWin) { 1.228 + testAboutURI(aWin, function() { 1.229 + testOnWindow({private: true}, function(aWin) { 1.230 + testPrivateBrowsingNonBookmarkedURI(aWin, function () { 1.231 + testOnWindow({}, function(aWin) { 1.232 + testDisabledHistory(aWin, function () { 1.233 + testOnWindow({}, function(aWin) { 1.234 + testErrorIcon(aWin, function() { 1.235 + testOnWindow({}, function(aWin) { 1.236 + testNonExistingPage(aWin, function() { 1.237 + testOnWindow({}, function(aWin) { 1.238 + testFinalVerification(aWin, function() { 1.239 + finish(); 1.240 + }); 1.241 + }); 1.242 + }); 1.243 + }); 1.244 + }); 1.245 + }); 1.246 + }); 1.247 + }); 1.248 + }); 1.249 + }); 1.250 + }); 1.251 + }); 1.252 + }); 1.253 + }); 1.254 + }); 1.255 + }); 1.256 + }); 1.257 +}