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 file tests the normal operation of setAndFetchFaviconForPage. |
michael@0 | 6 | function test() { |
michael@0 | 7 | // Initialization |
michael@0 | 8 | waitForExplicitFinish(); |
michael@0 | 9 | let windowsToClose = []; |
michael@0 | 10 | let testURI = "https://www.mozilla.org/en-US/"; |
michael@0 | 11 | let favIconLocation = |
michael@0 | 12 | "http://example.org/tests/toolkit/components/places/tests/browser/favicon-normal32.png"; |
michael@0 | 13 | let favIconURI = NetUtil.newURI(favIconLocation); |
michael@0 | 14 | let favIconMimeType= "image/png"; |
michael@0 | 15 | let pageURI; |
michael@0 | 16 | let favIconData; |
michael@0 | 17 | |
michael@0 | 18 | function testOnWindow(aOptions, aCallback) { |
michael@0 | 19 | whenNewWindowLoaded(aOptions, function(aWin) { |
michael@0 | 20 | windowsToClose.push(aWin); |
michael@0 | 21 | executeSoon(function() aCallback(aWin)); |
michael@0 | 22 | }); |
michael@0 | 23 | }; |
michael@0 | 24 | |
michael@0 | 25 | // This function is called after calling finish() on the test. |
michael@0 | 26 | registerCleanupFunction(function() { |
michael@0 | 27 | windowsToClose.forEach(function(aWin) { |
michael@0 | 28 | aWin.close(); |
michael@0 | 29 | }); |
michael@0 | 30 | }); |
michael@0 | 31 | |
michael@0 | 32 | function getIconFile(aCallback) { |
michael@0 | 33 | NetUtil.asyncFetch(favIconLocation, function(inputStream, status) { |
michael@0 | 34 | if (!Components.isSuccessCode(status)) { |
michael@0 | 35 | ok(false, "Could not get the icon file"); |
michael@0 | 36 | // Handle error. |
michael@0 | 37 | return; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | // Check the returned size versus the expected size. |
michael@0 | 41 | let size = inputStream.available(); |
michael@0 | 42 | favIconData = NetUtil.readInputStreamToString(inputStream, size); |
michael@0 | 43 | is(size, favIconData.length, "Check correct icon size"); |
michael@0 | 44 | // Check that the favicon loaded correctly before starting the actual tests. |
michael@0 | 45 | is(favIconData.length, 344, "Check correct icon length (344)"); |
michael@0 | 46 | |
michael@0 | 47 | if (aCallback) { |
michael@0 | 48 | aCallback(); |
michael@0 | 49 | } else { |
michael@0 | 50 | finish(); |
michael@0 | 51 | } |
michael@0 | 52 | }); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | function testNormal(aWindow, aCallback) { |
michael@0 | 56 | pageURI = NetUtil.newURI("http://example.com/normal"); |
michael@0 | 57 | waitForFaviconChanged(pageURI, favIconURI, aWindow, |
michael@0 | 58 | function testNormalCallback() { |
michael@0 | 59 | checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow, |
michael@0 | 60 | aCallback); |
michael@0 | 61 | } |
michael@0 | 62 | ); |
michael@0 | 63 | |
michael@0 | 64 | addVisits({uri: pageURI, transition: TRANSITION_TYPED}, aWindow, |
michael@0 | 65 | function () { |
michael@0 | 66 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, |
michael@0 | 67 | favIconURI, true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 68 | } |
michael@0 | 69 | ); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | function testAboutURIBookmarked(aWindow, aCallback) { |
michael@0 | 73 | pageURI = NetUtil.newURI("about:testAboutURI_bookmarked"); |
michael@0 | 74 | waitForFaviconChanged(pageURI, favIconURI, aWindow, |
michael@0 | 75 | function testAboutURIBookmarkedCallback() { |
michael@0 | 76 | checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow, |
michael@0 | 77 | aCallback); |
michael@0 | 78 | } |
michael@0 | 79 | ); |
michael@0 | 80 | |
michael@0 | 81 | aWindow.PlacesUtils.bookmarks.insertBookmark( |
michael@0 | 82 | aWindow.PlacesUtils.unfiledBookmarksFolderId, pageURI, |
michael@0 | 83 | aWindow.PlacesUtils.bookmarks.DEFAULT_INDEX, pageURI.spec); |
michael@0 | 84 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, favIconURI, |
michael@0 | 85 | true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | function testPrivateBrowsingBookmarked(aWindow, aCallback) { |
michael@0 | 89 | pageURI = NetUtil.newURI("http://example.com/privateBrowsing_bookmarked"); |
michael@0 | 90 | waitForFaviconChanged(pageURI, favIconURI, aWindow, |
michael@0 | 91 | function testPrivateBrowsingBookmarkedCallback() { |
michael@0 | 92 | checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow, |
michael@0 | 93 | aCallback); |
michael@0 | 94 | } |
michael@0 | 95 | ); |
michael@0 | 96 | |
michael@0 | 97 | aWindow.PlacesUtils.bookmarks.insertBookmark( |
michael@0 | 98 | aWindow.PlacesUtils.unfiledBookmarksFolderId, pageURI, |
michael@0 | 99 | aWindow.PlacesUtils.bookmarks.DEFAULT_INDEX, pageURI.spec); |
michael@0 | 100 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, favIconURI, |
michael@0 | 101 | true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_PRIVATE); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | function testDisabledHistoryBookmarked(aWindow, aCallback) { |
michael@0 | 105 | pageURI = NetUtil.newURI("http://example.com/disabledHistory_bookmarked"); |
michael@0 | 106 | waitForFaviconChanged(pageURI, favIconURI, aWindow, |
michael@0 | 107 | function testDisabledHistoryBookmarkedCallback() { |
michael@0 | 108 | checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow, |
michael@0 | 109 | aCallback); |
michael@0 | 110 | } |
michael@0 | 111 | ); |
michael@0 | 112 | |
michael@0 | 113 | // Disable history while changing the favicon. |
michael@0 | 114 | aWindow.Services.prefs.setBoolPref("places.history.enabled", false); |
michael@0 | 115 | |
michael@0 | 116 | aWindow.PlacesUtils.bookmarks.insertBookmark( |
michael@0 | 117 | aWindow.PlacesUtils.unfiledBookmarksFolderId, pageURI, |
michael@0 | 118 | aWindow.PlacesUtils.bookmarks.DEFAULT_INDEX, pageURI.spec); |
michael@0 | 119 | aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, favIconURI, |
michael@0 | 120 | true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); |
michael@0 | 121 | |
michael@0 | 122 | // The setAndFetchFaviconForPage function calls CanAddURI synchronously, thus |
michael@0 | 123 | // we can set the preference back to true immediately. We don't clear the |
michael@0 | 124 | // preference because not all products enable Places by default. |
michael@0 | 125 | aWindow.Services.prefs.setBoolPref("places.history.enabled", true); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | getIconFile(function () { |
michael@0 | 129 | testOnWindow({}, function(aWin) { |
michael@0 | 130 | testNormal(aWin, function () { |
michael@0 | 131 | testOnWindow({}, function(aWin) { |
michael@0 | 132 | testAboutURIBookmarked(aWin, function () { |
michael@0 | 133 | testOnWindow({private: true}, function(aWin) { |
michael@0 | 134 | testPrivateBrowsingBookmarked(aWin, function () { |
michael@0 | 135 | testOnWindow({}, function(aWin) { |
michael@0 | 136 | testDisabledHistoryBookmarked(aWin, finish); |
michael@0 | 137 | }); |
michael@0 | 138 | }); |
michael@0 | 139 | }); |
michael@0 | 140 | }); |
michael@0 | 141 | }); |
michael@0 | 142 | }); |
michael@0 | 143 | }); |
michael@0 | 144 | }); |
michael@0 | 145 | } |