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.event;
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 AudioProcessingEvent}.
24   *
25   * @author Ahmed Ashour
26   * @author Madis Pärn
27   * @author Ronald Brill
28   */
29  public class AudioProcessingEventTest extends WebDriverTestCase {
30  
31      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
32              + "    log(event);\n"
33              + "    log(event.type);\n"
34              + "    log(event.bubbles);\n"
35              + "    log(event.cancelable);\n"
36              + "    log(event.composed);\n"
37              + "  }\n";
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts("TypeError")
44      public void create_ctor() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head><script>\n"
47              + LOG_TITLE_FUNCTION
48              + "  function test() {\n"
49              + "    try {\n"
50              + "      var event = new AudioProcessingEvent('audioprocessing');\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(DEFAULT = {"[object AudioProcessingEvent]", "audioprocessing", "false", "false", "false"},
64              FF = "TypeError",
65              FF_ESR = "TypeError")
66      // audioCtx.createBuffer is missing
67      @HtmlUnitNYI(CHROME = "TypeError",
68              EDGE = "TypeError")
69      public void create_ctorAllDetails() throws Exception {
70          final String html = DOCTYPE_HTML
71              + "<html><head><script>\n"
72              + LOG_TITLE_FUNCTION
73              + "  function test() {\n"
74              + "    try {\n"
75              + "      var audioCtx = new AudioContext();\n"
76              + "      var inputBuffer = audioCtx.createBuffer(2, audioCtx.sampleRate * 3, audioCtx.sampleRate);\n"
77              + "      var outputBuffer = audioCtx.createBuffer(2, audioCtx.sampleRate * 3, audioCtx.sampleRate);\n"
78              + "      var event = new AudioProcessingEvent('audioprocessing', {"
79              + "        'inputBuffer': inputBuffer,\n"
80              + "        'outputBuffer': outputBuffer,\n"
81              + "        'playbackTime': 4,\n"
82              + "      });\n"
83              + "      dump(event);\n"
84              + "    } catch(e) { logEx(e) }\n"
85              + DUMP_EVENT_FUNCTION
86              + "  }\n"
87              + "</script></head><body onload='test()'>\n"
88              + "</body></html>";
89  
90          loadPageVerifyTitle2(html);
91      }
92  
93      /**
94       * @throws Exception if the test fails
95       */
96      @Test
97      @Alerts("TypeError")
98      public void create_ctorMissingDetails() throws Exception {
99          final String html = DOCTYPE_HTML
100             + "<html><head><script>\n"
101             + LOG_TITLE_FUNCTION
102             + "  function test() {\n"
103             + "    try {\n"
104             + "      var event = new AudioProcessingEvent('audioprocessing');\n"
105             + "      dump(event);\n"
106             + "    } catch(e) { logEx(e) }\n"
107             + DUMP_EVENT_FUNCTION
108             + "  }\n"
109             + "</script></head><body onload='test()'>\n"
110             + "</body></html>";
111 
112         loadPageVerifyTitle2(html);
113     }
114 
115     /**
116      * @throws Exception if the test fails
117      */
118     @Test
119     @Alerts("NotSupportedError/DOMException")
120     public void create_createEvent() throws Exception {
121         final String html = DOCTYPE_HTML
122             + "<html><head><script>\n"
123             + LOG_TITLE_FUNCTION
124             + "  function test() {\n"
125             + "    try {\n"
126             + "      var event = document.createEvent('AudioProcessingEvent');\n"
127             + "      dump(event);\n"
128             + "    } catch(e) { logEx(e); }\n"
129             + "  }\n"
130             + DUMP_EVENT_FUNCTION
131             + "</script></head><body onload='test()'>\n"
132             + "</body></html>";
133 
134         loadPageVerifyTitle2(html);
135     }
136 
137     /**
138      * @throws Exception if the test fails
139      */
140     @Test
141     @Alerts("true")
142     public void inWindow() throws Exception {
143         final String html = DOCTYPE_HTML
144             + "<html>\n"
145             + "<head>\n"
146             + "  <script>\n"
147             + LOG_TITLE_FUNCTION
148             + "    function test() {\n"
149             + "      log('AudioProcessingEvent' in window);\n"
150             + "    }\n"
151             + "  </script>\n"
152             + "</head>\n"
153             + "<body onload='test()'>\n"
154             + "</body>\n"
155             + "</html>";
156 
157         loadPageVerifyTitle2(html);
158     }
159 }