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.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link HtmlSearchInput}.
23   *
24   * @author Anton Demydenko
25   * @author Ronald Brill
26   */
27  public class HtmlSearchInput2Test extends SimpleWebTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      public void patternValidation() throws Exception {
34          final String htmlContent = DOCTYPE_HTML
35              + "<html>\n"
36              + "<head></head>\n"
37              + "<body>\n"
38              + "<form id='form1'>\n"
39              + "  <input type='search' pattern='[A-z]{2}[0-9]{4}' id='foo'>\n"
40              + "</form>\n"
41              + "</body></html>";
42  
43          final HtmlPage page = loadPage(htmlContent);
44  
45          final HtmlSearchInput input = (HtmlSearchInput) page.getElementById("foo");
46  
47          // empty
48          assertTrue(input.isValid());
49          // invalid
50          input.setValue("qwerty");
51          assertFalse(input.isValid());
52          // valid
53          input.setValue("AB1234");
54          assertTrue(input.isValid());
55      }
56  
57      /**
58       * @throws Exception
59       *         if the test fails
60       */
61      @Test
62      @Alerts({"true", "true", "true", "", "foo"})
63      public void maxLengthValidation() throws Exception {
64          final String htmlContent = DOCTYPE_HTML
65              + "<html>\n"
66              + "<head></head>\n"
67              + "<body>\n"
68              + "<form id='form1'>\n"
69              + "  <input type='search' id='foo' maxLength='3'>\n"
70              + "</form>\n"
71              + "</body></html>";
72  
73          final HtmlPage page = loadPage(htmlContent);
74  
75          final HtmlInput input = (HtmlInput) page.getElementById("foo");
76          assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
77          input.type("foo");
78          assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
79          input.type("bar");
80          assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
81          assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
82          assertEquals(getExpectedAlerts()[4], input.getValue());
83      }
84  
85      /**
86       * @throws Exception
87       *         if the test fails
88       */
89      @Test
90      @Alerts({"true", "false", "true", "", "foobar"})
91      public void minLengthValidation() throws Exception {
92          final String htmlContent = DOCTYPE_HTML
93              + "<html>\n"
94              + "<head></head>\n"
95              + "<body>\n"
96              + "<form id='form1'>\n"
97              + "  <input type='search' id='foo' minLength='4'>\n"
98              + "</form>\n"
99              + "</body></html>";
100 
101         final HtmlPage page = loadPage(htmlContent);
102 
103         final HtmlInput input = (HtmlInput) page.getElementById("foo");
104         assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
105         input.type("foo");
106         assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
107         input.type("bar");
108         assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
109         assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
110         assertEquals(getExpectedAlerts()[4], input.getValue());
111     }
112 }