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.junit.jupiter.api.Test;
19  
20  /**
21   * Tests for elements with onblur and onfocus attributes.
22   *
23   * @author David D. Kilzer
24   * @author Marc Guillemot
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  public class FocusableElementTest extends SimpleWebTestCase {
29  
30      /**
31       * Regression test for Bug #246.
32       *
33       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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 }