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