From d8ddf5203e5d710e192031df3b645ac00f4b5d3f Mon Sep 17 00:00:00 2001 From: Shachar <34343793+ShaMan123@users.noreply.github.com> Date: Sun, 6 Feb 2022 09:23:04 +0200 Subject: [PATCH] ci(tests): windows and linux paths resolutions (#7635) --- test/unit/canvas.js | 32 ++++++++++++++++++++--- test/unit/canvas_static.js | 28 +++++++++++++++++--- test/unit/image.js | 53 +++++++++++++++++++++++++++++++------- test/unit/util.js | 6 ++++- 4 files changed, 102 insertions(+), 17 deletions(-) diff --git a/test/unit/canvas.js b/test/unit/canvas.js index ba60535c316..ef8ad66bdb0 100644 --- a/test/unit/canvas.js +++ b/test/unit/canvas.js @@ -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; @@ -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 = ''; @@ -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); diff --git a/test/unit/canvas_static.js b/test/unit/canvas_static.js index 90574ebde4a..0e3257a306e 100644 --- a/test/unit/canvas_static.js +++ b/test/unit/canvas_static.js @@ -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; @@ -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; @@ -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; @@ -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'); diff --git a/test/unit/image.js b/test/unit/image.js index 9aebc858950..a1a0a2d8e36 100644 --- a/test/unit/image.js +++ b/test/unit/image.js @@ -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) { @@ -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(); }); }); @@ -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(); }); }); @@ -263,7 +298,7 @@ var done = assert.async(); createImageObject(function(image) { assert.ok(typeof image.toString === 'function'); - assert.equal(image.toString(), '#'); + assert.equal(image.toString(), '#'); done(); }); }); @@ -277,7 +312,7 @@ image.height -= 2; fabric.Object.__uid = 1; var expectedSVG = '\n\n\t\n\n\t\n\n'; - assert.equal(image.toSVG(), expectedSVG); + assert.equalImageSVG(image.toSVG(), expectedSVG); done(); }); }); @@ -306,7 +341,7 @@ createImageObject(function(image) { assert.ok(typeof image.toSVG === 'function'); var expectedSVG = '\n\t\n\n'; - assert.equal(image.toSVG(), expectedSVG); + assert.equalImageSVG(image.toSVG(), expectedSVG); done(); }); }); @@ -317,7 +352,7 @@ image.imageSmoothing = false; assert.ok(typeof image.toSVG === 'function'); var expectedSVG = '\n\t\n\n'; - assert.equal(image.toSVG(), expectedSVG); + assert.equalImageSVG(image.toSVG(), expectedSVG); done(); }); }); @@ -328,7 +363,7 @@ delete image._element; assert.ok(typeof image.toSVG === 'function'); var expectedSVG = '\n\n'; - assert.equal(image.toSVG(), expectedSVG); + assert.equalImageSVG(image.toSVG(), expectedSVG); done(); }); }); @@ -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(); }); }); @@ -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(); }); }); diff --git a/test/unit/util.js b/test/unit/util.js index dbf9de50b9f..c7006e787fa 100644 --- a/test/unit/util.js +++ b/test/unit/util.js @@ -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'); @@ -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();