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;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for the various TypedArray's.
25   *
26   * @author Ronald Brill
27   */
28  @RunWith(BrowserRunner.class)
29  public class NativeTypedArrayTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"Float32Array", "false", "true",
36               "undefined", "false", "true",
37               "undefined", "true", "true",
38               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
39      public void toStringFloat32Array() throws Exception {
40          loadPageVerifyTitle2(toStringTagTest("new Float32Array(1)"));
41      }
42  
43      /**
44       * @throws Exception if the test fails
45       */
46      @Test
47      @Alerts({"Float64Array", "false", "true",
48               "undefined", "false", "true",
49               "undefined", "true", "true",
50               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
51      public void toStringFloat64Array() throws Exception {
52          loadPageVerifyTitle2(toStringTagTest("new Float64Array(1)"));
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      @Alerts({"Int8Array", "false", "true",
60               "undefined", "false", "true",
61               "undefined", "true", "true",
62               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
63      public void toStringInt8Array() throws Exception {
64          loadPageVerifyTitle2(toStringTagTest("new Int8Array(1)"));
65      }
66  
67      /**
68       * @throws Exception if the test fails
69       */
70      @Test
71      @Alerts({"Int16Array", "false", "true",
72               "undefined", "false", "true",
73               "undefined", "true", "true",
74               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
75      public void toStringInt16Array() throws Exception {
76          loadPageVerifyTitle2(toStringTagTest("new Int16Array(1)"));
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts({"Int32Array", "false", "true",
84               "undefined", "false", "true",
85               "undefined", "true", "true",
86               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
87      public void toStringInt32Array() throws Exception {
88          loadPageVerifyTitle2(toStringTagTest("new Int32Array(1)"));
89      }
90  
91      /**
92       * @throws Exception if the test fails
93       */
94      @Test
95      @Alerts({"Uint8Array", "false", "true",
96               "undefined", "false", "true",
97               "undefined", "true", "true",
98               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
99      public void toStringUint8Array() throws Exception {
100         loadPageVerifyTitle2(toStringTagTest("new Uint8Array(1)"));
101     }
102 
103     /**
104      * @throws Exception if the test fails
105      */
106     @Test
107     @Alerts({"Uint16Array", "false", "true",
108              "undefined", "false", "true",
109              "undefined", "true", "true",
110              "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
111     public void toStringUint16Array() throws Exception {
112         loadPageVerifyTitle2(toStringTagTest("new Uint16Array(1)"));
113     }
114 
115     /**
116      * @throws Exception if the test fails
117      */
118     @Test
119     @Alerts({"Uint32Array", "false", "true",
120              "undefined", "false", "true",
121              "undefined", "true", "true",
122              "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
123     public void toStringUint32Array() throws Exception {
124         loadPageVerifyTitle2(toStringTagTest("new Uint32Array(1)"));
125     }
126 
127     /**
128      * @throws Exception if the test fails
129      */
130     @Test
131     @Alerts({"Uint8ClampedArray", "false", "true",
132              "undefined", "false", "true",
133              "undefined", "true", "true",
134              "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
135     public void toStringUint8ClampedArray() throws Exception {
136         loadPageVerifyTitle2(toStringTagTest("new Uint8ClampedArray(1)"));
137     }
138 
139     private static String toStringTagTest(final String init) {
140         return
141             DOCTYPE_HTML
142             + "<html></head>\n"
143             + "<body>"
144             + "<script>\n"
145             + LOG_TITLE_FUNCTION
146             + "  function sortFunction(s1, s2) {\n"
147             + "    var s1lc = s1.toLowerCase();\n"
148             + "    var s2lc =  s2.toLowerCase();\n"
149             + "    if (s1lc > s2lc) { return 1; }\n"
150             + "    if (s1lc < s2lc) { return -1; }\n"
151             + "    return s1 > s2 ? 1 : -1;\n"
152             + "  }\n"
153 
154             + "  var typedArray = " + init + ";\n"
155             + "  log((typedArray)[Symbol.toStringTag]);\n"
156             + "  log(typedArray.hasOwnProperty(Symbol.toStringTag));\n"
157             + "  log(Symbol.toStringTag in typedArray);\n"
158 
159             + "  var typedArrayProto = typedArray.__proto__;"
160             + "  log((typedArrayProto)[Symbol.toStringTag]);\n"
161             + "  log(typedArrayProto.hasOwnProperty(Symbol.toStringTag));\n"
162             + "  log(Symbol.toStringTag in typedArrayProto);\n"
163 
164             + "  var typedArrayProtoProto = typedArray.__proto__.__proto__;"
165             + "  log((typedArrayProtoProto)[Symbol.toStringTag]);\n"
166             + "  log(typedArrayProtoProto.hasOwnProperty(Symbol.toStringTag));\n"
167             + "  log(Symbol.toStringTag in typedArrayProtoProto);\n"
168 
169             + "  var syms = Object.getOwnPropertySymbols(typedArrayProtoProto);\n"
170             + "  var symStrs = [];"
171             + "  for (i = 0; i < syms.length; i++) {\n"
172             + "    symStrs.push(syms[i].toString());\n"
173             + "  }\n"
174             + "  symStrs.sort(sortFunction);\n"
175             + "  log(symStrs);"
176             + "</script>\n"
177             + "</body></html>";
178     }
179 }