summaryrefslogtreecommitdiffstats
path: root/parser/htmlparser/tests/mochitest/test_img_picture_preload.html
blob: 295993060bffdbd91b3e641a6bd211847490f4b4 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1067345
-->
<head>
  <title>Test for Bug 1067345</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
  <script>
    // Ensure srcset/picture are enabled, re-run the test at different DPI
    // levels to ensure preload step does the right responsive image selection

    SimpleTest.waitForExplicitFinish();

    var testDPIs = [ "1.0", "0.5", "2.0", "1.5" ];
    var iframe = document.createElement("iframe");

    // These accessed by child frame
    var currentDPI;

    document.body.appendChild(iframe);

    SpecialPowers.pushPrefEnv({"set": [ [ "dom.image.srcset.enabled", true ],
                                        [ "dom.image.picture.enabled", true ]] },
                              function() {
      // Reset the sjs helper so repeat runs work
      resetRequests();
      setTimeout(nextTest, 0);
    });

    function resetRequests() {
      // Ask the SJS to reset requests
      var request = new XMLHttpRequest();
      request.open("GET", "./file_img_picture_preload.sjs?reset", false);
      request.send(null);
      is(request.status, 200, "Sending reset to helper should succeed");
      // Script responds with pre-reset request count
      var previousRequests = +request.responseText;

      return previousRequests;
    }

    // Called when iframe is finished
    function childTestFinished(requestsMade) {
      setTimeout(function() {
        // Reset sjs, ensure no new requests appeared after test finished
        var requestsCleared = resetRequests();
        is(requestsCleared, requestsMade,
           "Should not have recorded new requests after test iteration completed");

        setTimeout(nextTest, 0);
      }, 0);
    }

    function nextTest() {
      // Re-run test for each DPI level
      if (testDPIs.length) {
        currentDPI = testDPIs.pop();
        info("Starting test for DPI: " + currentDPI);
        // To avoid spurious image loads being reported when the resolution changes,
        // load an intermediate iframe.
        iframe.src = "about:blank";
        iframe.addEventListener("load", function() {
          SpecialPowers.pushPrefEnv({"set": [ [ "layout.css.devPixelsPerPx", currentDPI ]] },
                                    function() {
            // Make sure we trigger a layout flush so that the frame is sized
            // appropriately after the DPI changes.
            iframe.getBoundingClientRect();
            // Clear image cache for next run (we don't try to validate cached items
            // in preload).
            SpecialPowers.Cc["@mozilla.org/image/tools;1"]
                         .getService(SpecialPowers.Ci.imgITools)
                         .getImgCacheForDocument(iframe.contentDocument)
                         .clearCache(false);
            iframe.src = "./file_img_picture_preload.html?" + currentDPI;
          });
        }, {once: true});
      } else {
        SimpleTest.finish();
      }
    }
  </script>

</body>
</html>