tor-browser

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

test_device_reset.html (2940B)


      1 <!DOCTYPE html>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=1274663
      5 -->
      6  <head>
      7    <meta charset="utf-8">
      8    <title>Test device reset</title>
      9    <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     10    <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
     11  </head>
     12  <body>
     13    <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1274663">Mozilla Bug 1274663</a>
     14    <script>
     15      var importObj = {};
     16 
     17      var windows = SpecialPowers.Services.ww.getWindowEnumerator();
     18      var windowutils;
     19      while (windows.hasMoreElements()) {
     20        windowutils = windows.getNext().windowUtils;
     21      }
     22 
     23      const PAGE_WIDTH = 200;
     24      const PAGE_HEIGHT = 200;
     25 
     26      // Helper functions
     27 
     28      function testCompositor(ctx) {
     29        takeWindowSnapshot(ctx);
     30        var testPassed = true;
     31 
     32        if (!verifyCanvasRendering(ctx)) {
     33          testPassed = false;
     34        }
     35 
     36        return testPassed;
     37      }
     38 
     39      function testPixel(ctx, x, y, r, g, b, a, fuzz) {
     40        var data = ctx.getImageData(x, y, 1, 1);
     41 
     42        if (Math.abs(data.data[0] - r) <= fuzz &&
     43            Math.abs(data.data[1] - g) <= fuzz &&
     44            Math.abs(data.data[2] - b) <= fuzz &&
     45            Math.abs(data.data[3] - a) <= fuzz) {
     46          return true;
     47        }
     48        return false;
     49      }
     50 
     51      function verifyCanvasRendering(ctx) {
     52        return testPixel(ctx, 20, 20, 140, 25, 86, 255, 0);
     53      }
     54 
     55      function takeWindowSnapshot(ctx) {
     56        var flags = ctx.DRAWWINDOW_DRAW_CARET | ctx.DRAWWINDOW_DRAW_VIEW | ctx.DRAWWINDOW_USE_WIDGET_LAYERS;
     57        ctx.drawWindow(window, 0, 0, PAGE_WIDTH, PAGE_HEIGHT, "rgb(140,25,86)", flags);
     58      }
     59 
     60      function createCanvas() {
     61        let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
     62 
     63        canvas.setAttribute("width", PAGE_WIDTH + "px");
     64        canvas.setAttribute("height", PAGE_HEIGHT + "px");
     65 
     66        return canvas;
     67      }
     68 
     69      // Test runner code
     70      windowutils.triggerDeviceReset();
     71 
     72      SimpleTest.waitForExplicitFinish();
     73      window.addEventListener("MozAfterPaint", function paintHandle() {
     74         // Add more latency before calling runCanvasTest()
     75         // runCanvasTest() needs to be called after gecko's device reset handling.
     76         // Since Bug 1757879 fix, the triggerDeviceReset() does the device reset
     77         // handling asynchronously.
     78         window.requestAnimationFrame(() => {
     79           runCanvasTest();
     80        });
     81        window.removeEventListener("MozAfterPaint", paintHandle);
     82      });
     83 
     84      function runCanvasTest() {
     85        const canvas = createCanvas();
     86        const ctx = canvas.getContext("2d");
     87        document.body.appendChild(canvas);
     88 
     89        ok(testCompositor(ctx), "Canvas did not get rendered after device reset");
     90        SimpleTest.finish();
     91      }
     92    </script>
     93  </body>
     94 </html>