From 847fe90c4bc055034e5e4581a417e97e2ccdfab9 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sun, 28 Oct 2018 21:46:32 +0100 Subject: [PATCH] added some tests for fabric.util (#5349) * added some tests * test --- src/util/misc.js | 2 +- test/unit/util.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/util/misc.js b/src/util/misc.js index 2f830e87..03902d58 100644 --- a/src/util/misc.js +++ b/src/util/misc.js @@ -588,7 +588,7 @@ * @return {CanvasElement} initialized canvas element */ copyCanvasElement: function(canvas) { - var newCanvas = fabric.document.createElement('canvas'); + var newCanvas = fabric.util.createCanvasElement(); newCanvas.width = canvas.width; newCanvas.height = canvas.height; newCanvas.getContext('2d').drawImage(canvas, 0, 0); diff --git a/test/unit/util.js b/test/unit/util.js index f8e497c1..bd1c21bb 100644 --- a/test/unit/util.js +++ b/test/unit/util.js @@ -993,4 +993,83 @@ assert.equal(fabric.util.cos(Math.PI), -1, 'cos 180 correct'); assert.equal(fabric.util.cos(3 * Math.PI / 2), 0,' cos 270 correct'); }); + + QUnit.test('fabric.util.getSvgAttributes', function(assert) { + assert.ok(typeof fabric.util.getSvgAttributes === 'function'); + assert.deepEqual(fabric.util.getSvgAttributes(''), + ['instantiated_by_use', 'style', 'id', 'class'], 'common attribs'); + assert.deepEqual(fabric.util.getSvgAttributes('linearGradient'), + ['instantiated_by_use', 'style', 'id', 'class', 'x1', 'y1', 'x2', 'y2', 'gradientUnits', 'gradientTransform'], + 'linearGradient attribs'); + assert.deepEqual(fabric.util.getSvgAttributes('radialGradient'), + ['instantiated_by_use', 'style', 'id', 'class', 'gradientUnits', 'gradientTransform', 'cx', 'cy', 'r', 'fx', 'fy', 'fr'], + 'radialGradient attribs'); + assert.deepEqual(fabric.util.getSvgAttributes('stop'), + ['instantiated_by_use', 'style', 'id', 'class', 'offset', 'stop-color', 'stop-opacity'], + 'stop attribs'); + }); + + QUnit.test('fabric.util.enlivenPatterns', function(assert) { + assert.ok(typeof fabric.util.enlivenPatterns === 'function'); + fabric.util.enlivenPatterns([], function() { + assert.ok(true, 'callBack is called when no patterns are available'); + }); + }); + + QUnit.test('fabric.util.copyCanvasElement', function(assert) { + assert.ok(typeof fabric.util.copyCanvasElement === 'function'); + var c = fabric.util.createCanvasElement(); + c.width = 10; + c.height = 20; + c.getContext('2d').fillStyle = 'red'; + c.getContext('2d').fillRect(0, 0, 10, 10); + var b = fabric.util.copyCanvasElement(c); + assert.equal(b.width, 10, 'width has been copied'); + assert.equal(b.height, 20, 'height has been copied'); + var data = b.getContext('2d').getImageData(1,1,1,1).data; + assert.equal(data[0], 255, 'red color has been copied'); + assert.equal(data[1], 0, 'red color has been copied'); + assert.equal(data[2], 0, 'red color has been copied'); + assert.equal(data[3], 255, 'red color has been copied'); + }); + + QUnit.test('fabric.util.findScaleToCover', function(assert) { + assert.ok(typeof fabric.util.findScaleToCover === 'function'); + var scale = fabric.util.findScaleToCover({ + width: 100, + height: 200, + }, { + width: 50, + height: 50, + }); + assert.equal(scale, 0.5, 'scaleToCover is 0.5'); + var scale = fabric.util.findScaleToCover({ + width: 10, + height: 25, + }, { + width: 50, + height: 50, + }); + assert.equal(scale, 5, 'scaleToCover is 5'); + }); + + QUnit.test('fabric.util.findScaleToFit', function(assert) { + assert.ok(typeof fabric.util.findScaleToFit === 'function'); + var scale = fabric.util.findScaleToFit({ + width: 100, + height: 200, + }, { + width: 50, + height: 50, + }); + assert.equal(scale, 0.25, 'findScaleToFit is 0.25'); + var scale = fabric.util.findScaleToFit({ + width: 10, + height: 25, + }, { + width: 50, + height: 50, + }); + assert.equal(scale, 2, 'findScaleToFit is 2'); + }); })();