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