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 java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.SimpleWebTestCase;
21  import org.htmlunit.junit.annotation.Alerts;
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Tests for {@link HtmlPage}.
26   *
27   * @author Ronald Brill
28   */
29  public class HtmlPageTest5 extends SimpleWebTestCase {
30      /**
31       * Test tabbing to the next element.
32       * @throws Exception if something goes wrong
33       */
34      @Test
35      @Alerts({"focus-0", "blur-0", "focus-1", "blur-1", "focus-2"})
36      public void tabNext() throws Exception {
37          final String html = DOCTYPE_HTML
38                  + "<html>\n"
39                  + "<head><title>First</title>\n"
40                  + "</head>\n"
41                  + "<body>\n"
42                  + "  <form name='form1' method='post' onsubmit='return false;'>\n"
43                  + "    <input type='submit' name='submit0' id='submit0' "
44                           + "tabindex='1' onblur='alert(\"blur-0\")' onfocus='alert(\"focus-0\")'>\n"
45                  + "    <input type='submit' name='submit1' id='submit1' "
46                           + "tabindex='2' onblur='alert(\"blur-1\")' onfocus='alert(\"focus-1\")'>\n"
47                  + "    <input type='submit' name='submit2' id='submit2' "
48                           + "tabindex='3' onblur='alert(\"blur-2\")' onfocus='alert(\"focus-2\")'>\n"
49                  + "    <div id='div1'>foo</div>\n"
50                  + "  </form>\n"
51                  + "</body></html>";
52  
53          final List<String> collectedAlerts = new ArrayList<>();
54          final HtmlPage page = loadPage(html, collectedAlerts);
55  
56          assertEquals("submit0", page.tabToNextElement().getAttribute("name"));
57          assertEquals("submit1", page.tabToNextElement().getAttribute("name"));
58          assertEquals("submit2", page.tabToNextElement().getAttribute("name"));
59  
60          assertEquals(getExpectedAlerts(), collectedAlerts);
61      }
62  
63      /**
64       * Test tabbing to the previous element.
65       * @throws Exception if something goes wrong
66       */
67      @Test
68      @Alerts({"focus-2", "blur-2", "focus-1", "blur-1", "focus-0"})
69      public void tabPrevious() throws Exception {
70          final String html = DOCTYPE_HTML
71                  + "<html>\n"
72                  + "<head><title>First</title>\n"
73                  + "</head>\n"
74                  + "<body>\n"
75                  + "  <form name='form1' method='post' onsubmit='return false;'>\n"
76                  + "    <input type='submit' name='submit0' id='submit0' "
77                           + "tabindex='1' onblur='alert(\"blur-0\")' onfocus='alert(\"focus-0\")'>\n"
78                  + "    <input type='submit' name='submit1' id='submit1' "
79                           + "tabindex='2' onblur='alert(\"blur-1\")' onfocus='alert(\"focus-1\")'>\n"
80                  + "    <input type='submit' name='submit2' id='submit2' "
81                           + "tabindex='3' onblur='alert(\"blur-2\")' onfocus='alert(\"focus-2\")'>\n"
82                  + "    <div id='div1'>foo</div>\n"
83                  + "  </form>\n"
84                  + "</body></html>";
85  
86          final List<String> collectedAlerts = new ArrayList<>();
87          final HtmlPage page = loadPage(html, collectedAlerts);
88  
89          assertEquals("submit2", page.tabToPreviousElement().getAttribute("name"));
90          assertEquals("submit1", page.tabToPreviousElement().getAttribute("name"));
91          assertEquals("submit0", page.tabToPreviousElement().getAttribute("name"));
92  
93          assertEquals(getExpectedAlerts(), collectedAlerts);
94      }
95  
96      /**
97       * Test tabbing where there are no tabbable elements.
98       * @throws Exception if something goes wrong
99       */
100     @Test
101     public void keyboard_NoTabbableElements() throws Exception {
102         final String html = DOCTYPE_HTML
103                 + "<html>\n"
104                 + "<head><title>First</title>\n"
105                 + "</head>\n"
106                 + "<body>\n"
107                 + "  <form name='form1' method='post' onsubmit='return false;'>\n"
108                 + "    <div id='div1'>foo</div>\n"
109                 + "  </form>\n"
110                 + "</body>\n"
111                 + "</html>";
112 
113         final List<String> collectedAlerts = new ArrayList<>();
114         final HtmlPage page = loadPage(html, collectedAlerts);
115 
116         DomElement focus = page.getFocusedElement();
117         assertTrue("original", (focus == null)
118                 || (focus == page.getDocumentElement())
119                 || (focus == page.getBody()));
120 
121         focus = page.tabToPreviousElement();
122         assertNull("previous", focus);
123 
124         focus = page.tabToNextElement();
125         assertNull("next", focus);
126 
127         focus = page.pressAccessKey('a');
128         assertNull("accesskey", focus);
129 
130         final String[] expectedAlerts = {};
131         assertEquals(expectedAlerts, collectedAlerts);
132     }
133 
134     /**
135      * Test tabbing where there is only one tabbable element.
136      * @throws Exception if something goes wrong
137      */
138     @Test
139     @Alerts({"focus-0", "blur-0", "focus-0"})
140     public void keyboard_OneTabbableElement() throws Exception {
141         final String html = DOCTYPE_HTML
142                 + "<html>\n"
143                 + "<head><title>First</title>\n"
144                 + "</head>\n"
145                 + "<body>\n"
146                 + "  <form name='form1' method='post' onsubmit='return false;'>\n"
147                 + "    <input type='submit' name='submit0' id='submit0' "
148                             + "onblur='alert(\"blur-0\")' onfocus='alert(\"focus-0\")'>"
149                 + "    <div id='div1'>foo</div>\n"
150                 + "  </form>\n"
151                 + "</body>\n"
152                 + "</html>";
153 
154         final List<String> collectedAlerts = new ArrayList<>();
155         final HtmlPage page = loadPage(html, collectedAlerts);
156 
157         final HtmlElement element = page.getHtmlElementById("submit0");
158 
159         final DomElement focus = page.getFocusedElement();
160         assertTrue("original", (focus == null)
161                 || (focus == page.getDocumentElement())
162                 || (focus == page.getBody()));
163 
164         final DomElement accessKey = page.pressAccessKey('x');
165         assertEquals("accesskey", focus, accessKey);
166 
167         assertEquals("next", element, page.tabToNextElement());
168         assertEquals("nextAgain", element, page.tabToNextElement());
169 
170         page.getFocusedElement().blur();
171         assertNull("original", page.getFocusedElement());
172 
173         assertEquals("previous", element, page.tabToPreviousElement());
174         assertEquals("previousAgain", element, page.tabToPreviousElement());
175 
176         assertEquals("accesskey", element, page.pressAccessKey('z'));
177 
178         assertEquals(getExpectedAlerts(), collectedAlerts);
179     }
180 
181     /**
182      * Test pressing an accesskey.
183      * @throws Exception if something goes wrong
184      */
185     @Test
186     @Alerts({"focus-0", "blur-0", "focus-2", "blur-2", "focus-1"})
187     public void accessKeys() throws Exception {
188         final String html = DOCTYPE_HTML
189                 + "<html>\n"
190                 + "<head><title>First</title>\n"
191                 + "</head>\n"
192                 + "<body>\n"
193                 + "  <form name='form1' method='post' onsubmit='return false;'>\n"
194                 + "    <input type='submit' name='submit0' id='submit0' "
195                          + "accesskey='a' onblur='alert(\"blur-0\")' onfocus='alert(\"focus-0\")'>\n"
196                 + "    <input type='submit' name='submit1' id='submit1' "
197                          + "accesskey='b' onblur='alert(\"blur-1\")' onfocus='alert(\"focus-1\")'>\n"
198                 + "    <input type='submit' name='submit2' id='submit2' "
199                          + "accesskey='c' onblur='alert(\"blur-2\")' onfocus='alert(\"focus-2\")'>\n"
200                 + "    <div id='div1'>foo</div>\n"
201                 + "  </form>\n"
202                 + "</body></html>";
203 
204         final List<String> collectedAlerts = new ArrayList<>();
205         final HtmlPage page = loadPage(html, collectedAlerts);
206 
207         assertEquals("submit0", page.pressAccessKey('a').getAttribute("name"));
208         assertEquals("submit2", page.pressAccessKey('c').getAttribute("name"));
209         assertEquals("submit1", page.pressAccessKey('b').getAttribute("name"));
210 
211         assertEquals(getExpectedAlerts(), collectedAlerts);
212     }
213 
214     /**
215      * Test that a button can be selected via accesskey.
216      * @throws Exception if something goes wrong
217      */
218     @Test
219     @Alerts("buttonPushed")
220     public void pressAccessKey_Button() throws Exception {
221         final String html = DOCTYPE_HTML
222                 + "<html>\n"
223                 + "<head><title>First</title>\n"
224                 + "</head>\n"
225                 + "<body>\n"
226                 + "  <button name='button1' id='button1' "
227                         + "accesskey='1' disabled onclick='alert(\"buttonPushed\")'>foo</button>\n"
228                 + "</body></html>";
229 
230         final List<String> collectedAlerts = new ArrayList<>();
231         final HtmlPage page = loadPage(html, collectedAlerts);
232 
233         final HtmlElement button = page.getHtmlElementById("button1");
234         page.pressAccessKey('1');
235         assertEquals(new String[0], collectedAlerts);
236 
237         button.removeAttribute("disabled");
238         page.pressAccessKey('1');
239         assertEquals(getExpectedAlerts(), collectedAlerts);
240     }
241 }