|
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 'use strict'; |
|
6 |
|
7 module.metadata = { |
|
8 'engines': { |
|
9 'Firefox': '*' |
|
10 } |
|
11 }; |
|
12 |
|
13 const { Cc, Ci, Cu } = require('chrome'); |
|
14 const { getFavicon } = require('sdk/places/favicon'); |
|
15 const tabs = require('sdk/tabs'); |
|
16 const open = tabs.open; |
|
17 const port = 8099; |
|
18 const host = 'http://localhost:' + port; |
|
19 const { onFaviconChange, serve, binFavicon } = require('../favicon-helpers'); |
|
20 const { once } = require('sdk/system/events'); |
|
21 const { defer } = require('sdk/core/promise'); |
|
22 const { resetPlaces } = require('../places-helper'); |
|
23 const faviconService = Cc["@mozilla.org/browser/favicon-service;1"]. |
|
24 getService(Ci.nsIFaviconService); |
|
25 |
|
26 exports.testStringGetFaviconCallbackSuccess = function (assert, done) { |
|
27 let name = 'callbacksuccess' |
|
28 let srv = makeServer(name); |
|
29 let url = host + '/' + name + '.html'; |
|
30 let favicon = host + '/' + name + '.ico'; |
|
31 let tab; |
|
32 |
|
33 onFaviconChange(url, function (faviconUrl) { |
|
34 getFavicon(url, function (url) { |
|
35 assert.equal(favicon, url, 'Callback returns correct favicon url'); |
|
36 complete(tab, srv, done); |
|
37 }); |
|
38 }); |
|
39 |
|
40 open({ |
|
41 url: url, |
|
42 onOpen: function (newTab) tab = newTab, |
|
43 inBackground: true |
|
44 }); |
|
45 }; |
|
46 |
|
47 exports.testStringGetFaviconCallbackFailure = function (assert, done) { |
|
48 let name = 'callbackfailure'; |
|
49 let srv = makeServer(name); |
|
50 let url = host + '/' + name + '.html'; |
|
51 let tab; |
|
52 |
|
53 waitAndExpire(url).then(function () { |
|
54 getFavicon(url, function (url) { |
|
55 assert.equal(url, null, 'Callback returns null'); |
|
56 complete(tab, srv, done); |
|
57 }); |
|
58 }); |
|
59 |
|
60 open({ |
|
61 url: url, |
|
62 onOpen: function (newTab) tab = newTab, |
|
63 inBackground: true |
|
64 }); |
|
65 }; |
|
66 |
|
67 exports.testStringGetFaviconPromiseSuccess = function (assert, done) { |
|
68 let name = 'promisesuccess' |
|
69 let srv = makeServer(name); |
|
70 let url = host + '/' + name + '.html'; |
|
71 let favicon = host + '/' + name + '.ico'; |
|
72 let tab; |
|
73 |
|
74 onFaviconChange(url, function (faviconUrl) { |
|
75 getFavicon(url).then(function (url) { |
|
76 assert.equal(url, favicon, 'Callback returns null'); |
|
77 }, function (err) { |
|
78 assert.fail('Reject should not be called'); |
|
79 }).then(complete.bind(null, tab, srv, done)); |
|
80 }); |
|
81 |
|
82 open({ |
|
83 url: url, |
|
84 onOpen: function (newTab) tab = newTab, |
|
85 inBackground: true |
|
86 }); |
|
87 }; |
|
88 |
|
89 exports.testStringGetFaviconPromiseFailure = function (assert, done) { |
|
90 let name = 'promisefailure' |
|
91 let srv = makeServer(name); |
|
92 let url = host + '/' + name + '.html'; |
|
93 let tab; |
|
94 |
|
95 waitAndExpire(url).then(function () { |
|
96 getFavicon(url).then(invalidResolve(assert), validReject(assert, 'expired url')) |
|
97 .then(complete.bind(null, tab, srv, done)); |
|
98 }); |
|
99 |
|
100 open({ |
|
101 url: url, |
|
102 onOpen: function (newTab) tab = newTab, |
|
103 inBackground: true |
|
104 }); |
|
105 }; |
|
106 |
|
107 exports.testTabsGetFaviconPromiseSuccess = function (assert, done) { |
|
108 let name = 'tabs-success' |
|
109 let srv = makeServer(name); |
|
110 let url = host + '/' + name + '.html'; |
|
111 let favicon = host + '/' + name + '.ico'; |
|
112 let tab; |
|
113 |
|
114 onFaviconChange(url, function () { |
|
115 getFavicon(tab).then(function (url) { |
|
116 assert.equal(url, favicon, "getFavicon should return url for tab"); |
|
117 complete(tab, srv, done); |
|
118 }); |
|
119 }); |
|
120 |
|
121 open({ |
|
122 url: url, |
|
123 onOpen: function (newTab) tab = newTab, |
|
124 inBackground: true |
|
125 }); |
|
126 }; |
|
127 |
|
128 |
|
129 exports.testTabsGetFaviconPromiseFailure = function (assert, done) { |
|
130 let name = 'tabs-failure' |
|
131 let srv = makeServer(name); |
|
132 let url = host + '/' + name + '.html'; |
|
133 let tab; |
|
134 |
|
135 waitAndExpire(url).then(function () { |
|
136 getFavicon(tab).then(invalidResolve(assert), validReject(assert, 'expired tab')) |
|
137 .then(complete.bind(null, tab, srv, done)); |
|
138 }); |
|
139 |
|
140 open({ |
|
141 url: url, |
|
142 onOpen: function (newTab) tab = newTab, |
|
143 inBackground: true |
|
144 }); |
|
145 }; |
|
146 |
|
147 exports.testRejects = function (assert, done) { |
|
148 getFavicon({}) |
|
149 .then(invalidResolve(assert), validReject(assert, 'Object')) |
|
150 .then(() => getFavicon(null)) |
|
151 .then(invalidResolve(assert), validReject(assert, 'null')) |
|
152 .then(() => getFavicon(undefined)) |
|
153 .then(invalidResolve(assert), validReject(assert, 'undefined')) |
|
154 .then(() => getFavicon([])) |
|
155 .then(invalidResolve(assert), validReject(assert, 'Array')) |
|
156 .catch(assert.fail).then(done); |
|
157 }; |
|
158 |
|
159 function invalidResolve (assert) { |
|
160 return function () assert.fail('Promise should not be resolved successfully'); |
|
161 } |
|
162 |
|
163 function validReject (assert, name) { |
|
164 return function () assert.pass(name + ' correctly rejected'); |
|
165 } |
|
166 |
|
167 function makeServer (name) { |
|
168 return serve({name: name, favicon: binFavicon, port: port, host: host}); |
|
169 } |
|
170 |
|
171 function waitAndExpire (url) { |
|
172 let deferred = defer(); |
|
173 onFaviconChange(url, function (faviconUrl) { |
|
174 once('places-favicons-expired', function () { |
|
175 deferred.resolve(); |
|
176 }); |
|
177 faviconService.expireAllFavicons(); |
|
178 }); |
|
179 return deferred.promise; |
|
180 } |
|
181 |
|
182 function complete(tab, srv, done) { |
|
183 tab.close(function () { |
|
184 resetPlaces(() => { |
|
185 srv.stop(done); |
|
186 }); |
|
187 }); |
|
188 } |