|
1 <?xml version="1.0"?> |
|
2 <!-- This Source Code Form is subject to the terms of the Mozilla Public |
|
3 - License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> |
|
5 <!-- |
|
6 * This file tests the moz-anno protocol, which was added in Bug 316077 and how |
|
7 * it loads favicons. |
|
8 --> |
|
9 |
|
10 <window title="Favicon Annotation Protocol Test" |
|
11 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" |
|
12 onload="test();"> |
|
13 |
|
14 <script type="application/javascript" |
|
15 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> |
|
16 <script type="application/javascript" |
|
17 src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"/> |
|
18 <script type="application/javascript" |
|
19 src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> |
|
20 |
|
21 <script type="application/javascript"> |
|
22 <![CDATA[ |
|
23 |
|
24 const Cc = Components.classes; |
|
25 const Ci = Components.interfaces; |
|
26 const Cr = Components.results; |
|
27 |
|
28 let fs = Cc["@mozilla.org/browser/favicon-service;1"]. |
|
29 getService(Ci.nsIFaviconService); |
|
30 |
|
31 // Test descriptions that will be printed in the case of failure. |
|
32 let testDescriptions = [ |
|
33 "moz-anno URI with no data in the database loads default icon", |
|
34 "URI added to the database is properly loaded", |
|
35 ]; |
|
36 |
|
37 // URIs to load (will be compared with expectedURIs of the same index). |
|
38 let testURIs = [ |
|
39 "http://mozilla.org/2009/made-up-favicon/places-rocks/", |
|
40 "http://mozilla.org/should-be-barney/", |
|
41 ]; |
|
42 |
|
43 // URIs to load for expected results. |
|
44 let expectedURIs = [ |
|
45 fs.defaultFavicon.spec, |
|
46 "data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%06%00%00%00%1F%F3%FFa%00%00%00%04gAMA%00%00%AF%C87%05%8A%E9%00%00%00%19tEXtSoftware%00Adobe%20ImageReadyq%C9e%3C%00%00%01%D6IDATx%DAb%FC%FF%FF%3F%03%25%00%20%80%98%909%EF%DF%BFg%EF%EC%EC%FC%AD%AC%AC%FC%DF%95%91%F1%BF%89%89%C9%7F%20%FF%D7%EA%D5%AB%B7%DF%BBwO%16%9B%01%00%01%C4%00r%01%08%9F9s%C6%CD%D8%D8%F8%BF%0B%03%C3%FF3%40%BC%0A%88%EF%02q%1A%10%BB%40%F1%AAU%ABv%C1%D4%C30%40%00%81%89%993g%3E%06%1A%F6%3F%14%AA%11D%97%03%F1%7Fc%08%0D%E2%2B))%FD%17%04%89%A1%19%00%10%40%0C%D00%F8%0F3%00%C8%F8%BF%1B%E4%0Ac%88a%E5%60%17%19%FF%0F%0D%0D%05%1B%02v%D9%DD%BB%0A0%03%00%02%08%AC%B9%A3%A3%E3%17%03%D4v%90%01%EF%18%106%C3%0Cz%07%C5%BB%A1%DE%82y%07%20%80%A0%A6%08B%FCn%0C1%60%26%D4%20d%C3VA%C3%06%26%BE%0A%EA-%80%00%82%B9%E0%F7L4%0D%EF%90%F8%C6%60%2F%0A%82%BD%01%13%07%0700%D0%01%02%88%11%E4%02P%B41%DC%BB%C7%D0%014%0D%E8l%06W%20%06%BA%88%A1%1C%1AS%15%40%7C%16%CA6.%2Fgx%BFg%0F%83%CB%D9%B3%0C%7B%80%7C%80%00%02%BB%00%E8%9F%ED%20%1B%3A%A0%A6%9F%81%DA%DC%01%C5%B0%80%ED%80%FA%BF%BC%BC%FC%3F%83%12%90%9D%96%F6%1F%20%80%18%DE%BD%7B%C7%0E%8E%05AD%20%FEGr%A6%A0%A0%E0%7F%25P%80%02%9D%0F%D28%13%18%23%C6%C0%B0%02E%3D%C8%F5%00%01%04%8F%05P%A8%BA%40my%87%E4%12c%A8%8D%20%8B%D0%D3%00%08%03%04%10%9C%01R%E4%82d%3B%C8%A0%99%C6%90%90%C6%A5%19%84%01%02%08%9E%17%80%C9x%F7%7B%A0%DBVC%F9%A0%C0%5C%7D%16%2C%CE%00%F4%C6O%5C%99%09%20%800L%04y%A5%03%1A%95%A0%80%05%05%14.%DBA%18%20%80%18)%CD%CE%00%01%06%00%0C'%94%C7%C0k%C9%2C%00%00%00%00IEND%AEB%60%82", |
|
47 ]; |
|
48 |
|
49 |
|
50 /** |
|
51 * The event listener placed on our test windows used to determine when it is |
|
52 * safe to compare the two windows. |
|
53 */ |
|
54 let _results = []; |
|
55 function loadEventHandler() |
|
56 { |
|
57 _results.push(snapshotWindow(window)); |
|
58 |
|
59 loadNextTest(); |
|
60 } |
|
61 |
|
62 /** |
|
63 * This runs the comparison. |
|
64 */ |
|
65 function compareResults(aIndex, aImage1, aImage2) |
|
66 { |
|
67 let [correct, data1, data2] = compareSnapshots(aImage1, aImage2, true); |
|
68 SimpleTest.ok(correct, |
|
69 "Test '" + testDescriptions[aIndex] + "' matches expectations. " + |
|
70 "Data from window 1 is '" + data1 + "'. " + |
|
71 "Data from window 2 is '" + data2 + "'"); |
|
72 } |
|
73 |
|
74 /** |
|
75 * Loads the next set of URIs to compare against. |
|
76 */ |
|
77 let _counter = -1; |
|
78 function loadNextTest() |
|
79 { |
|
80 _counter++; |
|
81 // If we have no more tests, finish. |
|
82 if (_counter / 2 == testDescriptions.length) { |
|
83 for (let i = 0; i < _results.length; i = i + 2) |
|
84 compareResults(i / 2, _results[i], _results[i + 1]); |
|
85 |
|
86 SimpleTest.finish(); |
|
87 return; |
|
88 } |
|
89 |
|
90 let nextURI = function() { |
|
91 let index = Math.floor(_counter / 2); |
|
92 if ((_counter % 2) == 0) |
|
93 return "moz-anno:favicon:" + testURIs[index]; |
|
94 return expectedURIs[index]; |
|
95 } |
|
96 |
|
97 let img = document.getElementById("favicon"); |
|
98 img.setAttribute("src", nextURI()); |
|
99 } |
|
100 |
|
101 function test() |
|
102 { |
|
103 SimpleTest.waitForExplicitFinish(); |
|
104 let db = Cc["@mozilla.org/browser/nav-history-service;1"]. |
|
105 getService(Ci.nsPIPlacesDatabase). |
|
106 DBConnection; |
|
107 |
|
108 // Empty any old favicons |
|
109 db.executeSimpleSQL("DELETE FROM moz_favicons"); |
|
110 |
|
111 let ios = Cc["@mozilla.org/network/io-service;1"]. |
|
112 getService(Ci.nsIIOService); |
|
113 let uri = function(aSpec) { |
|
114 return ios.newURI(aSpec, null, null); |
|
115 }; |
|
116 |
|
117 let pageURI = uri("http://example.com/favicon_annotations"); |
|
118 let history = Cc["@mozilla.org/browser/history;1"] |
|
119 .getService(Ci.mozIAsyncHistory); |
|
120 history.updatePlaces( |
|
121 { |
|
122 uri: pageURI, |
|
123 visits: [{ transitionType: Ci.nsINavHistoryService.TRANSITION_TYPED, |
|
124 visitDate: Date.now() * 1000 |
|
125 }], |
|
126 }, |
|
127 { |
|
128 handleError: function UP_handleError() { |
|
129 ok(false, "Unexpected error in adding visit."); |
|
130 }, |
|
131 handleResult: function () {}, |
|
132 handleCompletion: function UP_handleCompletion() { |
|
133 // Set the favicon data. Note that the "moz-anno:" protocol requires |
|
134 // the favicon to be stored in the database, but the |
|
135 // replaceFaviconDataFromDataURL function will not save the favicon |
|
136 // unless it is associated with a page. Thus, we must associate the |
|
137 // icon with a page explicitly in order for it to be visible through |
|
138 // the protocol. |
|
139 fs.replaceFaviconDataFromDataURL(uri(testURIs[1]), expectedURIs[1], |
|
140 (Date.now() + 60 * 60 * 24 * 1000) * 1000); |
|
141 fs.setAndFetchFaviconForPage(pageURI, uri(testURIs[1]), true, |
|
142 fs.FAVICON_LOAD_NON_PRIVATE); |
|
143 |
|
144 // And start our test process. |
|
145 loadNextTest(); |
|
146 } |
|
147 } |
|
148 ); |
|
149 |
|
150 |
|
151 } |
|
152 |
|
153 ]]> |
|
154 </script> |
|
155 |
|
156 <body xmlns="http://www.w3.org/1999/xhtml"> |
|
157 <img id="favicon" onload="loadEventHandler();"/> |
|
158 <p id="display"></p> |
|
159 <div id="content" style="display:none;"></div> |
|
160 <pre id="test"></pre> |
|
161 </body> |
|
162 </window> |