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.SimpleWebTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.junit.Test;
20  import org.junit.runner.RunWith;
21  
22  /**
23   * Tests for {@link HtmlDateInput}.
24   *
25   * @author Anton Demydenko
26   */
27  @RunWith(BrowserRunner.class)
28  public class HtmlDateInput2Test extends SimpleWebTestCase {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      public void minValidation() throws Exception {
35          final String htmlContent = DOCTYPE_HTML
36                  + "<html>\n"
37                  + "<head></head>\n"
38                  + "<body>\n"
39                  + "<form id='form1'>\n"
40                  + "  <input type='date' id='first' min='2018-12-01'>\n"
41                  + "  <input type='date' id='second'>\n"
42                  + "  <input type='date' id='third' min='foo'>\n"
43                  + "</form>\n"
44                  + "</body></html>";
45  
46          final HtmlPage page = loadPage(htmlContent);
47  
48          final HtmlDateInput first = (HtmlDateInput) page.getElementById("first");
49          final HtmlDateInput second = (HtmlDateInput) page.getElementById("second");
50          final HtmlDateInput third = (HtmlDateInput) page.getElementById("third");
51  
52          // empty
53          assertTrue(first.isValid());
54          // lesser
55          first.setValue("2018-11-01");
56          assertFalse(first.isValid());
57          // equal
58          first.setValue("2018-12-01");
59          assertTrue(first.isValid());
60          // bigger
61          first.setValue("2018-12-11");
62          assertTrue(first.isValid());
63  
64          second.setValue("2018-11-01");
65          assertTrue(second.isValid());
66          third.setValue("2018-11-01");
67          assertTrue(third.isValid());
68      }
69  
70      /**
71       * @throws Exception if the test fails
72       */
73      @Test
74      public void maxValidation() throws Exception {
75          final String htmlContent = DOCTYPE_HTML
76                  + "<html>\n"
77                  + "<head></head>\n"
78                  + "<body>\n"
79                  + "<form id='form1'>\n"
80                  + "  <input type='date' id='first' max='2018-12-01'>\n"
81                  + "  <input type='date' id='second'>\n"
82                  + "  <input type='date' id='third' max='foo'>\n"
83                  + "</form>\n"
84                  + "</body></html>";
85  
86          final HtmlPage page = loadPage(htmlContent);
87  
88          final HtmlDateInput first = (HtmlDateInput) page.getElementById("first");
89          final HtmlDateInput second = (HtmlDateInput) page.getElementById("second");
90          final HtmlDateInput third = (HtmlDateInput) page.getElementById("third");
91  
92          // empty
93          assertTrue(first.isValid());
94          // lesser
95          first.setValue("2018-11-01");
96          assertTrue(first.isValid());
97          // equal
98          first.setValue("2018-12-01");
99          assertTrue(first.isValid());
100         // bigger
101         first.setValue("2018-12-11");
102         assertFalse(first.isValid());
103 
104         second.setValue("2018-12-01");
105         assertTrue(second.isValid());
106         third.setValue("2018-12-01");
107         assertTrue(third.isValid());
108     }
109 }