More tests for SVGTransformList

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1857 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2010-11-08 16:26:58 +00:00
parent 711346f109
commit 1039b68c0a
2 changed files with 137 additions and 2 deletions

View File

@ -121,9 +121,12 @@ svgedit.transformlist.SVGTransformList = function(elem) {
var fname = 'set' + name.charAt(0).toUpperCase() + name.slice(1); var fname = 'set' + name.charAt(0).toUpperCase() + name.slice(1);
var values = name=='matrix'?[mtx]:val_arr; var values = name=='matrix'?[mtx]:val_arr;
if(name == 'scale' && values.length == 1) { if (name == 'scale' && values.length == 1) {
values.push(values[0]); values.push(values[0]);
} else if(name == 'translate' && values.length == 1) { } else if (name == 'translate' && values.length == 1) {
values.push(0);
} else if (name == 'rotate' && values.length == 1) {
values.push(0);
values.push(0); values.push(0);
} }
xform[fname].apply(xform, values); xform[fname].apply(xform, values);

View File

@ -24,6 +24,12 @@
var svgroot = document.getElementById('svgroot'); var svgroot = document.getElementById('svgroot');
var svgcontent, rect, circle; var svgcontent, rect, circle;
var NEAR_ZERO = 1e-7;
function almostEquals(a, b, msg) {
msg = msg || (a + ' did not equal ' + b);
ok(Math.abs(a - b) < NEAR_ZERO, msg);
}
function setUp() { function setUp() {
svgcontent = svgroot.appendChild(document.createElementNS(svgns, 'svg')); svgcontent = svgroot.appendChild(document.createElementNS(svgns, 'svg'));
rect = svgcontent.appendChild(document.createElementNS(svgns, 'rect')); rect = svgcontent.appendChild(document.createElementNS(svgns, 'rect'));
@ -231,6 +237,132 @@
tearDown(); tearDown();
}); });
test('Test SVGTransformList.init() for translate(200,100)', function() {
expect(8);
setUp();
rect.setAttribute('transform', 'translate(200,100)');
var rxform = svgedit.transformlist.getTransformList(rect);
equals(1, rxform.numberOfItems);
var translate = rxform.getItem(0);
equals(2, translate.type);
var m = translate.matrix;
equals(m.a, 1);
equals(m.b, 0);
equals(m.c, 0);
equals(m.d, 1);
equals(m.e, 200);
equals(m.f, 100);
tearDown();
});
test('Test SVGTransformList.init() for scale(4)', function() {
expect(8);
setUp();
rect.setAttribute('transform', 'scale(4)');
var rxform = svgedit.transformlist.getTransformList(rect);
equals(1, rxform.numberOfItems);
var scale = rxform.getItem(0);
equals(3, scale.type);
var m = scale.matrix;
equals(m.a, 4);
equals(m.b, 0);
equals(m.c, 0);
equals(m.d, 4);
equals(m.e, 0);
equals(m.f, 0);
tearDown();
});
test('Test SVGTransformList.init() for scale(4,3)', function() {
expect(8);
setUp();
rect.setAttribute('transform', 'scale(4,3)');
var rxform = svgedit.transformlist.getTransformList(rect);
equals(1, rxform.numberOfItems);
var scale = rxform.getItem(0);
equals(3, scale.type);
var m = scale.matrix;
equals(m.a, 4);
equals(m.b, 0);
equals(m.c, 0);
equals(m.d, 3);
equals(m.e, 0);
equals(m.f, 0);
tearDown();
});
test('Test SVGTransformList.init() for rotate(45)', function() {
expect(9);
setUp();
rect.setAttribute('transform', 'rotate(45)');
var rxform = svgedit.transformlist.getTransformList(rect);
equals(1, rxform.numberOfItems);
var rotate = rxform.getItem(0);
equals(4, rotate.type);
equals(45, rotate.angle);
var m = rotate.matrix;
almostEquals(m.a, 1/Math.sqrt(2));
almostEquals(m.b, 1/Math.sqrt(2));
almostEquals(m.c, -1/Math.sqrt(2));
almostEquals(m.d, 1/Math.sqrt(2));
equals(m.e, 0);
equals(m.f, 0);
tearDown();
});
test('Test SVGTransformList.init() for rotate(45, 100, 200)', function() {
expect(9);
setUp();
rect.setAttribute('transform', 'rotate(45, 100, 200)');
var rxform = svgedit.transformlist.getTransformList(rect);
equals(1, rxform.numberOfItems);
var rotate = rxform.getItem(0);
equals(4, rotate.type);
equals(45, rotate.angle);
var m = rotate.matrix;
almostEquals(m.a, 1/Math.sqrt(2));
almostEquals(m.b, 1/Math.sqrt(2));
almostEquals(m.c, -1/Math.sqrt(2));
almostEquals(m.d, 1/Math.sqrt(2));
// TODO(codedread): calculation
var r = svgcontent.createSVGMatrix();
r.a = 1/Math.sqrt(2); r.b = 1/Math.sqrt(2);
r.c = -1/Math.sqrt(2); r.d = 1/Math.sqrt(2);
var t = svgcontent.createSVGMatrix();
t.e = -100; t.f = -200;
var t_ = svgcontent.createSVGMatrix();
t_.e = 100; t_.f = 200;
var result = t_.multiply(r).multiply(t);
almostEquals(m.e, result.e);
almostEquals(m.f, result.f);
tearDown();
});
}); });
</script> </script>
</head> </head>