Skip to content

Commit

Permalink
Bug 1304003 - Part 1: Rewrite browser_bug_865871_variables_view_close…
Browse files Browse the repository at this point in the history
…_on_esc_key.js. r=bgrins

MozReview-Commit-ID: DHjTBSru0uu
  • Loading branch information
linclark committed Sep 21, 2016
1 parent e164a44 commit 8aed77e
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 43 deletions.
18 changes: 12 additions & 6 deletions devtools/client/webconsole/jsterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ JSTerm.prototype = {
if (this.hud.NEW_CONSOLE_OUTPUT_ENABLED) {
this.hud.newConsoleOutput.dispatchMessageAdd(response);
// @TODO figure out what to do about the callback.
callback && callback();
return;
}
let msg = new Messages.JavaScriptEvalOutput(response,
Expand Down Expand Up @@ -423,12 +424,17 @@ JSTerm.prototype = {
*/
execute: function (executeString, callback) {
let deferred = promise.defer();
let resultCallback = function (msg) {
deferred.resolve(msg);
if (callback) {
callback(msg);
}
};
let resultCallback;
if (this.hud.NEW_CONSOLE_OUTPUT_ENABLED) {
resultCallback = () => deferred.resolve();
} else {
resultCallback = (msg) => {
deferred.resolve(msg);
if (callback) {
callback(msg);
}
};
}

// attempt to execute the content of the inputNode
executeString = executeString || this.getInputValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ const {
DOM: dom,
PropTypes
} = require("devtools/client/shared/vendor/react");
const { ConsoleCommand: ConsoleCommandType } = require("devtools/client/webconsole/new-console-output/types");
const MessageIcon = createFactory(require("devtools/client/webconsole/new-console-output/components/message-icon").MessageIcon);

ConsoleCommand.displayName = "ConsoleCommand";

ConsoleCommand.propTypes = {
message: PropTypes.instanceOf(ConsoleCommandType).isRequired,
message: PropTypes.object.isRequired,
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {openVariablesView} = require("devtools/client/webconsole/new-console-outp
VariablesViewLink.displayName = "VariablesViewLink";

VariablesViewLink.propTypes = {
object: PropTypes.object.required
object: PropTypes.object.isRequired
};

function VariablesViewLink(props) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ support-files =
test-console.html

[browser_webconsole_init.js]
[browser_webconsole_vview_close_on_esc_key.js]
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,3 @@ add_task(function* () {

yield receievedMessages;
});

/**
* Wait for messages in the web console output, resolving once they are receieved.
*
* @param object options
* - hud: the webconsole
* - messages: Array[Object]. An array of messages to match. Current supported options:
* - text: Exact text match in .message-body
*/
function waitForMessages({ hud, messages }) {
return new Promise(resolve => {
let numMatched = 0;
let receivedLog = hud.ui.on("new-messages", function messagesReceieved(e, newMessage) {
for (let message of messages) {
if (message.matched) {
continue;
}

if (newMessage.node.querySelector(".message-body").textContent == message.text) {
numMatched++;
message.matched = true;
info("Matched a message with text: " + message.text + ", still waiting for " + (messages.length - numMatched) + " messages");
}

if (numMatched === messages.length) {
hud.ui.off("new-messages", messagesReceieved);
resolve();
return;
}
}
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */

// Check that the variables view sidebar can be closed by pressing Escape in the
// web console.

"use strict";

const TEST_URI =
"data:text/html;charset=utf8,<script>let fooObj = {testProp: 'testValue'}</script>";

add_task(function* () {
let hud = yield openNewTabAndConsole(TEST_URI);
let jsterm = hud.jsterm;
let vview;

yield openSidebar("fooObj", 'testProp: "testValue"');
vview.window.focus();

let sidebarClosed = jsterm.once("sidebar-closed");
EventUtils.synthesizeKey("VK_ESCAPE", {});
yield sidebarClosed;

function* openSidebar(objName, expectedText) {
yield jsterm.execute(objName);
info("JSTerm executed");

let msg = yield waitFor(() => findMessage(hud, "Object"));
ok(msg, "Message found");

let anchor = msg.querySelector("a");
let body = msg.querySelector(".message-body");
ok(anchor, "object anchor");
ok(body, "message body");
ok(body.textContent.includes(expectedText), "message text check");

msg.scrollIntoView();
yield EventUtils.synthesizeMouse(anchor, 2, 2, {}, hud.iframeWindow);

let vviewVar = yield jsterm.once("variablesview-fetched");
vview = vviewVar._variablesView;
ok(vview, "variables view object exists");
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,102 @@ Services.scriptloader.loadSubScript(
this);

Services.prefs.setBoolPref("devtools.webconsole.new-frontend-enabled", true);
registerCleanupFunction(() => {
registerCleanupFunction(function* () {
Services.prefs.clearUserPref("devtools.webconsole.new-frontend-enabled");

let browserConsole = HUDService.getBrowserConsole();
if (browserConsole) {
if (browserConsole.jsterm) {
browserConsole.jsterm.clearOutput(true);
}
yield HUDService.toggleBrowserConsole();
}
});

/**
* Add a new tab and open the toolbox in it, and select the webconsole.
*
* @param string url
* The URL for the tab to be opened.
* @return Promise
* Resolves when the tab has been added, loaded and the toolbox has been opened.
* Resolves to the toolbox.
*/
var openNewTabAndConsole = Task.async(function* (url) {
let toolbox = yield openNewTabAndToolbox(TEST_URI, "webconsole");
let hud = toolbox.getCurrentPanel().hud;
hud.jsterm._lazyVariablesView = false;
return hud;
});

/**
* Wait for messages in the web console output, resolving once they are receieved.
*
* @param object options
* - hud: the webconsole
* - messages: Array[Object]. An array of messages to match. Current supported options:
* - text: Exact text match in .message-body
*/
function waitForMessages({ hud, messages }) {
return new Promise(resolve => {
let numMatched = 0;
let receivedLog = hud.ui.on("new-messages", function messagesReceieved(e, newMessage) {
for (let message of messages) {
if (message.matched) {
continue;
}

if (newMessage.node.querySelector(".message-body").textContent == message.text) {
numMatched++;
message.matched = true;
info("Matched a message with text: " + message.text + ", still waiting for " + (messages.length - numMatched) + " messages");
}

if (numMatched === messages.length) {
hud.ui.off("new-messages", messagesReceieved);
resolve(receivedLog);
return;
}
}
});
});
}

/**
* Wait for a predicate to return a result.
*
* @param function condition
* Invoked once in a while until it returns a truthy value. This should be an
* idempotent function, since we have to run it a second time after it returns
* true in order to return the value.
* @param string message [optional]
* A message to output if the condition failes.
* @param number interval [optional]
* How often the predicate is invoked, in milliseconds.
* @return object
* A promise that is resolved with the result of the condition.
*/
function* waitFor(condition, message = "waitFor", interval = 100, maxTries = 50) {
return new Promise(resolve => {
BrowserTestUtils.waitForCondition(condition, message, interval, maxTries)
.then(resolve(condition()));
});
}

/**
* Find a message in the output.
*
* @param object hud
* The web console.
* @param string text
* A substring that can be found in the message.
* @param selector [optional]
* The selector to use in finding the message.
*/
function findMessage(hud, text, selector = ".message") {
const elements = Array.prototype.filter.call(
hud.ui.experimentalOutputNode.querySelectorAll(selector),
(el) => el.textContent.includes(text)
);
return elements.pop();
}

0 comments on commit 8aed77e

Please sign in to comment.