summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/test_picture_mutations.html
blob: b311ffc8a2675899db7057fb8b33828f38bade5e (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Image srcset mutations</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
  <script type="application/javascript">
    "use strict";

    // Tests the relevant mutations part of the spec for <img> inside <picture> tags
    // https://html.spec.whatwg.org/#relevant-mutations
    SimpleTest.waitForExplicitFinish();

    // 50x50 png
    var testPNG50 = new URL("image_50.png", location).href;
    // 100x100 png
    var testPNG100 = new URL("image_100.png", location).href;
    // 200x200 png
    var testPNG200 = new URL("image_200.png", location).href;

    var tests = [];
    var img;
    var picture;
    var source1;
    var source2;
    var source3;
    var expectingErrors = 0;
    var expectingLoads = 0;
    var afterExpectCallback;

    function onImgLoad() {
      ok(expectingLoads > 0, "expected load");
      if (expectingLoads > 0) {
        expectingLoads--;
      }
      if (!expectingLoads && !expectingErrors) {
        setTimeout(afterExpectCallback, 0);
      }
    }
    function onImgError() {
      ok(expectingErrors > 0, "expected error");
      if (expectingErrors > 0) {
        expectingErrors--;
      }
      if (!expectingLoads && !expectingErrors) {
        setTimeout(afterExpectCallback, 0);
      }
    }
    function expectEvents(loads, errors, callback) {
      if (!loads && !errors) {
        setTimeout(callback, 0);
      } else {
        expectingLoads += loads;
        expectingErrors += errors;
        info("Waiting for " + expectingLoads + " load and " + expectingErrors + " error events");
        afterExpectCallback = callback;
      }
    }

    // Setup image outside the tree dom, make sure it loads
    tests.push(function() {
      info("test 1");
      img.srcset = testPNG100;
      img.src = testPNG50;
      is(img.currentSrc, '', "Should not have synchronously selected source");

      // No events should have fired synchronously, now we should get just one load (and no 404 error)
      expectEvents(1, 0, nextTest);
    });

    // Binding to an empty picture should trigger an event, even if source doesn't change
    tests.push(function() {
      info("test 2");
      is(img.currentSrc, testPNG100, "Should have loaded testPNG100");
      document.body.appendChild(picture);
      picture.appendChild(img);
      is(img.currentSrc, testPNG100, "Should still have testPNG100");

      expectEvents(1, 0, nextTest);
    });

    // inserting and removing an empty source before the image should both trigger a no-op reload
    tests.push(function() {
      info("test 3");
      is(img.currentSrc, testPNG100, "Should still have testPNG100");
      picture.insertBefore(source1, img);
      is(img.currentSrc, testPNG100, "Should still have testPNG100");

      // should fire one event, not change source
      expectEvents(1, 0, function() {
        is(img.currentSrc, testPNG100, "Should still have testPNG100");
        picture.removeChild(source1);
        is(img.currentSrc, testPNG100, "Should still have testPNG100");

        // Should also no-op fire
        expectEvents(1, 0, nextTest);
      });
    });

    // insert and remove valid source before
    tests.push(function() {
      info("test 4");
      is(img.currentSrc, testPNG100, "Should still have testPNG100");

      // Insert source1 before img with valid candidate
      source1.srcset = testPNG50;
      picture.insertBefore(source1, img);
      is(img.currentSrc, testPNG100, "Should still have testPNG100");

      // should fire one event, change to the source
      expectEvents(1, 0, function() {
        is(img.currentSrc, testPNG50, "Should have switched to testPNG50");
        picture.removeChild(source1);
        is(img.currentSrc, testPNG50, "Should still have testPNG50");

        // Should also no-op fire
        expectEvents(1, 0, function() {
          is(img.currentSrc, testPNG100, "Should have returned to testPNG100");
          nextTest();
        });
      });
    });

    // insert and remove valid source after
    tests.push(function() {
      info("test 5");
      is(img.currentSrc, testPNG100, "Should still have testPNG100");

      // Insert source1 before img with valid candidate
      source1.srcset = testPNG50;
      picture.appendChild(source1);
      is(img.currentSrc, testPNG100, "Should still have testPNG100");

      // should fire nothing, no action
      expectEvents(0, 0, function() {
        is(img.currentSrc, testPNG100, "Should still have testPNG100");

        // Same with removing
        picture.removeChild(source1);
        expectEvents(0, 0, function() {
          is(img.currentSrc, testPNG100, "Should still have testPNG100");
          nextTest();
        });
      });
    });

    // Should re-consider earlier sources when a load event occurs.
    tests.push(function() {
      info("test 6");

      // Insert two valid sources, with MQ causing us to select the second
      source1.srcset = testPNG50 + " 1x";
      source1.media = "(min-resolution: 2dppx)"; // Wont match, test starts at 1x
      source2.srcset = testPNG200;
      picture.insertBefore(source1, img);
      picture.insertBefore(source2, img);
      is(img.currentSrc, testPNG100, "Should still have testPNG100");

      // should get one load, selecting source2
      expectEvents(1, 0, function() {
        is(img.currentSrc, testPNG200, "Should have selected testPNG200");

        expectEvents(1, 0, function() {
          is(img.currentSrc, testPNG50, "Should have switched to testPNG50");

          // Now add a source *also* wanting that DPI *just before* the
          // selected source. Properly re-running the algorithm should
          // re-consider all sources and thus go back to the first
          // source, not just the valid source just inserted before us.
          source3.media = source1.media;
          source3.srcset = testPNG100;
          picture.insertBefore(source3, source2);
          // This should trigger a reload, but we should re-consider
          // source1 and remain with that, not just the newly added source2
          expectEvents(1, 0, function() {
            is(img.currentSrc, testPNG50, "Should have remained on testPNG50");
            expectEvents(0, 0, nextTest);
          });
        });

        // Switch DPI to match the first source.
        SpecialPowers.pushPrefEnv({'set': [ ["layout.css.devPixelsPerPx", "2.0"] ] });
      });
    });

    // insert and remove valid source after our current source should
    // trigger a reload, but not switch source
    tests.push(function() {
      info("test 7");
      // Should be using source1 from last test
      is(img.currentSrc, testPNG50, "Should still have testPNG50");

      // Remove source2, should trigger an event even though we would
      // not switch

      picture.removeChild(source2);
      expectEvents(1, 0, function() {
        is(img.currentSrc, testPNG50, "Should still have testPNG50");

        // Same with re-adding
        picture.insertBefore(source2, img);
        expectEvents(1, 0, function() {
          is(img.currentSrc, testPNG50, "Should still have testPNG50");
          expectEvents(0, 0, nextTest);
        });
      });
    });

    // Changing source attributes should trigger updates
    tests.push(function() {
      info("test 8");
      // Should be using source1 from last test
      is(img.currentSrc, testPNG50, "Should still have testPNG50");

      // Reconfigure source1 to have empty srcset. Should switch to
      // next source due to becoming invalid.
      source1.srcset = "";
      is(img.currentSrc, testPNG50, "Should still have testPNG50");

      expectEvents(1, 0, function() {
        is(img.currentSrc, testPNG100, "Should have switched to testPNG100");

        // Give source1 valid sizes. Should trigger an event but not switch anywhere.
        source1.sizes = "100vw";

        expectEvents(1, 0, function() {
          is(img.currentSrc, testPNG100, "Should still have testPNG100");

          // And a valid MQ
          source1.media = "(min-resolution: 1dppx)";
          expectEvents(1, 0, function() {
            // And a valid type...
            source1.type = "image/png";
            expectEvents(1, 0, function() {
              // Finally restore srcset, should trigger load and re-consider source1 which is valid again
              source1.srcset = testPNG50;
              expectEvents(1, 0, function() {
                is(img.currentSrc, testPNG50, "Should have selected testPNG50");
                expectEvents(0, 0, nextTest);
              });
            });
          });
        });
      });
    });

    // Inserting a source after <img> and touching its attributes should all be no-ops
    tests.push(function() {
      info("test 9");
      // Setup: source2
      picture.removeChild(source2);

      expectEvents(1, 0, function() {
        is(img.currentSrc, testPNG50, "Should still have testPNG50");

        source2.srcset = testPNG200;
        source2.media = "";
        source2.sizes = "100vw";
        source2.type = "image/png";
        // Append valid source
        picture.appendChild(source2);
        // Touch all the things (but keep it valid)
        source2.srcset = testPNG100;
        source2.media = "(min-resolution: 2dppx)";
        source2.sizes = "50vw";
        source2.type = "image/png";

        // No event should fire. Source should not change.
        expectEvents(0, 0, function() {
          is(img.currentSrc, testPNG50, "Should still have testPNG50");
          expectEvents(0, 0, nextTest);
        });
      });
    });

    function nextTest() {
      if (tests.length) {
        // Spin event loop to make sure no unexpected image events are
        // pending (unexpected events will assert in the handlers)
        setTimeout(function() {
          (tests.shift())();
        }, 0);
      } else {
        // We'll get a flood of load events due to prefs being popped while cleaning up.
        // Ignore it all.
        img.removeEventListener("load", onImgLoad);
        img.removeEventListener("error", onImgError);
        SimpleTest.finish();
      }
    }

    addEventListener("load", function() {
      SpecialPowers.pushPrefEnv({'set': [["layout.css.devPixelsPerPx", "1.0" ]] },
                                function() {
        // Create these after the pref is set, as it is guarding webIDL attributes
        img = document.createElement("img");
        img.addEventListener("load", onImgLoad);
        img.addEventListener("error", onImgError);
        picture = document.createElement("picture");
        source1 = document.createElement("source");
        source2 = document.createElement("source");
        source3 = document.createElement("source");
        setTimeout(nextTest, 0);
      });
    });
  </script>
</body>
</html>