tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_has_transparency.html (4641B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=1089880
      5 -->
      6 <head>
      7  <title>Test for Bug 1089880</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <script src="/tests/SimpleTest/WindowSnapshot.js"></script>
     10  <script type="application/javascript" src="imgutils.js"></script>
     11  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     12 </head>
     13 <body>
     14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1089880">Mozilla Bug 1089880</a>
     15 <p id="display"></p>
     16 <div id="content">
     17 </div>
     18 <pre id="test">
     19 <script type="application/javascript">
     20 /** Test for Bug 1089880 */
     21 
     22 SimpleTest.requestFlakyTimeout("Early failure timeout");
     23 SimpleTest.waitForExplicitFinish();
     24 
     25 const FAILURE_TIMEOUT = 120000; // Fail early after 120 seconds (2 minutes)
     26 
     27 const Cc = SpecialPowers.Cc;
     28 const Ci = SpecialPowers.Ci;
     29 const gContent = document.getElementById("content");
     30 
     31 var gCanvas;
     32 var gCanvasCtx;
     33 var gImg;
     34 var gMyDecoderObserver;
     35 var gIsTestFinished = false;
     36 var gFiles;
     37 var gCurrentFileIsTransparent = false;
     38 var gHasTransparencyWasCalled = false;
     39 
     40 function* testFiles() {
     41  // [A, B] where 'A' is the image and 'B' is whether it's transparent.
     42 
     43  // PNGs and GIFs may be transparent or not.
     44  yield ["red.png", false];
     45  yield ["transparent.png", true];
     46  yield ["animated-gif-finalframe.gif", false];
     47  yield ["transparent.gif", true];
     48 
     49  // GIFs with padding on the first frame are always transparent.
     50  yield ["first-frame-padding.gif", true];
     51 
     52  // JPEGs are never transparent.
     53  yield ["damon.jpg", false];
     54 
     55  // Most BMPs are not transparent. (The TestMetadata GTest, which will
     56  // eventually replace this test totally, has coverage for the kinds that can be
     57  // transparent.)
     58  yield ["opaque.bmp", false];
     59 
     60  // ICO files which contain BMPs have an additional type of transparency - the
     61  // AND mask - that warrants separate testing. (Although, after bug 1201796,
     62  // all ICOs are considered transparent.)
     63  yield ["ico-bmp-opaque.ico", true];
     64  yield ["ico-bmp-transparent.ico", true];
     65 
     66  // SVGs are always transparent.
     67  yield ["lime100x100.svg", true];
     68 }
     69 
     70 function loadNext() {
     71  var currentFile = "";
     72  gHasTransparencyWasCalled = false;
     73  let {done, value} = gFiles.next();
     74  if (done) {
     75    // We ran out of test files.
     76    cleanUpAndFinish();
     77    return;
     78  }
     79  [currentFile, gCurrentFileIsTransparent] = value;
     80  gImg.setAttribute("src", currentFile);
     81 }
     82 
     83 function onHasTransparency() {
     84  gHasTransparencyWasCalled = true;
     85 }
     86 
     87 function onDecodeComplete() {
     88  if (!gCurrentFileIsTransparent) {
     89    ok(!gHasTransparencyWasCalled,
     90       "onHasTransparency was not called for non-transparent file " + gImg.src);
     91  } else {
     92    ok(gHasTransparencyWasCalled,
     93       "onHasTransparency was called for transparent file " + gImg.src);
     94  }
     95  loadNext();
     96 }
     97 
     98 function onError() {
     99  if (gIsTestFinished) {
    100    return;
    101  }
    102  ok(false, "Should successfully load " + gImg.src);
    103  loadNext();
    104 }
    105 
    106 function onLoad() {
    107  if (gIsTestFinished) {
    108    return;
    109  }
    110  ok(true, "Should successfully load " + gImg.src);
    111 
    112  // Force decoding of the image.
    113  SimpleTest.executeSoon(function() {
    114    gCanvasCtx.drawImage(gImg, 0, 0);
    115  });
    116 }
    117 
    118 function failTest() {
    119  ok(false, "timing out after " + FAILURE_TIMEOUT + "ms.  " +
    120            "currently displaying " + gImg.src);
    121  cleanUpAndFinish();
    122 }
    123 
    124 function cleanUpAndFinish() {
    125  if (gIsTestFinished) {
    126    return;
    127  }
    128  gIsTestFinished = true;
    129  let imgLoadingContent = SpecialPowers.wrap(gImg);
    130  imgLoadingContent.removeObserver(gMyDecoderObserver);
    131  SimpleTest.finish();
    132 }
    133 
    134 function main() {
    135  gFiles = testFiles();
    136  gCanvas = document.createElement('canvas');
    137  gCanvasCtx = gCanvas.getContext('2d');
    138  gImg = new Image();
    139  gImg.onload = onLoad;
    140  gImg.onerror = onError;
    141 
    142  // Create, customize & attach decoder observer.
    143  var observer = new ImageDecoderObserverStub();
    144  observer.hasTransparency = onHasTransparency;
    145  observer.decodeComplete = onDecodeComplete;
    146  gMyDecoderObserver =
    147    Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
    148      .createScriptedObserver(SpecialPowers.wrapCallbackObject(observer));
    149  let imgLoadingContent = SpecialPowers.wrap(gImg);
    150  imgLoadingContent.addObserver(gMyDecoderObserver);
    151 
    152  // We want to test the cold loading behavior, so clear cache in case an
    153  // earlier test got our image in there already.
    154  clearAllImageCaches();
    155 
    156  // Load the first image.
    157  loadNext();
    158 
    159  // In case something goes wrong, fail earlier than mochitest timeout,
    160  // and with more information.
    161  setTimeout(failTest, FAILURE_TIMEOUT);
    162 }
    163 
    164 window.onload = main;
    165 
    166 </script>
    167 </pre>
    168 </body>
    169 </html>