1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import org.htmlunit.SimpleWebTestCase;
18 import org.htmlunit.junit.BrowserRunner;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21
22
23
24
25
26
27
28
29 @RunWith(BrowserRunner.class)
30 public class FocusableElementTest extends SimpleWebTestCase {
31
32
33
34
35
36
37 @Test
38 public void onBlurWith2Pages() throws Exception {
39 final String html = DOCTYPE_HTML
40 + "<html>\n"
41 + "<head>\n"
42 + "<script>\n"
43 + " var bCalled = false;\n"
44 + " function testOnBlur() {\n"
45 + " if (bCalled)\n"
46 + " throw 'problem!'; // to get the error immediately rather than an infinite loop\n"
47 + " bCalled = true;\n"
48 + " document.getElementById('text2').focus();\n"
49 + " }\n"
50 + "</script>\n"
51 + "</head>\n"
52 + "<body onLoad='document.getElementById(\"text1\").focus()'>\n"
53 + " <input type='text' id='text1' onblur='testOnBlur()'>\n"
54 + " <input type='text' id='text2'>\n"
55 + " <a href='foo'>this page again</a>\n"
56 + "</body></html>";
57
58 final HtmlPage page = loadPage(html);
59 page.getAnchors().get(0).click();
60 }
61
62
63
64
65 @Test
66 public void focusin() throws Exception {
67 final String html = DOCTYPE_HTML
68 + "<html>\n"
69 + "<head>\n"
70 + "<script>\n"
71 + " function handler(_e) {\n"
72 + " var e = _e ? _e : window.event;\n"
73 + " var textarea = document.getElementById('myTextarea');\n"
74 + " textarea.value += e.type + ' ' + (e.target ? e.target : e.srcElement).id + ',';\n"
75 + " }\n"
76 + " function test() {\n"
77 + " document.getElementById('select1').onfocus = handler;\n"
78 + " document.getElementById('select1').onfocusin = handler;\n"
79 + " document.getElementById('select1').onfocusout = handler;\n"
80 + " document.getElementById('select1').onblur = handler;\n"
81 + " document.getElementById('select2').onfocus = handler;\n"
82 + " document.getElementById('select2').onfocusin = handler;\n"
83 + " document.getElementById('select2').onfocusout = handler;\n"
84 + " document.getElementById('select2').onblur = handler;\n"
85 + " }\n"
86 + "</script>\n"
87 + "</head>\n"
88 + "<body onload='test()'>\n"
89 + " <select id='select1'>\n"
90 + " <option>Austria</option><option>Belgium</option><option>Bulgaria</option>\n"
91 + " </select>\n"
92 + " <select id='select2'>\n"
93 + " <option>Austria</option><option>Belgium</option><option>Bulgaria</option>\n"
94 + " </select>\n"
95 + " <textarea id='myTextarea' cols='80' rows='20'></textarea>\n"
96 + "</body>\n"
97 + "</html>";
98
99 final HtmlPage page = loadPage(html);
100 final HtmlSelect select1 = (HtmlSelect) page.getElementById("select1");
101 final HtmlSelect select2 = (HtmlSelect) page.getElementById("select2");
102 final HtmlTextArea textArea = (HtmlTextArea) page.getElementById("myTextarea");
103 page.setFocusedElement(select1);
104 page.setFocusedElement(select2);
105 page.setFocusedElement(select1);
106 final String expectedString;
107 expectedString = "focus select1,blur select1,focus select2,blur select2,focus select1,";
108 assertEquals(expectedString, textArea.getText());
109 }
110 }