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.html;
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 HTMLMediaElement}.
26   *
27   * @author Ahmed Ashour
28   * @author Frank Danek
29   * @author Ronald Brill
30   */
31  @RunWith(BrowserRunner.class)
32  public class HTMLMediaElementTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts("")
39      public void canPlayTypeBlank() throws Exception {
40          canPlayType("");
41      }
42  
43      /**
44       * @throws Exception if the test fails
45       */
46      @Test
47      @Alerts("maybe")
48      public void canPlayTypeVideoOgg() throws Exception {
49          canPlayType("video/ogg");
50      }
51  
52      /**
53       * @throws Exception if the test fails
54       */
55      @Test
56      @Alerts("maybe")
57      public void canPlayTypeVideoMp4() throws Exception {
58          canPlayType("video/mp4");
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts("maybe")
66      public void canPlayTypeVideoWebm() throws Exception {
67          canPlayType("video/webm");
68      }
69  
70      /**
71       * @throws Exception if the test fails
72       */
73      @Test
74      @Alerts(DEFAULT = "maybe",
75              CHROME = "probably",
76              EDGE = "probably")
77      @HtmlUnitNYI(CHROME = "maybe",
78              EDGE = "maybe")
79      public void canPlayTypeAudioMpeg() throws Exception {
80          canPlayType("audio/mpeg");
81      }
82  
83      /**
84       * @throws Exception if the test fails
85       */
86      @Test
87      @Alerts("maybe")
88      public void canPlayTypeAudioMp4() throws Exception {
89          canPlayType("audio/mp4");
90      }
91  
92      /**
93       * @throws Exception if the test fails
94       */
95      @Test
96      @Alerts("probably")
97      public void canPlayTypeVideoOggCodecs() throws Exception {
98          canPlayType("video/ogg; codecs=\"theora, vorbis\"");
99      }
100 
101     /**
102      * @throws Exception if the test fails
103      */
104     @Test
105     @Alerts("probably")
106     public void canPlayTypeVideoMp4Codecs() throws Exception {
107         canPlayType("video/mp4; codecs=\"avc1.4D401E, mp4a.40.2\"");
108     }
109 
110     /**
111      * @throws Exception if the test fails
112      */
113     @Test
114     @Alerts("probably")
115     public void canPlayTypeAudioWebmCodecs() throws Exception {
116         canPlayType("video/webm; codecs=\"vp8.0, vorbis\"");
117     }
118 
119     /**
120      * @throws Exception if the test fails
121      */
122     @Test
123     @Alerts("probably")
124     public void canPlayTypeAudioOggCodecs() throws Exception {
125         canPlayType("audio/ogg; codecs=\"vorbis\"");
126     }
127 
128     /**
129      * @throws Exception if the test fails
130      */
131     @Test
132     @Alerts("probably")
133     public void canPlayTypeAudioMp4Codecs() throws Exception {
134         canPlayType("audio/mp4; codecs=\"mp4a.40.5\"");
135     }
136 
137     private void canPlayType(final String type) throws Exception {
138         final String html = DOCTYPE_HTML
139             + "<html>\n"
140             + "<head>\n"
141             + "  <script>\n"
142             + LOG_TITLE_FUNCTION
143             + "  </script>\n"
144             + "</head>\n"
145             + "<body>\n"
146             + "<script>\n"
147             + "try {\n"
148             + "  var video = document.createElement('video');"
149             + "  log(video.canPlayType('" + type + "'));\n"
150             + "} catch(e) { logEx(e); }\n"
151             + "</script>\n"
152             + "</body></html>";
153 
154         loadPageVerifyTitle2(html);
155     }
156 
157     /**
158      * @throws Exception if the test fails
159      */
160     @Test
161     @Alerts({"[object HTMLAudioElement]", "done"})
162     public void pause() throws Exception {
163         final String html = DOCTYPE_HTML
164                 + "<html><head>\n"
165                 + "<script>\n"
166                 + LOG_TITLE_FUNCTION
167                 + "  function test() {\n"
168                 + "    var a = new Audio('1.mp3');\n"
169                 + "    log(a);\n"
170                 + "    a.pause();\n"
171                 + "    log('done');\n"
172                 + "  }\n"
173                 + "</script>\n"
174                 + "</head>\n"
175                 + "<body onload='test()'>\n"
176                 + "</body></html>";
177 
178         loadPageVerifyTitle2(html);
179     }
180 }