browser/devtools/shared/test/browser_csstransformpreview.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* vim: set ts=2 et sw=2 tw=80: */
     2 /* Any copyright is dedicated to the Public Domain.
     3    http://creativecommons.org/publicdomain/zero/1.0/ */
     5 // Tests that the spectrum color picker works correctly
     7 const TEST_URI = "data:text/html;charset=utf-8,<div></div>";
     8 const {CSSTransformPreviewer} = devtools.require("devtools/shared/widgets/CSSTransformPreviewer");
    10 let doc, root;
    12 function test() {
    13   waitForExplicitFinish();
    14   addTab(TEST_URI, () => {
    15     doc = content.document;
    16     root = doc.querySelector("div");
    17     startTests();
    18   });
    19 }
    21 function endTests() {
    22   doc = root = null;
    23   gBrowser.removeCurrentTab();
    24   finish();
    25 }
    27 function startTests() {
    28   testCreateAndDestroyShouldAppendAndRemoveElements();
    29 }
    31 function testCreateAndDestroyShouldAppendAndRemoveElements() {
    32   ok(root, "We have the root node to append the preview to");
    33   is(root.childElementCount, 0, "Root node is empty");
    35   let p = new CSSTransformPreviewer(root);
    36   p.preview("matrix(1, -0.2, 0, 1, 0, 0)");
    37   ok(root.childElementCount > 0, "Preview has appended elements");
    38   ok(root.querySelector("canvas"), "Canvas preview element is here");
    40   p.destroy();
    41   is(root.childElementCount, 0, "Destroying preview removed all nodes");
    43   testCanvasDimensionIsConstrainedByMaxDim();
    44 }
    46 function testCanvasDimensionIsConstrainedByMaxDim() {
    47   let p = new CSSTransformPreviewer(root);
    48   p.MAX_DIM = 500;
    49   p.preview("scale(1)", "center", 1000, 1000);
    51   let canvas = root.querySelector("canvas");
    52   is(canvas.width, 500, "Canvas width is correct");
    53   is(canvas.height, 500, "Canvas height is correct");
    55   p.destroy();
    57   testCallingPreviewSeveralTimesReusesTheSameCanvas();
    58 }
    60 function testCallingPreviewSeveralTimesReusesTheSameCanvas() {
    61   let p = new CSSTransformPreviewer(root);
    63   p.preview("scale(1)", "center", 1000, 1000);
    64   let canvas = root.querySelector("canvas");
    66   p.preview("rotate(90deg)");
    67   let canvases = root.querySelectorAll("canvas");
    68   is(canvases.length, 1, "Still one canvas element");
    69   is(canvases[0], canvas, "Still the same canvas element");
    70   p.destroy();
    72   testCanvasDimensionAreCorrect();
    73 }
    75 function testCanvasDimensionAreCorrect() {
    76   // Only test a few simple transformations
    77   let p = new CSSTransformPreviewer(root);
    79   // Make sure we have a square
    80   let w = 200, h = w;
    81   p.MAX_DIM = w;
    83   // We can't test the content of the canvas here, just that, given a max width
    84   // the aspect ratio of the canvas seems correct.
    86   // Translate a square by its width, should be a rectangle
    87   p.preview("translateX(200px)", "center", w, h);
    88   let canvas = root.querySelector("canvas");
    89   is(canvas.width, w, "width is correct");
    90   is(canvas.height, h/2, "height is half of the width");
    92   // Rotate on the top right corner, should be a rectangle
    93   p.preview("rotate(-90deg)", "top right", w, h);
    94   is(canvas.width, w, "width is correct");
    95   is(canvas.height, h/2, "height is half of the width");
    97   // Rotate on the bottom left corner, should be a rectangle
    98   p.preview("rotate(90deg)", "top right", w, h);
    99   is(canvas.width, w/2, "width is half of the height");
   100   is(canvas.height, h, "height is correct");
   102   // Scale from center, should still be a square
   103   p.preview("scale(2)", "center", w, h);
   104   is(canvas.width, w, "width is correct");
   105   is(canvas.height, h, "height is correct");
   107   // Skew from center, 45deg, should be a rectangle
   108   p.preview("skew(45deg)", "center", w, h);
   109   is(canvas.width, w, "width is correct");
   110   is(canvas.height, h/2, "height is half of the height");
   112   p.destroy();
   114   testPreviewingInvalidTransformReturnsFalse();
   115 }
   117 function testPreviewingInvalidTransformReturnsFalse() {
   118   let p = new CSSTransformPreviewer(root);
   119   ok(!p.preview("veryWow(muchPx) suchTransform(soDeg)"), "Returned false for invalid transform");
   120   ok(!p.preview("rotae(3deg)"), "Returned false for invalid transform");
   122   // Verify the canvas is empty by checking the image data
   123   let canvas = root.querySelector("canvas"), ctx = canvas.getContext("2d");
   124   let data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
   125   for (let i = 0, n = data.length; i < n; i += 4) {
   126     // Let's not log 250*250*4 asserts! Instead, just log when it fails
   127     let red = data[i];
   128     let green = data[i + 1];
   129     let blue = data[i + 2];
   130     let alpha = data[i + 3];
   131     if (red !== 0 || green !== 0 || blue !== 0 || alpha !== 0) {
   132       ok(false, "Image data is not empty after an invalid transformed was previewed");
   133       break;
   134     }
   135   }
   137   is(p.preview("translateX(30px)"), true, "Returned true for a valid transform");
   138   endTests();
   139 }

mercurial