michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Tests that the spectrum color picker works correctly michael@0: michael@0: const TEST_URI = "data:text/html;charset=utf-8,
"; michael@0: const {CSSTransformPreviewer} = devtools.require("devtools/shared/widgets/CSSTransformPreviewer"); michael@0: michael@0: let doc, root; michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: addTab(TEST_URI, () => { michael@0: doc = content.document; michael@0: root = doc.querySelector("div"); michael@0: startTests(); michael@0: }); michael@0: } michael@0: michael@0: function endTests() { michael@0: doc = root = null; michael@0: gBrowser.removeCurrentTab(); michael@0: finish(); michael@0: } michael@0: michael@0: function startTests() { michael@0: testCreateAndDestroyShouldAppendAndRemoveElements(); michael@0: } michael@0: michael@0: function testCreateAndDestroyShouldAppendAndRemoveElements() { michael@0: ok(root, "We have the root node to append the preview to"); michael@0: is(root.childElementCount, 0, "Root node is empty"); michael@0: michael@0: let p = new CSSTransformPreviewer(root); michael@0: p.preview("matrix(1, -0.2, 0, 1, 0, 0)"); michael@0: ok(root.childElementCount > 0, "Preview has appended elements"); michael@0: ok(root.querySelector("canvas"), "Canvas preview element is here"); michael@0: michael@0: p.destroy(); michael@0: is(root.childElementCount, 0, "Destroying preview removed all nodes"); michael@0: michael@0: testCanvasDimensionIsConstrainedByMaxDim(); michael@0: } michael@0: michael@0: function testCanvasDimensionIsConstrainedByMaxDim() { michael@0: let p = new CSSTransformPreviewer(root); michael@0: p.MAX_DIM = 500; michael@0: p.preview("scale(1)", "center", 1000, 1000); michael@0: michael@0: let canvas = root.querySelector("canvas"); michael@0: is(canvas.width, 500, "Canvas width is correct"); michael@0: is(canvas.height, 500, "Canvas height is correct"); michael@0: michael@0: p.destroy(); michael@0: michael@0: testCallingPreviewSeveralTimesReusesTheSameCanvas(); michael@0: } michael@0: michael@0: function testCallingPreviewSeveralTimesReusesTheSameCanvas() { michael@0: let p = new CSSTransformPreviewer(root); michael@0: michael@0: p.preview("scale(1)", "center", 1000, 1000); michael@0: let canvas = root.querySelector("canvas"); michael@0: michael@0: p.preview("rotate(90deg)"); michael@0: let canvases = root.querySelectorAll("canvas"); michael@0: is(canvases.length, 1, "Still one canvas element"); michael@0: is(canvases[0], canvas, "Still the same canvas element"); michael@0: p.destroy(); michael@0: michael@0: testCanvasDimensionAreCorrect(); michael@0: } michael@0: michael@0: function testCanvasDimensionAreCorrect() { michael@0: // Only test a few simple transformations michael@0: let p = new CSSTransformPreviewer(root); michael@0: michael@0: // Make sure we have a square michael@0: let w = 200, h = w; michael@0: p.MAX_DIM = w; michael@0: michael@0: // We can't test the content of the canvas here, just that, given a max width michael@0: // the aspect ratio of the canvas seems correct. michael@0: michael@0: // Translate a square by its width, should be a rectangle michael@0: p.preview("translateX(200px)", "center", w, h); michael@0: let canvas = root.querySelector("canvas"); michael@0: is(canvas.width, w, "width is correct"); michael@0: is(canvas.height, h/2, "height is half of the width"); michael@0: michael@0: // Rotate on the top right corner, should be a rectangle michael@0: p.preview("rotate(-90deg)", "top right", w, h); michael@0: is(canvas.width, w, "width is correct"); michael@0: is(canvas.height, h/2, "height is half of the width"); michael@0: michael@0: // Rotate on the bottom left corner, should be a rectangle michael@0: p.preview("rotate(90deg)", "top right", w, h); michael@0: is(canvas.width, w/2, "width is half of the height"); michael@0: is(canvas.height, h, "height is correct"); michael@0: michael@0: // Scale from center, should still be a square michael@0: p.preview("scale(2)", "center", w, h); michael@0: is(canvas.width, w, "width is correct"); michael@0: is(canvas.height, h, "height is correct"); michael@0: michael@0: // Skew from center, 45deg, should be a rectangle michael@0: p.preview("skew(45deg)", "center", w, h); michael@0: is(canvas.width, w, "width is correct"); michael@0: is(canvas.height, h/2, "height is half of the height"); michael@0: michael@0: p.destroy(); michael@0: michael@0: testPreviewingInvalidTransformReturnsFalse(); michael@0: } michael@0: michael@0: function testPreviewingInvalidTransformReturnsFalse() { michael@0: let p = new CSSTransformPreviewer(root); michael@0: ok(!p.preview("veryWow(muchPx) suchTransform(soDeg)"), "Returned false for invalid transform"); michael@0: ok(!p.preview("rotae(3deg)"), "Returned false for invalid transform"); michael@0: michael@0: // Verify the canvas is empty by checking the image data michael@0: let canvas = root.querySelector("canvas"), ctx = canvas.getContext("2d"); michael@0: let data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; michael@0: for (let i = 0, n = data.length; i < n; i += 4) { michael@0: // Let's not log 250*250*4 asserts! Instead, just log when it fails michael@0: let red = data[i]; michael@0: let green = data[i + 1]; michael@0: let blue = data[i + 2]; michael@0: let alpha = data[i + 3]; michael@0: if (red !== 0 || green !== 0 || blue !== 0 || alpha !== 0) { michael@0: ok(false, "Image data is not empty after an invalid transformed was previewed"); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: is(p.preview("translateX(30px)"), true, "Returned true for a valid transform"); michael@0: endTests(); michael@0: }