michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: /* Bug 661762 */ michael@0: michael@0: michael@0: function test() michael@0: { michael@0: waitForExplicitFinish(); michael@0: michael@0: // To test for this bug we open a Scratchpad window, save its michael@0: // reference and then open another one. This way the first window michael@0: // loses its focus. michael@0: // michael@0: // Then we open a web console and execute a console.log statement michael@0: // from the first Scratch window (that's why we needed to save its michael@0: // reference). michael@0: // michael@0: // Then we wait for our message to appear in the console and click michael@0: // on the location link. After that we check which Scratchpad window michael@0: // is currently active (it should be the older one). michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function onLoad() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); michael@0: michael@0: openScratchpad(function () { michael@0: let sw = gScratchpadWindow; michael@0: let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}); michael@0: michael@0: openScratchpad(function () { michael@0: let target = devtools.TargetFactory.forTab(gBrowser.selectedTab); michael@0: gDevTools.showToolbox(target, "webconsole").then((toolbox) => { michael@0: let hud = toolbox.getCurrentPanel().hud; michael@0: hud.jsterm.clearOutput(true); michael@0: testFocus(sw, hud); michael@0: }); michael@0: }); michael@0: }); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html;charset=utf8,

test window focus for Scratchpad."; michael@0: } michael@0: michael@0: function testFocus(sw, hud) { michael@0: let sp = sw.Scratchpad; michael@0: michael@0: function onMessage(event, messages) { michael@0: let msg = [...messages][0]; michael@0: michael@0: var loc = msg.querySelector(".message-location"); michael@0: ok(loc, "location element exists"); michael@0: is(loc.textContent.trim(), sw.Scratchpad.uniqueName + ":1", michael@0: "location value is correct"); michael@0: michael@0: sw.addEventListener("focus", function onFocus() { michael@0: sw.removeEventListener("focus", onFocus, true); michael@0: michael@0: let win = Services.wm.getMostRecentWindow("devtools:scratchpad"); michael@0: michael@0: ok(win, "there are active Scratchpad windows"); michael@0: is(win.Scratchpad.uniqueName, sw.Scratchpad.uniqueName, michael@0: "correct window is in focus"); michael@0: michael@0: // gScratchpadWindow will be closed automatically but we need to michael@0: // close the second window ourselves. michael@0: sw.close(); michael@0: finish(); michael@0: }, true); michael@0: michael@0: // Simulate a click on the "Scratchpad/N:1" link. michael@0: EventUtils.synthesizeMouse(loc, 2, 2, {}, hud.iframeWindow); michael@0: } michael@0: michael@0: // Sending messages to web console is an asynchronous operation. That's michael@0: // why we have to setup an observer here. michael@0: hud.ui.once("messages-added", onMessage); michael@0: michael@0: sp.setText("console.log('foo');"); michael@0: sp.run().then(function ([selection, error, result]) { michael@0: is(selection, "console.log('foo');", "selection is correct"); michael@0: is(error, undefined, "error is correct"); michael@0: is(result.type, "undefined", "result is correct"); michael@0: }); michael@0: }