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