1 <html xmlns=
"http://www.w3.org/1999/xhtml">
3 <title>Test SVGMatrix behavior
</title>
4 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
5 <script type=
"text/javascript" src=
"matrixUtils.js"></script>
6 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
11 <svg xmlns=
"http://www.w3.org/2000/svg" width=
"100%" height=
"1" id=
"svg">
12 <g id=
"g" transform=
"translate(10, 20)"/>
16 <script class=
"testbody" type=
"text/javascript">
19 SimpleTest.waitForExplicitFinish();
36 for (var i =
0; i < tests.length; i++) {
42 function testCreateMatrix() {
44 var m = svg.createSVGMatrix();
46 // Should be initialised to identity
47 cmpMatrix(m, [
1,
0,
0,
1,
0,
0],
48 "createMatrix should produce identity matrix");
50 // Should return a new object each time;
51 ok(m != svg.createSVGMatrix(),
52 "Got identical objects when creating new matrix");
55 // SVGMatrix multiply(in SVGMatrix secondMatrix);
56 function testMultiply() {
57 // This is the example from SVG
1.1 section
7.5
58 var m1 = createMatrix(
1,
0,
0,
1,
50,
90);
59 var m2 = createMatrix(
0.707, -
0.707,
0.707,
0.707,
0,
0);
60 var m3 = createMatrix(
1,
0,
0,
1,
130,
160);
61 var result = m1.multiply(m2).multiply(m3);
62 roughCmpMatrix(result, [
0.707, -
0.707,
0.707,
0.707,
255.03,
111.21],
63 "Unexpected result after multiplying matrices");
65 // Check orig matrices are unchanged
66 cmpMatrix(m1, [
1,
0,
0,
1,
50,
90],
"Matrix changed after multiplication");
67 roughCmpMatrix(m2, [
0.707, -
0.707,
0.707,
0.707,
0,
0],
68 "Matrix changed after multiplication");
69 cmpMatrix(m3, [
1,
0,
0,
1,
130,
160],
"Matrix changed after multiplication");
72 // SVGMatrix inverse() raises(SVGException);
73 function testInverse() {
75 var m = createMatrix(
2,
0,
0,
4,
110, -
50);
76 roughCmpMatrix(m.inverse(), [
0.5,
0,
0,
0.25, -
55,
12.5],
77 "Unexpected result after inverting matrix");
79 // Test non-invertable
80 m = createMatrix(
0,
0,
1,
0,
0,
0);
83 ok(false,
"Failed to throw exception when inverting singular matrix");
85 is(e.name,
"InvalidStateError",
86 "Got unexpected exception " + e +
", expected InvalidStateError");
90 // SVGMatrix translate(in float x, in float y);
91 function testTranslate() {
92 var m = createMatrix(
2,
0,
0,
1,
120,
100);
93 roughCmpMatrix(m.translate(
100, -
50), [
2,
0,
0,
1,
320,
50],
94 "Unexpected result after translate");
97 // SVGMatrix scale(in float scaleFactor);
98 function testScale() {
99 var m = createMatrix(
2,
0,
0,
1,
120,
100);
100 roughCmpMatrix(m.scale(
0.5), [
1,
0,
0,
0.5,
120,
100],
101 "Unexpected result after scale");
104 // SVGMatrix scaleNonUniform(in float scaleFactorX, in float scaleFactorY);
105 function testScaleNonUniform() {
106 var m = createMatrix(
2,
0,
0,
1,
120,
100);
107 roughCmpMatrix(m.scaleNonUniform(
0.5, -
3), [
1,
0,
0, -
3,
120,
100],
108 "Unexpected result after scaleNonUniform");
111 // SVGMatrix rotate(in float angle);
112 function testRotate() {
113 var m = createMatrix(
2,
0,
0,
1,
120,
100);
114 roughCmpMatrix(m.rotate(
45),
115 [
2 * Math.cos(Math.PI /
4), Math.sin(Math.PI /
4),
116 2 * -Math.sin(Math.PI /
4), Math.cos(Math.PI /
4),
118 "Unexpected result after rotate");
121 // SVGMatrix rotateFromVector(in float x, in float y) raises(SVGException);
122 function testRotateFromVector() {
123 var m = createMatrix(
2,
0,
0,
1,
120,
100);
124 // Make a
150 degree angle
125 var result = m.rotateFromVector(-
2,
1.1547);
126 roughCmpMatrix(result,
127 [
2 * Math.cos(
5 * Math.PI /
6), Math.sin(
5 * Math.PI /
6),
128 2 * -Math.sin(
5 * Math.PI /
6), Math.cos(
5 * Math.PI /
6),
130 "Unexpected result after rotateFromVector");
132 // Test bad input (
1)
134 m.rotateFromVector(
1,
0);
135 ok(false,
"Failed to throw exception with zero coord for rotateFromVector");
137 is(e.name,
"InvalidAccessError",
138 "Got unexpected exception " + e +
", expected TypeError");
141 // Test bad input (
2)
143 m.rotateFromVector(
0,
1);
144 ok(false,
"Failed to throw exception with zero coord for rotateFromVector");
148 // SVGMatrix flipX();
149 function testFlipX() {
150 var m = createMatrix(
1,
2,
3,
4,
5,
6);
151 cmpMatrix(m.flipX(), [-
1, -
2,
3,
4,
5,
6],
"Unexpected result after flipX");
154 // SVGMatrix flipY();
155 function testFlipY() {
156 var m = createMatrix(
1,
2,
3,
4,
5,
6);
157 cmpMatrix(m.flipY(), [
1,
2, -
3, -
4,
5,
6],
"Unexpected result after flipY");
160 // SVGMatrix skewX(in float angle);
161 function testSkewX() {
162 var m = createMatrix(
2,
0,
0,
1,
120,
100);
163 roughCmpMatrix(m.skewX(
30), [
2,
0,
2 * Math.tan(Math.PI /
6),
1,
120,
100],
164 "Unexpected result after skewX");
167 // SVGMatrix skewY(in float angle);
168 function testSkewY() {
169 var m = createMatrix(
2,
0,
0,
1,
120,
100);
170 roughCmpMatrix(m.skewY(
30), [
2, Math.tan(Math.PI /
6),
0,
1,
120,
100],
171 "Unexpected result after skewY");
174 window.addEventListener(
"load", main);