browser/devtools/framework/test/browser_toolbox_options_disable_buttons.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/framework/test/browser_toolbox_options_disable_buttons.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,148 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let doc = null, toolbox = null, panelWin = null, modifiedPrefs = [];
     1.8 +
     1.9 +function test() {
    1.10 +  waitForExplicitFinish();
    1.11 +
    1.12 +  gBrowser.selectedTab = gBrowser.addTab();
    1.13 +  let target = TargetFactory.forTab(gBrowser.selectedTab);
    1.14 +
    1.15 +  gBrowser.selectedBrowser.addEventListener("load", function onLoad(evt) {
    1.16 +    gBrowser.selectedBrowser.removeEventListener(evt.type, onLoad, true);
    1.17 +    gDevTools.showToolbox(target)
    1.18 +      .then(testSelectTool)
    1.19 +      .then(testToggleToolboxButtons)
    1.20 +      .then(testPrefsAreRespectedWhenReopeningToolbox)
    1.21 +      .then(cleanup, errorHandler);
    1.22 +  }, true);
    1.23 +
    1.24 +  content.location = "data:text/html;charset=utf8,test for dynamically registering and unregistering tools";
    1.25 +}
    1.26 +
    1.27 +function testPrefsAreRespectedWhenReopeningToolbox() {
    1.28 +  let deferred = promise.defer();
    1.29 +  let target = TargetFactory.forTab(gBrowser.selectedTab);
    1.30 +
    1.31 +  info ("Closing toolbox to test after reopening");
    1.32 +  gDevTools.closeToolbox(target).then(() => {
    1.33 +    let target = TargetFactory.forTab(gBrowser.selectedTab);
    1.34 +    gDevTools.showToolbox(target)
    1.35 +      .then(testSelectTool)
    1.36 +      .then(() => {
    1.37 +        info ("Toolbox has been reopened.  Checking UI state.");
    1.38 +        testPreferenceAndUIStateIsConsistent();
    1.39 +        deferred.resolve();
    1.40 +      });
    1.41 +  });
    1.42 +
    1.43 +  return deferred.promise;
    1.44 +}
    1.45 +
    1.46 +function testSelectTool(aToolbox) {
    1.47 +  let deferred = promise.defer();
    1.48 +  info ("Selecting the options panel");
    1.49 +
    1.50 +  toolbox = aToolbox;
    1.51 +  doc = toolbox.doc;
    1.52 +  toolbox.once("options-selected", (event, tool) => {
    1.53 +    ok(true, "Options panel selected via selectTool method");
    1.54 +    panelWin = tool.panelWin;
    1.55 +    deferred.resolve();
    1.56 +  });
    1.57 +  toolbox.selectTool("options");
    1.58 +
    1.59 +  return deferred.promise;
    1.60 +}
    1.61 +
    1.62 +function testPreferenceAndUIStateIsConsistent() {
    1.63 +  let checkNodes = [...panelWin.document.querySelectorAll("#enabled-toolbox-buttons-box > checkbox")];
    1.64 +  let toolboxButtonNodes = [...doc.querySelectorAll("#toolbox-buttons > toolbarbutton")];
    1.65 +  let toggleableTools = toolbox.toolboxButtons;
    1.66 +
    1.67 +  for (let tool of toggleableTools) {
    1.68 +    let isVisible = getBoolPref(tool.visibilityswitch);
    1.69 +
    1.70 +    let button = toolboxButtonNodes.filter(button=>button.id === tool.id)[0];
    1.71 +    is (!button.hasAttribute("hidden"), isVisible, "Button visibility matches pref for " + tool.id);
    1.72 +
    1.73 +    let check = checkNodes.filter(node=>node.id === tool.id)[0];
    1.74 +    is (check.checked, isVisible, "Checkbox should be selected based on current pref for " + tool.id);
    1.75 +  }
    1.76 +}
    1.77 +
    1.78 +function testToggleToolboxButtons() {
    1.79 +  let checkNodes = [...panelWin.document.querySelectorAll("#enabled-toolbox-buttons-box > checkbox")];
    1.80 +  let toolboxButtonNodes = [...doc.querySelectorAll("#toolbox-buttons > toolbarbutton")];
    1.81 +  let visibleButtons = toolboxButtonNodes.filter(button=>!button.hasAttribute("hidden"));
    1.82 +  let toggleableTools = toolbox.toolboxButtons;
    1.83 +
    1.84 +  is (checkNodes.length, toggleableTools.length, "All of the buttons are toggleable." );
    1.85 +  is (checkNodes.length, toolboxButtonNodes.length, "All of the DOM buttons are toggleable." );
    1.86 +
    1.87 +  for (let tool of toggleableTools) {
    1.88 +    let id = tool.id;
    1.89 +    let matchedCheckboxes = checkNodes.filter(node=>node.id === id);
    1.90 +    let matchedButtons = toolboxButtonNodes.filter(button=>button.id === id);
    1.91 +    ok (matchedCheckboxes.length === 1,
    1.92 +      "There should be a single toggle checkbox for: " + id);
    1.93 +    ok (matchedButtons.length === 1,
    1.94 +      "There should be a DOM button for: " + id);
    1.95 +    is (matchedButtons[0], tool.button,
    1.96 +      "DOM buttons should match for: " + id);
    1.97 +
    1.98 +    is (matchedCheckboxes[0].getAttribute("label"), tool.label,
    1.99 +      "The label for checkbox matches the tool definition.")
   1.100 +    is (matchedButtons[0].getAttribute("tooltiptext"), tool.label,
   1.101 +      "The tooltip for button matches the tool definition.")
   1.102 +  }
   1.103 +
   1.104 +  // Store modified pref names so that they can be cleared on error.
   1.105 +  for (let tool of toggleableTools) {
   1.106 +    let pref = tool.visibilityswitch;
   1.107 +    modifiedPrefs.push(pref);
   1.108 +  }
   1.109 +
   1.110 +  // Try checking each checkbox, making sure that it changes the preference
   1.111 +  for (let node of checkNodes) {
   1.112 +    let tool = toggleableTools.filter(tool=>tool.id === node.id)[0];
   1.113 +    let isVisible = getBoolPref(tool.visibilityswitch);
   1.114 +
   1.115 +    testPreferenceAndUIStateIsConsistent();
   1.116 +    toggleButton(node);
   1.117 +    testPreferenceAndUIStateIsConsistent();
   1.118 +
   1.119 +    let isVisibleAfterClick = getBoolPref(tool.visibilityswitch);
   1.120 +
   1.121 +    is (isVisible, !isVisibleAfterClick,
   1.122 +      "Clicking on the node should have toggled visibility preference for " + tool.visibilityswitch);
   1.123 +  }
   1.124 +
   1.125 +  return promise.resolve();
   1.126 +}
   1.127 +
   1.128 +function getBoolPref(key) {
   1.129 +  return Services.prefs.getBoolPref(key);
   1.130 +}
   1.131 +
   1.132 +function toggleButton(node) {
   1.133 +  node.scrollIntoView();
   1.134 +  EventUtils.synthesizeMouseAtCenter(node, {}, panelWin);
   1.135 +}
   1.136 +
   1.137 +function cleanup() {
   1.138 +  toolbox.destroy().then(function() {
   1.139 +    gBrowser.removeCurrentTab();
   1.140 +    for (let pref of modifiedPrefs) {
   1.141 +      Services.prefs.clearUserPref(pref);
   1.142 +    }
   1.143 +    toolbox = doc = panelWin = modifiedPrefs = null;
   1.144 +    finish();
   1.145 +  });
   1.146 +}
   1.147 +
   1.148 +function errorHandler(error) {
   1.149 +  ok(false, "Unexpected error: " + error);
   1.150 +  cleanup();
   1.151 +}

mercurial