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.javascript.host.html;
16  
17  import org.htmlunit.SimpleWebTestCase;
18  import org.htmlunit.html.HtmlPage;
19  import org.htmlunit.html.HtmlTextArea;
20  import org.htmlunit.junit.BrowserRunner;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Tests for {@link HTMLTextAreaElement}.
26   *
27   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
28   * @author Marc Guillemot
29   * @author Ahmed Ashour
30   * @author Daniel Gredler
31   */
32  @RunWith(BrowserRunner.class)
33  public class HTMLTextAreaElement2Test extends SimpleWebTestCase {
34  
35      /**
36       * Method type(...) should not trigger onchange!
37       * @throws Exception if the test fails
38       */
39      @Test
40      public void type_onchange() throws Exception {
41          final String content = DOCTYPE_HTML
42              + "<html><head><title>foo</title>\n"
43              + "<script>\n"
44              + "  function changed(e) {\n"
45              + "    log('changed: ' + e.value);\n"
46              + "  }\n"
47              + "  function keypressed(e) {\n"
48              + "    log('keypressed: ' + e.value);\n"
49              + "  }\n"
50              + "  function log(msg) {\n"
51              + "    document.getElementById('log').value += msg + '; ';\n"
52              + "  }\n"
53              + "</script></head>\n"
54              + "<body>\n"
55              + "<form id='form1'>\n"
56              + "<textarea id='textArea1' onchange='changed(this)' onkeypress='keypressed(this)'></textarea>\n"
57              + "<textarea id='log'></textarea>\n"
58              + "</form>\n"
59              + "</body></html>";
60          final HtmlPage page = loadPage(content);
61          final HtmlTextArea textArea = page.getHtmlElementById("textArea1");
62          textArea.type("hello");
63          page.setFocusedElement(null); // remove focus on textarea to trigger onchange
64  
65          final HtmlTextArea log = page.getHtmlElementById("log");
66          final String expectation = "keypressed: ; "
67              + "keypressed: h; "
68              + "keypressed: he; "
69              + "keypressed: hel; "
70              + "keypressed: hell; "
71              + "changed: hello;";
72          assertEquals(expectation, log.asNormalizedText());
73      }
74  
75  }