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.HttpMethod;
18  import org.htmlunit.MockWebConnection;
19  import org.htmlunit.Page;
20  import org.htmlunit.SimpleWebTestCase;
21  import org.htmlunit.javascript.host.event.KeyboardEvent;
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Tests for {@link HtmlTextArea}.
26   *
27   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
28   * @author Marc Guillemot
29   * @author Ahmed Ashour
30   * @author Sudhan Moghe
31   * @author Amit Khanna
32   * @author Ronald Brill
33   */
34  public class HtmlTextAreaTest extends SimpleWebTestCase {
35  
36      /**
37       * @throws Exception if the test fails
38       */
39      @Test
40      public void formSubmission() throws Exception {
41          formSubmission("foo", "foo");
42          formSubmission("\r\nfoo\r\n", "foo%0D%0A");
43          formSubmission("\nfoo\n", "foo%0D%0A");
44          formSubmission("\r\n\r\nfoo\r\n", "%0D%0Afoo%0D%0A");
45      }
46  
47      private void formSubmission(final String textAreaText, final String expectedValue) throws Exception {
48          final String content = DOCTYPE_HTML
49              + "<html><head><title>foo</title></head><body>\n"
50              + "<form id='form1'>\n"
51              + "<textarea name='textArea1'>" + textAreaText + "</textarea>\n"
52              + "<input type='submit' id='mysubmit'/>\n"
53              + "</form></body></html>";
54          final HtmlPage page = loadPage(content);
55          final MockWebConnection webConnection = getMockConnection(page);
56          final HtmlForm form = page.getHtmlElementById("form1");
57  
58          final HtmlTextArea textArea = form.getTextAreaByName("textArea1");
59          assertNotNull(textArea);
60  
61          final Page secondPage = page.getHtmlElementById("mysubmit").click();
62  
63          assertEquals("url", URL_FIRST + "?textArea1=" + expectedValue, secondPage.getUrl());
64          assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
65      }
66  
67      /**
68       * @throws Exception if the test fails
69       */
70      @Test
71      public void formSubmission_NewValue() throws Exception {
72          final String content = DOCTYPE_HTML
73              + "<html><head><title>foo</title></head><body>\n"
74              + "<form id='form1'>\n"
75              + "<textarea name='textArea1'>foo</textarea>\n"
76              + "<input type='submit' id='mysubmit'/>\n"
77              + "</form></body></html>";
78          final HtmlPage page = loadPage(content);
79          final MockWebConnection webConnection = getMockConnection(page);
80          final HtmlForm form = page.getHtmlElementById("form1");
81  
82          final HtmlTextArea textArea = form.getTextAreaByName("textArea1");
83          textArea.setText("Flintstone");
84          final Page secondPage = page.getHtmlElementById("mysubmit").click();
85  
86          assertEquals("url", URL_FIRST + "?textArea1=Flintstone", secondPage.getUrl());
87          assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
88      }
89  
90      /**
91       * @throws Exception if the test fails
92       */
93      @Test
94      public void getText() throws Exception {
95          final String content = DOCTYPE_HTML
96              + "<html><head><title>foo</title></head><body>\n"
97              + "<form id='form1'>\n"
98              + "<textarea name='textArea1'> foo \n bar </textarea>\n"
99              + "</form></body></html>";
100         final HtmlPage page = loadPage(content);
101         final HtmlForm form = page.getHtmlElementById("form1");
102 
103         final HtmlTextArea textArea = form.getTextAreaByName("textArea1");
104         assertNotNull(textArea);
105         assertEquals("White space must be preserved!", " foo \n bar ", textArea.getText());
106         assertEquals(" foo \n bar ", textArea.getDefaultValue());
107     }
108 
109     /**
110      * @throws Exception if the test fails
111      */
112     @Test
113     public void asNormalizedText() throws Exception {
114         final String html = DOCTYPE_HTML
115             + "<html><head>\n"
116             + "</head>\n"
117             + "<body>\n"
118             + "<textarea id='tester'> foo \n bar\r\n test\r a "
119             + "<p>html snippet</p>\n"
120             + "</textarea>\n"
121             + "</body></html>";
122 
123         final HtmlPage page = loadPage(html);
124         final HtmlElement node = page.getHtmlElementById("tester");
125         final String expectedText = " foo \n bar\n test\n a <p>html snippet</p>";
126 
127         assertEquals(expectedText, node.asNormalizedText());
128         assertEquals(expectedText, page.asNormalizedText());
129     }
130 
131     /**
132      * @throws Exception if the test fails
133      */
134     @Test
135     public void asXml() throws Exception {
136         final String content = DOCTYPE_HTML
137             + "<html><head><title>foo</title></head><body>\n"
138             + "<form id='form1'>\n"
139             + "<textarea name='textArea1'> foo \n bar </textarea>\n"
140             + "<textarea name='textArea2'></textarea>\n"
141             + "<textarea name='textArea3'>1 &lt; 2 &amp; 3 &gt; 2</textarea>\n"
142             + "</form></body></html>";
143         final HtmlPage page = loadPage(content);
144         final HtmlForm form = page.getHtmlElementById("form1");
145 
146         final HtmlTextArea textArea1 = form.getTextAreaByName("textArea1");
147         final String expected1 = "<textarea name=\"textArea1\"> foo \n bar </textarea>";
148         assertEquals(expected1, textArea1.asXml());
149         assertTrue(form.asXml(), form.asXml().contains(expected1));
150 
151         final HtmlTextArea textArea2 = form.getTextAreaByName("textArea2");
152         assertEquals("<textarea name=\"textArea2\"></textarea>", textArea2.asXml());
153 
154         final HtmlTextArea textArea3 = form.getTextAreaByName("textArea3");
155         assertEquals("<textarea name=\"textArea3\">1 &lt; 2 &amp; 3 &gt; 2</textarea>", textArea3.asXml());
156     }
157 
158     /**
159      * @throws Exception if an error occurs
160      */
161     @Test
162     public void preventDefault() throws Exception {
163         final String html = DOCTYPE_HTML
164             + "<html><head><script>\n"
165             + "  function handler(e) {\n"
166             + "    if (e && e.target.value.length > 2)\n"
167             + "      e.preventDefault();\n"
168             + "    else if (!e && window.event.srcElement.value.length > 2)\n"
169             + "      return false;\n"
170             + "  }\n"
171             + "  function init() {\n"
172             + "    document.getElementById('text1').onkeydown = handler;\n"
173             + "  }\n"
174             + "</script></head>\n"
175             + "<body onload='init()'>\n"
176             + "<textarea id='text1'></textarea>\n"
177             + "</body></html>";
178 
179         final HtmlPage page = loadPage(html);
180         final HtmlTextArea text1 = page.getHtmlElementById("text1");
181         text1.type("abcd");
182         assertEquals("abc", text1.getText());
183     }
184 
185     /**
186      * @throws Exception if the test fails
187      */
188     @Test
189     public void type() throws Exception {
190         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
191         final HtmlPage page = loadPage(html, null);
192         final HtmlTextArea t = page.getHtmlElementById("t");
193         t.type("abc");
194         assertEquals("abc", t.getText());
195         t.type('\b');
196         assertEquals("ab", t.getText());
197         t.type('\b');
198         assertEquals("a", t.getText());
199         t.type('\b');
200         assertEquals("", t.getText());
201         t.type('\b');
202         assertEquals("", t.getText());
203         t.type("ab\ncd");
204         assertEquals("ab\ncd", t.getText());
205         t.type("\r\nef");
206         assertEquals("ab\ncd\r\nef", t.getText());
207     }
208 
209     /**
210      * @throws Exception if the test fails
211      */
212     @Test
213     public void typeWithSelection() throws Exception {
214         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
215         final HtmlPage page = loadPage(html, null);
216         final HtmlTextArea t = page.getHtmlElementById("t");
217         t.type("abc");
218         assertEquals("abc", t.getText());
219 
220         t.setSelectionStart(1);
221         t.setSelectionEnd(3);
222         t.type('x');
223         assertEquals("ax", t.getText());
224 
225         t.setSelectionStart(0);
226         t.setSelectionEnd(0);
227         t.type('y');
228         assertEquals("yax", t.getText());
229     }
230 
231     /**
232      * @throws Exception if the test fails
233      */
234     @Test
235     public void typeWhileDisabled() throws Exception {
236         final String html = DOCTYPE_HTML + "<html><body><textarea id='t' disabled='disabled'></textarea></body></html>";
237         final HtmlPage page = loadPage(html);
238         final HtmlTextArea t = page.getHtmlElementById("t");
239         t.type("abc");
240         assertEquals("", t.getText());
241     }
242 
243     /**
244      * Style=visibility:hidden should not affect getText().
245      * @throws Exception if the test fails
246      */
247     @Test
248     public void getTextAndVisibility() throws Exception {
249         final String content = DOCTYPE_HTML
250             + "<html><head><title>foo</title></head><body>\n"
251             + "<form id='form1'>\n"
252             + "<textarea name='textArea1' style='visibility:hidden'> foo \n bar "
253             + "</textarea>\n"
254             + "</form></body></html>";
255         final HtmlPage page = loadPage(content);
256         final HtmlForm form = page.getHtmlElementById("form1");
257         final HtmlTextArea textArea = form.getTextAreaByName("textArea1");
258         assertNotNull(textArea);
259         assertEquals(" foo \n bar ", textArea.getText());
260     }
261 
262     /**
263      * @throws Exception if the test fails
264      */
265     @Test
266     public void typeLeftArrow() throws Exception {
267         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
268         final HtmlPage page = loadPage(html);
269         final HtmlTextArea t = page.getHtmlElementById("t");
270         t.type('t');
271         t.type('e');
272         t.type('t');
273         assertEquals("tet", t.getText());
274         t.type(KeyboardEvent.DOM_VK_LEFT);
275         assertEquals("tet", t.getText());
276         t.type('s');
277         assertEquals("test", t.getText());
278         t.type(KeyboardEvent.DOM_VK_SPACE);
279         assertEquals("tes t", t.getText());
280     }
281 
282     /**
283      * @throws Exception if the test fails
284      */
285     @Test
286     public void typeDelKey() throws Exception {
287         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
288         final HtmlPage page = loadPage(html);
289         final HtmlTextArea t = page.getHtmlElementById("t");
290         t.type('t');
291         t.type('e');
292         t.type('t');
293         assertEquals("tet", t.getText());
294         t.type(KeyboardEvent.DOM_VK_LEFT);
295         t.type(KeyboardEvent.DOM_VK_LEFT);
296         assertEquals("tet", t.getText());
297         t.type(KeyboardEvent.DOM_VK_DELETE);
298         assertEquals("tt", t.getText());
299     }
300 
301     /**
302      * Make sure removeFocus does not throw.
303      *
304      * @throws Exception if the test fails
305      */
306     @Test
307     public void removeFocus() throws Exception {
308         final String html = DOCTYPE_HTML + "<html><head></head><body><textarea id='t'></textarea></body></html>";
309         final HtmlPage page = loadPage(html, null);
310         final HtmlTextArea t = page.getHtmlElementById("t");
311         t.removeFocus();
312     }
313 
314     /**
315      * @throws Exception if the test fails
316      */
317     @Test
318     public void typingAndClone() throws Exception {
319         final String htmlContent = DOCTYPE_HTML
320             + "<html>\n"
321             + "<head></head>\n"
322             + "<body>\n"
323             + "<form id='form1'>\n"
324             + "  <textarea id='foo'></textarea>\n"
325             + "</form>\n"
326             + "</body></html>";
327 
328         final HtmlPage page = loadPage(htmlContent);
329 
330         HtmlTextArea input = (HtmlTextArea) page.getElementById("foo");
331         input = (HtmlTextArea) input.cloneNode(true);
332         input.type("4711");
333         assertEquals("4711", input.getTextContent());
334     }
335 
336     /**
337      * @throws Exception if the test fails
338      */
339     @Test
340     public void typingAndReset() throws Exception {
341         final String htmlContent = DOCTYPE_HTML
342             + "<html>\n"
343             + "<head></head>\n"
344             + "<body>\n"
345             + "<form id='form1'>\n"
346             + "  <textarea id='foo'></textarea>\n"
347             + "</form>\n"
348             + "</body></html>";
349 
350         final HtmlPage page = loadPage(htmlContent);
351 
352         final HtmlTextArea input = (HtmlTextArea) page.getElementById("foo");
353 
354         input.type("4711");
355         input.reset();
356         input.type("0815");
357 
358         assertEquals("0815", input.getTextContent());
359     }
360 
361     /**
362      * @throws Exception if the test fails
363      */
364     @Test
365     public void typingAndSetTextContent() throws Exception {
366         final String htmlContent = DOCTYPE_HTML
367             + "<html>\n"
368             + "<head></head>\n"
369             + "<body>\n"
370             + "<form id='form1'>\n"
371             + "  <textarea id='foo'></textarea>\n"
372             + "</form>\n"
373             + "</body></html>";
374 
375         final HtmlPage page = loadPage(htmlContent);
376 
377         final HtmlTextArea input = (HtmlTextArea) page.getElementById("foo");
378 
379         input.type("4711");
380         input.setTextContent("");
381         input.type("0815");
382 
383         assertEquals("0815", input.getTextContent());
384     }
385 }