2011-05-14 13:59:04 -04:00
|
|
|
// Override equals to convert functions to message and execute them as tests()
|
2011-05-07 12:25:34 -04:00
|
|
|
function equals(actual, expected, message) {
|
|
|
|
if (typeof actual === 'function') {
|
|
|
|
if (!message) {
|
|
|
|
message = actual.toString().match(
|
|
|
|
/^\s*function[^\{]*\{([\s\S]*)\}\s*$/)[1]
|
|
|
|
.replace(/ /g, '')
|
2011-05-07 12:46:06 -04:00
|
|
|
.replace(/^\s+|\s+$/g, '');
|
|
|
|
if (/^return /.test(message)) {
|
|
|
|
message = message
|
|
|
|
.replace(/^return /, '')
|
|
|
|
.replace(/;$/, '');
|
|
|
|
}
|
2011-05-07 12:25:34 -04:00
|
|
|
}
|
|
|
|
actual = actual();
|
|
|
|
}
|
2011-05-14 13:59:04 -04:00
|
|
|
// Let's be strict
|
2011-05-07 12:25:34 -04:00
|
|
|
return strictEqual(actual, expected, message);
|
|
|
|
}
|
2011-05-05 08:38:20 -04:00
|
|
|
|
2011-05-14 13:59:04 -04:00
|
|
|
function test(testName, expected) {
|
|
|
|
return QUnit.test(testName, function() {
|
2011-05-20 09:08:17 -04:00
|
|
|
var project = new Project();
|
2011-05-14 13:59:04 -04:00
|
|
|
expected();
|
2011-05-20 09:08:17 -04:00
|
|
|
project.remove();
|
2011-05-14 13:59:04 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-02-13 10:48:20 -05:00
|
|
|
function compareNumbers(number1, number2, message) {
|
2011-05-03 03:55:01 -04:00
|
|
|
if (number1 !== 0)
|
2011-03-08 20:25:38 -05:00
|
|
|
number1 = Math.round(number1 * 100) / 100;
|
2011-05-03 03:55:01 -04:00
|
|
|
if (number2 !== 0)
|
2011-03-08 20:25:38 -05:00
|
|
|
number2 = Math.round(number2 * 100) / 100;
|
|
|
|
equals(number1, number2, message);
|
2011-02-13 10:48:20 -05:00
|
|
|
}
|
|
|
|
|
2011-02-07 13:28:09 -05:00
|
|
|
function comparePoints(point1, point2, message) {
|
2011-05-05 08:05:39 -04:00
|
|
|
compareNumbers(point1.x, point2.x,
|
|
|
|
(message || '') + ' x');
|
|
|
|
compareNumbers(point1.y, point2.y,
|
|
|
|
(message || '') + ' y');
|
2011-02-13 09:35:48 -05:00
|
|
|
}
|
|
|
|
|
2011-02-13 10:48:20 -05:00
|
|
|
function compareRectangles(rect1, rect2, message) {
|
2011-05-05 08:05:39 -04:00
|
|
|
compareNumbers(rect1.x, rect2.x,
|
|
|
|
(message || '') + ' x');
|
|
|
|
compareNumbers(rect1.y, rect2.y,
|
|
|
|
(message || '') + ' y');
|
2011-02-13 10:48:20 -05:00
|
|
|
compareNumbers(rect1.width, rect2.width,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' width');
|
2011-02-13 10:48:20 -05:00
|
|
|
compareNumbers(rect1.height, rect2.height,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' height');
|
2011-02-13 10:48:20 -05:00
|
|
|
}
|
2011-02-19 11:11:17 -05:00
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
function compareRgbColors(color1, color2, message) {
|
|
|
|
color1 = new RgbColor(color1);
|
|
|
|
color2 = new RgbColor(color2);
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-03-08 20:25:38 -05:00
|
|
|
compareNumbers(color1.red, color2.red,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' red');
|
2011-03-08 20:25:38 -05:00
|
|
|
compareNumbers(color1.green, color2.green,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' green');
|
2011-03-08 20:25:38 -05:00
|
|
|
compareNumbers(color1.blue, color2.blue,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' blue');
|
2011-03-08 20:25:38 -05:00
|
|
|
compareNumbers(color1.alpha, color2.alpha,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' alpha');
|
2011-03-08 20:25:38 -05:00
|
|
|
}
|
|
|
|
|
2011-11-10 13:16:34 -05:00
|
|
|
function compareHsbColors(color1, color2, message) {
|
|
|
|
color1 = new HsbColor(color1);
|
|
|
|
color2 = new HsbColor(color2);
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-03-08 20:25:38 -05:00
|
|
|
compareNumbers(color1.hue, color2.hue,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' hue');
|
2011-03-08 20:25:38 -05:00
|
|
|
compareNumbers(color1.saturation, color2.saturation,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' saturation');
|
2011-03-08 20:25:38 -05:00
|
|
|
compareNumbers(color1.brightness, color2.brightness,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' brightness');
|
2011-03-08 20:25:38 -05:00
|
|
|
compareNumbers(color1.alpha, color2.alpha,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' alpha');
|
2011-03-08 20:25:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function compareGrayColors(color1, color2, message) {
|
|
|
|
color1 = new GrayColor(color1);
|
|
|
|
color2 = new GrayColor(color2);
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-03-08 20:25:38 -05:00
|
|
|
compareNumbers(color1.gray, color2.gray,
|
2011-05-05 08:05:39 -04:00
|
|
|
(message || '') + ' gray');
|
2011-05-20 13:39:12 -04:00
|
|
|
}
|
|
|
|
|
2011-05-21 13:11:43 -04:00
|
|
|
function compareGradientColors(gradientColor, gradientColor2, checkIdentity) {
|
2011-05-21 06:09:00 -04:00
|
|
|
Base.each(['origin', 'destination', 'hilite'], function(key) {
|
2011-05-21 13:11:43 -04:00
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return gradientColor[key] !== gradientColor2[key];
|
2011-05-21 13:11:43 -04:00
|
|
|
}, true, 'Strict compare GradientColor#' + key);
|
|
|
|
}
|
2011-05-21 12:38:49 -04:00
|
|
|
equals(gradientColor[key].toString(), gradientColor2[key].toString(),
|
|
|
|
'Compare GradientColor#' + key);
|
2011-05-21 06:09:00 -04:00
|
|
|
});
|
|
|
|
equals(function() {
|
2011-05-21 12:38:49 -04:00
|
|
|
return gradientColor.gradient.equals(gradientColor2.gradient);
|
2011-05-21 06:09:00 -04:00
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2011-05-21 13:11:43 -04:00
|
|
|
function comparePathStyles(style, style2, checkIdentity) {
|
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return style !== style2;
|
2011-05-21 13:11:43 -04:00
|
|
|
}, true);
|
|
|
|
}
|
2011-05-21 12:38:49 -04:00
|
|
|
Base.each(['fillColor', 'strokeColor'], function(key) {
|
|
|
|
if (style[key]) {
|
|
|
|
// The color should not point to the same color object:
|
2011-05-21 13:11:43 -04:00
|
|
|
if (checkIdentity) {
|
2011-05-21 12:38:49 -04:00
|
|
|
equals(function() {
|
2011-05-21 13:11:43 -04:00
|
|
|
return style[key] !== style2[key];
|
|
|
|
}, true, 'The ' + key + ' should not point to the same color object:');
|
|
|
|
}
|
|
|
|
if (style[key] instanceof GradientColor) {
|
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return style[key].gradient === style2[key].gradient;
|
2011-05-21 13:11:43 -04:00
|
|
|
}, true, 'The ' + key + '.gradient should point to the same object:');
|
|
|
|
}
|
|
|
|
compareGradientColors(style[key], style2[key], checkIdentity);
|
2011-05-21 12:38:49 -04:00
|
|
|
} else {
|
|
|
|
equals(style[key].toString(), style2[key].toString(),
|
|
|
|
'Compare PathStyle#' + key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Base.each(['strokeCap', 'strokeJoin', 'dashOffset', 'miterLimit',
|
2011-05-26 04:20:21 -04:00
|
|
|
'strokeOverprint', 'fillOverprint'], function(key) {
|
2011-05-21 12:38:49 -04:00
|
|
|
if (style[key]) {
|
|
|
|
equals(function() {
|
|
|
|
return style[key] == style2[key];
|
|
|
|
}, true, 'Compare PathStyle#' + key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (style.dashArray) {
|
|
|
|
equals(style.dashArray.toString(), style2.dashArray.toString(),
|
|
|
|
'Compare CharacterStyle#dashArray');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 05:55:39 -04:00
|
|
|
function compareObjects(name, keys, obj, obj2, checkIdentity) {
|
2011-05-21 12:43:22 -04:00
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return obj !== obj2;
|
2011-05-21 12:43:22 -04:00
|
|
|
}, true);
|
|
|
|
}
|
2011-05-21 12:38:49 -04:00
|
|
|
Base.each(keys, function(key) {
|
2011-05-26 05:55:39 -04:00
|
|
|
equals(obj[key], obj2[key], 'Compare ' + name + '#' + key);
|
2011-05-21 12:38:49 -04:00
|
|
|
});
|
2011-05-26 04:20:21 -04:00
|
|
|
}
|
2011-05-21 12:38:49 -04:00
|
|
|
|
2011-05-26 04:20:21 -04:00
|
|
|
function compareCharacterStyles(characterStyle, characterStyle2, checkIdentity) {
|
|
|
|
compareObjects('CharacterStyle', ['fontSize', 'font'],
|
|
|
|
characterStyle, characterStyle2, checkIdentity);
|
2011-05-21 12:38:49 -04:00
|
|
|
}
|
|
|
|
|
2011-05-21 12:43:22 -04:00
|
|
|
function compareParagraphStyles(paragraphStyle, paragraphStyle2, checkIdentity) {
|
2011-05-26 04:20:21 -04:00
|
|
|
compareObjects('ParagraphStyle', ['justification'],
|
|
|
|
paragraphStyle, paragraphStyle2, checkIdentity);
|
2011-05-21 12:38:49 -04:00
|
|
|
}
|
|
|
|
|
2011-05-21 14:36:30 -04:00
|
|
|
function compareSegmentPoints(segmentPoint, segmentPoint2, checkIdentity) {
|
2011-05-26 05:54:46 -04:00
|
|
|
compareObjects('SegmentPoint', ['x', 'y', 'selected'],
|
2011-05-26 04:20:21 -04:00
|
|
|
segmentPoint, segmentPoint2, checkIdentity);
|
2011-05-21 14:36:30 -04:00
|
|
|
}
|
|
|
|
|
2011-05-21 13:41:02 -04:00
|
|
|
function compareSegments(segment, segment2, checkIdentity) {
|
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
|
|
|
return segment !== segment2;
|
|
|
|
}, true);
|
|
|
|
}
|
2011-05-21 14:37:25 -04:00
|
|
|
equals(function() {
|
|
|
|
return segment.selected == segment2.selected;
|
|
|
|
}, true);
|
2011-05-26 04:20:21 -04:00
|
|
|
Base.each(['handleIn', 'handleOut', 'point'], function(key) {
|
2011-05-21 14:37:25 -04:00
|
|
|
compareSegmentPoints(segment[key], segment2[key]);
|
2011-05-26 04:20:21 -04:00
|
|
|
});
|
2011-05-21 13:41:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function compareSegmentLists(segmentList, segmentList2, checkIdentity) {
|
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
|
|
|
return segmentList !== segmentList2;
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
equals(segmentList.toString(), segmentList2.toString(),
|
|
|
|
'Compare Item#segments');
|
|
|
|
if (checkIdentity) {
|
|
|
|
for (var i = 0, l = segmentList.length; i < l; i++) {
|
|
|
|
var segment = segmentList[i],
|
|
|
|
segment2 = segmentList2[i];
|
|
|
|
compareSegments(segment, segment2, checkIdentity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 01:27:29 -05:00
|
|
|
function compareItems(item, item2, cloned, checkIdentity) {
|
2011-05-21 12:43:22 -04:00
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return item !== item2;
|
2011-05-21 12:43:22 -04:00
|
|
|
}, true);
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-05-21 13:11:43 -04:00
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return item.id !== item2.id;
|
2011-05-21 13:11:43 -04:00
|
|
|
}, true);
|
2011-05-21 12:43:22 -04:00
|
|
|
}
|
2011-05-20 13:39:12 -04:00
|
|
|
|
2011-05-21 12:38:49 -04:00
|
|
|
equals(function() {
|
|
|
|
return item.constructor == item2.constructor;
|
|
|
|
}, true);
|
|
|
|
|
2011-05-20 13:39:12 -04:00
|
|
|
var itemProperties = ['opacity', 'locked', 'visible', 'blendMode', 'name',
|
2013-01-20 01:27:29 -05:00
|
|
|
'selected', 'clipMask'];
|
2011-05-20 13:39:12 -04:00
|
|
|
Base.each(itemProperties, function(key) {
|
2013-01-20 01:27:29 -05:00
|
|
|
var value = item[key];
|
|
|
|
// When item was cloned and had a name, the name will be versioned
|
|
|
|
equals(
|
|
|
|
key == 'name' && cloned && value
|
|
|
|
? value + ' 1'
|
|
|
|
: value,
|
|
|
|
item2[key],
|
|
|
|
'compare Item#' + key);
|
2011-05-20 13:39:12 -04:00
|
|
|
});
|
|
|
|
|
2011-05-21 13:11:43 -04:00
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return item.bounds !== item2.bounds;
|
2011-05-21 13:11:43 -04:00
|
|
|
}, true);
|
|
|
|
}
|
2011-05-21 06:49:15 -04:00
|
|
|
|
2011-05-21 12:38:49 -04:00
|
|
|
equals(item.bounds.toString(), item2.bounds.toString(),
|
|
|
|
'Compare Item#bounds');
|
2011-05-21 14:37:25 -04:00
|
|
|
|
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return item.position !== item2.position;
|
2011-05-21 14:37:25 -04:00
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
equals(item.position.toString(), item2.position.toString(),
|
|
|
|
'Compare Item#position');
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-05-21 06:48:43 -04:00
|
|
|
if (item.matrix) {
|
2011-05-21 13:11:43 -04:00
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return item.matrix !== item2.matrix;
|
2011-05-21 13:11:43 -04:00
|
|
|
}, true);
|
|
|
|
}
|
2011-05-21 06:48:43 -04:00
|
|
|
equals(item.matrix.toString(), item2.matrix.toString(),
|
2011-05-21 12:38:49 -04:00
|
|
|
'Compare Item#matrix');
|
2011-05-21 06:48:43 -04:00
|
|
|
}
|
2011-05-20 13:39:12 -04:00
|
|
|
|
2011-05-21 13:42:29 -04:00
|
|
|
// Path specific
|
|
|
|
if (item2 instanceof Path) {
|
2011-05-21 14:37:25 -04:00
|
|
|
var keys = ['closed', 'fullySelected', 'clockwise', 'length'];
|
|
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
|
|
|
var key = keys[i];
|
2011-05-26 05:55:39 -04:00
|
|
|
equals(item[key], item2[key], 'Compare Path#' + key);
|
2011-05-21 14:37:25 -04:00
|
|
|
}
|
2011-05-21 13:42:29 -04:00
|
|
|
compareSegmentLists(item.segments, item2.segments, checkIdentity);
|
|
|
|
}
|
2011-05-21 06:49:15 -04:00
|
|
|
|
2011-05-20 13:39:12 -04:00
|
|
|
// Group specific
|
|
|
|
if (item instanceof Group) {
|
|
|
|
equals(function() {
|
2011-05-21 12:38:49 -04:00
|
|
|
return item.clipped == item2.clipped;
|
2011-05-20 13:39:12 -04:00
|
|
|
}, true);
|
|
|
|
}
|
2011-05-21 06:49:15 -04:00
|
|
|
|
2011-05-20 13:39:12 -04:00
|
|
|
// Layer specific
|
|
|
|
if (item instanceof Layer) {
|
|
|
|
equals(function() {
|
|
|
|
return item.project == item2.project;
|
|
|
|
}, true);
|
|
|
|
}
|
2011-05-21 06:49:15 -04:00
|
|
|
|
2011-05-20 13:39:12 -04:00
|
|
|
// PlacedSymbol specific
|
|
|
|
if (item instanceof PlacedSymbol) {
|
|
|
|
equals(function() {
|
|
|
|
return item.symbol == item2.symbol;
|
|
|
|
}, true);
|
|
|
|
}
|
2011-05-21 06:47:58 -04:00
|
|
|
|
|
|
|
// Raster specific
|
|
|
|
if (item instanceof Raster) {
|
2011-05-21 12:38:49 -04:00
|
|
|
// TODO: remove access of private fields:
|
2011-05-21 06:47:58 -04:00
|
|
|
if (item._canvas) {
|
2011-05-21 13:11:43 -04:00
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return item._canvas !== item2._canvas;
|
2011-05-21 13:11:43 -04:00
|
|
|
}, true);
|
|
|
|
}
|
2011-05-21 06:47:58 -04:00
|
|
|
}
|
|
|
|
if (item._image) {
|
|
|
|
equals(function() {
|
2011-05-21 12:38:49 -04:00
|
|
|
return item._image == item2._image;
|
2011-05-21 06:47:58 -04:00
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
equals(item._size.toString(), item2._size.toString(),
|
2011-05-21 12:38:49 -04:00
|
|
|
'Compare Item#size');
|
2011-05-21 06:47:58 -04:00
|
|
|
}
|
|
|
|
|
2011-05-20 13:39:12 -04:00
|
|
|
// TextItem specific:
|
|
|
|
if (item instanceof TextItem) {
|
2011-05-21 12:38:49 -04:00
|
|
|
equals(item.content, item2.content, 'Compare Item#content');
|
2011-05-21 12:43:22 -04:00
|
|
|
compareCharacterStyles(item.characterStyle, item2.characterStyle,
|
|
|
|
checkIdentity);
|
2011-05-20 13:39:12 -04:00
|
|
|
}
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-05-20 13:39:12 -04:00
|
|
|
// PointText specific:
|
|
|
|
if (item instanceof PointText) {
|
2011-05-21 13:11:43 -04:00
|
|
|
if (checkIdentity) {
|
|
|
|
equals(function() {
|
2013-01-20 01:27:29 -05:00
|
|
|
return item.point !== item2.point;
|
2011-05-21 13:11:43 -04:00
|
|
|
}, true);
|
|
|
|
}
|
2011-12-20 17:32:28 -05:00
|
|
|
equals(item.point.toString(), item2.point.toString(),
|
|
|
|
'Compare Item#point');
|
2011-05-20 13:39:12 -04:00
|
|
|
}
|
2011-07-07 10:09:02 -04:00
|
|
|
|
2011-05-21 12:38:49 -04:00
|
|
|
if (item.style) {
|
2011-05-20 13:39:12 -04:00
|
|
|
// Path Style
|
2011-05-21 12:43:22 -04:00
|
|
|
comparePathStyles(item.style, item2.style, checkIdentity);
|
2011-05-20 13:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check length of children and recursively compare them:
|
|
|
|
if (item.children) {
|
|
|
|
equals(function() {
|
|
|
|
return item.children.length == item2.children.length;
|
|
|
|
}, true);
|
|
|
|
for (var i = 0, l = item.children.length; i < l; i++) {
|
2013-01-20 01:27:29 -05:00
|
|
|
compareItems(item.children[i], item2.children[i], cloned, checkIdentity);
|
2011-05-20 13:39:12 -04:00
|
|
|
}
|
|
|
|
}
|
2011-05-26 04:20:21 -04:00
|
|
|
}
|
2012-12-01 14:05:48 -05:00
|
|
|
|
|
|
|
// SVG
|
|
|
|
|
|
|
|
function createSvg(xml) {
|
|
|
|
return new DOMParser().parseFromString('<svg xmlns="http://www.w3.org/2000/svg">' + xml + '</svg>', 'application/xml');
|
|
|
|
}
|