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 HtmlTelInput}.
25   *
26   * @author Ronald Brill
27   * @author Anton Demydenko
28   */
29  @RunWith(BrowserRunner.class)
30  public class HtmlTelInput2Test extends SimpleWebTestCase {
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      public void typingAndClone() 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='tel' id='foo'>\n"
43              + "</form>\n"
44              + "</body></html>";
45  
46          final HtmlPage page = loadPage(htmlContent);
47  
48          HtmlTelInput input = (HtmlTelInput) page.getElementById("foo");
49          input = (HtmlTelInput) input.cloneNode(true);
50          input.type("4711");
51          assertEquals("", input.getValueAttribute());
52          assertEquals("4711", input.getValue());
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      public void typingAndReset() throws Exception {
60          final String htmlContent = DOCTYPE_HTML
61              + "<html>\n"
62              + "<head></head>\n"
63              + "<body>\n"
64              + "<form id='form1'>\n"
65              + "  <input type='tel' id='foo'>\n"
66              + "</form>\n"
67              + "</body></html>";
68  
69          final HtmlPage page = loadPage(htmlContent);
70  
71          final HtmlTelInput input = (HtmlTelInput) page.getElementById("foo");
72  
73          input.type("4711");
74          input.reset();
75          input.type("0815");
76  
77          assertEquals("", input.getValueAttribute());
78          assertEquals("0815", input.getValue());
79      }
80  
81      /**
82       * @throws Exception if the test fails
83       */
84      @Test
85      public void typingAndSetValueAttribute() throws Exception {
86          final String htmlContent = DOCTYPE_HTML
87              + "<html>\n"
88              + "<head></head>\n"
89              + "<body>\n"
90              + "<form id='form1'>\n"
91              + "  <input type='tel' id='foo'>\n"
92              + "</form>\n"
93              + "</body></html>";
94  
95          final HtmlPage page = loadPage(htmlContent);
96  
97          final HtmlTelInput input = (HtmlTelInput) page.getElementById("foo");
98  
99          input.type("4711");
100         input.setValueAttribute("");
101         input.type("0815");
102 
103         assertEquals("", input.getValueAttribute());
104         assertEquals("47110815", input.getValue());
105     }
106 
107     /**
108      * @throws Exception if the test fails
109      */
110     @Test
111     public void typingAndSetValue() throws Exception {
112         final String htmlContent = DOCTYPE_HTML
113             + "<html>\n"
114             + "<head></head>\n"
115             + "<body>\n"
116             + "<form id='form1'>\n"
117             + "  <input type='tel' id='foo'>\n"
118             + "</form>\n"
119             + "</body></html>";
120 
121         final HtmlPage page = loadPage(htmlContent);
122 
123         final HtmlTelInput input = (HtmlTelInput) page.getElementById("foo");
124 
125         input.type("4711");
126         input.setValue("");
127         input.type("0815");
128 
129         assertEquals("", input.getValueAttribute());
130         assertEquals("0815", input.getValue());
131     }
132 
133     /**
134      * @throws Exception if the test fails
135      */
136     @Test
137     public void patternValidation() throws Exception {
138         final String htmlContent = DOCTYPE_HTML
139             + "<html>\n"
140             + "<head></head>\n"
141             + "<body>\n"
142             + "<form id='form1'>\n"
143             + "  <input type='tel' pattern='[0-9]{3}-[0-9]{3}-[0-9]{4}' id='foo'>\n"
144             + "</form>\n"
145             + "</body></html>";
146 
147         final HtmlPage page = loadPage(htmlContent);
148 
149         final HtmlTelInput input = (HtmlTelInput) page.getElementById("foo");
150 
151         // empty
152         assertTrue(input.isValid());
153         // invalid
154         input.setValue("123-456-78901");
155         assertFalse(input.isValid());
156         // valid
157         input.setValue("123-456-7890");
158         assertTrue(input.isValid());
159     }
160 
161     /**
162      * @throws Exception
163      *         if the test fails
164      */
165     @Test
166     @Alerts({"true", "true", "true", "", "12345"})
167     public void maxLengthValidation() throws Exception {
168         final String htmlContent = DOCTYPE_HTML
169             + "<html>\n"
170             + "<head></head>\n"
171             + "<body>\n"
172             + "<form id='form1'>\n"
173             + "  <input type='tel' id='foo' maxLength='5'>\n"
174             + "</form>\n"
175             + "</body></html>";
176 
177         final HtmlPage page = loadPage(htmlContent);
178 
179         final HtmlInput input = (HtmlInput) page.getElementById("foo");
180         assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
181         input.type("12345");
182         assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
183         input.type("67890");
184         assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
185         assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
186         assertEquals(getExpectedAlerts()[4], input.getValue());
187     }
188 
189     /**
190      * @throws Exception
191      *         if the test fails
192      */
193     @Test
194     @Alerts({"true", "false", "true", "", "1234567890"})
195     public void minLengthValidation() throws Exception {
196         final String htmlContent = DOCTYPE_HTML
197             + "<html>\n"
198             + "<head></head>\n"
199             + "<body>\n"
200             + "<form id='form1'>\n"
201             + "  <input type='text' id='foo' minLength='5'>\n"
202             + "</form>\n"
203             + "</body></html>";
204 
205         final HtmlPage page = loadPage(htmlContent);
206 
207         final HtmlInput input = (HtmlInput) page.getElementById("foo");
208         assertEquals(getExpectedAlerts()[0], Boolean.toString(input.isValid()));
209         input.type("1234");
210         assertEquals(getExpectedAlerts()[1], Boolean.toString(input.isValid()));
211         input.type("567890");
212         assertEquals(getExpectedAlerts()[2], Boolean.toString(input.isValid()));
213         assertEquals(getExpectedAlerts()[3], input.getValueAttribute());
214         assertEquals(getExpectedAlerts()[4], input.getValue());
215     }
216 }