summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/test_WebKitCSSMatrix.html
blob: 690a7d2c633abd2a71cb57e9b9284ca893637cd5 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE html>
<meta charset=utf-8>
<title>Test for WebKitCSSMatrix</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
function RoughCompareMatrix(dm1, dm2)
{
  var m1 = dm1.toFloat32Array();
  var m2 = dm2.toFloat32Array();

  if (m1.length != m2.length) {
    return false;
  }

  const tolerance = 1 / 65535;
  for (var x = 0; x < m1.length; x++) {
    if (Math.abs(m1[x] - m2[x]) > tolerance) {
      return false;
    }
  }

  return true;
}

function CompareMatrix(dm1, dm2)
{
  var m1 = dm1.toFloat32Array();
  var m2 = dm2.toFloat32Array();

  if (m1.length != m2.length) {
    return false;
  }

  for (var x = 0; x < m1.length; x++) {
    if (m1[x] != m2[x] && !(Number.isNaN(m1[x]) && Number.isNaN(m2[x]))) {
      return false;
    }
  }

  return true;
}

test(function() {
  var m = new WebKitCSSMatrix();

  assert_equals(m.m11, 1, "m11 should be 1");
  assert_equals(m.m22, 1, "m22 should be 1");
  assert_equals(m.m33, 1, "m33 should be 1");
  assert_equals(m.m44, 1, "m44 should be 1");
  assert_equals(m.m12, 0, "m12 should be 0");
  assert_equals(m.m13, 0, "m13 should be 0");
  assert_equals(m.m14, 0, "m14 should be 0");
  assert_equals(m.m21, 0, "m21 should be 0");
  assert_equals(m.m23, 0, "m23 should be 0");
  assert_equals(m.m24, 0, "m24 should be 0");
  assert_equals(m.m31, 0, "m31 should be 0");
  assert_equals(m.m32, 0, "m32 should be 0");
  assert_equals(m.m34, 0, "m34 should be 0");
  assert_equals(m.m41, 0, "m41 should be 0");
  assert_equals(m.m42, 0, "m42 should be 0");
  assert_equals(m.m43, 0, "m43 should be 0");
}, "Test constructor with no arguments.");

test(function() {
  var m = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  assert_equals(m.m11, 1, "m11 should be 1");
  assert_equals(m.m12, 2, "m12 should be 2");
  assert_equals(m.m21, 3, "m21 should be 3");
  assert_equals(m.m22, 4, "m22 should be 4");
  assert_equals(m.m41, 5, "m41 should be 5");
  assert_equals(m.m42, 6, "m42 should be 6");
}, "Test constructor with transform list.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new WebKitCSSMatrix(m1);

  assert_true(RoughCompareMatrix(m1, m2), "Matrix should be equal.");
}, "Test constructor with other matrix.");

