browser/devtools/responsivedesign/test/browser_responsiveuiaddcustompreset.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:bc1ac08b055a
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 function test() {
5 let instance, deletedPresetA, deletedPresetB, oldPrompt;
6 let mgr = ResponsiveUI.ResponsiveUIManager;
7
8 waitForExplicitFinish();
9
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);
15
16 content.location = "data:text/html;charset=utf8,test custom presets in responsive mode";
17
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.
22
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 };
34
35 registerCleanupFunction(() => Services.prompt = oldPrompt);
36
37 info("test started, waiting for responsive mode to activate");
38
39 document.getElementById("Tools:ResponsiveUI").removeAttribute("disabled");
40 mgr.once("on", onUIOpen);
41 synthesizeKeyFromKeyTag("key_responsiveUI");
42 }
43
44 function onUIOpen() {
45 // Is it open?
46 let container = gBrowser.getBrowserContainer();
47 is(container.getAttribute("responsivemode"), "true", "In responsive mode.");
48
49 instance = gBrowser.selectedTab.__responsiveUI;
50 ok(instance, "instance of the module is attached to the tab.");
51
52 instance.transitionsEnabled = false;
53
54 testAddCustomPreset();
55 }
56
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;
61
62 Services.prompt.value = "";
63 Services.prompt.returnBool = false;
64 instance.addbutton.doCommand();
65
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");
68
69 let customHeight = 123, customWidth = 456;
70 instance.setSize(customWidth, customHeight);
71
72 // Adds the custom preset with "Testing preset"
73 Services.prompt.value = "Testing preset";
74 Services.prompt.returnBool = true;
75 instance.addbutton.doCommand();
76
77 instance.menulist.selectedIndex = 1;
78
79 info("waiting for responsive mode to turn off");
80 mgr.once("off", restart);
81
82 // Force document reflow to avoid intermittent failures.
83 info("document height " + document.height);
84
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 }
91
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.");
97
98 instance = gBrowser.selectedTab.__responsiveUI;
99
100 testCustomPresetInList();
101 });
102
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 }
109
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");
113
114 instance.menulist.selectedIndex = customPresetIndex;
115
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)");
118
119 testDeleteCustomPresets();
120 }
121
122 function testDeleteCustomPresets() {
123 instance.removebutton.doCommand();
124
125 instance.menulist.selectedIndex = 2;
126 deletedPresetA = instance.menulist.selectedItem.getAttribute("label");
127 instance.removebutton.doCommand();
128
129 instance.menulist.selectedIndex = 2;
130 deletedPresetB = instance.menulist.selectedItem.getAttribute("label");
131 instance.removebutton.doCommand();
132
133 info("waiting for responsive mode to turn off");
134 mgr.once("off", restartAgain);
135
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 }
140
141 function restartAgain() {
142 info("waiting for responsive mode to turn on");
143 mgr.once("on", () => {
144 instance = gBrowser.selectedTab.__responsiveUI;
145 testCustomPresetsNotInListAnymore();
146 });
147
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 }
152
153 function testCustomPresetsNotInListAnymore() {
154 let customPresetIndex = getPresetIndex(deletedPresetA);
155 is(customPresetIndex, -1, "deleted preset " + deletedPresetA + " is not in the list anymore");
156
157 customPresetIndex = getPresetIndex(deletedPresetB);
158 is(customPresetIndex, -1, "deleted preset " + deletedPresetB + " is not in the list anymore");
159
160 executeSoon(finishUp);
161 }
162
163 function finishUp() {
164 delete instance;
165 gBrowser.removeCurrentTab();
166
167 finish();
168 }
169
170 function getPresetIndex(presetLabel) {
171 function testOnePreset(c) {
172 if (c == 0) {
173 return -1;
174 }
175 instance.menulist.selectedIndex = c;
176
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 }
186
187 function synthesizeKeyFromKeyTag(aKeyId) {
188 let key = document.getElementById(aKeyId);
189 isnot(key, null, "Successfully retrieved the <key> node");
190
191 let name = null;
192
193 if (key.getAttribute("keycode"))
194 name = key.getAttribute("keycode");
195 else if (key.getAttribute("key"))
196 name = key.getAttribute("key");
197
198 isnot(name, null, "Successfully retrieved keycode/key");
199
200 key.doCommand();
201 }
202 }

mercurial