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.annotation.Alerts;
19  import org.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  import org.openqa.selenium.By;
22  import org.openqa.selenium.WebDriver;
23  import org.openqa.selenium.WebElement;
24  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
25  
26  /**
27   * Tests for {@link HtmlWeekInput}.
28   *
29   * @author Ronald Brill
30   * @author Anton Demydenko
31   */
32  public class HtmlWeekInputTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts({"--null", "--null", "--null"})
39      public void defaultValues() throws Exception {
40          final String html = DOCTYPE_HTML
41              + "<html><head>\n"
42              + "<script>\n"
43              + LOG_TITLE_FUNCTION
44              + "  function test() {\n"
45              + "    var input = document.getElementById('text1');\n"
46              + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
47  
48              + "    try {\n"
49              + "      input = document.createElement('input');\n"
50              + "      input.type = 'week';\n"
51              + "      log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
52              + "    } catch(e)  { logEx(e); }\n"
53  
54              + "    var builder = document.createElement('div');\n"
55              + "    builder.innerHTML = '<input type=\"week\">';\n"
56              + "    input = builder.firstChild;\n"
57              + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
58              + "  }\n"
59              + "</script>\n"
60              + "</head><body onload='test()'>\n"
61              + "<form>\n"
62              + "  <input type='week' id='text1'>\n"
63              + "</form>\n"
64              + "</body></html>";
65  
66          loadPageVerifyTitle2(html);
67      }
68  
69      /**
70       * @throws Exception if the test fails
71       */
72      @Test
73      @Alerts({"--null", "--null", "--null"})
74      public void defaultValuesAfterClone() throws Exception {
75          final String html = DOCTYPE_HTML
76              + "<html><head>\n"
77              + "<script>\n"
78              + LOG_TITLE_FUNCTION
79              + "  function test() {\n"
80              + "    var input = document.getElementById('text1');\n"
81              + "    input = input.cloneNode(false);\n"
82              + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
83  
84              + "    try {\n"
85              + "      input = document.createElement('input');\n"
86              + "      input.type = 'week';\n"
87              + "      input = input.cloneNode(false);\n"
88              + "      log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
89              + "    } catch(e)  { logEx(e); }\n"
90  
91              + "    var builder = document.createElement('div');\n"
92              + "    builder.innerHTML = '<input type=\"week\">';\n"
93              + "    input = builder.firstChild;\n"
94              + "    input = input.cloneNode(false);\n"
95              + "    log(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n"
96              + "  }\n"
97              + "</script>\n"
98              + "</head><body onload='test()'>\n"
99              + "<form>\n"
100             + "  <input type='week' id='text1'>\n"
101             + "</form>\n"
102             + "</body></html>";
103 
104         loadPageVerifyTitle2(html);
105     }
106 
107     /**
108      * @throws Exception if the test fails
109      */
110     @Test
111     @Alerts(DEFAULT = "36",
112             CHROME = "",
113             EDGE = "")
114     @HtmlUnitNYI(FF = "",
115             FF_ESR = "")
116     public void typing() throws Exception {
117         final String htmlContent = DOCTYPE_HTML
118             + "<html><head><title>foo</title></head><body>\n"
119             + "<form id='form1'>\n"
120             + "  <input type='week' id='foo'>\n"
121             + "</form></body></html>";
122 
123         final WebDriver driver = loadPage2(htmlContent);
124 
125         final WebElement input = driver.findElement(By.id("foo"));
126 
127         input.sendKeys("36");
128         assertNull(input.getDomAttribute("value"));
129         assertEquals(getExpectedAlerts()[0], input.getDomProperty("value"));
130     }
131 
132     /**
133      * Verifies getVisibleText().
134      * @throws Exception if the test fails
135      */
136     @Test
137     @Alerts("")
138     public void getVisibleText() throws Exception {
139         final String htmlContent = DOCTYPE_HTML
140             + "<html>\n"
141             + "<head></head>\n"
142             + "<body>\n"
143             + "<form id='form1'>\n"
144             + "  <input type='week' name='tester' id='tester' value='2018-W20' min='2018-W18' max='2018-W26'>\n"
145             + "</form>\n"
146             + "</body></html>";
147 
148         final WebDriver driver = loadPage2(htmlContent);
149         final String text = driver.findElement(By.id("tester")).getText();
150         assertEquals(getExpectedAlerts()[0], text);
151 
152         if (driver instanceof HtmlUnitDriver) {
153             final HtmlPage page = (HtmlPage) getEnclosedPage();
154             assertEquals(getExpectedAlerts()[0], page.getBody().getVisibleText());
155         }
156     }
157 
158     /**
159      * Verifies clear().
160      * @throws Exception if the test fails
161      */
162     @Test
163     @Alerts({"2018-W20", ""})
164     public void clearInput() throws Exception {
165         final String htmlContent = DOCTYPE_HTML
166                 + "<html>\n"
167                 + "<head></head>\n"
168                 + "<body>\n"
169                 + "<form id='form1'>\n"
170                 + "  <input type='week' name='tester' id='tester' value='2018-W20'>\n"
171                 + "</form>\n"
172                 + "</body></html>";
173 
174         final WebDriver driver = loadPage2(htmlContent);
175         final WebElement element = driver.findElement(By.id("tester"));
176 
177         assertEquals(getExpectedAlerts()[0], element.getDomAttribute("value"));
178         assertEquals(getExpectedAlerts()[0], element.getDomProperty("value"));
179 
180         element.clear();
181         assertEquals(getExpectedAlerts()[0], element.getDomAttribute("value"));
182         assertEquals(getExpectedAlerts()[1], element.getDomProperty("value"));
183     }
184 
185     /**
186      * @throws Exception if the test fails
187      */
188     @Test
189     @Alerts("--")
190     public void minMaxStep() throws Exception {
191         final String html = DOCTYPE_HTML
192             + "<html>\n"
193             + "<head>\n"
194             + "<script>\n"
195             + LOG_TITLE_FUNCTION
196             + "  function test() {\n"
197             + "    var input = document.getElementById('tester');\n"
198             + "    log(input.min + '-' + input.max + '-' + input.step);\n"
199             + "  }\n"
200             + "</script>\n"
201             + "</head>\n"
202             + "<body onload='test()'>\n"
203             + "<form>\n"
204             + "  <input type='week' id='tester'>\n"
205             + "</form>\n"
206             + "</body>\n"
207             + "</html>";
208 
209         loadPageVerifyTitle2(html);
210     }
211 
212     /**
213      * @throws Exception if an error occurs
214      */
215     @Test
216     @Alerts(DEFAULT = "true-true",
217             CHROME = "false-true",
218             EDGE = "false-true")
219     public void maxValidation() throws Exception {
220         final String html = DOCTYPE_HTML
221             + "<html>\n"
222             + "<head>\n"
223             + "<script>\n"
224             + LOG_TITLE_FUNCTION
225             + "  function test() {\n"
226             + "    var foo = document.getElementById('foo');\n"
227             + "    var bar = document.getElementById('bar');\n"
228             + "    log(foo.checkValidity() + '-' + bar.checkValidity() );\n"
229             + "  }\n"
230             + "</script>\n"
231             + "</head>\n"
232             + "<body onload='test()'>\n"
233             + "  <input type='week' max='2018-W10' id='foo' value='2018-W11'>\n"
234             + "  <input type='week' max='2018-W10' id='bar' value='2018-W10'>\n"
235             + "</body>\n"
236             + "</html>";
237 
238         loadPageVerifyTitle2(html);
239     }
240 
241     /**
242      * @throws Exception if an error occurs
243      */
244     @Test
245     @Alerts(DEFAULT = "true-true",
246             CHROME = "false-true",
247             EDGE = "false-true")
248     public void minValidation() throws Exception {
249         final String html = DOCTYPE_HTML
250             + "<html>\n"
251             + "<head>\n"
252             + "<script>\n"
253             + LOG_TITLE_FUNCTION
254             + "  function test() {\n"
255             + "    var foo = document.getElementById('foo');\n"
256             + "    var bar = document.getElementById('bar');\n"
257             + "    log(foo.checkValidity() + '-' + bar.checkValidity() );\n"
258             + "  }\n"
259             + "</script>\n"
260             + "</head>\n"
261             + "<body onload='test()'>\n"
262             + "  <input type='week' min='2018-W10' id='foo' value='2018-W09'>\n"
263             + "  <input type='week' min='2018-W10' id='bar' value='2018-W10'>\n"
264             + "</body>\n"
265             + "</html>";
266 
267         loadPageVerifyTitle2(html);
268     }
269 
270     /**
271      * @throws Exception if an error occurs
272      */
273     @Test
274     @Alerts({"true", "false", "true", "false", "true"})
275     public void willValidate() throws Exception {
276         final String html = DOCTYPE_HTML
277                 + "<html><head>\n"
278                 + "  <script>\n"
279                 + LOG_TITLE_FUNCTION
280                 + "    function test() {\n"
281                 + "      log(document.getElementById('o1').willValidate);\n"
282                 + "      log(document.getElementById('o2').willValidate);\n"
283                 + "      log(document.getElementById('o3').willValidate);\n"
284                 + "      log(document.getElementById('o4').willValidate);\n"
285                 + "      log(document.getElementById('o5').willValidate);\n"
286                 + "    }\n"
287                 + "  </script>\n"
288                 + "</head>\n"
289                 + "<body onload='test()'>\n"
290                 + "  <form>\n"
291                 + "    <input type='week' id='o1'>\n"
292                 + "    <input type='week' id='o2' disabled>\n"
293                 + "    <input type='week' id='o3' hidden>\n"
294                 + "    <input type='week' id='o4' readonly>\n"
295                 + "    <input type='week' id='o5' style='display: none'>\n"
296                 + "  </form>\n"
297                 + "</body></html>";
298 
299         loadPageVerifyTitle2(html);
300     }
301 
302     /**
303      * @throws Exception if an error occurs
304      */
305     @Test
306     @Alerts({"true",
307              "false-false-false-false-false-false-false-false-false-true-false",
308              "true"})
309     public void validationEmpty() throws Exception {
310         validation("<input type='week' id='e1'>\n", "");
311     }
312 
313     /**
314      * @throws Exception if an error occurs
315      */
316     @Test
317     @Alerts({"false",
318              "false-true-false-false-false-false-false-false-false-false-false",
319              "true"})
320     public void validationCustomValidity() throws Exception {
321         validation("<input type='week' id='e1'>\n", "elem.setCustomValidity('Invalid');");
322     }
323 
324     /**
325      * @throws Exception if an error occurs
326      */
327     @Test
328     @Alerts({"false",
329              "false-true-false-false-false-false-false-false-false-false-false",
330              "true"})
331     public void validationBlankCustomValidity() throws Exception {
332         validation("<input type='week' id='e1'>\n", "elem.setCustomValidity(' ');\n");
333     }
334 
335     /**
336      * @throws Exception if an error occurs
337      */
338     @Test
339     @Alerts({"true",
340              "false-false-false-false-false-false-false-false-false-true-false",
341              "true"})
342     public void validationResetCustomValidity() throws Exception {
343         validation("<input type='week' id='e1'>\n",
344                 "elem.setCustomValidity('Invalid');elem.setCustomValidity('');");
345     }
346 
347     /**
348      * @throws Exception if an error occurs
349      */
350     @Test
351     @Alerts({"false",
352              "false-false-false-false-false-false-false-false-false-false-true",
353              "true"})
354     public void validationRequired() throws Exception {
355         validation("<input type='week' id='e1' required>\n", "");
356     }
357 
358     /**
359      * @throws Exception if an error occurs
360      */
361     @Test
362     @Alerts({"true",
363              "false-false-false-false-false-false-false-false-false-true-false",
364              "true"})
365     public void validationRequiredValueSet() throws Exception {
366         validation("<input type='week' id='e1' required>\n", "elem.value='2018-W20';");
367     }
368 
369     private void validation(final String htmlPart, final String jsPart) throws Exception {
370         final String html = DOCTYPE_HTML
371                 + "<html><head>\n"
372                 + "  <script>\n"
373                 + LOG_TITLE_FUNCTION
374                 + "    function logValidityState(s) {\n"
375                 + "      log(s.badInput"
376                         + "+ '-' + s.customError"
377                         + "+ '-' + s.patternMismatch"
378                         + "+ '-' + s.rangeOverflow"
379                         + "+ '-' + s.rangeUnderflow"
380                         + "+ '-' + s.stepMismatch"
381                         + "+ '-' + s.tooLong"
382                         + "+ '-' + s.tooShort"
383                         + " + '-' + s.typeMismatch"
384                         + " + '-' + s.valid"
385                         + " + '-' + s.valueMissing);\n"
386                 + "    }\n"
387                 + "    function test() {\n"
388                 + "      var elem = document.getElementById('e1');\n"
389                 + jsPart
390                 + "      log(elem.checkValidity());\n"
391                 + "      logValidityState(elem.validity);\n"
392                 + "      log(elem.willValidate);\n"
393                 + "    }\n"
394                 + "  </script>\n"
395                 + "</head>\n"
396                 + "<body onload='test()'>\n"
397                 + "  <form>\n"
398                 + htmlPart
399                 + "  </form>\n"
400                 + "</body></html>";
401 
402         loadPageVerifyTitle2(html);
403     }
404 }