test(function() {
  var m = new WebKitCSSMatrix();
  var mr = m.setMatrixValue("matrix(1,2,3,4,5,6)");

  assert_equals(m.m11, 1, "m11 should be 1");
  assert_equals(m.m12, 2, "m12 should be 2");
  assert_equals(m.m21, 3, "m21 should be 3");
  assert_equals(m.m22, 4, "m22 should be 4");
  assert_equals(m.m41, 5, "m41 should be 5");
  assert_equals(m.m42, 6, "m42 should be 6");

  assert_equals(m, mr, "Return value of setMatrixValue should be the same matrix.");
}, "Test setMatrixValue.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(6,5,4,3,2,1)");
  var m4 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.multiply(m3);
  var m2r = m2.multiply(m3);

  assert_true(RoughCompareMatrix(m1r, m2r), "multiply should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m4), "Multiply should not mutate original matrix.");
}, "Test multiply.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.inverse();
  var m2r = m2.inverse();

  assert_true(RoughCompareMatrix(m1r, m2r), "inverse should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "inverse should not mutate original matrix.");
}, "Test inverse.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.translate(2, 3, 4);
  var m2r = m2.translate(2, 3, 4);

  assert_true(RoughCompareMatrix(m1r, m2r), "translate should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "translate should not mutate original matrix.");
}, "Test translate.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.scale(2);
  var m2r = m2.scale(2, 2, 1);

  assert_true(RoughCompareMatrix(m1r, m2r), "scale should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "scale should not mutate original matrix.");
}, "Test scale with 1 argument.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.scale(2, 3);
  var m2r = m2.scale(2, 3, 1);

  assert_true(RoughCompareMatrix(m1r, m2r), "scale should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "scale should not mutate original matrix.");
}, "Test scale with 2 arguments.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.scale(2, 3, 4);
  var m2r = m2.scale(2, 3, 4);

  assert_true(RoughCompareMatrix(m1r, m2r), "scale should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "scale should not mutate original matrix.");
}, "Test scale with 3 arguments.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.scale(undefined, 3, 4);
  var m2r = m2.scale(1, 3, 4);

  assert_true(RoughCompareMatrix(m1r, m2r), "scale should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "scale should not mutate original matrix.");
}, "Test scale with undefined scaleX argument.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.scale(2, undefined, 4);
  var m2r = m2.scale(2, 2, 4);

  assert_true(RoughCompareMatrix(m1r, m2r), "scale should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "scale should not mutate original matrix.");
}, "Test scale with undefined scaleY argument.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.scale(2, 3, undefined);
  var m2r = m2.scale(2, 3, 1);

  assert_true(RoughCompareMatrix(m1r, m2r), "scale should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "scale should not mutate original matrix.");
}, "Test scale with undefined scaleZ argument.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.rotate(2);
  var m2r = m2.rotateAxisAngle(0, 0, 1, 2); // Rotate around unit vector on z-axis.

  assert_true(RoughCompareMatrix(m1r, m2r));
  assert_true(CompareMatrix(m1, m3), "rotate should not mutate original matrix.");
}, "Test rotate with 1 argument.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.rotate(2, 3);
  var m2r = m2.rotateAxisAngle(0, 1, 0, 3); // Rotate around unit vector on x-axis.
  m2r = m2r.rotateAxisAngle(1, 0, 0, 2); // Rotate around unit vector on y-axis.

  assert_true(RoughCompareMatrix(m1r, m2r));
  assert_true(CompareMatrix(m1, m3), "rotate should not mutate original matrix.");
}, "Test rotate with 2 arguments.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.rotate(2, 3, 4);
  var m2r = m2.rotateAxisAngle(0, 0, 1, 4); // Rotate around unit vector on z-axis.
  m2r = m2r.rotateAxisAngle(0, 1, 0, 3); // Rotate around unit vector on y-axis.
  m2r = m2r.rotateAxisAngle(1, 0, 0, 2); // Rotate around unit vector on x-axis.

  assert_true(RoughCompareMatrix(m1r, m2r));
  assert_true(CompareMatrix(m1, m3), "rotate should not mutate original matrix.");
}, "Test rotate with 3 arguments.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.rotate(2, undefined, undefined);
  var m2r = m2.rotateAxisAngle(0, 0, 1, 2); // Rotate around unit vector on z-axis.

  assert_true(RoughCompareMatrix(m1r, m2r));
  assert_true(CompareMatrix(m1, m3), "rotate should not mutate original matrix.");
}, "Test rotate with rotY and rotZ as undefined.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.rotate(undefined, 3, 4);
  var m2r = m2.rotateAxisAngle(0, 0, 1, 4); // Rotate around unit vector on z-axis.
  m2r = m2r.rotateAxisAngle(0, 1, 0, 3); // Rotate around unit vector on y-axis.

  assert_true(RoughCompareMatrix(m1r, m2r));
  assert_true(CompareMatrix(m1, m3), "rotate should not mutate original matrix.");
}, "Test rotate with rotX as undefined.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.rotateAxisAngle(2, 3, 4, 5);
  var m2r = m2.rotateAxisAngle(2, 3, 4, 5);

  assert_true(RoughCompareMatrix(m1r, m2r), "rotateAxisAngle should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "rotateAxisAngle should not mutate original matrix.");
}, "Test rotateAxisAngle.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.skewX(2);
  var m2r = m2.skewX(2);

  assert_true(RoughCompareMatrix(m1r, m2r), "skewX should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "skewX should not mutate original matrix.");
}, "Test skewX.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");
  var m2 = new DOMMatrix("matrix(1,2,3,4,5,6)");
  var m3 = new WebKitCSSMatrix("matrix(1,2,3,4,5,6)");

  var m1r = m1.skewY(2);
  var m2r = m2.skewY(2);

  assert_true(RoughCompareMatrix(m1r, m2r), "skewY should return the same result as DOMMatrixReadOnly.");
  assert_true(CompareMatrix(m1, m3), "skewY should not mutate original matrix.");
}, "Test skewY.");

test(function() {
  var m1 = new WebKitCSSMatrix("matrix(1,0,0,0,0,0)");
  m1 = m1.inverse();

  var m2 = new DOMMatrix(new Array(16).fill(NaN));
  assert_true(CompareMatrix(m1, m2), "Inverting a non-invertible matrix should set all attributes to NaN.")
}, "Test that inverting an invertible matrix throws.");

test(function() {
  var m1 = new WebKitCSSMatrix("translate(10px, 10px)");
  var m2 = new DOMMatrix();
  m2.translateSelf(10, 10);
  assert_true(RoughCompareMatrix(m1, m2), "translate in constructor should result in translated matrix");

  assert_throws("SyntaxError", function() { new WebKitCSSMatrix("translate(10em, 10em)"); }, "Transform function may not contain relative units.")
  assert_throws("SyntaxError", function() { new WebKitCSSMatrix("translate(10%, 10%)"); }, "Transform function may not contain percentage.")
}, "Test constructor with translate");

test(function() {
  assert_throws("SyntaxError", function() { new WebKitCSSMatrix("initial"); }, "initial is not a valid constructor argument.")
  assert_throws("SyntaxError", function() { new WebKitCSSMatrix("inherit"); }, "inherit is not a valid constructor arugment.")
}, "Test invalid constructor arguments.");

test(function() {
  var m1 = new WebKitCSSMatrix();
  m1 = m1.rotateAxisAngle(0, 0, 1, 45);

  var m2 = new WebKitCSSMatrix();
  m2 = m2.rotateAxisAngle(0, 0, 3, 45);

  assert_true(RoughCompareMatrix(m1, m2), "rotateAxisAngle should normalize vector to unit vector.");
}, "Test normalization of vector for rotateAxisAngle");
</script>