Skip to content

Commit

Permalink
ci(tests): windows and linux paths resolutions (#7635)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 authored Feb 6, 2022
1 parent fbd5004 commit d8ddf52
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
32 changes: 28 additions & 4 deletions test/unit/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
}

var IMG_SRC = fabric.isLikelyNode ? ('file://' + __dirname + '/../fixtures/test_image.gif') : getAbsolutePath('../fixtures/test_image.gif');

var canvas = this.canvas = new fabric.Canvas(null, {enableRetinaScaling: false, width: 600, height: 600});
var upperCanvasEl = canvas.upperCanvasEl;
var lowerCanvasEl = canvas.lowerCanvasEl;
Expand All @@ -94,6 +94,28 @@
return new fabric.Triangle(fabric.util.object.extend(defaultOptions, options || { }));
}

function basename(path) {
return path.slice(Math.max(path.lastIndexOf('\\'), path.lastIndexOf('/')) + 1);
}

/**
*
* @param {*} actual
* @param {*} [expected]
*/
QUnit.assert.sameImageObject = function (actual, expected) {
var a = {}, b = {};
expected = expected || REFERENCE_IMG_OBJECT;
Object.assign(a, actual, { src: basename(actual.src) });
Object.assign(b, expected, { src: basename(expected.src) });
this.pushResult({
result: QUnit.equiv(a, b),
actual: actual,
expected: expected,
message: 'image object equal to ref'
})
}

