1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
<!DOCTYPE HTML>
<html>
<head>
<title>nsIContentViewer::overrideDPPX test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<iframe></iframe>
<script type="application/javascript;version=1.8">
SimpleTest.waitForExplicitFinish();
const frameWindow = document.querySelector("iframe").contentWindow;
const originalDPR = window.devicePixelRatio;
const originalZoom = SpecialPowers.getFullZoom(window);
const dppx = originalDPR * 1.5;
const zoom = originalZoom * 0.5;
const setOverrideDPPX = (value) => {
if (value > 0) {
info(`override window's dppx to ${value}`);
} else {
info(`restore window's dppx to default value`);
}
SpecialPowers.setOverrideDPPX(window, value);
}
const setFullZoom = (value) => {
info(`set window's fullZoom to ${value}`);
SpecialPowers.setFullZoom(window, value);
}
const createFontStyleForDPPX = (doc, value, size) => {
info(`adding a stylesheet that set font size to ${size}px for ${value}dppx`);
let style = doc.createElement("style");
style.setAttribute("media", `(resolution: ${value}dppx)`);
doc.head.appendChild(style);
style.sheet.insertRule(`body {font-size: ${size}px}`, 0);
return style;
}
const getBodyFontSize = (w) => w.getComputedStyle(w.document.body).fontSize;
const assertValuesAreInitial = () => {
is(window.devicePixelRatio, originalDPR,
"devicePixelRatio has the original value.");
is(SpecialPowers.getFullZoom(window), originalZoom,
"fullZoom has the original value.");
is(frameWindow.devicePixelRatio, originalDPR,
"devicePixelRatio has the original value.");
is(SpecialPowers.getFullZoom(frameWindow), originalZoom,
"fullZoom has the original value.");
}
const gTests = {
"test overrideDPPX with devicePixelRatio": (done) => {
assertValuesAreInitial();
setOverrideDPPX(dppx);
is(window.devicePixelRatio, dppx,
"devicePixelRatio overridden.");
is(frameWindow.devicePixelRatio, dppx,
"frame's devicePixelRatio overridden.");
setOverrideDPPX(0);
is(window.devicePixelRatio, originalDPR,
"devicePixelRatio back to default.");
is(frameWindow.devicePixelRatio, originalDPR,
"frame's devicePixelRatio back to default.");
done();
},
"test overrideDPPX with devicePixelRatio and fullZoom": (done) => {
assertValuesAreInitial();
setFullZoom(zoom);
setOverrideDPPX(dppx);
is(window.devicePixelRatio, dppx,
"devicePixelRatio overridden; fullZoom ignored");
is(frameWindow.devicePixelRatio, dppx,
"frame's devicePixelRatio overridden; fullZoom ignored");
setOverrideDPPX(0);
is(window.devicePixelRatio, originalDPR * zoom,
"devicePixelRatio now is affected by fullZoom");
is(frameWindow.devicePixelRatio, originalDPR * zoom,
"frame's devicePixelRatio now is affected by fullZoom");
isnot(dppx, originalDPR * zoom,
"test is no longer testing what it should be");
setFullZoom(originalZoom);
is(window.devicePixelRatio, originalDPR,
"devicePixelRatio back to default.");
is(frameWindow.devicePixelRatio, originalDPR,
"frame's devicePixelRatio back to default.");
done();
},
"test overrideDPPX with media queries": (done) => {
assertValuesAreInitial();
let frameDoc = frameWindow.document;
let originalFontSize = getBodyFontSize(window);
let frameOriginalFontSize = getBodyFontSize(frameWindow);
let style = createFontStyleForDPPX(document, dppx, "32");
let frameStyle = createFontStyleForDPPX(frameDoc, dppx, "32");
let currentFontSize = getBodyFontSize(window);
let frameCurrentFontSize = getBodyFontSize(frameWindow);
is(currentFontSize, originalFontSize,
"media queries are not applied yet");
is(frameCurrentFontSize, frameOriginalFontSize,
"frame's media queries are not applied yet");
setOverrideDPPX(dppx);
currentFontSize = getBodyFontSize(window);
frameCurrentFontSize = getBodyFontSize(frameWindow);
isnot(currentFontSize, originalFontSize,
"media queries are applied.");
isnot(frameCurrentFontSize, frameOriginalFontSize,
"frame's media queries are applied.");
is(currentFontSize, "32px",
"font size has the expected value.");
is(frameCurrentFontSize, "32px",
"frame's font size has the expected value.");
setOverrideDPPX(0);
currentFontSize = getBodyFontSize(window);
frameCurrentFontSize = getBodyFontSize(frameWindow);
is(currentFontSize, originalFontSize,
"media queries are not applied anymore.");
is(frameCurrentFontSize, frameOriginalFontSize,
"media queries are not applied anymore.");
style.remove();
frameStyle.remove();
done();
},
"test overrideDPPX with media queries and fullZoom": (done) => {
assertValuesAreInitial();
let frameDoc = frameWindow.document;
let styles = [
createFontStyleForDPPX(document, originalDPR, "23"),
createFontStyleForDPPX(document, dppx, "32"),
createFontStyleForDPPX(document, originalDPR * zoom, "48"),
createFontStyleForDPPX(frameDoc, originalDPR, "23"),
createFontStyleForDPPX(frameDoc, dppx, "32"),
createFontStyleForDPPX(frameDoc, originalDPR * zoom, "48")
];
let currentFontSize = getBodyFontSize(window);
let frameCurrentFontSize = getBodyFontSize(frameWindow);
is(currentFontSize, "23px",
"media queries are not applied yet");
is(frameCurrentFontSize, "23px",
"frame's media queries are not applied yet");
setFullZoom(zoom);
setOverrideDPPX(dppx);
currentFontSize = getBodyFontSize(window);
frameCurrentFontSize = getBodyFontSize(frameWindow);
is(currentFontSize, "32px",
"media queries are applied for overridden DDPX; fullZoom ignored.");
is(frameCurrentFontSize, "32px",
"frame's media queries are applied for overridden DDPX; fullZoom ignored.");
setOverrideDPPX(0);
currentFontSize = getBodyFontSize(window);
frameCurrentFontSize = getBodyFontSize(frameWindow);
is(currentFontSize, "48px",
"media queries are applied for fullZoom.");
is(frameCurrentFontSize, "48px",
"frame's media queries are applied for fullZoom.");
setFullZoom(originalZoom);
currentFontSize = getBodyFontSize(window);
frameCurrentFontSize = getBodyFontSize(frameWindow);
is(currentFontSize, "23px",
"media queries are not applied anymore.");
is(frameCurrentFontSize, "23px",
"frame's media queries are not applied anymore.");
styles.forEach(style => style.remove());
done();
},
"test OverrideDPPX with MediaQueryList": (done) => {
assertValuesAreInitial();
let promises = [
new Promise(resolve => {
let mql = window.matchMedia(`(resolution: ${dppx}dppx)`);
mql.addListener(function listener() {
ok("MediaQueryList's listener invoked.")
mql.removeListener(listener);
resolve();
});
}),
new Promise(resolve => {
let mql = frameWindow.matchMedia(`(resolution: ${dppx}dppx)`);
mql.addListener(function listener() {
ok("frame's MediaQueryList's listener invoked.")
mql.removeListener(listener);
resolve();
});
})
];
Promise.all(promises)
.then(() => setOverrideDPPX(0))
.then(done, e => {throw e});
setOverrideDPPX(dppx);
},
"test OverrideDPPX with MediaQueryList and fullZoom": (done) => {
assertValuesAreInitial();
let overridden = false;
let promises = [
new Promise(resolve => {
let mql = window.matchMedia(`(resolution: ${dppx}dppx)`);
mql.addListener(function listener() {
ok("MediaQueryList's listener for dppx invoked.");
mql.removeListener(listener);
overridden = true;
resolve();
});
}),
new Promise(resolve => {
let mql = window.matchMedia(`(resolution: ${originalDPR * zoom}dppx)`);
mql.addListener(function listener() {
ok(overridden,
"MediaQueryList's listener for zoom invoked in the right order");
mql.removeListener(listener);
resolve();
});
})
];
promises[0]
.then(() => setOverrideDPPX(0))
.then(promises[1])
.then(() => setFullZoom(originalZoom))
.then(done, e => {throw e});
setOverrideDPPX(dppx);
setFullZoom(zoom);
},
"test OverrideDPPX is kept on document navigation": (done) => {
assertValuesAreInitial();
let frameOriginalFontSize = getBodyFontSize(frameWindow);
let frameStyle = createFontStyleForDPPX(frameWindow.document, dppx, "32");
let frameCurrentFontSize = getBodyFontSize(frameWindow);
is(frameCurrentFontSize, frameOriginalFontSize,
"frame's media queries are not applied yet");
setOverrideDPPX(dppx);
frameCurrentFontSize = getBodyFontSize(frameWindow);
is(frameWindow.devicePixelRatio, dppx,
"frame's devicePixelRatio overridden.");
isnot(frameCurrentFontSize, frameOriginalFontSize,
"frame's media queries are applied.");
is(frameCurrentFontSize, "32px",
"frame's font size has the expected value.");
frameWindow.frameElement.addEventListener("load", function listener() {
this.removeEventListener("load", listener);
frameStyle = createFontStyleForDPPX(frameWindow.document, dppx, "32");
frameCurrentFontSize = getBodyFontSize(frameWindow);
is(frameWindow.devicePixelRatio, dppx,
"frame's devicePixelRatio is still overridden.");
isnot(frameCurrentFontSize, frameOriginalFontSize,
"frame's media queries are still applied.");
is(frameCurrentFontSize, "32px",
"frame's font size has still the expected value.");
frameStyle.remove();
setOverrideDPPX(0);
done();
});
frameWindow.location.reload(true);
}
};
function* runner(tests) {
for (let name of Object.keys(tests)) {
info(name);
tests[name](next);
yield undefined;
}
};
const gTestRunner = runner(gTests);
function next() {
SimpleTest.executeSoon(function() {
if (gTestRunner.next().done) {
SimpleTest.finish();
}
});
}
// Run the tests
addLoadEvent(next);
</script>
</body>
</html>
|