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.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link HTMLVideoElement}.
23   *
24   * @author Marc Guillemot
25   * @author Frank Danek
26   */
27  public class HTMLVideoElementTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts("false")
34      public void prototype() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html><body>\n"
37              + "<script>\n"
38              + LOG_TITLE_FUNCTION
39              + "try {\n"
40              + "log(HTMLVideoElement.prototype == null);\n"
41              + "} catch(e) { logEx(e); }\n"
42              + "</script>\n"
43              + "</body></html>";
44  
45          loadPageVerifyTitle2(html);
46      }
47  
48      /**
49       * @throws Exception if the test fails
50       */
51      @Test
52      @Alerts({"[object HTMLVideoElement]", "function HTMLVideoElement() { [native code] }"})
53      public void type() throws Exception {
54          final String html = DOCTYPE_HTML
55              + "<html><head>\n"
56              + "<script>\n"
57              + LOG_TITLE_FUNCTION
58              + "  function test() {\n"
59              + "    var elem = document.getElementById('v1');\n"
60              + "    try {\n"
61              + "      log(elem);\n"
62              + "      log(HTMLVideoElement);\n"
63              + "    } catch(e) { logEx(e); }\n"
64              + "  }\n"
65              + "</script>\n"
66              + "</head>\n"
67              + "<body onload='test()'>\n"
68              + "  <video id='v1'/>\n"
69              + "</body></html>";
70  
71          loadPageVerifyTitle2(html);
72      }
73  
74      /**
75       * @throws Exception if the test fails
76       */
77      @Test
78      @Alerts({"1", "VIDEO"})
79      public void nodeTypeName() throws Exception {
80          final String html = DOCTYPE_HTML
81              + "<html><body>\n"
82              + "<video id='v' src='flower.mp4'></video>"
83              + "<script>\n"
84              + LOG_TITLE_FUNCTION
85              + "try {\n"
86              + "  var video = document.getElementById('v');\n"
87              + "  log(video.nodeType);"
88              + "  log(video.nodeName);"
89              + "} catch(e) { logEx(e); }\n"
90              + "</script>\n"
91              + "</body></html>";
92  
93          loadPageVerifyTitle2(html);
94      }
95  
96      /**
97       * @throws Exception if the test fails
98       */
99      @Test
100     @Alerts({"string", "§§URL§§flower.mp4", "§§URL§§tree.mp4",
101              "<video id=\"v\" src=\"tree.mp4\"></video>"})
102     public void src() throws Exception {
103         final String html = DOCTYPE_HTML
104             + "<html><body>\n"
105             + "<video id='v' src='flower.mp4'></video>"
106             + "<script>\n"
107             + LOG_TITLE_FUNCTION
108             + "try {\n"
109             + "  var video = document.getElementById('v');\n"
110             + "  var src = video.src;\n"
111             + "  log(typeof src);"
112             + "  log(src);"
113             + "  video.src = 'tree.mp4';\n"
114             + "  log(video.src);"
115             + "  log(video.outerHTML);"
116             + "} catch(e) { logEx(e); }\n"
117             + "</script>\n"
118             + "</body></html>";
119 
120         expandExpectedAlertsVariables(URL_FIRST);
121         loadPageVerifyTitle2(html);
122     }
123 
124     /**
125      * @throws Exception if the test fails
126      */
127     @Test
128     @Alerts({"string", "", "§§URL§§tree.mp4",
129         "<video id=\"v\" src=\"tree.mp4\"><source src=\"flower.mp4\" type=\"video/mp4\"></video>"})
130     public void srcChild() throws Exception {
131         final String html = DOCTYPE_HTML
132             + "<html><body>\n"
133             + "<video id='v'><source src='flower.mp4' type='video/mp4'></video>"
134             + "<script>\n"
135             + LOG_TITLE_FUNCTION
136             + "try {\n"
137             + "  var video = document.getElementById('v');\n"
138             + "  var src = video.src;\n"
139             + "  log(typeof src);"
140             + "  log(src);"
141             + "  video.src = 'tree.mp4';\n"
142             + "  log(video.src);"
143             + "  log(video.outerHTML);"
144             + "} catch(e) { logEx(e); }\n"
145             + "</script>\n"
146             + "</body></html>";
147 
148         expandExpectedAlertsVariables(URL_FIRST);
149         loadPageVerifyTitle2(html);
150     }
151 
152     /**
153      * @throws Exception if the test fails
154      */
155     @Test
156     @Alerts({"string", ""})
157     public void srcNotDefined() throws Exception {
158         final String html = DOCTYPE_HTML
159             + "<html><body>\n"
160             + "<video id='v'></video>"
161             + "<script>\n"
162             + LOG_TITLE_FUNCTION
163             + "try {\n"
164             + "  var src = document.getElementById('v').src;\n"
165             + "  log(typeof src);"
166             + "  log(src);"
167             + "} catch(e) { logEx(e); }\n"
168             + "</script>\n"
169             + "</body></html>";
170 
171         loadPageVerifyTitle2(html);
172     }
173 
174     /**
175      * @throws Exception if the test fails
176      */
177     @Test
178     @Alerts({"string", ""})
179     public void currentSrc() throws Exception {
180         final String html = DOCTYPE_HTML
181             + "<html><body>\n"
182             + "<video id='v' src='flower.mp4'></video>"
183             + "<script>\n"
184             + LOG_TITLE_FUNCTION
185             + "try {\n"
186             + "  var currentSrc = document.getElementById('v').currentSrc;\n"
187             + "  log(typeof currentSrc);"
188             + "  log(currentSrc);"
189             + "} catch(e) { logEx(e); }\n"
190             + "</script>\n"
191             + "</body></html>";
192 
193         expandExpectedAlertsVariables(URL_FIRST);
194         loadPageVerifyTitle2(html);
195     }
196 
197     /**
198      * @throws Exception if the test fails
199      */
200     @Test
201     @Alerts({"string", ""})
202     public void currentSrcChild() throws Exception {
203         final String html = DOCTYPE_HTML
204             + "<html><body>\n"
205             + "<video id='v'><source src='flower.mp4' type='video/mp4'></video>"
206             + "<script>\n"
207             + LOG_TITLE_FUNCTION
208             + "try {\n"
209             + "  var currentSrc = document.getElementById('v').currentSrc;\n"
210             + "  log(typeof currentSrc);"
211             + "  log(currentSrc);"
212             + "} catch(e) { logEx(e); }\n"
213             + "</script>\n"
214             + "</body></html>";
215 
216         expandExpectedAlertsVariables(URL_FIRST);
217         loadPageVerifyTitle2(html);
218     }
219 
220     /**
221      * @throws Exception if the test fails
222      */
223     @Test
224     @Alerts({"string", ""})
225     public void currentSrcNotDefined() throws Exception {
226         final String html = DOCTYPE_HTML
227             + "<html><body>\n"
228             + "<video id='v'></video>"
229             + "<script>\n"
230             + LOG_TITLE_FUNCTION
231             + "try {\n"
232             + "  var currentSrc = document.getElementById('v').currentSrc;\n"
233             + "  log(typeof currentSrc);"
234             + "  log(currentSrc);"
235             + "} catch(e) { logEx(e); }\n"
236             + "</script>\n"
237             + "</body></html>";
238 
239         loadPageVerifyTitle2(html);
240     }
241 }