|
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 const HTML = "<html>\ |
|
8 <body>\ |
|
9 <div>foo</div>\ |
|
10 <div>and</div>\ |
|
11 <textarea>noodles</textarea>\ |
|
12 </body>\ |
|
13 </html>"; |
|
14 |
|
15 const URL = "data:text/html;charset=utf-8," + encodeURIComponent(HTML); |
|
16 |
|
17 const FRAME_HTML = "<iframe src='" + URL + "'><iframe>"; |
|
18 const FRAME_URL = "data:text/html;charset=utf-8," + encodeURIComponent(FRAME_HTML); |
|
19 |
|
20 const { defer } = require("sdk/core/promise"); |
|
21 const { browserWindows } = require("sdk/windows"); |
|
22 const tabs = require("sdk/tabs"); |
|
23 const { setTabURL, getActiveTab, getTabContentWindow, closeTab, getTabs, |
|
24 getTabTitle } = require("sdk/tabs/utils"); |
|
25 const { getMostRecentBrowserWindow, isFocused } = require("sdk/window/utils"); |
|
26 const { open: openNewWindow, close: closeWindow, focus } = require("sdk/window/helpers"); |
|
27 const { Loader } = require("sdk/test/loader"); |
|
28 const { merge } = require("sdk/util/object"); |
|
29 const { isPrivate } = require("sdk/private-browsing"); |
|
30 |
|
31 // General purpose utility functions |
|
32 |
|
33 /** |
|
34 * Opens the url given and return a promise, that will be resolved with the |
|
35 * content window when the document is ready. |
|
36 * |
|
37 * I believe this approach could be useful in most of our unit test, that |
|
38 * requires to open a tab and need to access to its content. |
|
39 */ |
|
40 function open(url, options) { |
|
41 let { promise, resolve } = defer(); |
|
42 |
|
43 if (options && typeof(options) === "object") { |
|
44 openNewWindow("", { |
|
45 features: merge({ toolbar: true }, options) |
|
46 }).then(function(chromeWindow) { |
|
47 if (isPrivate(chromeWindow) !== !!options.private) |
|
48 throw new Error("Window should have Private set to " + !!options.private); |
|
49 |
|
50 let tab = getActiveTab(chromeWindow); |
|
51 |
|
52 tab.linkedBrowser.addEventListener("load", function ready(event) { |
|
53 let { document } = getTabContentWindow(tab); |
|
54 |
|
55 if (document.readyState === "complete" && document.URL === url) { |
|
56 this.removeEventListener(event.type, ready); |
|
57 |
|
58 if (options.title) |
|
59 document.title = options.title; |
|
60 |
|
61 resolve(document.defaultView); |
|
62 } |
|
63 }, true); |
|
64 |
|
65 setTabURL(tab, url); |
|
66 }); |
|
67 |
|
68 return promise; |
|
69 }; |
|
70 |
|
71 tabs.open({ |
|
72 url: url, |
|
73 onReady: function(tab) { |
|
74 // Unfortunately there is no way to get a XUL Tab from SDK Tab on Firefox, |
|
75 // only on Fennec. We should implement `tabNS` also on Firefox in order |
|
76 // to have that. |
|
77 |
|
78 // Here we assuming that the most recent browser window is the one we're |
|
79 // doing the test, and the active tab is the one we just opened. |
|
80 let window = getTabContentWindow(getActiveTab(getMostRecentBrowserWindow())); |
|
81 |
|
82 resolve(window); |
|
83 } |
|
84 }); |
|
85 |
|
86 return promise; |
|
87 }; |
|
88 |
|
89 /** |
|
90 * Close the Active Tab |
|
91 */ |
|
92 function close(window) { |
|
93 let { promise, resolve } = defer(); |
|
94 |
|
95 if (window && typeof(window.close) === "function") { |
|
96 closeWindow(window).then(function() resolve()); |
|
97 } |
|
98 else { |
|
99 // Here we assuming that the most recent browser window is the one we're |
|
100 // doing the test, and the active tab is the one we just opened. |
|
101 closeTab(getActiveTab(getMostRecentBrowserWindow())); |
|
102 resolve(); |
|
103 } |
|
104 |
|
105 return promise; |
|
106 } |
|
107 |
|
108 /** |
|
109 * Reload the window given and return a promise, that will be resolved with the |
|
110 * content window after a small delay. |
|
111 */ |
|
112 function reload(window) { |
|
113 let { promise, resolve } = defer(); |
|
114 |
|
115 // Here we assuming that the most recent browser window is the one we're |
|
116 // doing the test, and the active tab is the one we just opened. |
|
117 let tab = tabs.activeTab; |
|
118 |
|
119 tab.once("ready", function () { |
|
120 resolve(window); |
|
121 }); |
|
122 |
|
123 window.location.reload(true); |
|
124 |
|
125 return promise; |
|
126 } |
|
127 |
|
128 // Selection's unit test utility function |
|
129 |
|
130 /** |
|
131 * Select the first div in the page, adding the range to the selection. |
|
132 */ |
|
133 function selectFirstDiv(window) { |
|
134 let div = window.document.querySelector("div"); |
|
135 let selection = window.getSelection(); |
|
136 let range = window.document.createRange(); |
|
137 |
|
138 if (selection.rangeCount > 0) |
|
139 selection.removeAllRanges(); |
|
140 |
|
141 range.selectNode(div); |
|
142 selection.addRange(range); |
|
143 |
|
144 return window; |
|
145 } |
|
146 |
|
147 /** |
|
148 * Select all divs in the page, adding the ranges to the selection. |
|
149 */ |
|
150 function selectAllDivs(window) { |
|
151 let divs = window.document.getElementsByTagName("div"); |
|
152 let selection = window.getSelection(); |
|
153 |
|
154 if (selection.rangeCount > 0) |
|
155 selection.removeAllRanges(); |
|
156 |
|
157 for (let i = 0; i < divs.length; i++) { |
|
158 let range = window.document.createRange(); |
|
159 |
|
160 range.selectNode(divs[i]); |
|
161 selection.addRange(range); |
|
162 } |
|
163 |
|
164 return window; |
|
165 } |
|
166 |
|
167 /** |
|
168 * Select the textarea content |
|
169 */ |
|
170 function selectTextarea(window) { |
|
171 let selection = window.getSelection(); |
|
172 let textarea = window.document.querySelector("textarea"); |
|
173 |
|
174 if (selection.rangeCount > 0) |
|
175 selection.removeAllRanges(); |
|
176 |
|
177 textarea.setSelectionRange(0, textarea.value.length); |
|
178 textarea.focus(); |
|
179 |
|
180 return window; |
|
181 } |
|
182 |
|
183 /** |
|
184 * Select the content of the first div |
|
185 */ |
|
186 function selectContentFirstDiv(window) { |
|
187 let div = window.document.querySelector("div"); |
|
188 let selection = window.getSelection(); |
|
189 let range = window.document.createRange(); |
|
190 |
|
191 if (selection.rangeCount > 0) |
|
192 selection.removeAllRanges(); |
|
193 |
|
194 range.selectNodeContents(div); |
|
195 selection.addRange(range); |
|
196 |
|
197 return window; |
|
198 } |
|
199 |
|
200 /** |
|
201 * Dispatch the selection event for the selection listener added by |
|
202 * `nsISelectionPrivate.addSelectionListener` |
|
203 */ |
|
204 function dispatchSelectionEvent(window) { |
|
205 // We modify the selection in order to dispatch the selection's event, by |
|
206 // contract the selection by one character. So if the text selected is "foo" |
|
207 // will be "fo". |
|
208 window.getSelection().modify("extend", "backward", "character"); |
|
209 |
|
210 return window; |
|
211 } |
|
212 |
|
213 /** |
|
214 * Dispatch the selection event for the selection listener added by |
|
215 * `window.onselect` / `window.addEventListener` |
|
216 */ |
|
217 function dispatchOnSelectEvent(window) { |
|
218 let { document } = window; |
|
219 let textarea = document.querySelector("textarea"); |
|
220 let event = document.createEvent("UIEvents"); |
|
221 |
|
222 event.initUIEvent("select", true, true, window, 1); |
|
223 |
|
224 textarea.dispatchEvent(event); |
|
225 |
|
226 return window; |
|
227 } |
|
228 |
|
229 // Test cases |
|
230 |
|
231 exports["test PWPB Selection Listener"] = function(assert, done) { |
|
232 let loader = Loader(module); |
|
233 let selection = loader.require("sdk/selection"); |
|
234 |
|
235 open(URL, {private: true, title: "PWPB Selection Listener"}). |
|
236 then(function(window) { |
|
237 selection.once("select", function() { |
|
238 assert.equal(browserWindows.length, 2, "there should be only two windows open."); |
|
239 assert.equal(getTabs().length, 2, "there should be only two tabs open: '" + |
|
240 getTabs().map(function(tab) getTabTitle(tab)).join("', '") + |
|
241 "'." |
|
242 ); |
|
243 |
|
244 // window should be focused, but force the focus anyhow.. see bug 841823 |
|
245 focus(window).then(function() { |
|
246 // check state of window |
|
247 assert.ok(isFocused(window), "the window is focused"); |
|
248 assert.ok(isPrivate(window), "the window should be a private window"); |
|
249 |
|
250 assert.equal(selection.text, "fo"); |
|
251 |
|
252 close(window). |
|
253 then(loader.unload). |
|
254 then(done). |
|
255 then(null, assert.fail); |
|
256 }); |
|
257 }); |
|
258 return window; |
|
259 }). |
|
260 then(selectContentFirstDiv). |
|
261 then(dispatchSelectionEvent). |
|
262 then(null, assert.fail); |
|
263 }; |
|
264 |
|
265 exports["test PWPB Textarea OnSelect Listener"] = function(assert, done) { |
|
266 let loader = Loader(module); |
|
267 let selection = loader.require("sdk/selection"); |
|
268 |
|
269 open(URL, {private: true, title: "PWPB OnSelect Listener"}). |
|
270 then(function(window) { |
|
271 selection.once("select", function() { |
|
272 assert.equal(browserWindows.length, 2, "there should be only two windows open."); |
|
273 assert.equal(getTabs().length, 2, "there should be only two tabs open: '" + |
|
274 getTabs().map(function(tab) getTabTitle(tab)).join("', '") + |
|
275 "'." |
|
276 ); |
|
277 |
|
278 // window should be focused, but force the focus anyhow.. see bug 841823 |
|
279 focus(window).then(function() { |
|
280 assert.equal(selection.text, "noodles"); |
|
281 |
|
282 close(window). |
|
283 then(loader.unload). |
|
284 then(done). |
|
285 then(null, assert.fail); |
|
286 }); |
|
287 }); |
|
288 return window; |
|
289 }). |
|
290 then(selectTextarea). |
|
291 then(dispatchOnSelectEvent). |
|
292 then(null, assert.fail); |
|
293 }; |
|
294 |
|
295 exports["test PWPB Single DOM Selection"] = function(assert, done) { |
|
296 let loader = Loader(module); |
|
297 let selection = loader.require("sdk/selection"); |
|
298 |
|
299 open(URL, {private: true, title: "PWPB Single DOM Selection"}). |
|
300 then(selectFirstDiv). |
|
301 then(focus).then(function() { |
|
302 assert.equal(selection.isContiguous, true, |
|
303 "selection.isContiguous with single DOM Selection works."); |
|
304 |
|
305 assert.equal(selection.text, "foo", |
|
306 "selection.text with single DOM Selection works."); |
|
307 |
|
308 assert.equal(selection.html, "<div>foo</div>", |
|
309 "selection.html with single DOM Selection works."); |
|
310 |
|
311 let selectionCount = 0; |
|
312 for each (let sel in selection) { |
|
313 selectionCount++; |
|
314 |
|
315 assert.equal(sel.text, "foo", |
|
316 "iterable selection.text with single DOM Selection works."); |
|
317 |
|
318 assert.equal(sel.html, "<div>foo</div>", |
|
319 "iterable selection.html with single DOM Selection works."); |
|
320 } |
|
321 |
|
322 assert.equal(selectionCount, 1, |
|
323 "One iterable selection"); |
|
324 }).then(close).then(loader.unload).then(done).then(null, assert.fail); |
|
325 } |
|
326 |
|
327 exports["test PWPB Textarea Selection"] = function(assert, done) { |
|
328 let loader = Loader(module); |
|
329 let selection = loader.require("sdk/selection"); |
|
330 |
|
331 open(URL, {private: true, title: "PWPB Textarea Listener"}). |
|
332 then(selectTextarea). |
|
333 then(focus). |
|
334 then(function() { |
|
335 |
|
336 assert.equal(selection.isContiguous, true, |
|
337 "selection.isContiguous with Textarea Selection works."); |
|
338 |
|
339 assert.equal(selection.text, "noodles", |
|
340 "selection.text with Textarea Selection works."); |
|
341 |
|
342 assert.strictEqual(selection.html, null, |
|
343 "selection.html with Textarea Selection works."); |
|
344 |
|
345 let selectionCount = 0; |
|
346 for each (let sel in selection) { |
|
347 selectionCount++; |
|
348 |
|
349 assert.equal(sel.text, "noodles", |
|
350 "iterable selection.text with Textarea Selection works."); |
|
351 |
|
352 assert.strictEqual(sel.html, null, |
|
353 "iterable selection.html with Textarea Selection works."); |
|
354 } |
|
355 |
|
356 assert.equal(selectionCount, 1, |
|
357 "One iterable selection"); |
|
358 }).then(close).then(loader.unload).then(done).then(null, assert.fail); |
|
359 }; |
|
360 |
|
361 exports["test PWPB Set HTML in Multiple DOM Selection"] = function(assert, done) { |
|
362 let loader = Loader(module); |
|
363 let selection = loader.require("sdk/selection"); |
|
364 |
|
365 open(URL, {private: true, title: "PWPB Set HTML in Multiple DOM Selection"}). |
|
366 then(selectAllDivs). |
|
367 then(focus). |
|
368 then(function() { |
|
369 let html = "<span>b<b>a</b>r</span>"; |
|
370 |
|
371 let expectedText = ["bar", "and"]; |
|
372 let expectedHTML = [html, "<div>and</div>"]; |
|
373 |
|
374 selection.html = html; |
|
375 |
|
376 assert.equal(selection.text, expectedText[0], |
|
377 "set selection.text with DOM Selection works."); |
|
378 |
|
379 assert.equal(selection.html, expectedHTML[0], |
|
380 "selection.html with DOM Selection works."); |
|
381 |
|
382 let selectionCount = 0; |
|
383 for each (let sel in selection) { |
|
384 |
|
385 assert.equal(sel.text, expectedText[selectionCount], |
|
386 "iterable selection.text with multiple DOM Selection works."); |
|
387 |
|
388 assert.equal(sel.html, expectedHTML[selectionCount], |
|
389 "iterable selection.html with multiple DOM Selection works."); |
|
390 |
|
391 selectionCount++; |
|
392 } |
|
393 |
|
394 assert.equal(selectionCount, 2, |
|
395 "Two iterable selections"); |
|
396 }).then(close).then(loader.unload).then(done).then(null, assert.fail); |
|
397 }; |
|
398 |
|
399 exports["test PWPB Set Text in Textarea Selection"] = function(assert, done) { |
|
400 let loader = Loader(module); |
|
401 let selection = loader.require("sdk/selection"); |
|
402 |
|
403 open(URL, {private: true, title: "test PWPB Set Text in Textarea Selection"}). |
|
404 then(selectTextarea). |
|
405 then(focus). |
|
406 then(function() { |
|
407 |
|
408 let text = "bar"; |
|
409 |
|
410 selection.text = text; |
|
411 |
|
412 assert.equal(selection.text, text, |
|
413 "set selection.text with Textarea Selection works."); |
|
414 |
|
415 assert.strictEqual(selection.html, null, |
|
416 "selection.html with Textarea Selection works."); |
|
417 |
|
418 let selectionCount = 0; |
|
419 for each (let sel in selection) { |
|
420 selectionCount++; |
|
421 |
|
422 assert.equal(sel.text, text, |
|
423 "iterable selection.text with Textarea Selection works."); |
|
424 |
|
425 assert.strictEqual(sel.html, null, |
|
426 "iterable selection.html with Textarea Selection works."); |
|
427 } |
|
428 |
|
429 assert.equal(selectionCount, 1, |
|
430 "One iterable selection"); |
|
431 |
|
432 }).then(close).then(loader.unload).then(done).then(null, assert.fail); |
|
433 }; |
|
434 |
|
435 // If the platform doesn't support the PBPW, we're replacing PBPW tests |
|
436 if (!require("sdk/private-browsing/utils").isWindowPBSupported) { |
|
437 module.exports = { |
|
438 "test PBPW Unsupported": function Unsupported (assert) { |
|
439 assert.pass("Private Window Per Browsing is not supported on this platform."); |
|
440 } |
|
441 } |
|
442 } |
|
443 |
|
444 // If the module doesn't support the app we're being run in, require() will |
|
445 // throw. In that case, remove all tests above from exports, and add one dummy |
|
446 // test that passes. |
|
447 try { |
|
448 require("sdk/selection"); |
|
449 } |
|
450 catch (err) { |
|
451 if (!/^Unsupported Application/.test(err.message)) |
|
452 throw err; |
|
453 |
|
454 module.exports = { |
|
455 "test Unsupported Application": function Unsupported (assert) { |
|
456 assert.pass(err.message); |
|
457 } |
|
458 } |
|
459 } |