summaryrefslogtreecommitdiffstats
path: root/image/test/reftest/ico/ico-bmp-corrupted/wrapper.html
blob: 56c1f79b3685cff135a650d562cdb8c1d3392c1e (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
<!DOCTYPE HTML>
<html class="reftest-wait">
<head>
<title>Image reftest wrapper</title>
<style type="text/css">
  #image1 { background-color: rgb(10, 100, 250); }
</style>
<script>
  var gImg;

  function runAfterAsyncEvents(aCallback) {
    function handlePostMessage(aEvent) {
      if (aEvent.data == 'next') {
        window.removeEventListener('message', handlePostMessage, false);
        aCallback();
      }
    }

    window.addEventListener('message', handlePostMessage, false);

    // We'll receive the 'message' event after everything else that's currently in
    // the event queue (which is a stronger guarantee than setTimeout, because
    // setTimeout events may be coalesced). This lets us ensure that we run
    // aCallback *after* any asynchronous events are delivered.
    window.postMessage('next', '*');
  }

  // The image is loaded async after the page loads
  // wait for it to finish loading
  function onImageLoad() { 
    // Use a canvas to force the image to get sync decoded.
    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');

    try {
      ctx.drawImage(gImg, 0, 0);
    } catch (e) {
    }

    // Continue after pumping the event loop.
    runAfterAsyncEvents(step2);
  }

  function step2() {
    // Only now, once the image has already been sync decoded, do we load it in
    // the <img> element we're going to snapshot. That's because for some of the
    // tests that use wrapper.html, an error is only detected when decoding the
    // actual image data - i.e., the error isn't detected in the header. The
    // precise time when we detect the error, unfortunately, affects how we draw
    // the image. This will be fixed in bug 1182531, and then we can simplify this
    // code.

    var finalImg = document.getElementById('image1');
    finalImg.onload = finalImg.onerror = step3;
    finalImg.src = gImg.src;
  }

  function step3() {
    // We're ready to take the snapshot, but pump the event loop first just to
    // be sure that everything has settled down.
    runAfterAsyncEvents(takeSnapshot);
  }

  function takeSnapshot() {
    document.documentElement.removeAttribute("class");
  }
</script>
</head>
<body>
<!-- non-empty alt to avoid the broken image icon -->
<img id="image1" alt=" ">
<script>
  // Use as "wrapper.html?image.png
  gImg = document.createElement('img');
  gImg.onload = gImg.onerror = onImageLoad;
  gImg.src = document.location.search.substr(1);
</script>
</body>
</html>