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.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   * Tests for elements with onblur and onfocus attributes.
24   *
25   * @author David D. Kilzer
26   * @author Marc Guillemot
27   * @author Ahmed Ashour
28   */
29  @RunWith(BrowserRunner.class)
30  public class FocusableElementTest extends SimpleWebTestCase {
31  
32      /**
33       * Regression test for Bug #246.
34       *
35       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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 }