added some tests for fabric.util (#5349)

* added some tests

* test
This commit is contained in:
Andrea Bogazzi 2018-10-28 21:46:32 +01:00 committed by GitHub
parent 5ebdfdf839
commit 847fe90c4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 1 deletions

View file

@ -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);

View file

@ -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');
});
})();