toolkit/components/places/tests/browser/browser_favicon_setAndFetchFaviconForPage.js

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

mercurial