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.

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

mercurial