michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This file tests the normal operation of setAndFetchFaviconForPage. michael@0: function test() { michael@0: // Initialization michael@0: waitForExplicitFinish(); michael@0: let windowsToClose = []; michael@0: let testURI = "https://www.mozilla.org/en-US/"; michael@0: let favIconLocation = michael@0: "http://example.org/tests/toolkit/components/places/tests/browser/favicon-normal32.png"; michael@0: let favIconURI = NetUtil.newURI(favIconLocation); michael@0: let favIconMimeType= "image/png"; michael@0: let pageURI; michael@0: let favIconData; michael@0: michael@0: function testOnWindow(aOptions, aCallback) { michael@0: whenNewWindowLoaded(aOptions, function(aWin) { michael@0: windowsToClose.push(aWin); michael@0: executeSoon(function() aCallback(aWin)); michael@0: }); michael@0: }; michael@0: michael@0: // This function is called after calling finish() on the test. michael@0: registerCleanupFunction(function() { michael@0: windowsToClose.forEach(function(aWin) { michael@0: aWin.close(); michael@0: }); michael@0: }); michael@0: michael@0: function getIconFile(aCallback) { michael@0: NetUtil.asyncFetch(favIconLocation, function(inputStream, status) { michael@0: if (!Components.isSuccessCode(status)) { michael@0: ok(false, "Could not get the icon file"); michael@0: // Handle error. michael@0: return; michael@0: } michael@0: michael@0: // Check the returned size versus the expected size. michael@0: let size = inputStream.available(); michael@0: favIconData = NetUtil.readInputStreamToString(inputStream, size); michael@0: is(size, favIconData.length, "Check correct icon size"); michael@0: // Check that the favicon loaded correctly before starting the actual tests. michael@0: is(favIconData.length, 344, "Check correct icon length (344)"); michael@0: michael@0: if (aCallback) { michael@0: aCallback(); michael@0: } else { michael@0: finish(); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: function testNormal(aWindow, aCallback) { michael@0: pageURI = NetUtil.newURI("http://example.com/normal"); michael@0: waitForFaviconChanged(pageURI, favIconURI, aWindow, michael@0: function testNormalCallback() { michael@0: checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow, michael@0: aCallback); michael@0: } michael@0: ); michael@0: michael@0: addVisits({uri: pageURI, transition: TRANSITION_TYPED}, aWindow, michael@0: function () { michael@0: aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, michael@0: favIconURI, true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); michael@0: } michael@0: ); michael@0: } michael@0: michael@0: function testAboutURIBookmarked(aWindow, aCallback) { michael@0: pageURI = NetUtil.newURI("about:testAboutURI_bookmarked"); michael@0: waitForFaviconChanged(pageURI, favIconURI, aWindow, michael@0: function testAboutURIBookmarkedCallback() { michael@0: checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow, michael@0: aCallback); michael@0: } michael@0: ); michael@0: michael@0: aWindow.PlacesUtils.bookmarks.insertBookmark( michael@0: aWindow.PlacesUtils.unfiledBookmarksFolderId, pageURI, michael@0: aWindow.PlacesUtils.bookmarks.DEFAULT_INDEX, pageURI.spec); michael@0: aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, favIconURI, michael@0: true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); michael@0: } michael@0: michael@0: function testPrivateBrowsingBookmarked(aWindow, aCallback) { michael@0: pageURI = NetUtil.newURI("http://example.com/privateBrowsing_bookmarked"); michael@0: waitForFaviconChanged(pageURI, favIconURI, aWindow, michael@0: function testPrivateBrowsingBookmarkedCallback() { michael@0: checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow, michael@0: aCallback); michael@0: } michael@0: ); michael@0: michael@0: aWindow.PlacesUtils.bookmarks.insertBookmark( michael@0: aWindow.PlacesUtils.unfiledBookmarksFolderId, pageURI, michael@0: aWindow.PlacesUtils.bookmarks.DEFAULT_INDEX, pageURI.spec); michael@0: aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, favIconURI, michael@0: true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_PRIVATE); michael@0: } michael@0: michael@0: function testDisabledHistoryBookmarked(aWindow, aCallback) { michael@0: pageURI = NetUtil.newURI("http://example.com/disabledHistory_bookmarked"); michael@0: waitForFaviconChanged(pageURI, favIconURI, aWindow, michael@0: function testDisabledHistoryBookmarkedCallback() { michael@0: checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow, michael@0: aCallback); michael@0: } michael@0: ); michael@0: michael@0: // Disable history while changing the favicon. michael@0: aWindow.Services.prefs.setBoolPref("places.history.enabled", false); michael@0: michael@0: aWindow.PlacesUtils.bookmarks.insertBookmark( michael@0: aWindow.PlacesUtils.unfiledBookmarksFolderId, pageURI, michael@0: aWindow.PlacesUtils.bookmarks.DEFAULT_INDEX, pageURI.spec); michael@0: aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, favIconURI, michael@0: true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); michael@0: michael@0: // The setAndFetchFaviconForPage function calls CanAddURI synchronously, thus michael@0: // we can set the preference back to true immediately. We don't clear the michael@0: // preference because not all products enable Places by default. michael@0: aWindow.Services.prefs.setBoolPref("places.history.enabled", true); michael@0: } michael@0: michael@0: getIconFile(function () { michael@0: testOnWindow({}, function(aWin) { michael@0: testNormal(aWin, function () { michael@0: testOnWindow({}, function(aWin) { michael@0: testAboutURIBookmarked(aWin, function () { michael@0: testOnWindow({private: true}, function(aWin) { michael@0: testPrivateBrowsingBookmarked(aWin, function () { michael@0: testOnWindow({}, function(aWin) { michael@0: testDisabledHistoryBookmarked(aWin, finish); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }