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