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.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for NativeArrayBuffer.
23   *
24   * @author Ronald Brill
25   */
26  public class NativeArrayBufferTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts({"true", "8", "2", "4",
33               "4", "2", "undefined",
34               "8", "2", "0"})
35      public void transfer() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><head></head>\n"
38              + "<body>\n"
39              + "<script>\n"
40              + LOG_TITLE_FUNCTION
41  
42              // Create an ArrayBuffer and write a few bytes\
43              + "let buffer = new ArrayBuffer(8);\n"
44              + "let view = new Uint8Array(buffer);\n"
45              + "view[1] = 2;\n"
46              + "view[7] = 4;\n"
47  
48              // Copy the buffer to the same size
49              + "let buffer2 = buffer.transfer();\n"
50              + "log(buffer.detached);\n"
51              + "log(buffer2.byteLength);\n"
52              + "let view2 = new Uint8Array(buffer2);\n"
53              + "log(view2[1]);\n"
54              + "log(view2[7]);\n"
55  
56              // Copy the buffer to a smaller size
57              + "let buffer3 = buffer2.transfer(4);\n"
58              + "log(buffer3.byteLength);\n"
59              + "let view3 = new Uint8Array(buffer3);\n"
60              + "log(view3[1]);\n"
61              + "log(view3[7]);\n"
62  
63              // Copy the buffer to a larger size
64              + "let buffer4 = buffer3.transfer(8);\n"
65              + "log(buffer4.byteLength);\n"
66              + "let view4 = new Uint8Array(buffer4);\n"
67              + "log(view4[1]);\n"
68              + "log(view4[7]);\n"
69              + "</script>\n"
70              + "</body></html>";
71  
72          loadPageVerifyTitle2(html);
73      }
74  
75      /**
76       * @throws Exception if the test fails
77       */
78      @Test
79      @Alerts({"8", "2", "4"})
80      public void transferToFixedLength() throws Exception {
81          final String html = DOCTYPE_HTML
82              + "<html><head></head>\n"
83              + "<body>\n"
84              + "<script>\n"
85              + LOG_TITLE_FUNCTION
86  
87              + "let buffer = new ArrayBuffer(8, { maxByteLength: 16 });\n"
88              + "let view = new Uint8Array(buffer);\n"
89              + "view[1] = 2;\n"
90              + "view[7] = 4;\n"
91  
92              + "let buffer2 = buffer.transferToFixedLength();\n"
93              + "log(buffer2.byteLength);\n"
94  
95              + "let view2 = new Uint8Array(buffer2);\n"
96              + "log(view2[1]);\n"
97              + "log(view2[7]);\n"
98              + "</script>\n"
99              + "</body></html>";
100 
101         loadPageVerifyTitle2(html);
102     }
103 }