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