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