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