browser/devtools/responsivedesign/test/browser_responsiveuiaddcustompreset.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/responsivedesign/test/browser_responsiveuiaddcustompreset.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,202 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function test() {
     1.8 +  let instance, deletedPresetA, deletedPresetB, oldPrompt;
     1.9 +  let mgr = ResponsiveUI.ResponsiveUIManager;
    1.10 +
    1.11 +  waitForExplicitFinish();
    1.12 +
    1.13 +  gBrowser.selectedTab = gBrowser.addTab();
    1.14 +  gBrowser.selectedBrowser.addEventListener("load", function onload() {
    1.15 +    gBrowser.selectedBrowser.removeEventListener("load", onload, true);
    1.16 +    waitForFocus(startTest, content);
    1.17 +  }, true);
    1.18 +
    1.19 +  content.location = "data:text/html;charset=utf8,test custom presets in responsive mode";
    1.20 +
    1.21 +  // This test uses executeSoon() when responsive mode is initialized and when
    1.22 +  // it is destroyed such that we get out of the init/destroy loops. If we try
    1.23 +  // to init/destroy immediately, without waiting for the next loop, we get
    1.24 +  // intermittent test failures.
    1.25 +
    1.26 +  function startTest() {
    1.27 +    // Mocking prompt
    1.28 +    oldPrompt = Services.prompt;
    1.29 +    Services.prompt = {
    1.30 +      value: "",
    1.31 +      returnBool: true,
    1.32 +      prompt: function(aParent, aDialogTitle, aText, aValue, aCheckMsg, aCheckState) {
    1.33 +        aValue.value = this.value;
    1.34 +        return this.returnBool;
    1.35 +      }
    1.36 +    };
    1.37 +
    1.38 +    registerCleanupFunction(() => Services.prompt = oldPrompt);
    1.39 +
    1.40 +    info("test started, waiting for responsive mode to activate");
    1.41 +
    1.42 +    document.getElementById("Tools:ResponsiveUI").removeAttribute("disabled");
    1.43 +    mgr.once("on", onUIOpen);
    1.44 +    synthesizeKeyFromKeyTag("key_responsiveUI");
    1.45 +  }
    1.46 +
    1.47 +  function onUIOpen() {
    1.48 +    // Is it open?
    1.49 +    let container = gBrowser.getBrowserContainer();
    1.50 +    is(container.getAttribute("responsivemode"), "true", "In responsive mode.");
    1.51 +
    1.52 +    instance = gBrowser.selectedTab.__responsiveUI;
    1.53 +    ok(instance, "instance of the module is attached to the tab.");
    1.54 +
    1.55 +    instance.transitionsEnabled = false;
    1.56 +
    1.57 +    testAddCustomPreset();
    1.58 +  }
    1.59 +
    1.60 +  function testAddCustomPreset() {
    1.61 +    // Tries to add a custom preset and cancel the prompt
    1.62 +    let idx = instance.menulist.selectedIndex;
    1.63 +    let presetCount = instance.presets.length;
    1.64 +
    1.65 +    Services.prompt.value = "";
    1.66 +    Services.prompt.returnBool = false;
    1.67 +    instance.addbutton.doCommand();
    1.68 +
    1.69 +    is(idx, instance.menulist.selectedIndex, "selected item didn't change after add preset and cancel");
    1.70 +    is(presetCount, instance.presets.length, "number of presets didn't change after add preset and cancel");
    1.71 +
    1.72 +    let customHeight = 123, customWidth = 456;
    1.73 +    instance.setSize(customWidth, customHeight);
    1.74 +
    1.75 +    // Adds the custom preset with "Testing preset"
    1.76 +    Services.prompt.value = "Testing preset";
    1.77 +    Services.prompt.returnBool = true;
    1.78 +    instance.addbutton.doCommand();
    1.79 +
    1.80 +    instance.menulist.selectedIndex = 1;
    1.81 +
    1.82 +    info("waiting for responsive mode to turn off");
    1.83 +    mgr.once("off", restart);
    1.84 +
    1.85 +    // Force document reflow to avoid intermittent failures.
    1.86 +    info("document height " + document.height);
    1.87 +
    1.88 +    // We're still in the loop of initializing the responsive mode.
    1.89 +    // Let's wait next loop to stop it.
    1.90 +    executeSoon(function() {
    1.91 +      instance.close();
    1.92 +    });
    1.93 +  }
    1.94 +
    1.95 +  function restart() {
    1.96 +    info("Restarting Responsive Mode");
    1.97 +    mgr.once("on", function() {
    1.98 +      let container = gBrowser.getBrowserContainer();
    1.99 +      is(container.getAttribute("responsivemode"), "true", "In responsive mode.");
   1.100 +
   1.101 +      instance = gBrowser.selectedTab.__responsiveUI;
   1.102 +
   1.103 +      testCustomPresetInList();
   1.104 +    });
   1.105 +
   1.106 +    // We're still in the loop of destroying the responsive mode.
   1.107 +    // Let's wait next loop to start it.
   1.108 +    executeSoon(function() {
   1.109 +      synthesizeKeyFromKeyTag("key_responsiveUI");
   1.110 +    });
   1.111 +  }
   1.112 +
   1.113 +  function testCustomPresetInList() {
   1.114 +    let customPresetIndex = getPresetIndex("456x123 (Testing preset)");
   1.115 +    ok(customPresetIndex >= 0, "is the previously added preset (idx = " + customPresetIndex + ") in the list of items");
   1.116 +
   1.117 +    instance.menulist.selectedIndex = customPresetIndex;
   1.118 +
   1.119 +    is(content.innerWidth, 456, "add preset, and selected in the list, dimension valid (width)");
   1.120 +    is(content.innerHeight, 123, "add preset, and selected in the list, dimension valid (height)");
   1.121 +
   1.122 +    testDeleteCustomPresets();
   1.123 +  }
   1.124 +
   1.125 +  function testDeleteCustomPresets() {
   1.126 +    instance.removebutton.doCommand();
   1.127 +
   1.128 +    instance.menulist.selectedIndex = 2;
   1.129 +    deletedPresetA = instance.menulist.selectedItem.getAttribute("label");
   1.130 +    instance.removebutton.doCommand();
   1.131 +
   1.132 +    instance.menulist.selectedIndex = 2;
   1.133 +    deletedPresetB = instance.menulist.selectedItem.getAttribute("label");
   1.134 +    instance.removebutton.doCommand();
   1.135 +
   1.136 +    info("waiting for responsive mode to turn off");
   1.137 +    mgr.once("off", restartAgain);
   1.138 +
   1.139 +    // We're still in the loop of initializing the responsive mode.
   1.140 +    // Let's wait next loop to stop it.
   1.141 +    executeSoon(() => instance.close());
   1.142 +  }
   1.143 +
   1.144 +  function restartAgain() {
   1.145 +    info("waiting for responsive mode to turn on");
   1.146 +    mgr.once("on", () => {
   1.147 +      instance = gBrowser.selectedTab.__responsiveUI;
   1.148 +      testCustomPresetsNotInListAnymore();
   1.149 +    });
   1.150 +
   1.151 +    // We're still in the loop of destroying the responsive mode.
   1.152 +    // Let's wait next loop to start it.
   1.153 +    executeSoon(() => synthesizeKeyFromKeyTag("key_responsiveUI"));
   1.154 +  }
   1.155 +
   1.156 +  function testCustomPresetsNotInListAnymore() {
   1.157 +    let customPresetIndex = getPresetIndex(deletedPresetA);
   1.158 +    is(customPresetIndex, -1, "deleted preset " + deletedPresetA + " is not in the list anymore");
   1.159 +
   1.160 +    customPresetIndex = getPresetIndex(deletedPresetB);
   1.161 +    is(customPresetIndex, -1, "deleted preset " + deletedPresetB + " is not in the list anymore");
   1.162 +
   1.163 +    executeSoon(finishUp);
   1.164 +  }
   1.165 +
   1.166 +  function finishUp() {
   1.167 +    delete instance;
   1.168 +    gBrowser.removeCurrentTab();
   1.169 +
   1.170 +    finish();
   1.171 +  }
   1.172 +
   1.173 +  function getPresetIndex(presetLabel) {
   1.174 +    function testOnePreset(c) {
   1.175 +      if (c == 0) {
   1.176 +        return -1;
   1.177 +      }
   1.178 +      instance.menulist.selectedIndex = c;
   1.179 +
   1.180 +      let item = instance.menulist.firstChild.childNodes[c];
   1.181 +      if (item.getAttribute("label") === presetLabel) {
   1.182 +        return c;
   1.183 +      } else {
   1.184 +        return testOnePreset(c - 1);
   1.185 +      }
   1.186 +    }
   1.187 +    return testOnePreset(instance.menulist.firstChild.childNodes.length - 4);
   1.188 +  }
   1.189 +
   1.190 +  function synthesizeKeyFromKeyTag(aKeyId) {
   1.191 +    let key = document.getElementById(aKeyId);
   1.192 +    isnot(key, null, "Successfully retrieved the <key> node");
   1.193 +
   1.194 +    let name = null;
   1.195 +
   1.196 +    if (key.getAttribute("keycode"))
   1.197 +      name = key.getAttribute("keycode");
   1.198 +    else if (key.getAttribute("key"))
   1.199 +      name = key.getAttribute("key");
   1.200 +
   1.201 +    isnot(name, null, "Successfully retrieved keycode/key");
   1.202 +
   1.203 +    key.doCommand();
   1.204 +  }
   1.205 +}

mercurial