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