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.media;
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 OfflineAudioContext}.
24   *
25   * @author Ronald Brill
26   */
27  public class OfflineAudioContextTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts("true")
34      public void inWindow() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html>\n"
37              + "<head>\n"
38              + "  <script>\n"
39              + LOG_TITLE_FUNCTION
40              + "    function test() {\n"
41              + "      log('OfflineAudioContext' in window);\n"
42              + "    }\n"
43              + "  </script>\n"
44              + "</head>\n"
45              + "<body onload='test()'>\n"
46              + "</body>\n"
47              + "</html>";
48  
49          loadPageVerifyTitle2(html);
50      }
51  
52      /**
53       * @throws Exception if the test fails
54       */
55      @Test
56      @Alerts({"function", "TypeError", "[object OfflineAudioContext]"})
57      public void ctor() throws Exception {
58          final String html = DOCTYPE_HTML
59              + "<html>\n"
60              + "<head>\n"
61              + "  <script>\n"
62              + LOG_TEXTAREA_FUNCTION
63  
64              + "    function test() {\n"
65              + "      if (!('OfflineAudioContext' in window)) {\n"
66              + "        log('OfflineAudioContext not available');\n"
67              + "        return;\n"
68              + "      }\n"
69  
70              + "      log(typeof OfflineAudioContext);\n"
71              + "      try {\n"
72              + "        log(new OfflineAudioContext());\n"
73              + "      } catch(e) { logEx(e); }\n"
74              + "      log(new OfflineAudioContext({length: 44100 * 1, sampleRate: 44100}));\n"
75              + "    }\n"
76              + "  </script>\n"
77              + "</head>\n"
78              + "<body onload='test()'>\n"
79              + LOG_TEXTAREA
80              + "</body>\n"
81              + "</html>";
82  
83          loadPageVerifyTextArea2(html);
84      }
85  
86      /**
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts("[object AudioBufferSourceNode]")
91      public void createBufferSource() throws Exception {
92          final String html = DOCTYPE_HTML
93              + "<html>\n"
94              + "<head>\n"
95              + "  <script>\n"
96              + LOG_TITLE_FUNCTION
97              + "    function test() {\n"
98              + "      if (!('OfflineAudioContext' in window)) {\n"
99              + "        log('OfflineAudioContext not available');\n"
100             + "        return;\n"
101             + "      }\n"
102 
103             + "      var audioCtx = new OfflineAudioContext({length: 44100 * 1, sampleRate: 44100});\n"
104             + "      var source = audioCtx.createBufferSource();\n"
105             + "      log(source);\n"
106             + "    }\n"
107             + "  </script>\n"
108             + "</head>\n"
109             + "<body onload='test()'>\n"
110             + "</body>\n"
111             + "</html>";
112 
113         loadPageVerifyTitle2(html);
114     }
115 
116     /**
117      * @throws Exception if the test fails
118      */
119     @Test
120     @Alerts({"OfflineAudioContext prep done", "Error with decoding audio data", "EncodingError/DOMException"})
121     @HtmlUnitNYI(CHROME = {"OfflineAudioContext prep done", "Error with decoding audio data",
122                            "NotSupportedError/DOMException"},
123             EDGE = {"OfflineAudioContext prep done", "Error with decoding audio data",
124                     "NotSupportedError/DOMException"},
125             FF = {"OfflineAudioContext prep done", "Error with decoding audio data",
126                   "NotSupportedError/DOMException"},
127             FF_ESR = {"OfflineAudioContext prep done", "Error with decoding audio data",
128                       "NotSupportedError/DOMException"})
129     public void decodeAudioData() throws Exception {
130         final String html = DOCTYPE_HTML
131             + "<html>\n"
132             + "<head>\n"
133             + "  <script>\n"
134             + LOG_TEXTAREA_FUNCTION
135 
136             + "    function test() {\n"
137             + "      if (!('OfflineAudioContext' in window)) {\n"
138             + "        log('OfflineAudioContext not available');\n"
139             + "        return;\n"
140             + "      }\n"
141 
142             + "      var audioCtx = new OfflineAudioContext({length: 44100 * 1, sampleRate: 44100});\n"
143             + "      var audioData = new ArrayBuffer(0);\n"
144             + "      audioCtx.decodeAudioData(audioData,\n"
145             + "             function(buffer) { log('Decoding audio data done'); },\n"
146             + "             function(e) { log('Error with decoding audio data'); logEx(e); }\n"
147             + "           );\n"
148             + "      log('OfflineAudioContext prep done');\n"
149             + "    }\n"
150             + "  </script>\n"
151             + "</head>\n"
152             + "<body onload='test()'>\n"
153             + LOG_TEXTAREA
154             + "</body>\n"
155             + "</html>";
156 
157         loadPageVerifyTextArea2(html);
158     }
159 
160     /**
161      * @throws Exception if the test fails
162      */
163     @Test
164     @Alerts({"OfflineAudioContext prep done", "Error with decoding audio data", "EncodingError/DOMException"})
165     @HtmlUnitNYI(CHROME = {"OfflineAudioContext prep done", "Error with decoding audio data",
166                            "NotSupportedError/DOMException"},
167             EDGE = {"OfflineAudioContext prep done", "Error with decoding audio data",
168                     "NotSupportedError/DOMException"},
169             FF = {"OfflineAudioContext prep done", "Error with decoding audio data",
170                   "NotSupportedError/DOMException"},
171             FF_ESR = {"OfflineAudioContext prep done", "Error with decoding audio data",
172                       "NotSupportedError/DOMException"})
173     public void decodeAudioData2() throws Exception {
174         final String html = DOCTYPE_HTML
175             + "<html>\n"
176             + "<head>\n"
177             + "  <script>\n"
178             + LOG_TEXTAREA_FUNCTION
179 
180             + "    function test() {\n"
181             + "      if (!('OfflineAudioContext' in window)) {\n"
182             + "        log('OfflineAudioContext not available');\n"
183             + "        return;\n"
184             + "      }\n"
185 
186             + "      var audioCtx = new OfflineAudioContext({length: 44100 * 1, sampleRate: 44100});\n"
187             + "      var audioData = new ArrayBuffer(0);\n"
188             + "      audioCtx.decodeAudioData(audioData).then(\n"
189             + "             function(buffer) { log('Decoding audio data done'); },\n"
190             + "             function(e) { log('Error with decoding audio data'); logEx(e); }\n"
191             + "           );\n"
192             + "      log('OfflineAudioContext prep done');\n"
193             + "    }\n"
194             + "  </script>\n"
195             + "</head>\n"
196             + "<body onload='test()'>\n"
197             + LOG_TEXTAREA
198             + "</body>\n"
199             + "</html>";
200 
201         loadPageVerifyTextArea2(html);
202     }
203 
204     /**
205      * @throws Exception if the test fails
206      */
207     @Test
208     @Alerts({"1", "-3.4028234663852886e+38", "3.4028234663852886e+38", "1", "0.5"})
209     public void createGain() throws Exception {
210         final String html = DOCTYPE_HTML
211             + "<html>\n"
212             + "<head>\n"
213             + "  <script>\n"
214             + LOG_TEXTAREA_FUNCTION
215 
216             + "    function test() {\n"
217             + "      if (!('OfflineAudioContext' in window)) {\n"
218             + "        log('OfflineAudioContext not available');\n"
219             + "        return;\n"
220             + "      }\n"
221 
222             + "      var audioCtx = new OfflineAudioContext({length: 44100 * 1, sampleRate: 44100});\n"
223             + "      var gainNode = audioCtx.createGain();\n"
224             + "      log(gainNode.gain.defaultValue);\n"
225             + "      log(gainNode.gain.minValue);\n"
226             + "      log(gainNode.gain.maxValue);\n"
227             + "      log(gainNode.gain.value);\n"
228 
229             + "      gainNode.gain.value = 0.5;\n"
230             + "      log(gainNode.gain.value);\n"
231             + "    }\n"
232             + "  </script>\n"
233             + "</head>\n"
234             + "<body onload='test()'>\n"
235             + LOG_TEXTAREA
236             + "</body>\n"
237             + "</html>";
238 
239         loadPageVerifyTextArea2(html);
240     }
241 
242     /**
243      * @throws Exception if the test fails
244      */
245     @Test
246     @Alerts("function startRendering() { [native code] }")
247     public void startRendering() throws Exception {
248         final String html = DOCTYPE_HTML
249             + "<html>\n"
250             + "<head>\n"
251             + "  <script>\n"
252             + LOG_TITLE_FUNCTION
253             + "    function test() {\n"
254             + "      if (!('OfflineAudioContext' in window)) {\n"
255             + "        log('OfflineAudioContext not available');\n"
256             + "        return;\n"
257             + "      }\n"
258             + "      var offlineCtx = new OfflineAudioContext(2, 44100*40, 44100);\n"
259             + "      log(offlineCtx.startRendering);\n"
260             + "    }\n"
261             + "  </script>\n"
262             + "</head>\n"
263             + "<body onload='test()'>\n"
264             + "</body>\n"
265             + "</html>";
266 
267         loadPageVerifyTitle2(html);
268     }
269 }