test_has_transparency.html 4.7 KB

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