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.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  import org.openqa.selenium.By;
23  import org.openqa.selenium.WebDriver;
24  import org.openqa.selenium.WebElement;
25  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
26  
27  /**
28   * Tests for {@link HtmlDateTimeLocalInput}.
29   *
30   * @author Ronald Brill
31   * @author Anton Demydenko
32   */
33  @RunWith(BrowserRunner.class)
34  public class HtmlDateTimeInputTest extends WebDriverTestCase {
35  
36      /**
37       * @throws Exception if the test fails
38       */
39      @Test
40      @Alerts({"--null", "--null", "--null"})
41      public void defaultValues() throws Exception {
42          final String html = DOCTYPE_HTML
43              + "<html><head>\n"
44              + "<script>\n"
45              + LOG_TITLE_FUNCTION
46              + "  function test() {\n"
47              + "    var input = document.getElementById('text1');\n"
48              + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
49  
50              + "    try {\n"
51              + "      input = document.createElement('input');\n"
52              + "      input.type = 'datetime';\n"
53              + "      log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
54              + "    } catch(e)  { logEx(e); }\n"
55  
56              + "    var builder = document.createElement('div');\n"
57              + "    builder.innerHTML = '<input type=\"datetime\">';\n"
58              + "    input = builder.firstChild;\n"
59              + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
60              + "  }\n"
61              + "</script>\n"
62              + "</head><body onload='test()'>\n"
63              + "<form>\n"
64              + "  <input type='datetime' id='text1'>\n"
65              + "</form>\n"
66              + "</body></html>";
67  
68          loadPageVerifyTitle2(html);
69      }
70  
71      /**
72       * @throws Exception if the test fails
73       */
74      @Test
75      @Alerts({"--null", "--null", "--null"})
76      public void defaultValuesAfterClone() throws Exception {
77          final String html = DOCTYPE_HTML
78              + "<html><head>\n"
79              + "<script>\n"
80              + LOG_TITLE_FUNCTION
81              + "  function test() {\n"
82              + "    var input = document.getElementById('text1');\n"
83              + "    input = input.cloneNode(false);\n"
84              + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
85  
86              + "    try{\n"
87              + "      input = document.createElement('input');\n"
88              + "      input.type = 'datetime';\n"
89              + "      input = input.cloneNode(false);\n"
90              + "      log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
91              + "    } catch(e)  { logEx(e); }\n"
92  
93              + "    var builder = document.createElement('div');\n"
94              + "    builder.innerHTML = '<input type=\"datetime\">';\n"
95              + "    input = builder.firstChild;\n"
96              + "    input = input.cloneNode(false);\n"
97              + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
98              + "  }\n"
99              + "</script>\n"
100             + "</head><body onload='test()'>\n"
101             + "<form>\n"
102             + "  <input type='datetime' id='text1'>\n"
103             + "</form>\n"
104             + "</body></html>";
105 
106         loadPageVerifyTitle2(html);
107     }
108 
109     /**
110      * Verifies getVisibleText().
111      * @throws Exception if the test fails
112      */
113     @Test
114     @Alerts("")
115     public void getVisibleText() throws Exception {
116         final String htmlContent = DOCTYPE_HTML
117             + "<html>\n"
118             + "<head></head>\n"
119             + "<body>\n"
120             + "<form id='form1'>\n"
121             + "  <input type='datetime' name='tester' id='tester' value='2018-06-12' "
122                                     + "min='2018-06-07' max='2018-06-14'>\n"
123             + "</form>\n"
124             + "</body></html>";
125 
126         final WebDriver driver = loadPage2(htmlContent);
127         final String text = driver.findElement(By.id("tester")).getText();
128         assertEquals(getExpectedAlerts()[0], text);
129 
130         if (driver instanceof HtmlUnitDriver) {
131             final HtmlPage page = (HtmlPage) getEnclosedPage();
132             assertEquals(getExpectedAlerts()[0], page.getBody().getVisibleText());
133         }
134     }
135 
136     /**
137      * Verifies clear().
138      * @throws Exception if the test fails
139      */
140     @Test
141     @Alerts({"2018-06-12", ""})
142     public void clearInput() throws Exception {
143         final String htmlContent = DOCTYPE_HTML
144                 + "<html>\n"
145                 + "<head></head>\n"
146                 + "<body>\n"
147                 + "<form id='form1'>\n"
148                 + "  <input type='datetime' name='tester' id='tester' value='2018-06-12'>\n"
149                 + "</form>\n"
150                 + "</body></html>";
151 
152         final WebDriver driver = loadPage2(htmlContent);
153         final WebElement element = driver.findElement(By.id("tester"));
154 
155         assertEquals(getExpectedAlerts()[0], element.getDomAttribute("value"));
156         assertEquals(getExpectedAlerts()[0], element.getDomProperty("value"));
157 
158         element.clear();
159         assertEquals(getExpectedAlerts()[0], element.getDomAttribute("value"));
160         assertEquals(getExpectedAlerts()[1], element.getDomProperty("value"));
161     }
162 
163     /**
164      * @throws Exception if the test fails
165      */
166     @Test
167     @Alerts("--")
168     public void minMaxStep() throws Exception {
169         final String html = DOCTYPE_HTML
170             + "<html>\n"
171             + "<head>\n"
172             + "<script>\n"
173             + LOG_TITLE_FUNCTION
174             + "  function test() {\n"
175             + "    var input = document.getElementById('tester');\n"
176             + "    log(input.min + '-' + input.max + '-' + input.step);\n"
177             + "  }\n"
178             + "</script>\n"
179             + "</head>\n"
180             + "<body onload='test()'>\n"
181             + "<form>\n"
182             + "  <input type='datetime' id='tester'>\n"
183             + "</form>\n"
184             + "</body>\n"
185             + "</html>";
186 
187         loadPageVerifyTitle2(html);
188     }
189 
190     /**
191      * @throws Exception if an error occurs
192      */
193     @Test
194     @Alerts("true-true")
195     public void maxValidation() throws Exception {
196         final String html = DOCTYPE_HTML
197             + "<html>\n"
198             + "<head>\n"
199             + "<script>\n"
200             + LOG_TITLE_FUNCTION
201             + "  function test() {\n"
202             + "    var foo = document.getElementById('foo');\n"
203             + "    var bar = document.getElementById('bar');\n"
204             + "    log(foo.checkValidity() + '-' + bar.checkValidity() );\n"
205             + "  }\n"
206             + "</script>\n"
207             + "</head>\n"
208             + "<body onload='test()'>\n"
209             + "  <input type='datetime' max='2018-12-01' id='foo' value='2018-12-11'>\n"
210             + "  <input type='datetime' max='2018-12-01' id='bar' value='2018-11-01'>\n"
211             + "</body>\n"
212             + "</html>";
213 
214         loadPageVerifyTitle2(html);
215     }
216 
217     /**
218      * @throws Exception if an error occurs
219      */
220     @Test
221     @Alerts("true-true")
222     public void minValidation() throws Exception {
223         final String html = DOCTYPE_HTML
224             + "<html>\n"
225             + "<head>\n"
226             + "<script>\n"
227             + LOG_TITLE_FUNCTION
228             + "  function test() {\n"
229             + "    var foo = document.getElementById('foo');\n"
230             + "    var bar = document.getElementById('bar');\n"
231             + "    log(foo.checkValidity() + '-' + bar.checkValidity() );\n"
232             + "  }\n"
233             + "</script>\n"
234             + "</head>\n"
235             + "<body onload='test()'>\n"
236             + "  <input type='datetime' min='2018-12-01' id='foo' value='2018-11-01'>\n"
237             + "  <input type='datetime' min='2018-12-01' id='bar' value='2018-12-01'>\n"
238             + "</body>\n"
239             + "</html>";
240 
241         loadPageVerifyTitle2(html);
242     }
243 
244     /**
245      * @throws Exception if the test fails
246      */
247     @Test
248     @Alerts({"true", "true", "false", "false", "true"})
249     public void checkValidity() throws Exception {
250         final String html = DOCTYPE_HTML
251             + "<html>\n"
252             + "<head>\n"
253             + "<script>\n"
254             + LOG_TITLE_FUNCTION
255             + "  function test() {\n"
256             + "    var foo = document.getElementById('foo');\n"
257             + "    log(foo.checkValidity());\n"
258 
259             + "    foo.setCustomValidity('');\n"
260             + "    log(foo.checkValidity());\n"
261 
262             + "    foo.setCustomValidity(' ');\n"
263             + "    log(foo.checkValidity());\n"
264 
265             + "    foo.setCustomValidity('invalid');\n"
266             + "    log(foo.checkValidity());\n"
267 
268             + "    foo.setCustomValidity('');\n"
269             + "    log(foo.checkValidity());\n"
270             + "  }\n"
271             + "</script>\n"
272             + "</head>\n"
273             + "<body onload='test()'>\n"
274             + "  <input type='datetime' id='foo'>\n"
275             + "</body>\n"
276             + "</html>";
277 
278         loadPageVerifyTitle2(html);
279     }
280 }