browser/devtools/scratchpad/test/browser_scratchpad_wrong_window_focus.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:c3293b89dc5a
1 /* vim:set ts=2 sw=2 sts=2 et: */
2 /* Any copyright is dedicated to the Public Domain.
3 http://creativecommons.org/publicdomain/zero/1.0/ */
4 /* Bug 661762 */
5
6
7 function test()
8 {
9 waitForExplicitFinish();
10
11 // To test for this bug we open a Scratchpad window, save its
12 // reference and then open another one. This way the first window
13 // loses its focus.
14 //
15 // Then we open a web console and execute a console.log statement
16 // from the first Scratch window (that's why we needed to save its
17 // reference).
18 //
19 // Then we wait for our message to appear in the console and click
20 // on the location link. After that we check which Scratchpad window
21 // is currently active (it should be the older one).
22
23 gBrowser.selectedTab = gBrowser.addTab();
24 gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
25 gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
26
27 openScratchpad(function () {
28 let sw = gScratchpadWindow;
29 let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
30
31 openScratchpad(function () {
32 let target = devtools.TargetFactory.forTab(gBrowser.selectedTab);
33 gDevTools.showToolbox(target, "webconsole").then((toolbox) => {
34 let hud = toolbox.getCurrentPanel().hud;
35 hud.jsterm.clearOutput(true);
36 testFocus(sw, hud);
37 });
38 });
39 });
40 }, true);
41
42 content.location = "data:text/html;charset=utf8,<p>test window focus for Scratchpad.";
43 }
44
45 function testFocus(sw, hud) {
46 let sp = sw.Scratchpad;
47
48 function onMessage(event, messages) {
49 let msg = [...messages][0];
50
51 var loc = msg.querySelector(".message-location");
52 ok(loc, "location element exists");
53 is(loc.textContent.trim(), sw.Scratchpad.uniqueName + ":1",
54 "location value is correct");
55
56 sw.addEventListener("focus", function onFocus() {
57 sw.removeEventListener("focus", onFocus, true);
58
59 let win = Services.wm.getMostRecentWindow("devtools:scratchpad");
60
61 ok(win, "there are active Scratchpad windows");
62 is(win.Scratchpad.uniqueName, sw.Scratchpad.uniqueName,
63 "correct window is in focus");
64
65 // gScratchpadWindow will be closed automatically but we need to
66 // close the second window ourselves.
67 sw.close();
68 finish();
69 }, true);
70
71 // Simulate a click on the "Scratchpad/N:1" link.
72 EventUtils.synthesizeMouse(loc, 2, 2, {}, hud.iframeWindow);
73 }
74
75 // Sending messages to web console is an asynchronous operation. That's
76 // why we have to setup an observer here.
77 hud.ui.once("messages-added", onMessage);
78
79 sp.setText("console.log('foo');");
80 sp.run().then(function ([selection, error, result]) {
81 is(selection, "console.log('foo');", "selection is correct");
82 is(error, undefined, "error is correct");
83 is(result.type, "undefined", "result is correct");
84 });
85 }

mercurial