summaryrefslogtreecommitdiffstats
path: root/dom/svg/test/test_SVGMatrix.xhtml
blob: e6fc057bc9e181a411640b12cf827f60f8e8c87d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test SVGMatrix behavior</title>
  <script type="text/javascript" src="/MochiKit/packed.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="matrixUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content">
  <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="1" id="svg">
    <g id="g" transform="translate(10, 20)"/>
  </svg>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
<![CDATA[

SimpleTest.waitForExplicitFinish();

function main()
{
  var tests =
    [ testCreateMatrix,
      testMultiply,
      testInverse,
      testTranslate,
      testScale,
      testScaleNonUniform,
      testRotate,
      testRotateFromVector,
      testFlipX,
      testFlipY,
      testSkewX,
      testSkewY
    ];
  for (var i = 0; i < tests.length; i++) {
    tests[i]();
  }
  SimpleTest.finish();
}

function testCreateMatrix()
{
  svg = $('svg');
  var m = svg.createSVGMatrix();
  
  // Should be initialised to identity
  cmpMatrix(m, [1, 0, 0, 1, 0, 0],
            "createMatrix should produce identity matrix");

  // Should return a new object each time;
  ok(m != svg.createSVGMatrix(),
     "Got identical objects when creating new matrix");
}

// SVGMatrix multiply(in SVGMatrix secondMatrix);
function testMultiply()
{
  // This is the example from SVG 1.1 section 7.5
  var m1 = createMatrix(1, 0, 0, 1, 50, 90);
  var m2 = createMatrix(0.707, -0.707, 0.707, 0.707, 0, 0);
  var m3 = createMatrix(1, 0, 0, 1, 130, 160);
  var result = m1.multiply(m2).multiply(m3);
  roughCmpMatrix(result, [0.707, -0.707, 0.707, 0.707, 255.03, 111.21],
    "Unexpected result after multiplying matrices");

  // Check orig matrices are unchanged
  cmpMatrix(m1, [1, 0, 0, 1, 50, 90], "Matrix changed after multiplication");
  roughCmpMatrix(m2, [0.707, -0.707, 0.707, 0.707, 0, 0],
                 "Matrix changed after multiplication");
  cmpMatrix(m3, [1, 0, 0, 1, 130, 160], "Matrix changed after multiplication");
}

// SVGMatrix inverse() raises(SVGException);
function testInverse()
{
  // Test inversion
  var m = createMatrix(2, 0, 0, 4, 110, -50);
  roughCmpMatrix(m.inverse(), [0.5, 0, 0, 0.25, -55, 12.5],
    "Unexpected result after inverting matrix");

  // Test non-invertable
  m = createMatrix(0, 0, 1, 0, 0, 0);
  try {
    m.inverse();
    ok(false, "Failed to throw exception when inverting singular matrix");
  } catch (e) {
    is(e.name, "InvalidStateError",
      "Got unexpected exception " + e + ", expected InvalidStateError");
  }
}

// SVGMatrix translate(in float x, in float y);
function testTranslate()
{
  var m = createMatrix(2, 0, 0, 1, 120, 100);
  roughCmpMatrix(m.translate(100, -50), [2, 0, 0, 1, 320, 50],
    "Unexpected result after translate");
}

// SVGMatrix scale(in float scaleFactor);
function testScale()
{
  var m = createMatrix(2, 0, 0, 1, 120, 100);
  roughCmpMatrix(m.scale(0.5), [1, 0, 0, 0.5, 120, 100],
    "Unexpected result after scale");
}

// SVGMatrix scaleNonUniform(in float scaleFactorX, in float scaleFactorY);
function testScaleNonUniform()
{
  var m = createMatrix(2, 0, 0, 1, 120, 100);
  roughCmpMatrix(m.scaleNonUniform(0.5, -3), [1, 0, 0, -3, 120, 100],
    "Unexpected result after scaleNonUniform");
}

// SVGMatrix rotate(in float angle);
function testRotate()
{
  var m = createMatrix(2, 0, 0, 1, 120, 100);
  roughCmpMatrix(m.rotate(45),
                 [2*Math.cos(Math.PI/4), Math.sin(Math.PI/4),
                  2*-Math.sin(Math.PI/4), Math.cos(Math.PI/4),
                  120, 100],
                 "Unexpected result after rotate");
}

// SVGMatrix rotateFromVector(in float x, in float y) raises(SVGException);
function testRotateFromVector()
{
  var m = createMatrix(2, 0, 0, 1, 120, 100);
  // Make a 150 degree angle
  var result = m.rotateFromVector(-2, 1.1547);
  roughCmpMatrix(result,
                 [2*Math.cos(5*Math.PI/6), Math.sin(5*Math.PI/6),
                  2*-Math.sin(5*Math.PI/6), Math.cos(5*Math.PI/6),
                  120, 100],
                 "Unexpected result after rotateFromVector");

  // Test bad input (1)
  try {
    m.rotateFromVector(1, 0);
    ok(false, "Failed to throw exception with zero coord for rotateFromVector");
  } catch (e) {
    is(e.name, "InvalidAccessError",
      "Got unexpected exception " + e + ", expected TypeError");
  }

  // Test bad input (2)
  try {
    m.rotateFromVector(0, 1);
    ok(false, "Failed to throw exception with zero coord for rotateFromVector");
  } catch (e) { }
}

// SVGMatrix flipX();
function testFlipX()
{
  var m = createMatrix(1, 2, 3, 4, 5, 6);
  cmpMatrix(m.flipX(), [-1, -2, 3, 4, 5, 6], "Unexpected result after flipX");
}

// SVGMatrix flipY();
function testFlipY()
{
  var m = createMatrix(1, 2, 3, 4, 5, 6);
  cmpMatrix(m.flipY(), [1, 2, -3, -4, 5, 6], "Unexpected result after flipY");
}

// SVGMatrix skewX(in float angle);
function testSkewX()
{
  var m = createMatrix(2, 0, 0, 1, 120, 100);
  roughCmpMatrix(m.skewX(30), [2, 0, 2*Math.tan(Math.PI/6), 1, 120, 100],
                 "Unexpected result after skewX");
}

// SVGMatrix skewY(in float angle);
function testSkewY()
{
  var m = createMatrix(2, 0, 0, 1, 120, 100);
  roughCmpMatrix(m.skewY(30), [2, Math.tan(Math.PI/6), 0, 1, 120, 100],
                 "Unexpected result after skewY");
}

window.addEventListener("load", main, false);

]]>
</script>
</pre>
</body>
</html>