paper.js/test/tests/Layer.js

148 lines
4.3 KiB
JavaScript
Raw Normal View History

2011-07-01 06:17:45 -04:00
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
2011-07-01 06:17:45 -04:00
* http://paperjs.org/
*
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
2011-07-01 06:17:45 -04:00
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
QUnit.module('Layer');
2011-02-16 16:11:26 -05:00
2016-08-07 09:58:54 -04:00
test('#previousSibling / #nextSibling', function() {
2014-08-16 13:24:54 -04:00
var project = paper.project;
var firstLayer = project.activeLayer;
var secondLayer = new Layer();
equals(function() {
return secondLayer.previousSibling == firstLayer;
}, true);
equals(function() {
return secondLayer.nextSibling == null;
}, true);
2011-07-07 10:09:02 -04:00
2014-08-16 13:24:54 -04:00
// Move another layer into secondLayer and check nextSibling /
// previousSibling:
var path = new Path();
var thirdLayer = new Layer();
secondLayer.insertChild(0, thirdLayer);
equals(function() {
return secondLayer.children.length;
}, 2);
equals(function() {
return thirdLayer.nextSibling === path;
2014-08-16 13:24:54 -04:00
}, true);
secondLayer.addChild(thirdLayer);
equals(function() {
return thirdLayer.nextSibling;
}, null);
2014-08-16 13:24:54 -04:00
equals(function() {
return thirdLayer.previousSibling === path;
2014-08-16 13:24:54 -04:00
}, true);
equals(function() {
return project.layers.length;
}, 2);
2011-07-07 10:09:02 -04:00
2014-08-16 13:24:54 -04:00
firstLayer.addChild(secondLayer);
equals(function() {
return project.layers.length;
}, 1);
2011-02-16 16:11:26 -05:00
});
2016-08-07 09:58:54 -04:00
test('#insertAbove() / #insertBelow()', function() {
2014-08-16 13:24:54 -04:00
var project = paper.project;
var firstLayer = project.activeLayer;
firstLayer.name = 'first';
var secondLayer = new Layer();
secondLayer.name = 'second';
var thirdLayer = new Layer();
thirdLayer.name = 'third';
2012-11-19 23:41:04 -05:00
2014-08-16 13:24:54 -04:00
thirdLayer.insertBelow(firstLayer);
equals(function() {
return thirdLayer.previousSibling == null;
}, true);
equals(function() {
return thirdLayer.nextSibling == firstLayer;
}, true);
2012-11-19 23:41:04 -05:00
2014-08-16 13:24:54 -04:00
secondLayer.insertBelow(firstLayer);
equals(function() {
return secondLayer.previousSibling == thirdLayer;
}, true);
equals(function() {
return secondLayer.nextSibling == firstLayer;
}, true);
2011-07-07 10:09:02 -04:00
2014-08-16 13:24:54 -04:00
var path = new Path();
firstLayer.addChild(path);
2011-02-16 16:11:26 -05:00
2014-08-16 13:24:54 -04:00
// move the layer above the path, inside the firstLayer.
// 'Above' means visually appearing on top, thus with a larger index.
secondLayer.insertAbove(path);
equals(function() {
return path.nextSibling == secondLayer;
}, true);
equals(function() {
return secondLayer.parent == firstLayer;
}, true);
// There should now only be two layers left:
equals(function() {
return project.layers.length;
}, 2);
2011-05-20 09:08:17 -04:00
});
2016-08-07 09:58:54 -04:00
test('#addChild() / #insertBelow() with nesting', function() {
2014-08-16 13:24:54 -04:00
var project = paper.project;
var firstLayer = project.activeLayer;
var secondLayer = new Layer();
// There should be two layers now in project.layers
equals(function() {
return project.layers.length;
}, 2);
firstLayer.addChild(secondLayer);
equals(function() {
return secondLayer.parent == firstLayer;
}, true);
// There should only be the firsLayer now in project.layers
equals(function() {
return project.layers.length;
}, 1);
equals(function() {
return project.layers[0] == firstLayer;
}, true);
// Now move secondLayer bellow the first again, in which case it should
// reappear in project.layers
secondLayer.insertBelow(firstLayer);
// There should be two layers now in project.layers again now
equals(function() {
return project.layers.length;
}, 2);
equals(function() {
return project.layers[0] == secondLayer
&& project.layers[1] == firstLayer;
}, true);
2012-11-19 23:41:04 -05:00
});
2016-08-05 21:03:42 -04:00
2016-08-07 09:58:54 -04:00
test('#remove() with named layers', function(){
var name = 'my layer';
var layer1 = new Layer({name: name });
var layer2 = new Layer({name: name });
2016-08-05 21:03:42 -04:00
var removeCount = 0;
2016-08-07 09:58:54 -04:00
while (project.layers[name]) {
project.layers[name].remove();
if (++removeCount > 2)
break;
2016-08-05 21:03:42 -04:00
}
2016-08-07 09:58:54 -04:00
equals(removeCount, 2,
'project.layers[name].remove(); should be called twice');
2016-08-05 21:03:42 -04:00
});
test('#bounds with nested empty items', function() {
var item = new Path.Rectangle(new Point(10,10), new Size(10));
new Group(new Group());
equals(item.bounds, project.activeLayer.bounds);
});