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