paper.js/test/tests/Item.js

473 lines
11 KiB
JavaScript
Raw Normal View History

2011-07-01 06:17:45 -04:00
/*
* Paper.js
*
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
* based on Scriptographer.org and designed to be largely API compatible.
* http://paperjs.org/
* http://scriptographer.org/
*
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
2011-02-11 12:50:26 -05:00
module('Item');
test('copyTo(project)', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
var path = new Path();
var secondDoc = new Project();
var copy = path.copyTo(secondDoc);
equals(function() {
return secondDoc.activeLayer.children.indexOf(copy) != -1;
}, true);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.children.indexOf(copy) == -1;
}, true);
equals(function() {
return copy != path;
}, true);
});
test('copyTo(layer)', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
var path = new Path();
var layer = new Layer();
var copy = path.copyTo(layer);
equals(function() {
return layer.children.indexOf(copy) != -1;
}, true);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.layers[0].children.indexOf(copy) == -1;
}, true);
});
test('clone()', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
var path = new Path();
var copy = path.clone();
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.children.length;
}, 2);
equals(function() {
return path != copy;
}, true);
});
test('addChild(item)', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
2011-02-11 12:50:26 -05:00
var path = new Path();
project.activeLayer.addChild(path);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.children.length;
}, 1);
2011-02-11 12:50:26 -05:00
});
test('item.parent / item.isChild / item.isParent', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
var secondDoc = new Project();
2011-02-11 12:50:26 -05:00
var path = new Path();
project.activeLayer.addChild(path);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.children.indexOf(path) != -1;
}, true);
secondDoc.activeLayer.addChild(path);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.isChild(path);
}, false);
equals(function() {
2011-05-20 09:08:17 -04:00
return path.isParent(project.activeLayer);
}, false);
equals(function() {
return secondDoc.activeLayer.isChild(path);
}, true);
equals(function() {
return path.isParent(secondDoc.activeLayer);
}, true);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.children.indexOf(path) == -1;
}, true);
equals(function() {
return secondDoc.activeLayer.children.indexOf(path) == 0;
}, true);
2011-02-11 12:50:26 -05:00
});
test('item.lastChild / item.firstChild', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
2011-02-11 12:50:26 -05:00
var path = new Path();
var secondPath = new Path();
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.firstChild == path;
}, true);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.lastChild == secondPath;
}, true);
2011-02-11 12:50:26 -05:00
});
test('insertChild(0, item)', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
2011-02-11 12:50:26 -05:00
var path = new Path();
var secondPath = new Path();
project.activeLayer.insertChild(0, secondPath);
equals(function() {
return secondPath.index < path.index;
}, true);
2011-02-11 12:50:26 -05:00
});
test('insertAbove(item)', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
2011-02-11 12:50:26 -05:00
var path = new Path();
var secondPath = new Path();
path.insertAbove(secondPath);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.lastChild == path;
}, true);
2011-02-11 12:50:26 -05:00
});
test('insertBelow(item)', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
2011-02-11 12:50:26 -05:00
var firstPath = new Path();
var secondPath = new Path();
equals(function() {
return secondPath.index > firstPath.index;
}, true);
secondPath.insertBelow(firstPath);
equals(function() {
return secondPath.index < firstPath.index;
}, true);
2011-02-11 12:50:26 -05:00
});
test('isDescendant(item) / isAncestor(item)', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
2011-02-11 12:50:26 -05:00
var path = new Path();
equals(function() {
2011-05-20 09:08:17 -04:00
return path.isDescendant(project.activeLayer);
}, true);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.isDescendant(path);
}, false);
equals(function() {
2011-05-20 09:08:17 -04:00
return path.isAncestor(project.activeLayer);
}, false);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.isAncestor(path);
}, true);
// an item can't be its own descendant:
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.isDescendant(project.activeLayer);
}, false);
// an item can't be its own ancestor:
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.isAncestor(project.activeLayer);
}, false);
});
test('isGroupedWith', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
var path = new Path();
var secondPath = new Path();
var group = new Group([path]);
var secondGroup = new Group([secondPath]);
equals(function() {
return path.isGroupedWith(secondPath);
}, false);
secondGroup.addChild(path);
equals(function() {
return path.isGroupedWith(secondPath);
}, true);
equals(function() {
return path.isGroupedWith(group);
}, false);
equals(function() {
return path.isDescendant(secondGroup);
}, true);
equals(function() {
return secondGroup.isDescendant(path);
}, false);
equals(function() {
return secondGroup.isDescendant(secondGroup);
}, false);
equals(function() {
return path.isGroupedWith(secondGroup);
}, false);
paper.project.activeLayer.addChild(path);
equals(function() {
return path.isGroupedWith(secondPath);
}, false);
paper.project.activeLayer.addChild(secondPath);
equals(function() {
return path.isGroupedWith(secondPath);
}, false);
2011-02-11 12:50:26 -05:00
});
test('getPreviousSibling() / getNextSibling()', function() {
var firstPath = new Path();
var secondPath = new Path();
equals(function() {
return firstPath.nextSibling == secondPath;
}, true);
equals(function() {
return secondPath.previousSibling == firstPath;
}, true);
equals(function() {
return secondPath.nextSibling == null;
}, true);
2011-02-11 12:50:26 -05:00
});
test('reverseChildren()', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
var path = new Path();
var secondPath = new Path();
var thirdPath = new Path();
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.firstChild == path;
}, true);
2011-05-20 09:08:17 -04:00
project.activeLayer.reverseChildren();
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.firstChild == path;
}, false);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.firstChild == thirdPath;
}, true);
equals(function() {
2011-05-20 09:08:17 -04:00
return project.activeLayer.lastChild == path;
}, true);
});
test('Check item#project when moving items across projects', function() {
2011-05-20 09:08:17 -04:00
var project = paper.project;
var doc1 = new Project();
var path = new Path();
var group = new Group();
group.addChild(new Path());
equals(function() {
return path.project == doc1;
}, true);
var doc2 = new Project();
doc2.activeLayer.addChild(path);
equals(function() {
return path.project == doc2;
}, true);
doc2.activeLayer.addChild(group);
equals(function() {
return group.children[0].project == doc2;
}, true);
});
test('group.selected', function() {
var path = new Path([0, 0]);
var path2 = new Path([0, 0]);
var group = new Group([path, path2]);
path.selected = true;
equals(function() {
return group.selected;
}, true);
path.selected = false;
equals(function() {
return group.selected;
}, false);
group.selected = true;
equals(function() {
return path.selected;
}, true);
equals(function() {
return path2.selected;
}, true);
group.selected = false;
equals(function() {
return path.selected;
}, false);
equals(function() {
return path2.selected;
2011-05-15 13:13:55 -04:00
}, false);
});
test('Check parent children object for named item', function() {
var path = new Path();
path.name = 'test';
equals(function() {
return paper.project.activeLayer.children['test'] == path;
2011-05-15 13:13:55 -04:00
}, true);
var path2 = new Path();
path2.name = 'test';
equals(function() {
return paper.project.activeLayer.children['test'] == path2;
2011-05-15 13:13:55 -04:00
}, true);
path2.remove();
equals(function() {
return paper.project.activeLayer.children['test'] == path;
2011-05-15 13:13:55 -04:00
}, true);
path.remove();
equals(function() {
return !paper.project.activeLayer.children['test'];
2011-05-15 13:13:55 -04:00
}, true);
});
2011-06-17 11:33:25 -04:00
test('Named child access 1', function() {
2011-05-15 13:13:55 -04:00
var path = new Path();
path.name = 'test';
var path2 = new Path();
path2.name = 'test';
path.remove();
equals(function() {
return paper.project.activeLayer.children['test'] == path2;
2011-05-15 13:13:55 -04:00
}, true);
});
test('Named child access 2', function() {
var path = new Path();
path.name = 'test';
var path2 = new Path();
path2.name = 'test';
path.remove();
equals(function() {
return paper.project.activeLayer.children['test'] == path2;
2011-05-15 13:13:55 -04:00
}, true);
equals(function() {
return paper.project.activeLayer._namedChildren['test'].length == 1;
2011-05-15 13:13:55 -04:00
}, true);
path2.remove();
equals(function() {
return !paper.project.activeLayer._namedChildren['test'];
2011-05-15 13:13:55 -04:00
}, true);
equals(function() {
return paper.project.activeLayer.children['test'] === undefined;
2011-05-15 13:13:55 -04:00
}, true);
});
2011-06-17 11:33:25 -04:00
test('Named child access 3', function() {
2011-05-15 13:13:55 -04:00
var path = new Path();
path.name = 'test';
var path2 = new Path();
path2.name = 'test';
var group = new Group();
group.addChild(path2);
2011-05-15 13:13:55 -04:00
equals(function() {
return paper.project.activeLayer.children['test'] == path;
2011-05-15 13:13:55 -04:00
}, true);
2011-06-17 11:33:25 -04:00
// TODO: Tests should not access internal properties
2011-05-15 13:13:55 -04:00
equals(function() {
2011-06-17 11:33:25 -04:00
return paper.project.activeLayer._namedChildren['test'].length;
}, 1);
2011-05-15 13:13:55 -04:00
equals(function() {
return group.children['test'] == path2;
}, true);
2011-06-17 11:33:25 -04:00
2011-05-15 13:13:55 -04:00
equals(function() {
return group._namedChildren['test'].length == 1;
}, true);
2011-06-17 11:33:25 -04:00
equals(function() {
return paper.project.activeLayer._namedChildren['test'][0] == path;
}, true);
paper.project.activeLayer.appendTop(path2);
2011-05-15 13:13:55 -04:00
2011-06-17 11:33:25 -04:00
equals(function() {
return group.children['test'] == null;
}, true);
2011-05-15 13:13:55 -04:00
equals(function() {
return group._namedChildren['test'] === undefined;
}, true);
2011-06-17 11:33:25 -04:00
2011-05-15 13:13:55 -04:00
equals(function() {
2011-06-17 11:33:25 -04:00
return paper.project.activeLayer.children['test'] == path2;
2011-05-15 13:13:55 -04:00
}, true);
2011-06-17 11:33:25 -04:00
equals(function() {
return paper.project.activeLayer._namedChildren['test'].length;
}, 2);
2011-05-15 13:13:55 -04:00
});
test('Setting name of child back to null', function() {
var path = new Path();
path.name = 'test';
2011-05-15 13:53:22 -04:00
2011-05-15 13:13:55 -04:00
var path2 = new Path();
path2.name = 'test';
2011-05-15 13:53:22 -04:00
equals(function() {
return paper.project.activeLayer.children['test'] == path2;
2011-05-15 13:53:22 -04:00
}, true);
2011-05-15 13:13:55 -04:00
path2.name = null;
equals(function() {
return paper.project.activeLayer.children['test'] == path;
2011-05-15 13:13:55 -04:00
}, true);
path.name = null;
equals(function() {
return paper.project.activeLayer.children['test'] === undefined;
2011-05-15 13:13:55 -04:00
}, true);
});
test('Renaming item', function() {
var path = new Path();
path.name = 'test';
path.name = 'test2';
equals(function() {
return paper.project.activeLayer.children['test'] === undefined;
2011-05-15 13:13:55 -04:00
}, true);
equals(function() {
return paper.project.activeLayer.children['test2'] == path;
2011-05-15 13:13:55 -04:00
}, true);
});
test('Changing item#position.x', function() {
var path = new Path.Circle(new Point(50, 50), 50);
path.position.x += 5;
equals(path.position.toString(), '{ x: 55, y: 50 }', 'path.position.x += 5');
});
test('Cloning a linked size', function() {
var path = new Path([40, 75], [140, 75]);
var error = null;
try {
var cloneSize = path.bounds.size.clone();
} catch (e) {
error = e;
}
var description = 'Cloning a linked size should not throw an error';
if (error)
description += ': ' + error;
equals(error == null, true, description);
});