From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../shared/test/xpcshell/test_bezierCanvas.js | 122 +++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 devtools/client/shared/test/xpcshell/test_bezierCanvas.js (limited to 'devtools/client/shared/test/xpcshell/test_bezierCanvas.js') diff --git a/devtools/client/shared/test/xpcshell/test_bezierCanvas.js b/devtools/client/shared/test/xpcshell/test_bezierCanvas.js new file mode 100644 index 0000000000..d7fac599c5 --- /dev/null +++ b/devtools/client/shared/test/xpcshell/test_bezierCanvas.js @@ -0,0 +1,122 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Tests the BezierCanvas API in the CubicBezierWidget module + +var { + CubicBezier, + BezierCanvas, +} = require("resource://devtools/client/shared/widgets/CubicBezierWidget.js"); + +function run_test() { + offsetsGetterReturnsData(); + convertsOffsetsToCoordinates(); + plotsCanvas(); +} + +function offsetsGetterReturnsData() { + info("offsets getter returns an array of 2 offset objects"); + + let b = new BezierCanvas(getCanvasMock(), getCubicBezier(), [0.25, 0]); + let offsets = b.offsets; + + Assert.equal(offsets.length, 2); + + Assert.ok("top" in offsets[0]); + Assert.ok("left" in offsets[0]); + Assert.ok("top" in offsets[1]); + Assert.ok("left" in offsets[1]); + + Assert.equal(offsets[0].top, "300px"); + Assert.equal(offsets[0].left, "0px"); + Assert.equal(offsets[1].top, "100px"); + Assert.equal(offsets[1].left, "200px"); + + info("offsets getter returns data according to current padding"); + + b = new BezierCanvas(getCanvasMock(), getCubicBezier(), [0, 0]); + offsets = b.offsets; + + Assert.equal(offsets[0].top, "400px"); + Assert.equal(offsets[0].left, "0px"); + Assert.equal(offsets[1].top, "0px"); + Assert.equal(offsets[1].left, "200px"); +} + +function convertsOffsetsToCoordinates() { + info("Converts offsets to coordinates"); + + const b = new BezierCanvas(getCanvasMock(), getCubicBezier(), [0.25, 0]); + + let coordinates = b.offsetsToCoordinates({ + style: { + left: "0px", + top: "0px", + }, + }); + Assert.equal(coordinates.length, 2); + Assert.equal(coordinates[0], 0); + Assert.equal(coordinates[1], 1.5); + + coordinates = b.offsetsToCoordinates({ + style: { + left: "0px", + top: "300px", + }, + }); + Assert.equal(coordinates[0], 0); + Assert.equal(coordinates[1], 0); + + coordinates = b.offsetsToCoordinates({ + style: { + left: "200px", + top: "100px", + }, + }); + Assert.equal(coordinates[0], 1); + Assert.equal(coordinates[1], 1); +} + +function plotsCanvas() { + info("Plots the curve to the canvas"); + + let hasDrawnCurve = false; + const b = new BezierCanvas(getCanvasMock(), getCubicBezier(), [0.25, 0]); + b.ctx.bezierCurveTo = () => { + hasDrawnCurve = true; + }; + b.plot(); + + Assert.ok(hasDrawnCurve); +} + +function getCubicBezier() { + return new CubicBezier([0, 0, 1, 1]); +} + +function getCanvasMock(w = 200, h = 400) { + return { + getContext() { + return { + scale: () => {}, + translate: () => {}, + clearRect: () => {}, + beginPath: () => {}, + closePath: () => {}, + moveTo: () => {}, + lineTo: () => {}, + stroke: () => {}, + arc: () => {}, + fill: () => {}, + bezierCurveTo: () => {}, + save: () => {}, + restore: () => {}, + setTransform: () => {}, + }; + }, + width: w, + height: h, + }; +} -- cgit v1.2.3