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.arrays;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for DataView.
23   *
24   * @author Ahmed Ashour
25   * @author Frank Danek
26   * @author Ronald Brill
27   */
28  public class DataViewTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      @Alerts({"22", "3.1415927410125732"})
35      public void arrayConstruction() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><head>\n"
38              + "<script>\n"
39              + LOG_TITLE_FUNCTION
40              + "function test() {\n"
41              + "  try {\n"
42              + "    var buffer = new ArrayBuffer(12);\n"
43              + "    var x = new DataView(buffer);\n"
44              + "    x.setInt8(0, 22);\n"
45              + "    x.setFloat32(1, Math.PI);\n"
46              + "    log(x.getInt8(0));\n"
47              + "    log(x.getFloat32(1));\n"
48              + "  } catch(e) {logEx(e);}\n"
49              + "}\n"
50              + "</script></head><body onload='test()'>\n"
51              + "</body></html>";
52  
53          loadPageVerifyTitle2(html);
54      }
55  
56      /**
57       * @throws Exception if the test fails
58       */
59      @Test
60      @Alerts({"1146420001", "570119236", "-8.36147406512941e+35", "2.1426990032196045",
61                  "0", "0", "0", "0", "64", "9",
62                  "33", "-5", "84", "68", "45", "24"})
63      public void endian() throws Exception {
64          final String html = DOCTYPE_HTML
65              + "<html><head>\n"
66              + "<script>\n"
67              + LOG_TITLE_FUNCTION
68              + "function test() {\n"
69              + "  try {\n"
70              + "    var array = new DataView(new ArrayBuffer(12), 4);\n"
71              + "    array.setFloat64(0, Math.PI);\n"
72              + "    log(array.getInt32(2, true));\n"
73              + "    log(array.getInt32(2, false));\n"
74              + "    log(array.getFloat32(0, true));\n"
75              + "    log(array.getFloat32(0, false));\n"
76  
77              + "    var array2 = new Int8Array(array.buffer);\n"
78              + "    for (var i = 0; i < array2.length; i++)\n"
79              + "      log(array2[i]);\n"
80              + "  } catch(e) {logEx(e);}\n"
81              + "}\n"
82              + "</script></head><body onload='test()'>\n"
83              + "</body></html>";
84  
85          loadPageVerifyTitle2(html);
86      }
87  
88      /**
89       * @throws Exception if the test fails
90       */
91      @Test
92      @Alerts({"1234", "0", "4", "-46", "0", "0", "0"})
93      public void uint16() throws Exception {
94          final String html = DOCTYPE_HTML
95              + "<html><head>\n"
96              + "<script>\n"
97              + LOG_TITLE_FUNCTION
98              + "function test() {\n"
99              + "  try {\n"
100             + "    var array = new DataView(new ArrayBuffer(6), 0);\n"
101             + "    array.setUint16(1, 1234);\n"
102             + "    log(array.getUint16(1));\n"
103             + "    var array2 = new Int8Array(array.buffer);\n"
104             + "    for (var i = 0; i < array2.length; i++)\n"
105             + "      log(array2[i]);\n"
106             + "  } catch(e) {logEx(e);}\n"
107             + "}\n"
108             + "</script></head><body onload='test()'>\n"
109             + "</body></html>";
110 
111         loadPageVerifyTitle2(html);
112     }
113 
114     /**
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts("TypeError")
119     public void nullConstructor() throws Exception {
120         final String html = DOCTYPE_HTML
121             + "<html><head>\n"
122             + "<script>\n"
123             + LOG_TITLE_FUNCTION
124             + "function test() {\n"
125             + "  try {\n"
126             + "    new DataView(null);\n"
127             + "  } catch(e) {logEx(e);}\n"
128             + "}\n"
129             + "</script></head><body onload='test()'>\n"
130             + "</body></html>";
131 
132         loadPageVerifyTitle2(html);
133     }
134 
135 }