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