1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_wrong_window_focus.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 +/* Bug 661762 */ 1.8 + 1.9 + 1.10 +function test() 1.11 +{ 1.12 + waitForExplicitFinish(); 1.13 + 1.14 + // To test for this bug we open a Scratchpad window, save its 1.15 + // reference and then open another one. This way the first window 1.16 + // loses its focus. 1.17 + // 1.18 + // Then we open a web console and execute a console.log statement 1.19 + // from the first Scratch window (that's why we needed to save its 1.20 + // reference). 1.21 + // 1.22 + // Then we wait for our message to appear in the console and click 1.23 + // on the location link. After that we check which Scratchpad window 1.24 + // is currently active (it should be the older one). 1.25 + 1.26 + gBrowser.selectedTab = gBrowser.addTab(); 1.27 + gBrowser.selectedBrowser.addEventListener("load", function onLoad() { 1.28 + gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); 1.29 + 1.30 + openScratchpad(function () { 1.31 + let sw = gScratchpadWindow; 1.32 + let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}); 1.33 + 1.34 + openScratchpad(function () { 1.35 + let target = devtools.TargetFactory.forTab(gBrowser.selectedTab); 1.36 + gDevTools.showToolbox(target, "webconsole").then((toolbox) => { 1.37 + let hud = toolbox.getCurrentPanel().hud; 1.38 + hud.jsterm.clearOutput(true); 1.39 + testFocus(sw, hud); 1.40 + }); 1.41 + }); 1.42 + }); 1.43 + }, true); 1.44 + 1.45 + content.location = "data:text/html;charset=utf8,<p>test window focus for Scratchpad."; 1.46 +} 1.47 + 1.48 +function testFocus(sw, hud) { 1.49 + let sp = sw.Scratchpad; 1.50 + 1.51 + function onMessage(event, messages) { 1.52 + let msg = [...messages][0]; 1.53 + 1.54 + var loc = msg.querySelector(".message-location"); 1.55 + ok(loc, "location element exists"); 1.56 + is(loc.textContent.trim(), sw.Scratchpad.uniqueName + ":1", 1.57 + "location value is correct"); 1.58 + 1.59 + sw.addEventListener("focus", function onFocus() { 1.60 + sw.removeEventListener("focus", onFocus, true); 1.61 + 1.62 + let win = Services.wm.getMostRecentWindow("devtools:scratchpad"); 1.63 + 1.64 + ok(win, "there are active Scratchpad windows"); 1.65 + is(win.Scratchpad.uniqueName, sw.Scratchpad.uniqueName, 1.66 + "correct window is in focus"); 1.67 + 1.68 + // gScratchpadWindow will be closed automatically but we need to 1.69 + // close the second window ourselves. 1.70 + sw.close(); 1.71 + finish(); 1.72 + }, true); 1.73 + 1.74 + // Simulate a click on the "Scratchpad/N:1" link. 1.75 + EventUtils.synthesizeMouse(loc, 2, 2, {}, hud.iframeWindow); 1.76 + } 1.77 + 1.78 + // Sending messages to web console is an asynchronous operation. That's 1.79 + // why we have to setup an observer here. 1.80 + hud.ui.once("messages-added", onMessage); 1.81 + 1.82 + sp.setText("console.log('foo');"); 1.83 + sp.run().then(function ([selection, error, result]) { 1.84 + is(selection, "console.log('foo');", "selection is correct"); 1.85 + is(error, undefined, "error is correct"); 1.86 + is(result.type, "undefined", "result is correct"); 1.87 + }); 1.88 +}