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