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.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link AudioContext}.
23   *
24   * @author Ronald Brill
25   */
26  public class AudioContextTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts("true")
33      public void inWindow() throws Exception {
34          final String html = DOCTYPE_HTML
35              + "<html>\n"
36              + "<head>\n"
37              + "  <script>\n"
38              + LOG_TITLE_FUNCTION
39              + "    function test() {\n"
40              + "      log('AudioContext' in window);\n"
41              + "    }\n"
42              + "  </script>\n"
43              + "</head>\n"
44              + "<body onload='test()'>\n"
45              + "</body>\n"
46              + "</html>";
47  
48          loadPageVerifyTitle2(html);
49      }
50  
51      /**
52       * @throws Exception if the test fails
53       */
54      @Test
55      @Alerts({"function", "[object AudioContext]"})
56      public void ctor() throws Exception {
57          final String html = DOCTYPE_HTML
58              + "<html>\n"
59              + "<head>\n"
60              + "  <script>\n"
61              + LOG_TEXTAREA_FUNCTION
62  
63              + "    function test() {\n"
64              + "      if (!('AudioContext' in window)) {\n"
65              + "        log('AudioContext not available');\n"
66              + "        return;\n"
67              + "      }\n"
68  
69              + "      try {\n"
70              + "        log(typeof AudioContext);\n"
71              + "        log(new AudioContext());\n"
72              + "      } catch(e) { logEx(e); }\n"
73              + "    }\n"
74              + "  </script>\n"
75              + "</head>\n"
76              + "<body onload='test()'>\n"
77              + LOG_TEXTAREA
78              + "</body>\n"
79              + "</html>";
80  
81          loadPageVerifyTextArea2(html);
82      }
83  
84      /**
85       * @throws Exception if the test fails
86       */
87      @Test
88      @Alerts("[object AudioBufferSourceNode]")
89      public void createBufferSource() throws Exception {
90          final String html = DOCTYPE_HTML
91              + "<html>\n"
92              + "<head>\n"
93              + "  <script>\n"
94              + LOG_TITLE_FUNCTION
95              + "    function test() {\n"
96              + "      if (!('AudioContext' in window)) {\n"
97              + "        log('AudioContext not available');\n"
98              + "        return;\n"
99              + "      }\n"
100 
101             + "      var audioCtx = new AudioContext();\n"
102             + "      var source = audioCtx.createBufferSource();\n"
103             + "      log(source);\n"
104             + "    }\n"
105             + "  </script>\n"
106             + "</head>\n"
107             + "<body onload='test()'>\n"
108             + "</body>\n"
109             + "</html>";
110 
111         loadPageVerifyTitle2(html);
112     }
113 
114     /**
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts({"AudioContext prep done", "Error with decoding audio data"})
119     public void decodeAudioData() throws Exception {
120         final String html = DOCTYPE_HTML
121             + "<html>\n"
122             + "<head>\n"
123             + "  <script>\n"
124             + LOG_TEXTAREA_FUNCTION
125 
126             + "    function test() {\n"
127             + "      if (!('AudioContext' in window)) {\n"
128             + "        log('AudioContext not available');\n"
129             + "        return;\n"
130             + "      }\n"
131 
132             + "      var audioCtx = new AudioContext();\n"
133             + "      var audioData = new ArrayBuffer(0);\n"
134             + "      audioCtx.decodeAudioData(audioData,\n"
135             + "             function(buffer) { log('Decoding audio data done'); },\n"
136             + "             function(e) { log('Error with decoding audio data'); }\n"
137             + "           );\n"
138             + "      log('AudioContext prep done');\n"
139             + "    }\n"
140             + "  </script>\n"
141             + "</head>\n"
142             + "<body onload='test()'>\n"
143             + LOG_TEXTAREA
144             + "</body>\n"
145             + "</html>";
146 
147         loadPageVerifyTextArea2(html);
148     }
149 
150     /**
151      * @throws Exception if the test fails
152      */
153     @Test
154     @Alerts({"AudioContext prep done", "Error with decoding audio data"})
155     public void decodeAudioData2() throws Exception {
156         final String html = DOCTYPE_HTML
157             + "<html>\n"
158             + "<head>\n"
159             + "  <script>\n"
160             + LOG_TEXTAREA_FUNCTION
161 
162             + "    function test() {\n"
163             + "      if (!('AudioContext' in window)) {\n"
164             + "        log('AudioContext not available');\n"
165             + "        return;\n"
166             + "      }\n"
167 
168             + "      var audioCtx = new AudioContext();\n"
169             + "      var audioData = new ArrayBuffer(0);\n"
170             + "      audioCtx.decodeAudioData(audioData).then(\n"
171             + "             function(buffer) { log('Decoding audio data done'); },\n"
172             + "             function(e) { log('Error with decoding audio data'); }\n"
173             + "           );\n"
174             + "      log('AudioContext prep done');\n"
175             + "    }\n"
176             + "  </script>\n"
177             + "</head>\n"
178             + "<body onload='test()'>\n"
179             + LOG_TEXTAREA
180             + "</body>\n"
181             + "</html>";
182 
183         loadPageVerifyTextArea2(html);
184     }
185 
186     /**
187      * @throws Exception if the test fails
188      */
189     @Test
190     @Alerts({"1", "-3.4028234663852886e+38", "3.4028234663852886e+38", "1", "0.5"})
191     public void createGain() throws Exception {
192         final String html = DOCTYPE_HTML
193             + "<html>\n"
194             + "<head>\n"
195             + "  <script>\n"
196             + LOG_TEXTAREA_FUNCTION
197 
198             + "    function test() {\n"
199             + "      if (!('AudioContext' in window)) {\n"
200             + "        log('AudioContext not available');\n"
201             + "        return;\n"
202             + "      }\n"
203 
204             + "      var audioCtx = new AudioContext();\n"
205             + "      var gainNode = audioCtx.createGain();\n"
206             + "      log(gainNode.gain.defaultValue);\n"
207             + "      log(gainNode.gain.minValue);\n"
208             + "      log(gainNode.gain.maxValue);\n"
209             + "      log(gainNode.gain.value);\n"
210 
211             + "      gainNode.gain.value = 0.5;\n"
212             + "      log(gainNode.gain.value);\n"
213             + "    }\n"
214             + "  </script>\n"
215             + "</head>\n"
216             + "<body onload='test()'>\n"
217             + LOG_TEXTAREA
218             + "</body>\n"
219             + "</html>";
220 
221         loadPageVerifyTextArea2(html);
222     }
223 }