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