browser/devtools/netmonitor/test/browser_net_prefs-reload.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:08c7d947f4ff
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 /**
5 * Tests if the prefs that should survive across tool reloads work.
6 */
7
8 function test() {
9 initNetMonitor(SIMPLE_URL).then(([aTab, aDebuggee, aMonitor]) => {
10 info("Starting test... ");
11
12 // This test reopens the network monitor a bunch of times, for different
13 // hosts (bottom, side, window). This seems to be slow on debug builds.
14 requestLongerTimeout(3);
15
16 // Use these getters instead of caching instances inside the panel win,
17 // since the tool is reopened a bunch of times during this test
18 // and the instances will differ.
19 let getView = () => aMonitor.panelWin.NetMonitorView;
20 let getController = () => aMonitor.panelWin.NetMonitorController;
21
22 let prefsToCheck = {
23 filters: {
24 // A custom new value to be used for the verified preference.
25 newValue: ["html", "css"],
26 // Getter used to retrieve the current value from the frontend, in order
27 // to verify that the pref was applied properly.
28 validateValue: ($) => getView().RequestsMenu._activeFilters,
29 // Predicate used to modify the frontend when setting the new pref value,
30 // before trying to validate the changes.
31 modifyFrontend: ($, aValue) => aValue.forEach(e => getView().RequestsMenu.filterOn(e))
32 },
33 networkDetailsWidth: {
34 newValue: ~~(Math.random() * 200 + 100),
35 validateValue: ($) => ~~$("#details-pane").getAttribute("width"),
36 modifyFrontend: ($, aValue) => $("#details-pane").setAttribute("width", aValue)
37 },
38 networkDetailsHeight: {
39 newValue: ~~(Math.random() * 300 + 100),
40 validateValue: ($) => ~~$("#details-pane").getAttribute("height"),
41 modifyFrontend: ($, aValue) => $("#details-pane").setAttribute("height", aValue)
42 }
43 /* add more prefs here... */
44 };
45
46 function storeFirstPrefValues() {
47 info("Caching initial pref values.");
48
49 for (let name in prefsToCheck) {
50 let currentValue = aMonitor.panelWin.Prefs[name];
51 prefsToCheck[name].firstValue = currentValue;
52 }
53 }
54
55 function validateFirstPrefValues() {
56 info("Validating current pref values to the UI elements.");
57
58 for (let name in prefsToCheck) {
59 let currentValue = aMonitor.panelWin.Prefs[name];
60 let firstValue = prefsToCheck[name].firstValue;
61 let validateValue = prefsToCheck[name].validateValue;
62
63 is(currentValue.toSource(), firstValue.toSource(),
64 "Pref " + name + " should be equal to first value: " + firstValue);
65 is(currentValue.toSource(), validateValue(aMonitor.panelWin.$).toSource(),
66 "Pref " + name + " should validate: " + currentValue);
67 }
68 }
69
70 function modifyFrontend() {
71 info("Modifying UI elements to the specified new values.");
72
73 for (let name in prefsToCheck) {
74 let currentValue = aMonitor.panelWin.Prefs[name];
75 let firstValue = prefsToCheck[name].firstValue;
76 let newValue = prefsToCheck[name].newValue;
77 let validateValue = prefsToCheck[name].validateValue;
78 let modifyFrontend = prefsToCheck[name].modifyFrontend;
79
80 modifyFrontend(aMonitor.panelWin.$, newValue);
81 info("Modified UI element affecting " + name + " to: " + newValue);
82
83 is(currentValue.toSource(), firstValue.toSource(),
84 "Pref " + name + " should still be equal to first value: " + firstValue);
85 isnot(currentValue.toSource(), newValue.toSource(),
86 "Pref " + name + " should't yet be equal to second value: " + newValue);
87 is(newValue.toSource(), validateValue(aMonitor.panelWin.$).toSource(),
88 "The UI element affecting " + name + " should validate: " + newValue);
89 }
90 }
91
92 function validateNewPrefValues() {
93 info("Invalidating old pref values to the modified UI elements.");
94
95 for (let name in prefsToCheck) {
96 let currentValue = aMonitor.panelWin.Prefs[name];
97 let firstValue = prefsToCheck[name].firstValue;
98 let newValue = prefsToCheck[name].newValue;
99 let validateValue = prefsToCheck[name].validateValue;
100
101 isnot(currentValue.toSource(), firstValue.toSource(),
102 "Pref " + name + " should't be equal to first value: " + firstValue);
103 is(currentValue.toSource(), newValue.toSource(),
104 "Pref " + name + " should now be equal to second value: " + newValue);
105 is(newValue.toSource(), validateValue(aMonitor.panelWin.$).toSource(),
106 "The UI element affecting " + name + " should validate: " + newValue);
107 }
108 }
109
110 function resetFrontend() {
111 info("Resetting UI elements to the cached initial pref values.");
112
113 for (let name in prefsToCheck) {
114 let currentValue = aMonitor.panelWin.Prefs[name];
115 let firstValue = prefsToCheck[name].firstValue;
116 let newValue = prefsToCheck[name].newValue;
117 let validateValue = prefsToCheck[name].validateValue;
118 let modifyFrontend = prefsToCheck[name].modifyFrontend;
119
120 modifyFrontend(aMonitor.panelWin.$, firstValue);
121 info("Modified UI element affecting " + name + " to: " + firstValue);
122
123 isnot(currentValue.toSource(), firstValue.toSource(),
124 "Pref " + name + " should't yet be equal to first value: " + firstValue);
125 is(currentValue.toSource(), newValue.toSource(),
126 "Pref " + name + " should still be equal to second value: " + newValue);
127 is(firstValue.toSource(), validateValue(aMonitor.panelWin.$).toSource(),
128 "The UI element affecting " + name + " should validate: " + firstValue);
129 }
130 }
131
132 function testBottom() {
133 info("Testing prefs reload for a bottom host.");
134 storeFirstPrefValues();
135
136 // Validate and modify while toolbox is on the bottom.
137 validateFirstPrefValues();
138 modifyFrontend();
139
140 return restartNetMonitor(aMonitor)
141 .then(([,, aNewMonitor]) => {
142 aMonitor = aNewMonitor;
143
144 // Revalidate and reset frontend while toolbox is on the bottom.
145 validateNewPrefValues();
146 resetFrontend();
147
148 return restartNetMonitor(aMonitor);
149 })
150 .then(([,, aNewMonitor]) => {
151 aMonitor = aNewMonitor;
152
153 // Revalidate.
154 validateFirstPrefValues();
155 });
156 }
157
158 function testSide() {
159 info("Moving toolbox to the side...");
160
161 return aMonitor._toolbox.switchHost(Toolbox.HostType.SIDE)
162 .then(() => {
163 info("Testing prefs reload for a side host.");
164 storeFirstPrefValues();
165
166 // Validate and modify frontend while toolbox is on the side.
167 validateFirstPrefValues();
168 modifyFrontend();
169
170 return restartNetMonitor(aMonitor);
171 })
172 .then(([,, aNewMonitor]) => {
173 aMonitor = aNewMonitor;
174
175 // Revalidate and reset frontend while toolbox is on the side.
176 validateNewPrefValues();
177 resetFrontend();
178
179 return restartNetMonitor(aMonitor);
180 })
181 .then(([,, aNewMonitor]) => {
182 aMonitor = aNewMonitor;
183
184 // Revalidate.
185 validateFirstPrefValues();
186 });
187 }
188
189 function testWindow() {
190 info("Moving toolbox into a window...");
191
192 return aMonitor._toolbox.switchHost(Toolbox.HostType.WINDOW)
193 .then(() => {
194 info("Testing prefs reload for a window host.");
195 storeFirstPrefValues();
196
197 // Validate and modify frontend while toolbox is in a window.
198 validateFirstPrefValues();
199 modifyFrontend();
200
201 return restartNetMonitor(aMonitor);
202 })
203 .then(([,, aNewMonitor]) => {
204 aMonitor = aNewMonitor;
205
206 // Revalidate and reset frontend while toolbox is in a window.
207 validateNewPrefValues();
208 resetFrontend();
209
210 return restartNetMonitor(aMonitor);
211 })
212 .then(([,, aNewMonitor]) => {
213 aMonitor = aNewMonitor;
214
215 // Revalidate.
216 validateFirstPrefValues();
217 });
218 }
219
220 function cleanupAndFinish() {
221 info("Moving toolbox back to the bottom...");
222
223 aMonitor._toolbox.switchHost(Toolbox.HostType.BOTTOM)
224 .then(() => teardown(aMonitor))
225 .then(finish);
226 }
227
228 testBottom()
229 .then(testSide)
230 .then(testWindow)
231 .then(cleanupAndFinish);
232 });
233 }

mercurial