Bug 1941128 - Turn off network.dns.native_https_query on Mac again
[gecko.git] / dom / svg / test / test_SVGMatrix.xhtml
blob9f5b50b555302018e4685bba386d13c97bf67ff0
1 <html xmlns="http://www.w3.org/1999/xhtml">
2 <head>
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" />
7 </head>
8 <body>
9 <p id="display"></p>
10 <div id="content">
11 <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="1" id="svg">
12 <g id="g" transform="translate(10, 20)"/>
13 </svg>
14 </div>
15 <pre id="test">
16 <script class="testbody" type="text/javascript">
17 <![CDATA[
19 SimpleTest.waitForExplicitFinish();
21 function main() {
22 var tests =
23 [ testCreateMatrix,
24 testMultiply,
25 testInverse,
26 testTranslate,
27 testScale,
28 testScaleNonUniform,
29 testRotate,
30 testRotateFromVector,
31 testFlipX,
32 testFlipY,
33 testSkewX,
34 testSkewY,
36 for (var i = 0; i < tests.length; i++) {
37 tests[i]();
39 SimpleTest.finish();
42 function testCreateMatrix() {
43 var svg = $("svg");
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() {
74 // Test inversion
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);
81 try {
82 m.inverse();
83 ok(false, "Failed to throw exception when inverting singular matrix");
84 } catch (e) {
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),
117 120, 100],
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),
129 120, 100],
130 "Unexpected result after rotateFromVector");
132 // Test bad input (1)
133 try {
134 m.rotateFromVector(1, 0);
135 ok(false, "Failed to throw exception with zero coord for rotateFromVector");
136 } catch (e) {
137 is(e.name, "InvalidAccessError",
138 "Got unexpected exception " + e + ", expected TypeError");
141 // Test bad input (2)
142 try {
143 m.rotateFromVector(0, 1);
144 ok(false, "Failed to throw exception with zero coord for rotateFromVector");
145 } catch (e) { }
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);
177 </script>
178 </pre>
179 </body>
180 </html>