summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_media_query_list.html
blob: 8fec174e969b1a31f3675fca6a859d6d96d140ab (plain)
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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=542058
-->
<head>
  <title>Test for MediaQueryList (Bug 542058)</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="run()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=542058">Mozilla Bug 542058</a>
<iframe id="subdoc" src="about:blank"></iframe>
<div id="content" style="display:none"></div>
<pre id="test">
<script type="application/javascript">

/** Test for MediaQueryList (Bug 542058) **/

SimpleTest.waitForExplicitFinish();

function run() {
  var iframe = document.getElementById("subdoc");
  var subdoc = iframe.contentDocument;
  var subwin = iframe.contentWindow;
  var subroot = subdoc.documentElement;

  var content_div = document.getElementById("content");
  content_div.style.font = "initial";
  var em_size =
    getComputedStyle(content_div).fontSize.match(/^(\d+)px$/)[1];

  var w = Math.floor(em_size * 9.3);
  var h = Math.floor(em_size * 4.2);
  iframe.style.width = w + "px";
  iframe.style.height = h + "px";
  subroot.offsetWidth; // flush layout

  function setup_mql(str) {
    var obj = {
      str: str,
      mql: subwin.matchMedia(str),
      notifyCount: 0,
      listener: function(event) {
                  ok(event instanceof subwin.MediaQueryListEvent,
                     "correct argument to listener: " + obj.str);
                  is(event.media, obj.mql.media,
                     "correct media in the event: " + obj.str);
                  is(event.target, obj.mql,
                     "correct target in the event: " + obj.str);
                  ++obj.notifyCount;
                  // Test the last match result only on odd
                  // notifications.
                  if (obj.notifyCount & 1) {
                    obj.lastOddMatchResult = event.target.matches;
                  }
                }
    }
    obj.mql.addListener(obj.listener);
    return obj;
  }

  function finish_mql(obj) {
    obj.mql.removeListener(obj.listener);
  }

  var w_exact_w = setup_mql("(width: " + w + "px)");
  var w_min_9em = setup_mql("(min-width : 9em)");
  var w_min_10em = setup_mql("(  min-width: 10em ) ");
  var w_max_9em = setup_mql("(max-width: 9em)");
  var w_max_10em = setup_mql("(max-width: 10em)");

  is(w_exact_w.mql.media, "(width: " + w + "px)", "serialization");
  is(w_min_9em.mql.media, "(min-width: 9em)", "serialization");
  is(w_min_10em.mql.media, "(min-width: 10em)", "serialization");
  is(w_max_9em.mql.media, "(max-width: 9em)", "serialization");
  is(w_max_10em.mql.media, "(max-width: 10em)", "serialization");

  function check_match(obj, expected, desc) {
    is(obj.mql.matches, expected,
       obj.str + " media query list .matches " + desc);
    if (obj.notifyCount & 1) { // odd notifications only
      is(obj.lastOddMatchResult, expected,
       obj.str + " media query list last notify result " + desc);
    }
  }
  function check_notify(obj, expected, desc) {
    is(obj.notifyCount, expected,
       obj.str + " media query list .notify count " + desc);
  }
  check_match(w_exact_w, true, "initially");
  check_notify(w_exact_w, 0, "initially");
  check_match(w_min_9em, true, "initially");
  check_notify(w_min_9em, 0, "initially");
  check_match(w_min_10em, false, "initially");
  check_notify(w_min_10em, 0, "initially");
  check_match(w_max_9em, false, "initially");
  check_notify(w_max_9em, 0, "initially");
  check_match(w_max_10em, true, "initially");
  check_notify(w_max_10em, 0, "initially");

  var w2 = Math.floor(em_size * 10.3);
  iframe.style.width = w2 + "px";
  subroot.offsetWidth; // flush layout

  check_match(w_exact_w, false, "after width increase to around 10.3em");
  check_notify(w_exact_w, 1, "after width increase to around 10.3em");
  check_match(w_min_9em, true, "after width increase to around 10.3em");
  check_notify(w_min_9em, 0, "after width increase to around 10.3em");
  check_match(w_min_10em, true, "after width increase to around 10.3em");
  check_notify(w_min_10em, 1, "after width increase to around 10.3em");
  check_match(w_max_9em, false, "after width increase to around 10.3em");
  check_notify(w_max_9em, 0, "after width increase to around 10.3em");
  check_match(w_max_10em, false, "after width increase to around 10.3em");
  check_notify(w_max_10em, 1, "after width increase to around 10.3em");

  var w3 = w * 2;
  iframe.style.width = w3 + "px";
  subroot.offsetWidth; // flush layout

  check_match(w_exact_w, false, "after width double from original");
  check_notify(w_exact_w, 1, "after width double from original");
  check_match(w_min_9em, true, "after width double from original");
  check_notify(w_min_9em, 0, "after width double from original");
  check_match(w_min_10em, true, "after width double from original");
  check_notify(w_min_10em, 1, "after width double from original");
  check_match(w_max_9em, false, "after width double from original");
  check_notify(w_max_9em, 0, "after width double from original");
  check_match(w_max_10em, false, "after width double from original");
  check_notify(w_max_10em, 1, "after width double from original");

  SpecialPowers.setFullZoom(subwin, 2.0);
  subroot.offsetWidth; // flush layout

  check_match(w_exact_w, true, "after zoom");
  check_notify(w_exact_w, 2, "after zoom");
  check_match(w_min_9em, true, "after zoom");
  check_notify(w_min_9em, 0, "after zoom");
  check_match(w_min_10em, false, "after zoom");
  check_notify(w_min_10em, 2, "after zoom");
  check_match(w_max_9em, false, "after zoom");
  check_notify(w_max_9em, 0, "after zoom");
  check_match(w_max_10em, true, "after zoom");
  check_notify(w_max_10em, 2, "after zoom");

  SpecialPowers.setFullZoom(subwin, 1.0);

  finish_mql(w_exact_w);
  finish_mql(w_min_9em);
  finish_mql(w_min_10em);
  finish_mql(w_max_9em);
  finish_mql(w_max_10em);

  // Additional tests of listener mutation.
  (function() {
    var received = [];
    var received_mql = [];
    function listener1(event) {
      received.push(1);
      received_mql.push(event.target);
    }
    function listener2(event) {
      received.push(2);
      received_mql.push(event.target);
    }

    iframe.style.width = "200px";
    subroot.offsetWidth; // flush layout

    var mql = subwin.matchMedia("(min-width: 150px)");
    mql.addListener(listener1);
    mql.addListener(listener1);
    mql.addListener(listener2);
    is(JSON.stringify(received), "[]", "listeners before notification");

    iframe.style.width = "100px";
    subroot.offsetWidth; // flush layout

    is(JSON.stringify(received), "[1,2]", "duplicate listeners removed");
    received = [];
    mql.removeListener(listener1);

    iframe.style.width = "200px";
    subroot.offsetWidth; // flush layout

    is(JSON.stringify(received), "[2]", "listener removal");
    received = [];
    mql.addListener(listener1);

    iframe.style.width = "100px";
    subroot.offsetWidth; // flush layout

    is(JSON.stringify(received), "[2,1]", "listeners notified in order");
    received = [];
    mql.addListener(listener2);

    iframe.style.width = "200px";
    subroot.offsetWidth; // flush layout

    is(JSON.stringify(received), "[2,1]", "add of existing listener is no-op");
    received = [];
    mql.addListener(listener1);

    iframe.style.width = "100px";
    subroot.offsetWidth; // flush layout

    is(JSON.stringify(received), "[2,1]", "add of existing listener is no-op");
    mql.removeListener(listener2);
    received = [];
    received_mql = [];

    var mql2 = subwin.matchMedia("(min-width: 160px)");
    mql2.addListener(listener1);
    mql.addListener(listener2);

    iframe.style.width = "200px";
    subroot.offsetWidth; // flush layout

    // mql (1, 2), mql2 (1)
    is(JSON.stringify(received), "[1,2,1]",
       "notification of lists in order created");
    is(received_mql[0], mql,
       "notification of lists in order created");
    is(received_mql[1], mql,
       "notification of lists in order created");
    is(received_mql[2], mql2,
       "notification of lists in order created");
    received = [];
    received_mql = [];

    function removing_listener(event) {
      received.push(3);
      received_mql.push(event.target);
      event.target.removeListener(listener2);
      mql2.removeListener(listener1);
    }

    mql.addListener(removing_listener);
    mql.removeListener(listener2);
    mql.addListener(listener2); // after removing_listener (3)

    iframe.style.width = "100px";
    subroot.offsetWidth; // flush layout

    // mql(1, 3)
    is(JSON.stringify(received), "[1,3]",
       "listeners still notified after removed if change was before");
    is(received_mql[0], mql,
       "notification order (removal tests)");
    is(received_mql[1], mql,
       "notification order (removal tests)");
    received = [];
    received_mql = [];

    iframe.style.width = "200px";
    subroot.offsetWidth; // flush layout

    // mql(1, 3)
    is(JSON.stringify(received), "[1,3]",
       "listeners not notified for changes after their removal");
    is(received_mql[0], mql,
       "notification order (removal tests)");
    is(received_mql[1], mql,
       "notification order (removal tests)");
  })();

  /* Bug 753777: test that things work in a freshly-created iframe */
  (function() {
    let newIframe = document.createElement("iframe");
    document.body.appendChild(newIframe);

    is(newIframe.contentWindow.matchMedia("all").matches, true,
       "matchMedia should work in newly-created iframe");
    is(newIframe.contentWindow.matchMedia("(min-width: 1px)").matches, true,
       "(min-width: 1px) should match in newly-created iframe");
    is(newIframe.contentWindow.matchMedia("(max-width: 1px)").matches, false,
       "(max-width: 1px) should not match in newly-created iframe");

    document.body.removeChild(newIframe);
  })();

  /* Bug 716751: listeners lost due to GC */
  var gc_received = [];
  (function() {
    var received = [];
    var listener1 = function(event) {
      gc_received.push(1);
    }

    iframe.style.width = "200px";
    subroot.offsetWidth; // flush layout

    var mql = subwin.matchMedia("(min-width: 150px)");
    mql.addListener(listener1);
    is(JSON.stringify(gc_received), "[]", "GC test: before notification");

    iframe.style.width = "100px";
    subroot.offsetWidth; // flush layout

    is(JSON.stringify(gc_received), "[1]", "GC test: after notification 1");

    // Because of conservative GC, we need to go back to the event loop
    // to GC properly.
    setTimeout(step2, 0);
  })();

  function step2() {
    SpecialPowers.DOMWindowUtils.garbageCollect();

    iframe.style.width = "200px";
    subroot.offsetWidth; // flush layout

    is(JSON.stringify(gc_received), "[1,1]", "GC test: after notification 2");

    bug1270626();
  }

  /* Bug 1270626: listeners that throw exceptions */
  function bug1270626() {
    var throwingListener = function(event) {
      throw "error";
    }

    iframe.style.width = "200px";
    subroot.offsetWidth; // flush layout

    var mql = subwin.matchMedia("(min-width: 150px)");
    mql.addListener(throwingListener);

    SimpleTest.expectUncaughtException(true);
    is(SimpleTest.isExpectingUncaughtException(), true,
       "should be waiting for an uncaught exception");

    iframe.style.width = "100px";
    subroot.offsetWidth; // flush layout

    is(SimpleTest.isExpectingUncaughtException(), false,
       "should have gotten an uncaught exception");

    SimpleTest.finish();
  }
}

</script>
</pre>
</body>
</html>