Bug 1058167 - Activate split console command button when initially opened. r=jwalker
authorBrian Grinstead <bgrinstead@mozilla.com>
Mon, 25 Aug 2014 17:01:00 -0400
changeset 202191 8883019fca40023d4f5d6941491d03f3e339dd10
parent 202190 f6beabe7cfb60c8a405199cff263b5e7ecd5cd93
child 202192 3fa315846f97505fb1d42c4b4cbbac1d694782ce
push idunknown
push userunknown
push dateunknown
reviewersjwalker
bugs1058167
milestone34.0a1
Bug 1058167 - Activate split console command button when initially opened. r=jwalker
browser/devtools/webconsole/console-commands.js
browser/devtools/webconsole/test/browser_webconsole_split_persist.js
--- a/browser/devtools/webconsole/console-commands.js
+++ b/browser/devtools/webconsole/console-commands.js
@@ -1,56 +1,44 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 const gcli = require("gcli/index");
-const EventEmitter = require("devtools/toolkit/event-emitter");
 const { gDevTools } = require("resource:///modules/devtools/gDevTools.jsm");
 
-const eventEmitter = new EventEmitter();
-
-gDevTools.on("toolbox-ready", (e, toolbox) => {
-  if (!toolbox.target) {
-    return;
-  }
-
-  let fireChangeForTab = () => {
-    eventEmitter.emit("changed", { target: toolbox.target });
-  };
-
-  toolbox.on("split-console", fireChangeForTab);
-  toolbox.on("select", fireChangeForTab);
-
-  toolbox.once("destroyed", () => {
-    toolbox.off("split-console", fireChangeForTab);
-    toolbox.off("select", fireChangeForTab);
-  });
-});
-
 exports.items = [
   {
     name: 'splitconsole',
     hidden: true,
     buttonId: "command-button-splitconsole",
     buttonClass: "command-button command-button-invertable",
     tooltipText: gcli.lookup("splitconsoleTooltip"),
     state: {
       isChecked: function(target) {
         let toolbox = gDevTools.getToolbox(target);
         return !!(toolbox && toolbox.splitConsole);
       },
       onChange: function(target, changeHandler) {
-        eventEmitter.on("changed", changeHandler);
-      },
-      offChange: function(target, changeHandler) {
-        eventEmitter.off("changed", changeHandler);
-      },
+        // Register handlers for when a change event should be fired
+        // (which resets the checked state of the button).
+        let toolbox = gDevTools.getToolbox(target);
+        let callback = changeHandler.bind(null, "changed", { target: target });
+
+        if (!toolbox) {
+          return;
+        }
+
+        toolbox.on("split-console", callback);
+        toolbox.once("destroyed", () => {
+          toolbox.off("split-console", callback);
+        });
+      }
     },
     exec: function(args, context) {
       let target = context.environment.target;
       let toolbox = gDevTools.getToolbox(target);
 
       if (!toolbox) {
         return gDevTools.showToolbox(target, "inspector").then((toolbox) => {
           toolbox.toggleSplitConsole();
--- a/browser/devtools/webconsole/test/browser_webconsole_split_persist.js
+++ b/browser/devtools/webconsole/test/browser_webconsole_split_persist.js
@@ -13,31 +13,34 @@ function test() {
 
   function* runner() {
     info("Opening a tab while there is no user setting on split console pref");
     let {tab} = yield loadTab(TEST_URI);
     let target = TargetFactory.forTab(tab);
     toolbox = yield gDevTools.showToolbox(target, "inspector");
 
     ok(!toolbox.splitConsole, "Split console is hidden by default.");
+    ok(!isCommandButtonChecked(), "Split console button is unchecked by default.");
     yield toggleSplitConsoleWithEscape();
     ok(toolbox.splitConsole, "Split console is now visible.");
+    ok(isCommandButtonChecked(), "Split console button is now checked.");
     ok(getVisiblePrefValue(), "Visibility pref is true");
 
     is(getHeightPrefValue(), toolbox.webconsolePanel.height, "Panel height matches the pref");
     toolbox.webconsolePanel.height = 200;
 
     yield toolbox.destroy();
 
     info("Opening a tab while there is a true user setting on split console pref");
     let {tab} = yield loadTab(TEST_URI);
     let target = TargetFactory.forTab(tab);
     toolbox = yield gDevTools.showToolbox(target, "inspector");
 
     ok(toolbox.splitConsole, "Split console is visible by default.");
+    ok(isCommandButtonChecked(), "Split console button is checked by default.");
     is(getHeightPrefValue(), 200, "Height is set based on panel height after closing");
 
     // Use the binding element since jsterm.inputNode is a XUL textarea element.
     let activeElement = getActiveElement(toolbox.doc);
     activeElement = activeElement.ownerDocument.getBindingParent(activeElement);
     let inputNode = toolbox.getPanel("webconsole").hud.jsterm.inputNode;
     is(activeElement, inputNode, "Split console input is focused by default");
 
@@ -46,16 +49,17 @@ function test() {
         "The actual height of the console is bound with a min height");
 
     toolbox.webconsolePanel.height = 10000;
     ok (toolbox.webconsolePanel.clientHeight < 10000,
         "The actual height of the console is bound with a max height");
 
     yield toggleSplitConsoleWithEscape();
     ok(!toolbox.splitConsole, "Split console is now hidden.");
+    ok(!isCommandButtonChecked(), "Split console button is now unchecked.");
     ok(!getVisiblePrefValue(), "Visibility pref is false");
 
     yield toolbox.destroy();
 
     is(getHeightPrefValue(), 10000, "Height is set based on panel height after closing");
 
 
     info("Opening a tab while there is a false user setting on split console pref");
@@ -80,16 +84,21 @@ function test() {
   function getVisiblePrefValue() {
     return Services.prefs.getBoolPref("devtools.toolbox.splitconsoleEnabled");
   }
 
   function getHeightPrefValue() {
     return Services.prefs.getIntPref("devtools.toolbox.splitconsoleHeight");
   }
 
+  function isCommandButtonChecked() {
+    return toolbox.doc.querySelector("#command-button-splitconsole").
+      hasAttribute("checked");
+  }
+
   function toggleSplitConsoleWithEscape() {
     let onceSplitConsole = toolbox.once("split-console");
     let contentWindow = toolbox.frame.contentWindow;
     contentWindow.focus();
     EventUtils.sendKey("ESCAPE", contentWindow);
     return onceSplitConsole;
   }