View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.dom;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link DOMMatrix}.
24   *
25   * @author Ronald Brill
26   */
27  public class DOMMatrixTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception on test failure
31       */
32      @Test
33      @Alerts({"true", "function DOMMatrix() { [native code] }", "function DOMMatrix() { [native code] }"})
34      public void webKitCSSMatrixIsAlias() throws Exception {
35          final String html = DOCTYPE_HTML
36                  + "<html><head>\n"
37                  + "<script>\n"
38                  + LOG_TITLE_FUNCTION
39                  + "function doTest() {\n"
40                  + "  log(WebKitCSSMatrix === DOMMatrix);\n"
41                  + "  log(WebKitCSSMatrix);\n"
42                  + "  log(DOMMatrix);\n"
43                  + "}\n"
44                  + "</script>\n"
45                  + "</head>\n"
46                  + "<body onload='doTest()'>\n"
47                  + "</body></html>";
48  
49          loadPageVerifyTitle2(html);
50      }
51  
52      /**
53       * @throws Exception on test failure
54       */
55      @Test
56      @Alerts({"false", "function SVGMatrix() { [native code] }", "function DOMMatrix() { [native code] }"})
57      public void svgMatrixIsNotAlias() throws Exception {
58          final String html = DOCTYPE_HTML
59                  + "<html><head>\n"
60                  + "<script>\n"
61                  + LOG_TITLE_FUNCTION
62                  + "function doTest() {\n"
63                  + "  log(SVGMatrix === DOMMatrix);\n"
64                  + "  log(SVGMatrix);\n"
65                  + "  log(DOMMatrix);\n"
66                  + "}\n"
67                  + "</script>\n"
68                  + "</head>\n"
69                  + "<body onload='doTest()'>\n"
70                  + "</body></html>";
71  
72          loadPageVerifyTitle2(html);
73      }
74  
75      /**
76       * @throws Exception on test failure
77       */
78      @Test
79      @Alerts({"[1, 0, 0, 1, 0, 0]",
80               "1[1, 0, 0, 0]",
81               "2[0, 1, 0, 0]",
82               "3[0, 0, 1, 0]",
83               "4[0, 0, 0, 1]",
84               "true"})
85      public void contructor() throws Exception {
86          final String html = DOCTYPE_HTML
87                  + "<html>\n"
88                  + "<body>\n"
89                  + "<script>\n"
90                  + LOG_TITLE_FUNCTION
91                  + DOMMatrixReadOnlyTest.DUMP_FUNCTION
92                  + "let m = new DOMMatrix();\n"
93                  + "dump(m);\n"
94                  + "</script>\n"
95                  + "</body></html>";
96  
97          loadPageVerifyTitle2(html);
98      }
99  
100     /**
101      * @throws Exception on test failure
102      */
103     @Test
104     @Alerts({"[6, 5, 4, 3, 2, 1]",
105              "1[6, 5, 0, 0]",
106              "2[4, 3, 0, 0]",
107              "3[0, 0, 1, 0]",
108              "4[2, 1, 0, 1]",
109              "true"})
110     public void contructor6Numbers() throws Exception {
111         final String html = DOCTYPE_HTML
112                 + "<html>\n"
113                 + "<body>\n"
114                 + "<script>\n"
115                 + LOG_TITLE_FUNCTION
116                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
117                 + "let m = new DOMMatrix([6, 5, 4, 3, 2, 1]);\n"
118                 + "dump(m);\n"
119                 + "</script>\n"
120                 + "</body></html>";
121 
122         loadPageVerifyTitle2(html);
123     }
124 
125     /**
126      * @throws Exception on test failure
127      */
128     @Test
129     @Alerts({"[6, 1, 0, NaN, 2, NaN]",
130              "true"})
131     @HtmlUnitNYI(CHROME = {"[6, 1, 0, 0, 2, NaN]", "true"},
132             EDGE = {"[6, 1, 0, 0, 2, NaN]", "true"},
133             FF = {"[6, 1, 0, 0, 2, NaN]", "true"},
134             FF_ESR = {"[6, 1, 0, 0, 2, NaN]", "true"})
135     public void contructor6Mixed() throws Exception {
136         final String html = DOCTYPE_HTML
137                 + "<html>\n"
138                 + "<body>\n"
139                 + "<script>\n"
140                 + LOG_TITLE_FUNCTION
141                 + DOMMatrixReadOnlyTest.DUMP_2D_FUNCTION
142                 + "let m = new DOMMatrix([6, true, null, undefined, '2', 'eins']);\n"
143                 + "dump(m);\n"
144                 + "</script>\n"
145                 + "</body></html>";
146 
147         loadPageVerifyTitle2(html);
148     }
149 
150     /**
151      * @throws Exception on test failure
152      */
153     @Test
154     @Alerts("TypeError")
155     public void contructor5Numbers() throws Exception {
156         final String html = DOCTYPE_HTML
157                 + "<html>\n"
158                 + "<body>\n"
159                 + "<script>\n"
160                 + LOG_TITLE_FUNCTION
161                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
162                 + "try {"
163                 + "  let m = new DOMMatrix([5, 4, 3, 2, 1]);\n"
164                 + "  dump(m);\n"
165                 + "} catch(e) { logEx(e); }"
166                 + "</script>\n"
167                 + "</body></html>";
168 
169         loadPageVerifyTitle2(html);
170     }
171 
172     /**
173      * @throws Exception on test failure
174      */
175     @Test
176     @Alerts("TypeError")
177     public void contructor7Numbers() throws Exception {
178         final String html = DOCTYPE_HTML
179                 + "<html>\n"
180                 + "<body>\n"
181                 + "<script>\n"
182                 + LOG_TITLE_FUNCTION
183                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
184                 + "try {"
185                 + "  let m = new DOMMatrix([7, 6, 5, 4, 3, 2, 1]);\n"
186                 + "  dump(m);\n"
187                 + "} catch(e) { logEx(e); }"
188                 + "</script>\n"
189                 + "</body></html>";
190 
191         loadPageVerifyTitle2(html);
192     }
193 
194     /**
195      * @throws Exception on test failure
196      */
197     @Test
198     @Alerts({"[16, 15, 12, 11, 4, 3]",
199              "1[16, 15, 14, 13]",
200              "2[12, 11, 10, 9]",
201              "3[8, 7, 6, 5]",
202              "4[4, 3, 2, 1]",
203              "false"})
204     public void contructor16Numbers() throws Exception {
205         final String html = DOCTYPE_HTML
206                 + "<html>\n"
207                 + "<body>\n"
208                 + "<script>\n"
209                 + LOG_TITLE_FUNCTION
210                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
211                 + "let m = new DOMMatrix([16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);\n"
212                 + "dump(m);\n"
213                 + "</script>\n"
214                 + "</body></html>";
215 
216         loadPageVerifyTitle2(html);
217     }
218 
219     /**
220      * @throws Exception on test failure
221      */
222     @Test
223     @Alerts("TypeError")
224     public void contructor15Numbers() throws Exception {
225         final String html = DOCTYPE_HTML
226                 + "<html>\n"
227                 + "<body>\n"
228                 + "<script>\n"
229                 + LOG_TITLE_FUNCTION
230                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
231                 + "try {"
232                 + "  let m = new DOMMatrix([15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);\n"
233                 + "  dump(m);\n"
234                 + "} catch(e) { logEx(e); }"
235                 + "</script>\n"
236                 + "</body></html>";
237 
238         loadPageVerifyTitle2(html);
239     }
240 
241     /**
242      * @throws Exception on test failure
243      */
244     @Test
245     @Alerts("TypeError")
246     public void contructor17Numbers() throws Exception {
247         final String html = DOCTYPE_HTML
248                 + "<html>\n"
249                 + "<body>\n"
250                 + "<script>\n"
251                 + LOG_TITLE_FUNCTION
252                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
253                 + "try {"
254                 + "  let m = new DOMMatrix([17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);\n"
255                 + "  dump(m);\n"
256                 + "} catch(e) { logEx(e); }"
257                 + "</script>\n"
258                 + "</body></html>";
259 
260         loadPageVerifyTitle2(html);
261     }
262 
263     /**
264      * @throws Exception on test failure
265      */
266     @Test
267     @Alerts("SyntaxError/DOMException")
268     public void contructorSingleNumber() throws Exception {
269         final String html = DOCTYPE_HTML
270                 + "<html>\n"
271                 + "<body>\n"
272                 + "<script>\n"
273                 + LOG_TITLE_FUNCTION
274                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
275                 + "try {"
276                 + "  let m = new DOMMatrix(7);\n"
277                 + "  dump(m);\n"
278                 + "} catch(e) { logEx(e); }"
279                 + "</script>\n"
280                 + "</body></html>";
281 
282         loadPageVerifyTitle2(html);
283     }
284 
285     /**
286      * @throws Exception on test failure
287      */
288     @Test
289     @Alerts("SyntaxError/DOMException")
290     public void contructorNull() throws Exception {
291         final String html = DOCTYPE_HTML
292                 + "<html>\n"
293                 + "<body>\n"
294                 + "<script>\n"
295                 + LOG_TITLE_FUNCTION
296                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
297                 + "try {"
298                 + "  let m = new DOMMatrix(null);\n"
299                 + "  dump(m);\n"
300                 + "} catch(e) { logEx(e); }"
301                 + "</script>\n"
302                 + "</body></html>";
303 
304         loadPageVerifyTitle2(html);
305     }
306 
307     /**
308      * @throws Exception on test failure
309      */
310     @Test
311     @Alerts({"[1, 0, 0, 1, 0, 0]",
312              "1[1, 0, 0, 0]",
313              "2[0, 1, 0, 0]",
314              "3[0, 0, 1, 0]",
315              "4[0, 0, 0, 1]",
316              "true"})
317     public void contructorUndefined() throws Exception {
318         final String html = DOCTYPE_HTML
319                 + "<html>\n"
320                 + "<body>\n"
321                 + "<script>\n"
322                 + LOG_TITLE_FUNCTION
323                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
324                 + "try {"
325                 + "  let m = new DOMMatrix(undefined);\n"
326                 + "  dump(m);\n"
327                 + "} catch(e) { logEx(e); }"
328                 + "</script>\n"
329                 + "</body></html>";
330 
331         loadPageVerifyTitle2(html);
332     }
333 
334     /**
335      * @throws Exception on test failure
336      */
337     @Test
338     @Alerts("TypeError")
339     public void contructorEmptyArray() throws Exception {
340         final String html = DOCTYPE_HTML
341                 + "<html>\n"
342                 + "<body>\n"
343                 + "<script>\n"
344                 + LOG_TITLE_FUNCTION
345                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
346                 + "try {"
347                 + "  let m = new DOMMatrix([]);\n"
348                 + "  dump(m);\n"
349                 + "} catch(e) { logEx(e); }"
350                 + "</script>\n"
351                 + "</body></html>";
352 
353         loadPageVerifyTitle2(html);
354     }
355 
356     /**
357      * @throws Exception on test failure
358      */
359     @Test
360     @Alerts({"[1, 0, 0, 1, 0, 0]", "true"})
361     public void inverse_identity2D() throws Exception {
362         final String html = DOCTYPE_HTML
363                 + "<html><body><script>"
364                 + LOG_TITLE_FUNCTION
365                 + DOMMatrixReadOnlyTest.DUMP_2D_FUNCTION
366                 + "let m = new DOMMatrix();"
367                 + "m.invertSelf();"
368                 + "dump(m);"
369                 + "</script></body></html>";
370         loadPageVerifyTitle2(html);
371     }
372 
373     /**
374      * @throws Exception on test failure
375      */
376     @Test
377     @Alerts({"true",
378              "[0.25, 0, 0, 0.2, -2.5, -2.6]",
379              "1[0.25, 0, 0, 0]",
380              "2[0, 0.2, 0, 0]",
381              "3[0, 0, 1, 0]",
382              "4[-2.5, -2.6, 0, 1]",
383              "true"})
384     public void inverse_general2D() throws Exception {
385         final String html = DOCTYPE_HTML
386                 + "<html><body><script>"
387                 + LOG_TITLE_FUNCTION
388                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
389                 + "let m = new DOMMatrix([4, 0, 0, 5, 10, 13]);"
390                 + "m.invertSelf();"
391                 + "log(m instanceof DOMMatrix);\n"
392                 + "dump(m);"
393                 + "</script></body></html>";
394         loadPageVerifyTitle2(html);
395     }
396 
397     /**
398      * @throws Exception on test failure
399      */
400     @Test
401     @Alerts({"[NaN, NaN, NaN, NaN, NaN, NaN]", "false"})
402     public void inverse_singular2D() throws Exception {
403         final String html = DOCTYPE_HTML
404                 + "<html><body><script>"
405                 + LOG_TITLE_FUNCTION
406                 + DOMMatrixReadOnlyTest.DUMP_2D_FUNCTION
407                 + "try {"
408                 + "  let m = new DOMMatrix([0, 0, 0, 0, 0, 0]);"
409                 + "  m.invertSelf();"
410                 + "  dump(m);"
411                 + "} catch(e) { logEx(e); }"
412                 + "</script></body></html>";
413         loadPageVerifyTitle2(html);
414     }
415 
416     /**
417      * @throws Exception on test failure
418      */
419     @Test
420     @Alerts({"[1, 0, 0, 1, 0, 0]",
421              "1[1, 0, 0, 0]",
422              "2[0, 1, 0, 0]",
423              "3[0, 0, 1, 0]",
424              "4[0, 0, 0, 1]",
425              "false"})
426     public void inverse_identity3D() throws Exception {
427         final String html = DOCTYPE_HTML
428                 + "<html><body><script>"
429                 + LOG_TITLE_FUNCTION
430                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
431                 + "let m = new DOMMatrix([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]);"
432                 + "m.invertSelf();"
433                 + "dump(m);"
434                 + "</script></body></html>";
435         loadPageVerifyTitle2(html);
436     }
437 
438     /**
439      * @throws Exception on test failure
440      */
441     @Test
442     @Alerts({"[NaN, NaN, NaN, NaN, NaN, NaN]",
443              "1[NaN, NaN, NaN, NaN]",
444              "2[NaN, NaN, NaN, NaN]",
445              "3[NaN, NaN, NaN, NaN]",
446              "4[NaN, NaN, NaN, NaN]",
447              "false"})
448     public void inverse_singular3D() throws Exception {
449         final String html = DOCTYPE_HTML
450                 + "<html><body><script>"
451                 + LOG_TITLE_FUNCTION
452                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
453                 + "try {"
454                 + "  let m = new DOMMatrix([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);"
455                 + "  m.invertSelf();"
456                 + "  dump(m);"
457                 + "} catch(e) { logEx(e); }"
458                 + "</script></body></html>";
459         loadPageVerifyTitle2(html);
460     }
461 
462     /**
463      * @throws Exception on test failure
464      */
465     @Test
466     @Alerts({"true",
467              "[-0.066, 0.115, 0.295, -0.016, 0, 0]",
468              "1[-0.066, 0.115, 0.115, -0.459]",
469              "2[0.295, -0.016, -0.516, 0.066]",
470              "3[0.148, -0.008, -0.008, 0.033]",
471              "4[0, 0, 0, 1]",
472              "false"})
473     public void inverse_general3D() throws Exception {
474         final String html = DOCTYPE_HTML
475                 + "<html><body><script>"
476                 + LOG_TITLE_FUNCTION
477                 + DOMMatrixReadOnlyTest.DUMP_FUNCTION
478                 + "let m = new DOMMatrix([0.5,0,7,0,9,2,0,4,0,-2,4,0,0,0,0,1]);"
479                 + "m.invertSelf();"
480                 + "log(m instanceof DOMMatrix);\n"
481                 + "dump(m);"
482                 + "</script></body></html>";
483         loadPageVerifyTitle2(html);
484     }
485 }