1
2
3
4
5
6
7
8
9
10
11
12
13
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
30
31
32
33
34
35
36 @RunWith(BrowserRunner.class)
37 public class HTMLInputElement2Test extends SimpleWebTestCase {
38
39
40
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
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 }