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