summaryrefslogtreecommitdiffstats
path: root/image/test/mochitest/test_removal_ondecode.html
blob: fd594acf682959add7d56492552a090427bed1f5 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=841579
-->
<head>
  <title>Test for Bug 841579</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" 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=841579">Mozilla Bug 841579</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript;version=1.8">
/** Test for Bug 841579**/

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 gImg;
var gMyDecoderObserver;
var gIsTestFinished = false;
var gFiles;
var gNotifications = 0;
var gLoads = 0;

function fileToLoad() {
  yield "red.png";
  yield "invalid.jpg";
  yield "lime100x100.svg";
  yield "bad.jpg";
  yield "rillybad.jpg";
}

function onSizeAvailable(aRequest) {
  ok(true, "AfterLoad.onSizeAvailable called for " + gImg.src);
}
function onLoadComplete(aRequest) {
  ok(true, "AfterLoad.onLoadComplete called for " + gImg.src);
  gLoads++;
}
function onDecodeComplete(aRequest) {
  ok(true, "AfterLoad.onDecodeComplete called for " + gImg.src);
  SimpleTest.executeSoon(function() {
    try {
      gContent.removeChild(gImg);
    } 
    catch (e) {} 
  });
}

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

function onNotification()
{
  gNotifications++;
  try {
    gImg.src = gFiles.next();
    gContent.appendChild(gImg);
  } catch(e) {
    cleanUpAndFinish();
  }
}

function cleanUpAndFinish() {
  // On the off chance that failTest and myOnStopFrame are triggered
  // back-to-back, use a flag to prevent multiple calls to SimpleTest.finish.
  if (gIsTestFinished) {
    return;
  }
  let imgLoadingContent = SpecialPowers.wrap(gImg).QueryInterface(Ci.nsIImageLoadingContent);
  imgLoadingContent.removeObserver(gMyDecoderObserver);
  // TODO - this isn't the case until post-bug 716140's refactorings
  // ok(gNotifications == gLoads, "Should be notified the same number of times as loads");
  SimpleTest.finish();
  gIsTestFinished = true;
}

function main() {
  gFiles = fileToLoad();
  gImg = new Image();
  gImg.onload = onNotification;
  gImg.onerror = onNotification;

  // Create, customize & attach decoder observer
  observer = new ImageDecoderObserverStub();
  observer.sizeAvailable = onSizeAvailable;
  observer.loadComplete = onLoadComplete;
  observer.decodeComplete = onDecodeComplete;
  gMyDecoderObserver =
    Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
      .createScriptedObserver(SpecialPowers.wrapCallbackObject(observer));
  let imgLoadingContent = SpecialPowers.wrap(gImg).QueryInterface(Ci.nsIImageLoadingContent);
  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();

  // kick off image-loading! myOnStopFrame handles the rest.
  gImg.setAttribute("src", gFiles.next());

  // 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>