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.

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

mercurial