image/test/mochitest/test_removal_ondecode.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/test/mochitest/test_removal_ondecode.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=841579
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 841579</title>
    1.11 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"></script>
    1.13 +  <script type="application/javascript" src="imgutils.js"></script>
    1.14 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
    1.15 +</head>
    1.16 +<body>
    1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=841579">Mozilla Bug 841579</a>
    1.18 +<p id="display"></p>
    1.19 +<div id="content">
    1.20 +</div>
    1.21 +<pre id="test">
    1.22 +<script type="application/javascript;version=1.8">
    1.23 +/** Test for Bug 841579**/
    1.24 +
    1.25 +SimpleTest.waitForExplicitFinish();
    1.26 +
    1.27 +const FAILURE_TIMEOUT = 120000; // Fail early after 120 seconds (2 minutes)
    1.28 +
    1.29 +const Cc = Components.classes;
    1.30 +const Ci = Components.interfaces;
    1.31 +const gContent = document.getElementById("content");
    1.32 +
    1.33 +var gImg;
    1.34 +var gMyDecoderObserver;
    1.35 +var gIsTestFinished = false;
    1.36 +var gFiles;
    1.37 +var gNotifications = 0;
    1.38 +var gLoads = 0;
    1.39 +
    1.40 +function fileToLoad() {
    1.41 +  yield "red.png";
    1.42 +  yield "invalid.jpg";
    1.43 +  yield "lime100x100.svg";
    1.44 +  yield "bad.jpg";
    1.45 +  yield "rillybad.jpg";
    1.46 +}
    1.47 +
    1.48 +function onSizeAvailable(aRequest) {
    1.49 +  ok(true, "AfterLoad.onSizeAvailable called for " + gImg.src);
    1.50 +}
    1.51 +function onLoadComplete(aRequest) {
    1.52 +  ok(true, "AfterLoad.onLoadComplete called for " + gImg.src);
    1.53 +  gLoads++;
    1.54 +}
    1.55 +function onDecodeComplete(aRequest) {
    1.56 +  ok(true, "AfterLoad.onDecodeComplete called for " + gImg.src);
    1.57 +  SimpleTest.executeSoon(function() {
    1.58 +    try {
    1.59 +      gContent.removeChild(gImg);
    1.60 +    } 
    1.61 +    catch (e) {} 
    1.62 +  });
    1.63 +}
    1.64 +
    1.65 +function failTest() {
    1.66 +  ok(false, "timing out after " + FAILURE_TIMEOUT + "ms.  " +
    1.67 +            "currently displaying " + gImg.src);
    1.68 +  cleanUpAndFinish();
    1.69 +}
    1.70 +
    1.71 +function onNotification()
    1.72 +{
    1.73 +  gNotifications++;
    1.74 +  try {
    1.75 +    gImg.src = gFiles.next();
    1.76 +    gContent.appendChild(gImg);
    1.77 +  } catch(e) {
    1.78 +    cleanUpAndFinish();
    1.79 +  }
    1.80 +}
    1.81 +
    1.82 +function cleanUpAndFinish() {
    1.83 +  // On the off chance that failTest and myOnStopFrame are triggered
    1.84 +  // back-to-back, use a flag to prevent multiple calls to SimpleTest.finish.
    1.85 +  if (gIsTestFinished) {
    1.86 +    return;
    1.87 +  }
    1.88 +  let imgLoadingContent = gImg.QueryInterface(Ci.nsIImageLoadingContent);
    1.89 +  imgLoadingContent.removeObserver(gMyDecoderObserver);
    1.90 +  // TODO - this isn't the case until post-bug 716140's refactorings
    1.91 +  // ok(gNotifications == gLoads, "Should be notified the same number of times as loads");
    1.92 +  SimpleTest.finish();
    1.93 +  gIsTestFinished = true;
    1.94 +}
    1.95 +
    1.96 +function main() {
    1.97 +  gFiles = fileToLoad();
    1.98 +  gImg = new Image();
    1.99 +  gImg.onload = onNotification;
   1.100 +  gImg.onerror = onNotification;
   1.101 +
   1.102 +  // Create, customize & attach decoder observer
   1.103 +  observer = new ImageDecoderObserverStub();
   1.104 +  observer.sizeAvailable = onSizeAvailable;
   1.105 +  observer.loadComplete = onLoadComplete;
   1.106 +  observer.decodeComplete = onDecodeComplete;
   1.107 +  gMyDecoderObserver =
   1.108 +    Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
   1.109 +      .createScriptedObserver(observer);
   1.110 +  let imgLoadingContent = gImg.QueryInterface(Ci.nsIImageLoadingContent);
   1.111 +  imgLoadingContent.addObserver(gMyDecoderObserver);
   1.112 +
   1.113 +  // We want to test the cold loading behavior, so clear cache in case an
   1.114 +  // earlier test got our image in there already.
   1.115 +  clearImageCache();
   1.116 +
   1.117 +  // kick off image-loading! myOnStopFrame handles the rest.
   1.118 +  gImg.setAttribute("src", gFiles.next());
   1.119 +
   1.120 +  // In case something goes wrong, fail earlier than mochitest timeout,
   1.121 +  // and with more information.
   1.122 +  setTimeout(failTest, FAILURE_TIMEOUT);
   1.123 +}
   1.124 +
   1.125 +window.onload = main;
   1.126 +
   1.127 +</script>
   1.128 +</pre>
   1.129 +</body>
   1.130 +</html>

mercurial