2013-01-28 21:03:27 -05:00
|
|
|
/*
|
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
2015-12-27 12:09:25 -05:00
|
|
|
* Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
|
2014-01-03 19:47:16 -05:00
|
|
|
* http://scratchdisk.com/ & http://jonathanpuckey.com/
|
2013-01-28 21:03:27 -05:00
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2016-01-27 13:57:07 -05:00
|
|
|
var isNode = typeof global === 'object',
|
2016-02-12 11:59:37 -05:00
|
|
|
isPhantom = typeof window === 'object' && !!window.callPhantom,
|
2016-01-27 13:57:07 -05:00
|
|
|
root;
|
|
|
|
|
|
|
|
if (isNode) {
|
|
|
|
root = global;
|
2016-01-27 20:21:34 -05:00
|
|
|
// Resemble.js needs the Image constructor global.
|
2016-01-27 13:57:07 -05:00
|
|
|
global.Image = paper.window.Image;
|
|
|
|
} else {
|
|
|
|
root = window;
|
|
|
|
// This is only required when running in the browser:
|
|
|
|
// Until window.history.pushState() works when running locally, we need to
|
|
|
|
// trick qunit into thinking that the feature is not present. This appears
|
|
|
|
// to work...
|
|
|
|
// TODO: Ideally we should fix this in QUnit instead.
|
|
|
|
delete window.history;
|
|
|
|
window.history = {};
|
|
|
|
QUnit.begin(function() {
|
|
|
|
if (QUnit.urlParams.hidepassed) {
|
|
|
|
document.getElementById('qunit-tests').className += ' hidepass';
|
|
|
|
}
|
2015-12-28 16:42:16 -05:00
|
|
|
});
|
2016-01-27 13:57:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The unit-tests expect the paper classes to be global.
|
2016-01-31 10:52:51 -05:00
|
|
|
paper.install(root);
|
2015-12-28 15:15:10 -05:00
|
|
|
|
2016-01-27 20:21:34 -05:00
|
|
|
// Override console.error, so that we can catch errors that are only logged to
|
|
|
|
// the console.
|
2016-01-04 06:20:00 -05:00
|
|
|
var errorHandler = console.error;
|
|
|
|
console.error = function() {
|
|
|
|
QUnit.pushFailure([].join.call(arguments, ' '), QUnit.config.current.stack);
|
|
|
|
errorHandler.apply(this, arguments);
|
2016-01-26 14:33:42 -05:00
|
|
|
};
|
2016-01-04 06:20:00 -05:00
|
|
|
|
2016-01-27 20:21:34 -05:00
|
|
|
QUnit.done(function(details) {
|
|
|
|
console.error = errorHandler;
|
|
|
|
});
|
|
|
|
|
2016-01-28 08:39:40 -05:00
|
|
|
var currentProject;
|
2016-01-27 20:21:34 -05:00
|
|
|
|
2016-01-27 13:57:07 -05:00
|
|
|
// NOTE: In order to "export" all methods into the shared Prepro.js scope when
|
|
|
|
// using node-qunit, we need to define global functions as:
|
|
|
|
// `var name = function() {}`. `function name() {}` does not work!
|
|
|
|
var test = function(testName, expected) {
|
2016-01-28 08:39:40 -05:00
|
|
|
return QUnit.test(testName, function(assert) {
|
2016-01-27 20:21:34 -05:00
|
|
|
// Since tests can be asynchronous, remove the old project before
|
|
|
|
// running the next test.
|
|
|
|
if (currentProject)
|
|
|
|
currentProject.remove();
|
|
|
|
currentProject = new Project();
|
2016-01-28 08:39:40 -05:00
|
|
|
expected(assert);
|
2016-01-27 20:21:34 -05:00
|
|
|
});
|
2016-01-27 13:57:07 -05:00
|
|
|
};
|
|
|
|
|
2016-01-27 04:45:39 -05:00
|
|
|
// Override equals to convert functions to message and execute them as tests()
|
2016-01-27 13:57:07 -05:00
|
|
|
var equals = function(actual, expected, message, options) {
|
2016-01-27 04:45:39 -05:00
|
|
|
// Allow the use of functions for actual, which will get called and their
|
|
|
|
// source content extracted for readable reports.
|
|
|
|
if (typeof actual === 'function') {
|
|
|
|
if (!message)
|
|
|
|
message = getFunctionMessage(actual);
|
|
|
|
actual = actual();
|
|
|
|
}
|
|
|
|
// Get the comparator based on the expected value's type only and ignore the
|
|
|
|
// actual value's type.
|
|
|
|
var type = typeof expected,
|
|
|
|
cls;
|
|
|
|
type = expected === null && 'Null'
|
|
|
|
|| type === 'number' && 'Number'
|
|
|
|
|| type === 'boolean' && 'Boolean'
|
|
|
|
|| type === 'undefined' && 'Undefined'
|
|
|
|
|| Array.isArray(expected) && 'Array'
|
2016-01-27 13:57:07 -05:00
|
|
|
|| expected instanceof window.Element && 'Element' // handle DOM Elements
|
2016-01-27 04:45:39 -05:00
|
|
|
|| (cls = expected && expected._class) // check _class 2nd last
|
|
|
|
|| type === 'object' && 'Object'; // Object as catch-all
|
|
|
|
var comparator = type && comparators[type];
|
|
|
|
if (!message)
|
|
|
|
message = type ? type.toLowerCase() : 'value';
|
|
|
|
if (comparator) {
|
|
|
|
comparator(actual, expected, message, options);
|
|
|
|
} else if (expected && expected.equals) {
|
|
|
|
// Fall back to equals
|
|
|
|
QUnit.push(expected.equals(actual), actual, expected, message);
|
|
|
|
} else {
|
|
|
|
// Finally perform a strict compare
|
|
|
|
QUnit.push(actual === expected, actual, expected, message);
|
|
|
|
}
|
|
|
|
if (options && options.cloned && cls) {
|
|
|
|
var identical = identicalAfterCloning[cls];
|
|
|
|
QUnit.push(identical ? actual === expected : actual !== expected,
|
|
|
|
actual, identical ? expected : 'not ' + expected,
|
|
|
|
message + ': identical after cloning');
|
|
|
|
}
|
2016-01-27 13:57:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// A list of classes that should be identical after their owners were cloned.
|
|
|
|
var identicalAfterCloning = {
|
|
|
|
Gradient: true,
|
2016-01-31 10:52:51 -05:00
|
|
|
SymbolDefinition: true
|
2016-01-27 13:57:07 -05:00
|
|
|
};
|
|
|
|
|
2013-06-11 16:43:29 -04:00
|
|
|
// Register a jsDump parser for Base.
|
2013-06-11 16:38:08 -04:00
|
|
|
QUnit.jsDump.setParser('Base', function (obj, stack) {
|
2014-08-16 13:24:54 -04:00
|
|
|
// Just compare the string representation of classes inheriting from Base,
|
|
|
|
// since they hide the internal values.
|
|
|
|
return obj.toString();
|
2013-06-11 16:38:08 -04:00
|
|
|
});
|
|
|
|
|
2013-06-11 16:43:29 -04:00
|
|
|
// Override the default object parser to handle Base objects.
|
|
|
|
// We need to keep a reference to the previous implementation.
|
|
|
|
var objectParser = QUnit.jsDump.parsers.object;
|
|
|
|
|
2013-06-11 16:38:08 -04:00
|
|
|
QUnit.jsDump.setParser('object', function (obj, stack) {
|
2014-08-16 13:24:54 -04:00
|
|
|
return (obj instanceof Base
|
|
|
|
? QUnit.jsDump.parsers.Base
|
|
|
|
: objectParser).call(this, obj, stack);
|
2013-06-11 16:38:08 -04:00
|
|
|
});
|
|
|
|
|
2016-01-27 13:57:07 -05:00
|
|
|
var compareProperties = function(actual, expected, properties, message, options) {
|
2014-12-28 12:23:04 -05:00
|
|
|
for (var i = 0, l = properties.length; i < l; i++) {
|
|
|
|
var key = properties[i];
|
2016-02-14 06:39:35 -05:00
|
|
|
equals(actual[key], expected[key],
|
|
|
|
message + ' (#' + key + ')', options);
|
2014-12-28 12:23:04 -05:00
|
|
|
}
|
2016-01-27 13:57:07 -05:00
|
|
|
};
|
2014-12-28 12:10:53 -05:00
|
|
|
|
2016-02-01 14:15:37 -05:00
|
|
|
var comparePixels = function(actual, expected, message, options) {
|
2015-12-28 17:49:02 -05:00
|
|
|
function rasterize(item, group, resolution) {
|
|
|
|
var raster = null;
|
|
|
|
if (group) {
|
|
|
|
group.addChild(item);
|
|
|
|
raster = group.rasterize(resolution, false);
|
|
|
|
item.remove();
|
|
|
|
}
|
|
|
|
return raster;
|
|
|
|
}
|
|
|
|
|
2016-01-02 18:29:28 -05:00
|
|
|
function getImageTag(raster) {
|
|
|
|
return '<img width="' + raster.width + '" height="' + raster.height
|
2016-01-26 14:33:42 -05:00
|
|
|
+ '" src="' + raster.source + '">';
|
2016-01-02 18:29:28 -05:00
|
|
|
}
|
|
|
|
|
2016-02-01 14:15:37 -05:00
|
|
|
options = options || {};
|
2016-01-27 20:21:34 -05:00
|
|
|
// In order to properly compare pixel by pixel, we need to put each item
|
|
|
|
// into a group with a white background of the united dimensions of the
|
|
|
|
// bounds of both items before rasterizing.
|
2016-02-01 14:15:37 -05:00
|
|
|
var resolution = options.resolution || 72,
|
2016-01-27 20:21:34 -05:00
|
|
|
actualBounds = actual.strokeBounds,
|
|
|
|
expecedBounds = expected.strokeBounds,
|
|
|
|
bounds = actualBounds.isEmpty()
|
|
|
|
? expecedBounds
|
|
|
|
: expecedBounds.isEmpty()
|
|
|
|
? actualBounds
|
|
|
|
: actualBounds.unite(expecedBounds);
|
|
|
|
if (bounds.isEmpty()) {
|
2016-02-01 14:15:37 -05:00
|
|
|
QUnit.equal('empty', 'empty', message);
|
2016-01-27 20:21:34 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var group = actual && expected && new Group({
|
|
|
|
insert: false,
|
|
|
|
children: [
|
|
|
|
new Shape.Rectangle({
|
|
|
|
rectangle: bounds,
|
|
|
|
fillColor: 'white'
|
|
|
|
})
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
actual = rasterize(actual, group, resolution),
|
|
|
|
expected = rasterize(expected, group, resolution);
|
|
|
|
if (!actual || !expected) {
|
2016-02-01 14:15:37 -05:00
|
|
|
QUnit.push(false, null, null, 'Unable to compare rasterized items: ' +
|
2016-01-27 20:21:34 -05:00
|
|
|
(!actual ? 'actual' : 'expected') + ' item is null',
|
|
|
|
QUnit.stack(2));
|
|
|
|
} else {
|
|
|
|
// Use resemble.js to compare the two rasterized items.
|
|
|
|
var id = QUnit.config.current.testId,
|
|
|
|
index = QUnit.config.current.assertions.length + 1,
|
|
|
|
result;
|
|
|
|
if (!resemble._setup) {
|
|
|
|
resemble._setup = true;
|
|
|
|
resemble.outputSettings({
|
|
|
|
errorColor: { red: 255, green: 51, blue: 0 },
|
|
|
|
errorType: 'flat',
|
|
|
|
transparency: 1
|
|
|
|
});
|
2016-01-04 06:20:00 -05:00
|
|
|
}
|
2016-01-27 20:21:34 -05:00
|
|
|
resemble(actual.getImageData())
|
|
|
|
.compareTo(expected.getImageData())
|
2016-02-01 14:15:37 -05:00
|
|
|
.ignoreAntialiasing()
|
2016-01-27 20:21:34 -05:00
|
|
|
// When working with imageData, this call is synchronous:
|
|
|
|
.onComplete(function(data) { result = data; });
|
2016-02-01 14:15:37 -05:00
|
|
|
// Compare with tolerance in percentage...
|
|
|
|
var tolerance = (options.tolerance || 1e-4) * 100,
|
2016-04-13 17:37:13 -04:00
|
|
|
fixed = tolerance < 1 ? ((1 / tolerance) + '').length - 1 : 0,
|
2016-01-27 20:21:34 -05:00
|
|
|
identical = result ? 100 - result.misMatchPercentage : 0,
|
|
|
|
reached = identical.toFixed(fixed),
|
|
|
|
hundred = (100).toFixed(fixed),
|
2016-02-01 14:15:37 -05:00
|
|
|
ok = reached == hundred,
|
|
|
|
text = reached + '% identical';
|
|
|
|
QUnit.push(ok, text, hundred + '% identical', message);
|
2016-01-27 20:21:34 -05:00
|
|
|
if (!ok && result && !isNode) {
|
|
|
|
// Get the right entry for this unit test and assertion, and
|
|
|
|
// replace the results with images
|
|
|
|
var entry = document.getElementById('qunit-test-output-' + id)
|
|
|
|
.querySelector('li:nth-child(' + (index) + ')'),
|
|
|
|
bounds = result.diffBounds;
|
|
|
|
entry.querySelector('.test-expected td').innerHTML =
|
|
|
|
getImageTag(expected);
|
|
|
|
entry.querySelector('.test-actual td').innerHTML =
|
|
|
|
getImageTag(actual);
|
|
|
|
entry.querySelector('.test-diff td').innerHTML = '<pre>' + text
|
|
|
|
+ '</pre><br>'
|
|
|
|
+ '<img src="' + result.getImageDataUrl() + '">';
|
2015-12-28 16:42:16 -05:00
|
|
|
}
|
|
|
|
}
|
2016-01-27 13:57:07 -05:00
|
|
|
};
|
2014-12-28 12:10:53 -05:00
|
|
|
|
2016-01-27 20:21:34 -05:00
|
|
|
var compareItem = function(actual, expected, message, options, properties) {
|
|
|
|
options = options || {};
|
|
|
|
if (options.rasterize) {
|
2016-02-01 14:15:37 -05:00
|
|
|
comparePixels(actual, expected, message, options);
|
|
|
|
} else {
|
|
|
|
if (options.cloned)
|
|
|
|
QUnit.notStrictEqual(actual.id, expected.id,
|
2016-02-14 06:39:35 -05:00
|
|
|
message + ' (not #id)');
|
2016-02-01 14:15:37 -05:00
|
|
|
QUnit.strictEqual(actual.constructor, expected.constructor,
|
2016-02-14 06:39:35 -05:00
|
|
|
message + ' (#constructor)');
|
2016-02-01 14:15:37 -05:00
|
|
|
// When item is cloned and has a name, the name will be versioned:
|
|
|
|
equals(actual.name,
|
|
|
|
options.cloned && expected.name
|
|
|
|
? expected.name + ' 1' : expected.name,
|
2016-02-14 06:39:35 -05:00
|
|
|
message + ' (#name)');
|
2016-02-01 14:15:37 -05:00
|
|
|
compareProperties(actual, expected, ['children', 'bounds', 'position',
|
|
|
|
'matrix', 'data', 'opacity', 'locked', 'visible', 'blendMode',
|
|
|
|
'selected', 'fullySelected', 'clipMask', 'guide'],
|
|
|
|
message, options);
|
|
|
|
if (properties)
|
|
|
|
compareProperties(actual, expected, properties, message, options);
|
|
|
|
// Style
|
2016-02-02 15:43:44 -05:00
|
|
|
var styles = ['fillColor',
|
2016-02-01 14:15:37 -05:00
|
|
|
'strokeColor', 'strokeCap', 'strokeJoin', 'dashArray',
|
2016-02-02 15:43:44 -05:00
|
|
|
'dashOffset', 'miterLimit'];
|
|
|
|
if (expected instanceof TextItem)
|
|
|
|
styles.push('fontSize', 'font', 'leading', 'justification');
|
|
|
|
compareProperties(actual.style, expected.style, styles,
|
2016-02-14 06:39:35 -05:00
|
|
|
message + ' (#style)', options);
|
2016-01-27 20:21:34 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-28 12:23:04 -05:00
|
|
|
// A list of comparator functions, based on `expected` type. See equals() for
|
|
|
|
// an explanation of how the type is determined.
|
2014-12-28 08:33:22 -05:00
|
|
|
var comparators = {
|
2014-12-28 09:21:38 -05:00
|
|
|
Null: QUnit.strictEqual,
|
|
|
|
Undefined: QUnit.strictEqual,
|
2014-12-28 10:41:23 -05:00
|
|
|
Boolean: QUnit.strictEqual,
|
|
|
|
|
|
|
|
Object: function(actual, expected, message, options) {
|
|
|
|
QUnit.push(Base.equals(actual, expected), actual, expected, message);
|
|
|
|
},
|
|
|
|
|
2015-07-27 05:42:41 -04:00
|
|
|
Element: function(actual, expected, message, options) {
|
|
|
|
// Convention: Loop through the attribute lists of both actual and
|
|
|
|
// expected element, and compare values even if they may be inherited.
|
2016-02-01 06:31:18 -05:00
|
|
|
// This is to handle styling values on SVGElement items more flexibly.
|
2015-07-27 05:42:41 -04:00
|
|
|
equals(actual && actual.tagName, expected.tagName,
|
2016-02-14 06:39:35 -05:00
|
|
|
(message || '') + ' (#tagName)', options);
|
2015-07-27 05:42:41 -04:00
|
|
|
for (var i = 0; i < expected.attributes.length; i++) {
|
|
|
|
var attr = expected.attributes[i];
|
|
|
|
if (attr.specified) {
|
|
|
|
equals(actual && actual.getAttribute(attr.name), attr.value,
|
2016-02-14 06:39:35 -05:00
|
|
|
(message || '') + ' (#' + attr.name + ')', options);
|
2015-07-27 05:42:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var i = 0; i < actual && actual.attributes.length; i++) {
|
|
|
|
var attr = actual.attributes[i];
|
|
|
|
if (attr.specified) {
|
|
|
|
equals(attr.value, expected.getAttribute(attr.name)
|
2016-02-14 06:39:35 -05:00
|
|
|
(message || '') + ' #(' + attr.name + ')', options);
|
2015-07-27 05:42:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-12-28 10:41:23 -05:00
|
|
|
Base: function(actual, expected, message, options) {
|
|
|
|
comparators.Object(actual, expected, message, options);
|
|
|
|
},
|
2014-12-28 09:21:38 -05:00
|
|
|
|
2014-12-28 08:33:22 -05:00
|
|
|
Number: function(actual, expected, message, options) {
|
2015-01-04 08:07:43 -05:00
|
|
|
// Compare with a default tolerance of 1e-5:
|
2014-12-28 08:33:22 -05:00
|
|
|
var ok = Math.abs(actual - expected)
|
2015-01-04 08:07:43 -05:00
|
|
|
<= Base.pick(options && options.tolerance, 1e-5);
|
2014-12-28 08:33:22 -05:00
|
|
|
QUnit.push(ok, ok ? expected : actual, expected, message);
|
|
|
|
},
|
|
|
|
|
|
|
|
Array: function(actual, expected, message, options) {
|
2016-02-14 06:39:35 -05:00
|
|
|
QUnit.strictEqual(actual.length, expected.length, message
|
|
|
|
+ ' (#length)');
|
2014-12-28 08:33:22 -05:00
|
|
|
for (var i = 0, l = actual.length; i < l; i++) {
|
2014-12-28 12:10:53 -05:00
|
|
|
equals(actual[i], expected[i], (message || '') + '[' + i + ']',
|
2014-12-28 08:33:22 -05:00
|
|
|
options);
|
|
|
|
}
|
2014-12-28 08:59:48 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Point: function(actual, expected, message, options) {
|
2016-02-14 06:39:35 -05:00
|
|
|
comparators.Number(actual.x, expected.x, message + ' (#x)', options);
|
|
|
|
comparators.Number(actual.y, expected.y, message + ' (#y)', options);
|
2014-12-28 08:59:48 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Size: function(actual, expected, message, options) {
|
2016-02-14 06:39:35 -05:00
|
|
|
comparators.Number(actual.width, expected.width,
|
|
|
|
message + ' (#width)', options);
|
|
|
|
comparators.Number(actual.height, expected.height,
|
|
|
|
message + ' (#height)', options);
|
2014-12-28 08:59:48 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Rectangle: function(actual, expected, message, options) {
|
|
|
|
comparators.Point(actual, expected, message, options);
|
|
|
|
comparators.Size(actual, expected, message, options);
|
2014-12-28 09:21:38 -05:00
|
|
|
},
|
|
|
|
|
2014-12-28 10:41:23 -05:00
|
|
|
Matrix: function(actual, expected, message, options) {
|
|
|
|
comparators.Array(actual.values, expected.values, message, options);
|
|
|
|
},
|
|
|
|
|
2014-12-28 09:21:38 -05:00
|
|
|
Color: function(actual, expected, message, options) {
|
|
|
|
if (actual && expected) {
|
2016-02-14 06:39:35 -05:00
|
|
|
equals(actual.type, expected.type, message + ' (#type)', options);
|
2014-12-28 10:41:23 -05:00
|
|
|
// NOTE: This also compares gradients, with identity checks and all.
|
2014-12-28 09:21:38 -05:00
|
|
|
equals(actual.components, expected.components,
|
2016-02-14 06:39:35 -05:00
|
|
|
message + ' (#components)', options);
|
2014-12-28 09:21:38 -05:00
|
|
|
} else {
|
2014-12-28 12:10:53 -05:00
|
|
|
QUnit.strictEqual(actual, expected, message);
|
2014-12-28 09:21:38 -05:00
|
|
|
}
|
2014-12-28 10:41:23 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Segment: function(actual, expected, message, options) {
|
2014-12-28 12:23:04 -05:00
|
|
|
compareProperties(actual, expected, ['handleIn', 'handleOut', 'point',
|
|
|
|
'selected'], message, options);
|
2014-12-28 10:41:23 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
SegmentPoint: function(actual, expected, message, options) {
|
|
|
|
comparators.Point(actual, expected, message, options);
|
|
|
|
comparators.Boolean(actual.selected, expected.selected,
|
2016-02-14 06:39:35 -05:00
|
|
|
message + ' (#selected)', options);
|
2014-12-28 12:10:53 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Item: compareItem,
|
|
|
|
|
|
|
|
Group: function(actual, expected, message, options) {
|
2014-12-28 12:23:04 -05:00
|
|
|
compareItem(actual, expected, message, options, ['clipped']);
|
2014-12-28 12:10:53 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Layer: function(actual, expected, message, options) {
|
|
|
|
compareItem(actual, expected, message, options);
|
2014-12-28 12:23:04 -05:00
|
|
|
var sameProject = actual.project === expected.project;
|
|
|
|
var sharedProject = !(options && options.dontShareProject);
|
|
|
|
QUnit.push(sharedProject ? sameProject : !sameProject,
|
|
|
|
actual.project,
|
|
|
|
sharedProject ? expected.project : 'not ' + expected.project,
|
2016-02-14 06:39:35 -05:00
|
|
|
message + ' (#project)');
|
2014-12-28 12:10:53 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Path: function(actual, expected, message, options) {
|
|
|
|
compareItem(actual, expected, message, options,
|
|
|
|
['segments', 'closed', 'clockwise', 'length']);
|
|
|
|
},
|
|
|
|
|
|
|
|
CompoundPath: function(actual, expected, message, options) {
|
|
|
|
compareItem(actual, expected, message, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
Raster: function(actual, expected, message, options) {
|
2016-02-01 14:15:37 -05:00
|
|
|
var pixels = options && options.pixels,
|
|
|
|
properties = ['size', 'width', 'height', 'resolution'];
|
|
|
|
if (!pixels)
|
|
|
|
properties.push('source', 'image');
|
|
|
|
compareItem(actual, expected, message, options, properties);
|
|
|
|
if (pixels) {
|
|
|
|
comparePixels(actual, expected, message, options);
|
|
|
|
} else {
|
|
|
|
equals(actual.toDataURL(), expected.toDataURL(),
|
2016-02-14 06:39:35 -05:00
|
|
|
message + ' (#toDataUrl())');
|
2016-02-01 14:15:37 -05:00
|
|
|
}
|
2014-12-28 12:10:53 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Shape: function(actual, expected, message, options) {
|
|
|
|
compareItem(actual, expected, message, options,
|
|
|
|
['shape', 'size', 'radius']);
|
|
|
|
},
|
|
|
|
|
2016-01-31 10:52:51 -05:00
|
|
|
PointText: function(actual, expected, message, options) {
|
|
|
|
compareItem(actual, expected, message, options,
|
|
|
|
['content', 'point']);
|
|
|
|
},
|
|
|
|
|
|
|
|
SymbolItem: function(actual, expected, message, options) {
|
2014-12-28 12:10:53 -05:00
|
|
|
compareItem(actual, expected, message,
|
2016-01-31 10:52:51 -05:00
|
|
|
// Cloning SymbolItems does not result in cloned
|
|
|
|
// SymbolDefinitions
|
2014-12-28 12:10:53 -05:00
|
|
|
options && options.cloned
|
2016-02-13 17:10:09 -05:00
|
|
|
? Base.set({}, options, { cloned: false })
|
2014-12-28 12:10:53 -05:00
|
|
|
: options,
|
|
|
|
['symbol']);
|
|
|
|
},
|
|
|
|
|
2016-01-31 10:52:51 -05:00
|
|
|
SymbolDefinition: function(actual, expected, message, options) {
|
2016-02-14 06:39:35 -05:00
|
|
|
equals(actual.definition, expected.definition,
|
|
|
|
message + ' (#definition)', options);
|
2014-12-28 12:10:53 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Project: function(actual, expected, message, options) {
|
2016-01-31 10:52:51 -05:00
|
|
|
compareProperties(actual, expected, ['layers'], message, options);
|
2014-12-28 08:33:22 -05:00
|
|
|
}
|
|
|
|
};
|
2016-01-27 20:21:34 -05:00
|
|
|
|
|
|
|
// Some other helpers:
|
|
|
|
|
|
|
|
var getFunctionMessage = function(func) {
|
|
|
|
var message = func.toString().match(
|
|
|
|
/^\s*function[^\{]*\{([\s\S]*)\}\s*$/)[1]
|
|
|
|
.replace(/ /g, '')
|
|
|
|
.replace(/^\s+|\s+$/g, '');
|
|
|
|
if (/^return /.test(message)) {
|
|
|
|
message = message
|
|
|
|
.replace(/^return /, '')
|
|
|
|
.replace(/;$/, '');
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2016-02-11 04:05:21 -05:00
|
|
|
var compareBoolean = function(actual, expected, message, options) {
|
|
|
|
expected = typeof expected === 'string'
|
2016-02-14 17:22:39 -05:00
|
|
|
? PathItem.create(expected)
|
2016-02-11 04:05:21 -05:00
|
|
|
: expected;
|
|
|
|
if (typeof actual === 'function') {
|
|
|
|
if (!message)
|
|
|
|
message = getFunctionMessage(actual);
|
|
|
|
actual = actual();
|
|
|
|
}
|
|
|
|
actual.style = expected.style = {
|
|
|
|
strokeColor: 'black',
|
2016-02-13 16:41:09 -05:00
|
|
|
fillColor: expected.closed || expected.children ? 'yellow' : null
|
2016-02-11 04:05:21 -05:00
|
|
|
};
|
|
|
|
equals(actual, expected, message, Base.set({ rasterize: true }, options));
|
|
|
|
};
|
|
|
|
|
2016-01-27 20:21:34 -05:00
|
|
|
var createSVG = function(str, attrs) {
|
2016-02-14 16:51:50 -05:00
|
|
|
// Similar to SvgElement.create():
|
|
|
|
var node = document.createElementNS('http://www.w3.org/2000/svg', str);
|
|
|
|
for (var key in attrs)
|
|
|
|
node.setAttribute(key, attrs[key]);
|
|
|
|
// Paper.js paths do not have a fill by default, SVG does.
|
|
|
|
node.setAttribute('fill', 'none');
|
|
|
|
return node;
|
2016-01-27 20:21:34 -05:00
|
|
|
};
|
|
|
|
|
2016-02-11 04:05:21 -05:00
|
|
|
var compareSVG = function(done, actual, expected, message, options) {
|
|
|
|
function getItem(item) {
|
|
|
|
return item instanceof Item
|
|
|
|
? item
|
|
|
|
: typeof item === 'string'
|
2016-02-15 02:42:59 -05:00
|
|
|
? new Raster({
|
2016-02-15 03:21:11 -05:00
|
|
|
source: 'data:image/svg+xml;base64,' + window.btoa(item),
|
2016-02-15 02:42:59 -05:00
|
|
|
insert: false
|
|
|
|
})
|
2016-02-11 04:05:21 -05:00
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = getItem(actual);
|
|
|
|
expected = getItem(expected);
|
|
|
|
actual.position = expected.position;
|
|
|
|
|
|
|
|
if (typeof actual === 'function') {
|
|
|
|
if (!message)
|
|
|
|
message = getFunctionMessage(actual);
|
|
|
|
actual = actual();
|
|
|
|
}
|
|
|
|
|
2016-02-14 18:13:38 -05:00
|
|
|
function compare() {
|
2016-02-11 04:05:21 -05:00
|
|
|
comparePixels(actual, expected, message, Base.set({
|
2016-03-17 06:35:06 -04:00
|
|
|
tolerance: 1e-3,
|
2016-02-11 04:05:21 -05:00
|
|
|
resolution: 72
|
|
|
|
}, options));
|
|
|
|
done();
|
2016-02-14 18:13:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (expected instanceof Raster) {
|
|
|
|
expected.onLoad = compare;
|
|
|
|
} else if (actual instanceof Raster) {
|
|
|
|
actual.onLoad = compare;
|
|
|
|
} else {
|
|
|
|
compare();
|
|
|
|
}
|
2016-02-11 04:05:21 -05:00
|
|
|
};
|