test_removal_ondecode.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=841579
  5. -->
  6. <head>
  7. <title>Test for Bug 841579</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=841579">Mozilla Bug 841579</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 841579**/
  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 gImg;
  28. var gMyDecoderObserver;
  29. var gIsTestFinished = false;
  30. var gFiles;
  31. var gNotifications = 0;
  32. var gLoads = 0;
  33. function fileToLoad() {
  34. yield "red.png";
  35. yield "invalid.jpg";
  36. yield "lime100x100.svg";
  37. yield "bad.jpg";
  38. yield "rillybad.jpg";
  39. }
  40. function onSizeAvailable(aRequest) {
  41. ok(true, "AfterLoad.onSizeAvailable called for " + gImg.src);
  42. }
  43. function onLoadComplete(aRequest) {
  44. ok(true, "AfterLoad.onLoadComplete called for " + gImg.src);
  45. gLoads++;
  46. }
  47. function onDecodeComplete(aRequest) {
  48. ok(true, "AfterLoad.onDecodeComplete called for " + gImg.src);
  49. SimpleTest.executeSoon(function() {
  50. try {
  51. gContent.removeChild(gImg);
  52. }
  53. catch (e) {}
  54. });
  55. }
  56. function failTest() {
  57. ok(false, "timing out after " + FAILURE_TIMEOUT + "ms. " +
  58. "currently displaying " + gImg.src);
  59. cleanUpAndFinish();
  60. }
  61. function onNotification()
  62. {
  63. gNotifications++;
  64. try {
  65. gImg.src = gFiles.next();
  66. gContent.appendChild(gImg);
  67. } catch(e) {
  68. cleanUpAndFinish();
  69. }
  70. }
  71. function cleanUpAndFinish() {
  72. // On the off chance that failTest and myOnStopFrame are triggered
  73. // back-to-back, use a flag to prevent multiple calls to SimpleTest.finish.
  74. if (gIsTestFinished) {
  75. return;
  76. }
  77. let imgLoadingContent = SpecialPowers.wrap(gImg).QueryInterface(Ci.nsIImageLoadingContent);
  78. imgLoadingContent.removeObserver(gMyDecoderObserver);
  79. // TODO - this isn't the case until post-bug 716140's refactorings
  80. // ok(gNotifications == gLoads, "Should be notified the same number of times as loads");
  81. SimpleTest.finish();
  82. gIsTestFinished = true;
  83. }
  84. function main() {
  85. gFiles = fileToLoad();
  86. gImg = new Image();
  87. gImg.onload = onNotification;
  88. gImg.onerror = onNotification;
  89. // Create, customize & attach decoder observer
  90. observer = new ImageDecoderObserverStub();
  91. observer.sizeAvailable = onSizeAvailable;
  92. observer.loadComplete = onLoadComplete;
  93. observer.decodeComplete = onDecodeComplete;
  94. gMyDecoderObserver =
  95. Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
  96. .createScriptedObserver(SpecialPowers.wrapCallbackObject(observer));
  97. let imgLoadingContent = SpecialPowers.wrap(gImg).QueryInterface(Ci.nsIImageLoadingContent);
  98. imgLoadingContent.addObserver(gMyDecoderObserver);
  99. // We want to test the cold loading behavior, so clear cache in case an
  100. // earlier test got our image in there already.
  101. clearAllImageCaches();
  102. // kick off image-loading! myOnStopFrame handles the rest.
  103. gImg.setAttribute("src", gFiles.next());
  104. // In case something goes wrong, fail earlier than mochitest timeout,
  105. // and with more information.
  106. setTimeout(failTest, FAILURE_TIMEOUT);
  107. }
  108. window.onload = main;
  109. </script>
  110. </pre>
  111. </body>
  112. </html>