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 java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.SimpleWebTestCase;
21  import org.htmlunit.html.HtmlPage;
22  import org.htmlunit.html.HtmlTextInput;
23  import org.htmlunit.junit.BrowserRunner;
24  import org.htmlunit.junit.annotation.Alerts;
25  import org.junit.Test;
26  import org.junit.runner.RunWith;
27  
28  /**
29   * Tests for {@link HTMLInputElement} and buttons.
30   *
31   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
32   * @author Marc Guillemot
33   * @author Chris Erskine
34   * @author Ahmed Ashour
35   */
36  @RunWith(BrowserRunner.class)
37  public class HTMLInputElement2Test extends SimpleWebTestCase {
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts({"hello", "me te"})
44      public void selectionRange() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head><title>foo</title><script>\n"
47              + "function test() {\n"
48              + "  var input = document.getElementById('myInput');\n"
49              + "  input.setSelectionRange(2, 7);\n"
50              + "  alert('hello');\n"
51              + "}\n"
52              + "</script></head>\n"
53              + "<body onload='test()'>\n"
54              + "  <input id='myInput' value='some test'>\n"
55              + "</body></html>";
56  
57          final String[] expected = getExpectedAlerts();
58          setExpectedAlerts(new String[] {expected[0]});
59          final HtmlPage page = loadPageWithAlerts(html);
60          final HtmlTextInput input = page.getHtmlElementById("myInput");
61  
62          assertEquals(expected[1], input.getSelectedText());
63      }
64  
65      /**
66       * @throws Exception if an error occurs
67       */
68      @Test
69      @Alerts("initial")
70      public void focus() throws Exception {
71          final String html = DOCTYPE_HTML
72              + "<html><body>\n"
73              + "<iframe name='theFrame' src='" + URL_SECOND + "'></iframe>\n"
74              + "</body></html>";
75          final String frame = DOCTYPE_HTML
76              + "<html><body>\n"
77              + "<input id='input' value='initial' onfocus='alert(this.value)'>\n"
78              + "<div id='div'>click me</div>\n"
79              + "</body></html>";
80  
81          getMockWebConnection().setResponse(URL_SECOND, frame);
82  
83          final List<String> collectedAlerts = new ArrayList<>();
84          final HtmlPage page = loadPage(html, collectedAlerts);
85          final HtmlPage framePage = (HtmlPage) page.getFrames().get(0).getEnclosedPage();
86  
87          framePage.getHtmlElementById("input").type("foo");
88  
89          framePage.getHtmlElementById("div").click();
90          assertEquals(getExpectedAlerts(), collectedAlerts);
91      }
92  }