browser/devtools/responsivedesign/test/browser_responsiveuiaddcustompreset.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 function test() {
     5   let instance, deletedPresetA, deletedPresetB, oldPrompt;
     6   let mgr = ResponsiveUI.ResponsiveUIManager;
     8   waitForExplicitFinish();
    10   gBrowser.selectedTab = gBrowser.addTab();
    11   gBrowser.selectedBrowser.addEventListener("load", function onload() {
    12     gBrowser.selectedBrowser.removeEventListener("load", onload, true);
    13     waitForFocus(startTest, content);
    14   }, true);
    16   content.location = "data:text/html;charset=utf8,test custom presets in responsive mode";
    18   // This test uses executeSoon() when responsive mode is initialized and when
    19   // it is destroyed such that we get out of the init/destroy loops. If we try
    20   // to init/destroy immediately, without waiting for the next loop, we get
    21   // intermittent test failures.
    23   function startTest() {
    24     // Mocking prompt
    25     oldPrompt = Services.prompt;
    26     Services.prompt = {
    27       value: "",
    28       returnBool: true,
    29       prompt: function(aParent, aDialogTitle, aText, aValue, aCheckMsg, aCheckState) {
    30         aValue.value = this.value;
    31         return this.returnBool;
    32       }
    33     };
    35     registerCleanupFunction(() => Services.prompt = oldPrompt);
    37     info("test started, waiting for responsive mode to activate");
    39     document.getElementById("Tools:ResponsiveUI").removeAttribute("disabled");
    40     mgr.once("on", onUIOpen);
    41     synthesizeKeyFromKeyTag("key_responsiveUI");
    42   }
    44   function onUIOpen() {
    45     // Is it open?
    46     let container = gBrowser.getBrowserContainer();
    47     is(container.getAttribute("responsivemode"), "true", "In responsive mode.");
    49     instance = gBrowser.selectedTab.__responsiveUI;
    50     ok(instance, "instance of the module is attached to the tab.");
    52     instance.transitionsEnabled = false;
    54     testAddCustomPreset();
    55   }
    57   function testAddCustomPreset() {
    58     // Tries to add a custom preset and cancel the prompt
    59     let idx = instance.menulist.selectedIndex;
    60     let presetCount = instance.presets.length;
    62     Services.prompt.value = "";
    63     Services.prompt.returnBool = false;
    64     instance.addbutton.doCommand();
    66     is(idx, instance.menulist.selectedIndex, "selected item didn't change after add preset and cancel");
    67     is(presetCount, instance.presets.length, "number of presets didn't change after add preset and cancel");
    69     let customHeight = 123, customWidth = 456;
    70     instance.setSize(customWidth, customHeight);
    72     // Adds the custom preset with "Testing preset"
    73     Services.prompt.value = "Testing preset";
    74     Services.prompt.returnBool = true;
    75     instance.addbutton.doCommand();
    77     instance.menulist.selectedIndex = 1;
    79     info("waiting for responsive mode to turn off");
    80     mgr.once("off", restart);
    82     // Force document reflow to avoid intermittent failures.
    83     info("document height " + document.height);
    85     // We're still in the loop of initializing the responsive mode.
    86     // Let's wait next loop to stop it.
    87     executeSoon(function() {
    88       instance.close();
    89     });
    90   }
    92   function restart() {
    93     info("Restarting Responsive Mode");
    94     mgr.once("on", function() {
    95       let container = gBrowser.getBrowserContainer();
    96       is(container.getAttribute("responsivemode"), "true", "In responsive mode.");
    98       instance = gBrowser.selectedTab.__responsiveUI;
   100       testCustomPresetInList();
   101     });
   103     // We're still in the loop of destroying the responsive mode.
   104     // Let's wait next loop to start it.
   105     executeSoon(function() {
   106       synthesizeKeyFromKeyTag("key_responsiveUI");
   107     });
   108   }
   110   function testCustomPresetInList() {
   111     let customPresetIndex = getPresetIndex("456x123 (Testing preset)");
   112     ok(customPresetIndex >= 0, "is the previously added preset (idx = " + customPresetIndex + ") in the list of items");
   114     instance.menulist.selectedIndex = customPresetIndex;
   116     is(content.innerWidth, 456, "add preset, and selected in the list, dimension valid (width)");
   117     is(content.innerHeight, 123, "add preset, and selected in the list, dimension valid (height)");
   119     testDeleteCustomPresets();
   120   }
   122   function testDeleteCustomPresets() {
   123     instance.removebutton.doCommand();
   125     instance.menulist.selectedIndex = 2;
   126     deletedPresetA = instance.menulist.selectedItem.getAttribute("label");
   127     instance.removebutton.doCommand();
   129     instance.menulist.selectedIndex = 2;
   130     deletedPresetB = instance.menulist.selectedItem.getAttribute("label");
   131     instance.removebutton.doCommand();
   133     info("waiting for responsive mode to turn off");
   134     mgr.once("off", restartAgain);
   136     // We're still in the loop of initializing the responsive mode.
   137     // Let's wait next loop to stop it.
   138     executeSoon(() => instance.close());
   139   }
   141   function restartAgain() {
   142     info("waiting for responsive mode to turn on");
   143     mgr.once("on", () => {
   144       instance = gBrowser.selectedTab.__responsiveUI;
   145       testCustomPresetsNotInListAnymore();
   146     });
   148     // We're still in the loop of destroying the responsive mode.
   149     // Let's wait next loop to start it.
   150     executeSoon(() => synthesizeKeyFromKeyTag("key_responsiveUI"));
   151   }
   153   function testCustomPresetsNotInListAnymore() {
   154     let customPresetIndex = getPresetIndex(deletedPresetA);
   155     is(customPresetIndex, -1, "deleted preset " + deletedPresetA + " is not in the list anymore");
   157     customPresetIndex = getPresetIndex(deletedPresetB);
   158     is(customPresetIndex, -1, "deleted preset " + deletedPresetB + " is not in the list anymore");
   160     executeSoon(finishUp);
   161   }
   163   function finishUp() {
   164     delete instance;
   165     gBrowser.removeCurrentTab();
   167     finish();
   168   }
   170   function getPresetIndex(presetLabel) {
   171     function testOnePreset(c) {
   172       if (c == 0) {
   173         return -1;
   174       }
   175       instance.menulist.selectedIndex = c;
   177       let item = instance.menulist.firstChild.childNodes[c];
   178       if (item.getAttribute("label") === presetLabel) {
   179         return c;
   180       } else {
   181         return testOnePreset(c - 1);
   182       }
   183     }
   184     return testOnePreset(instance.menulist.firstChild.childNodes.length - 4);
   185   }
   187   function synthesizeKeyFromKeyTag(aKeyId) {
   188     let key = document.getElementById(aKeyId);
   189     isnot(key, null, "Successfully retrieved the <key> node");
   191     let name = null;
   193     if (key.getAttribute("keycode"))
   194       name = key.getAttribute("keycode");
   195     else if (key.getAttribute("key"))
   196       name = key.getAttribute("key");
   198     isnot(name, null, "Successfully retrieved keycode/key");
   200     key.doCommand();
   201   }
   202 }

mercurial