summaryrefslogtreecommitdiffstats
path: root/image/test/mochitest/test_has_transparency.html
blob: cec8edfe7874974777bccd3e7edfe364c88a884b (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1089880
-->
<head>
  <title>Test for Bug 1089880</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/WindowSnapshot.js"></script>
  <script type="application/javascript" src="imgutils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1089880">Mozilla Bug 1089880</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 1089880 **/

SimpleTest.requestFlakyTimeout("Early failure timeout");
SimpleTest.waitForExplicitFinish();

const FAILURE_TIMEOUT = 120000; // Fail early after 120 seconds (2 minutes)

const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;
const gContent = document.getElementById("content");

var gCanvas;
var gCanvasCtx;
var gImg;
var gMyDecoderObserver;
var gIsTestFinished = false;
var gFiles;
var gCurrentFileIsTransparent = false;
var gHasTransparencyWasCalled = false;

function* testFiles() {
  // [A, B] where 'A' is the image and 'B' is whether it's transparent.

  // PNGs and GIFs may be transparent or not.
  yield ["red.png", false];
  yield ["transparent.png", true];
  yield ["animated-gif-finalframe.gif", false];
  yield ["transparent.gif", true];

  // GIFs with padding on the first frame are always transparent.
  yield ["first-frame-padding.gif", true];

  // JPEGs are never transparent.
  yield ["damon.jpg", false];

  // Most BMPs are not transparent. (The TestMetadata GTest, which will
  // eventually replace this test totally, has coverage for the kinds that can be
  // transparent.)
  yield ["opaque.bmp", false];

  // ICO files which contain BMPs have an additional type of transparency - the
  // AND mask - that warrants separate testing. (Although, after bug 1201796,
  // all ICOs are considered transparent.)
  yield ["ico-bmp-opaque.ico", true];
  yield ["ico-bmp-transparent.ico", true];

  // SVGs are always transparent.
  yield ["lime100x100.svg", true];
}

function loadNext() {
  var currentFile = "";
  gHasTransparencyWasCalled = false;
  let {done, value} = gFiles.next();
  if (done) {
    // We ran out of test files.
    cleanUpAndFinish();
    return;
  }
  [currentFile, gCurrentFileIsTransparent] = value;
  gImg.setAttribute("src", currentFile);
}

function onHasTransparency() {
  gHasTransparencyWasCalled = true;
}

function onDecodeComplete() {
  if (!gCurrentFileIsTransparent) {
    ok(!gHasTransparencyWasCalled,
       "onHasTransparency was not called for non-transparent file " + gImg.src);
  } else {
    ok(gHasTransparencyWasCalled,
       "onHasTransparency was called for transparent file " + gImg.src);
  }
  loadNext();
}

function onError() {
  if (gIsTestFinished) {
    return;
  }
  ok(false, "Should successfully load " + gImg.src);
  loadNext();
}

function onLoad() {
  if (gIsTestFinished) {
    return;
  }
  ok(true, "Should successfully load " + gImg.src);

  // Force decoding of the image.
  SimpleTest.executeSoon(function() {
    gCanvasCtx.drawImage(gImg, 0, 0);
  });
}

function failTest() {
  ok(false, "timing out after " + FAILURE_TIMEOUT + "ms.  " +
            "currently displaying " + gImg.src);
  cleanUpAndFinish();
}

function cleanUpAndFinish() {
  if (gIsTestFinished) {
    return;
  }
  gIsTestFinished = true;
  let imgLoadingContent = SpecialPowers.wrap(gImg);
  imgLoadingContent.removeObserver(gMyDecoderObserver);
  SimpleTest.finish();
}

function main() {
  gFiles = testFiles();
  gCanvas = document.createElement('canvas');
  gCanvasCtx = gCanvas.getContext('2d');
  gImg = new Image();
  gImg.onload = onLoad;
  gImg.onerror = onError;

  // Create, customize & attach decoder observer.
  var observer = new ImageDecoderObserverStub();
  observer.hasTransparency = onHasTransparency;
  observer.decodeComplete = onDecodeComplete;
  gMyDecoderObserver =
    Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
      .createScriptedObserver(SpecialPowers.wrapCallbackObject(observer));
  let imgLoadingContent = SpecialPowers.wrap(gImg);
  imgLoadingContent.addObserver(gMyDecoderObserver);

  // We want to test the cold loading behavior, so clear cache in case an
  // earlier test got our image in there already.
  clearAllImageCaches();

  // Load the first image.
  loadNext();

  // In case something goes wrong, fail earlier than mochitest timeout,
  // and with more information.
  setTimeout(failTest, FAILURE_TIMEOUT);
}

window.onload = main;

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