browser/devtools/framework/test/browser_toolbox_tabsswitch_shortcuts.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_tabsswitch_shortcuts.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let Toolbox = devtools.Toolbox;
     1.8 +
     1.9 +let toolbox, toolIDs, idIndex, secondTime = false,
    1.10 +    reverse = false, nextKey = null, prevKey = null;
    1.11 +
    1.12 +function test() {
    1.13 +  waitForExplicitFinish();
    1.14 +
    1.15 +  addTab("about:blank", function() {
    1.16 +    let target = TargetFactory.forTab(gBrowser.selectedTab);
    1.17 +    idIndex = 0;
    1.18 +
    1.19 +    target.makeRemote().then(() => {
    1.20 +      toolIDs = gDevTools.getToolDefinitionArray()
    1.21 +                  .filter(def => def.isTargetSupported(target))
    1.22 +                  .map(def => def.id);
    1.23 +      gDevTools.showToolbox(target, toolIDs[0], Toolbox.HostType.BOTTOM)
    1.24 +               .then(testShortcuts);
    1.25 +    });
    1.26 +  });
    1.27 +}
    1.28 +
    1.29 +function testShortcuts(aToolbox, aIndex) {
    1.30 +  if (aIndex == toolIDs.length) {
    1.31 +    aIndex = 0;
    1.32 +    if (secondTime) {
    1.33 +      secondTime = false;
    1.34 +      reverse = true;
    1.35 +      aIndex = toolIDs.length - 2;
    1.36 +    }
    1.37 +    else {
    1.38 +      secondTime = true;
    1.39 +    }
    1.40 +  }
    1.41 +  else if (aIndex == -1) {
    1.42 +    aIndex = toolIDs.length - 1;
    1.43 +    if (secondTime) {
    1.44 +      tidyUp();
    1.45 +      return;
    1.46 +    }
    1.47 +    secondTime = true;
    1.48 +  }
    1.49 +
    1.50 +  toolbox = aToolbox;
    1.51 +  if (!nextKey) {
    1.52 +    nextKey = toolbox.doc.getElementById("toolbox-next-tool-key")
    1.53 +                     .getAttribute("key");
    1.54 +    prevKey = toolbox.doc.getElementById("toolbox-previous-tool-key")
    1.55 +                     .getAttribute("key");
    1.56 +  }
    1.57 +  info("Toolbox fired a `ready` event");
    1.58 +
    1.59 +  toolbox.once("select", onSelect);
    1.60 +
    1.61 +  if (aIndex != null) {
    1.62 +    // This if block is to allow the call of onSelect without shortcut press for
    1.63 +    // the first time. That happens because on opening of toolbox, one tool gets
    1.64 +    // selected atleast.
    1.65 +
    1.66 +    let key = (reverse ? prevKey: nextKey);
    1.67 +    let modifiers = {
    1.68 +      accelKey: true
    1.69 +    };
    1.70 +    idIndex = aIndex;
    1.71 +    info("Testing shortcut to switch to tool " + aIndex + ":" + toolIDs[aIndex] +
    1.72 +         " using key " + key);
    1.73 +    EventUtils.synthesizeKey(key, modifiers, toolbox.doc.defaultView);
    1.74 +  }
    1.75 +}
    1.76 +
    1.77 +function onSelect(event, id) {
    1.78 +  info("toolbox-select event from " + id);
    1.79 +
    1.80 +  is(toolIDs.indexOf(id), idIndex,
    1.81 +     "Correct tool is selected on pressing the shortcut for " + id);
    1.82 +  // Execute soon to reset the stack trace.
    1.83 +  executeSoon(() => {
    1.84 +    testShortcuts(toolbox, idIndex + (reverse ? -1: 1));
    1.85 +  });
    1.86 +}
    1.87 +
    1.88 +function tidyUp() {
    1.89 +  toolbox.destroy().then(function() {
    1.90 +    gBrowser.removeCurrentTab();
    1.91 +
    1.92 +    toolbox = toolIDs = idIndex = Toolbox = secondTime = reverse = nextKey =
    1.93 +      prevKey = null;
    1.94 +    finish();
    1.95 +  });
    1.96 +}

mercurial