QUnit.module('fabric.Canvas', {
beforeEach: function() {
upperCanvasEl.style.display = '';
Expand Down Expand Up @@ -1658,9 +1680,11 @@

QUnit.test('loadFromJSON with custom properties on Canvas with image', function(assert) {
var done = assert.async();
var JSON_STRING = '{"objects":[{"type":"image","originX":"left","originY":"top","left":13.6,"top":-1.4,"width":3000,"height":3351,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":0,"strokeDashArray":null,"strokeLineCap":"butt","strokeDashOffset":0,"strokeLineJoin":"miter","strokeMiterLimit":4,"scaleX":0.05,"scaleY":0.05,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","skewX":0,"skewY":0,"src":"' + IMG_SRC + '","filters":[],"crossOrigin":""}],'
+ '"background":"green"}';
var serialized = JSON.parse(JSON_STRING);
var serialized = {
"objects": [
{ "type": "image", "originX": "left", "originY": "top", "left": 13.6, "top": -1.4, "width": 3000, "height": 3351, "fill": "rgb(0,0,0)", "stroke": null, "strokeWidth": 0, "strokeDashArray": null, "strokeLineCap": "butt", "strokeDashOffset": 0, "strokeLineJoin": "miter", "strokeMiterLimit": 4, "scaleX": 0.05, "scaleY": 0.05, "angle": 0, "flipX": false, "flipY": false, "opacity": 1, "shadow": null, "visible": true, "backgroundColor": "", "fillRule": "nonzero", "globalCompositeOperation": "source-over", "skewX": 0, "skewY": 0, "src": IMG_SRC, "filters": [], "crossOrigin": "" }],
"background": "green"
};
serialized.controlsAboveOverlay = true;
serialized.preserveObjectStacking = true;
assert.equal(canvas.controlsAboveOverlay, fabric.Canvas.prototype.controlsAboveOverlay);
Expand Down
28 changes: 25 additions & 3 deletions test/unit/canvas_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@
return new fabric.Rect(fabric.util.object.extend(defaultOptions, options || { }));
}

function basename(path) {
return path.slice(Math.max(path.lastIndexOf('\\'), path.lastIndexOf('/')) + 1);
}

/**
*
* @param {*} actual
* @param {*} [expected]
*/
QUnit.assert.sameImageObject = function (actual, expected) {
var a = {}, b = {};
expected = expected || REFERENCE_IMG_OBJECT;
Object.assign(a, actual, { src: basename(actual.src) });
Object.assign(b, expected, { src: basename(expected.src) });
this.pushResult({
result: QUnit.equiv(a, b),
actual: actual,
expected: expected,
message: 'image object equal to ref'
})
}

QUnit.module('fabric.StaticCanvas', {
beforeEach: function() {
fabric.Object.__uid = 0;
Expand Down Expand Up @@ -974,7 +996,7 @@
var json = canvas.toJSON();

fixImageDimension(json.backgroundImage);
assert.deepEqual(json.backgroundImage, REFERENCE_IMG_OBJECT);
assert.sameImageObject(json.backgroundImage, REFERENCE_IMG_OBJECT);

canvas.backgroundImage = null;

Expand Down Expand Up @@ -1003,7 +1025,7 @@
var json = canvas.toJSON();

fixImageDimension(json.overlayImage);
assert.deepEqual(json.overlayImage, REFERENCE_IMG_OBJECT);
assert.sameImageObject(json.overlayImage, REFERENCE_IMG_OBJECT);

canvas.overlayImage = null;

Expand Down Expand Up @@ -1247,7 +1269,7 @@
var done = assert.async();
var serialized = JSON.parse(PATH_JSON);
serialized.background = 'green';
serialized.backgroundImage = JSON.parse('{"type":"image","originX":"left","originY":"top","left":13.6,"top":-1.4,"width":3000,"height":3351,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":0,"strokeDashArray":null,"strokeLineCap":"butt","strokeDashOffset":0,"strokeLineJoin":"miter","strokeMiterLimit":4,"scaleX":0.05,"scaleY":0.05,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","skewX":0,"skewY":0,"src":"' + IMG_SRC + '","filters":[],"crossOrigin":""}');
serialized.backgroundImage = { "type": "image", "originX": "left", "originY": "top", "left": 13.6, "top": -1.4, "width": 3000, "height": 3351, "fill": "rgb(0,0,0)", "stroke": null, "strokeWidth": 0, "strokeDashArray": null, "strokeLineCap": "butt", "strokeDashOffset": 0, "strokeLineJoin": "miter", "strokeMiterLimit": 4, "scaleX": 0.05, "scaleY": 0.05, "angle": 0, "flipX": false, "flipY": false, "opacity": 1, "shadow": null, "visible": true, "backgroundColor": "", "fillRule": "nonzero", "globalCompositeOperation": "source-over", "skewX": 0, "skewY": 0, "src": IMG_SRC, "filters": [], "crossOrigin": "" };
canvas.loadFromJSON(serialized, function() {
assert.ok(!canvas.isEmpty(), 'canvas is not empty');
assert.equal(canvas.backgroundColor, 'green');
Expand Down
53 changes: 44 additions & 9 deletions test/unit/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,41 @@
img.src = src;
}

function basename(path) {
return path.slice(Math.max(path.lastIndexOf('\\'), path.lastIndexOf('/')) + 1);
}

QUnit.assert.equalImageSVG = function (actual, expected) {
function extractBasename(s) {
var p = 'xlink:href', pos = s.indexOf(p) + p.length;
return basename(s.slice(pos, s.indexOf(' ', pos)));
}
this.pushResult({
result: extractBasename(actual) === extractBasename(expected),
actual: actual,
expected: expected,
message: 'svg is not equal to ref'
});
}

/**
*
* @param {*} actual
* @param {*} [expected]
*/
QUnit.assert.sameImageObject = function (actual, expected) {
var a = {}, b = {};
expected = expected || REFERENCE_IMG_OBJECT;
Object.assign(a, actual, { src: basename(actual.src) });
Object.assign(b, expected, { src: basename(expected.src) });
this.pushResult({
result: QUnit.equiv(a, b),
actual: actual,
expected: expected,
message: 'image object equal to ref'
})
}

QUnit.module('fabric.Image');

QUnit.test('constructor', function(assert) {
Expand Down Expand Up @@ -132,7 +167,7 @@
if (toObject.height === 0) {
toObject.height = IMG_HEIGHT;
}
assert.deepEqual(toObject, REFERENCE_IMG_OBJECT);
assert.sameImageObject(toObject, REFERENCE_IMG_OBJECT);
done();
});
});
Expand Down Expand Up @@ -184,7 +219,7 @@
if (toObject.height === 0) {
toObject.height = IMG_HEIGHT;
}
assert.deepEqual(toObject, REFERENCE_IMG_OBJECT);
assert.sameImageObject(toObject, REFERENCE_IMG_OBJECT);
done();
});
});
Expand Down Expand Up @@ -263,7 +298,7 @@
var done = assert.async();
createImageObject(function(image) {
assert.ok(typeof image.toString === 'function');
assert.equal(image.toString(), '#<fabric.Image: { src: "' + IMG_SRC + '" }>');
assert.equal(image.toString(), '#<fabric.Image: { src: "' + image.getSrc() + '" }>');
done();
});
});
Expand All @@ -277,7 +312,7 @@
image.height -= 2;
fabric.Object.__uid = 1;
var expectedSVG = '<g transform=\"matrix(1 0 0 1 137 54)\" >\n<clipPath id=\"imageCrop_1\">\n\t<rect x=\"-137\" y=\"-54\" width=\"274\" height=\"108\" />\n</clipPath>\n\t<image style=\"stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;\" xlink:href=\"' + IMG_SRC + '\" x=\"-138\" y=\"-55\" width=\"276\" height=\"110\" clip-path=\"url(#imageCrop_1)\" ></image>\n</g>\n';
assert.equal(image.toSVG(), expectedSVG);
assert.equalImageSVG(image.toSVG(), expectedSVG);
done();
});
});
Expand Down Expand Up @@ -306,7 +341,7 @@
createImageObject(function(image) {
assert.ok(typeof image.toSVG === 'function');
var expectedSVG = '<g transform=\"matrix(1 0 0 1 138 55)\" >\n\t<image style=\"stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;\" xlink:href=\"' + IMG_SRC + '\" x=\"-138\" y=\"-55\" width=\"276\" height=\"110\"></image>\n</g>\n';
assert.equal(image.toSVG(), expectedSVG);
assert.equalImageSVG(image.toSVG(), expectedSVG);
done();
});
});
Expand All @@ -317,7 +352,7 @@
image.imageSmoothing = false;
assert.ok(typeof image.toSVG === 'function');
var expectedSVG = '<g transform="matrix(1 0 0 1 138 55)" >\n\t<image style=\"stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;\" xlink:href=\"' + IMG_SRC + '\" x=\"-138\" y=\"-55\" width=\"276\" height=\"110\" image-rendering=\"optimizeSpeed\"></image>\n</g>\n';
assert.equal(image.toSVG(), expectedSVG);
assert.equalImageSVG(image.toSVG(), expectedSVG);
done();
});
});
Expand All @@ -328,7 +363,7 @@
delete image._element;
assert.ok(typeof image.toSVG === 'function');
var expectedSVG = '<g transform="matrix(1 0 0 1 138 55)" >\n</g>\n';
assert.equal(image.toSVG(), expectedSVG);
assert.equalImageSVG(image.toSVG(), expectedSVG);
done();
});
});
Expand All @@ -337,7 +372,7 @@
var done = assert.async();
createImageObject(function(image) {
assert.ok(typeof image.getSrc === 'function');
assert.equal(image.getSrc(), IMG_SRC);
assert.equal(basename(image.getSrc()), basename(IMG_SRC));
done();
});
});
Expand Down Expand Up @@ -519,7 +554,7 @@
assert.ok(typeof fabric.Image.fromURL === 'function');
fabric.Image.fromURL(IMG_SRC, function(instance) {
assert.ok(instance instanceof fabric.Image);
assert.deepEqual(REFERENCE_IMG_OBJECT, instance.toObject());
assert.sameImageObject(REFERENCE_IMG_OBJECT, instance.toObject());
done();
});
});
Expand Down
6 changes: 5 additions & 1 deletion test/unit/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
return src;
}

function basename(path) {
return path.slice(Math.max(path.lastIndexOf('\\'), path.lastIndexOf('/')) + 1);
}

var IMG_URL = fabric.isLikelyNode
? 'file://' + require('path').join(__dirname, '../fixtures/', 'very_large_image.jpg')
: getAbsolutePath('../fixtures/very_large_image.jpg');
Expand Down Expand Up @@ -475,7 +479,7 @@
}
try {
fabric.util.loadImage(IMG_URL, function(img, isError) {
assert.equal(img.src, IMG_URL, 'src is set');
assert.equal(basename(img.src), basename(IMG_URL), 'src is set');
assert.equal(img.crossOrigin, 'anonymous', 'crossOrigin is set');
assert.ok(!isError);
done();
Expand Down

0 comments on commit d8ddf52

Please sign in to comment.