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.javascript.host.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link HTMLAreaElement}.
23   *
24   * @author Daniel Gredler
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   * @author Frank Danek
28   */
29  public class HTMLAreaElementTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if an error occurs
33       */
34      @Test
35      @Alerts({"", "A", "a", "A", "a8", "8Afoo", "8", "@"})
36      public void readWriteAccessKey() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html><body><map><area id='a1'/><area id='a2' accesskey='A'/></map><script>\n"
39              + LOG_TITLE_FUNCTION
40              + "var a1 = document.getElementById('a1'), a2 = document.getElementById('a2');\n"
41              + "log(a1.accessKey);\n"
42              + "log(a2.accessKey);\n"
43              + "a1.accessKey = 'a';\n"
44              + "a2.accessKey = 'A';\n"
45              + "log(a1.accessKey);\n"
46              + "log(a2.accessKey);\n"
47              + "a1.accessKey = 'a8';\n"
48              + "a2.accessKey = '8Afoo';\n"
49              + "log(a1.accessKey);\n"
50              + "log(a2.accessKey);\n"
51              + "a1.accessKey = '8';\n"
52              + "a2.accessKey = '@';\n"
53              + "log(a1.accessKey);\n"
54              + "log(a2.accessKey);\n"
55              + "</script></body></html>";
56          loadPageVerifyTitle2(html);
57      }
58  
59      /**
60       * @throws Exception if the test fails
61       */
62      @Test
63      @Alerts({"", "function HTMLAreaElement() { [native code] }"})
64      public void type() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html><head>\n"
67              + "<script>\n"
68              + LOG_TITLE_FUNCTION
69              + "  function test() {\n"
70              + "  var elem = document.getElementById('a1');\n"
71              + "    try {\n"
72              + "      log(elem);\n"
73              + "      log(HTMLAreaElement);\n"
74              + "    } catch(e) { logEx(e); }\n"
75              + "  }\n"
76              + "</script>\n"
77              + "</head>\n"
78              + "<body onload='test()'>\n"
79              + "  <map><area id='a1'/><area id='a2' accesskey='A'/></map>\n"
80              + "</body></html>";
81  
82          loadPageVerifyTitle2(html);
83      }
84  
85      /**
86       * @throws Exception if the test fails
87       */
88      @Test
89      @Alerts(DEFAULT = {"[object HTMLButtonElement]", "[object HTMLButtonElement]",
90                         "§§URL§§", "http://srv/htmlunit.org"},
91              FF = {"[object HTMLButtonElement]", "", "§§URL§§", "http://srv/htmlunit.org"},
92              FF_ESR = {"[object HTMLButtonElement]", "", "§§URL§§", "http://srv/htmlunit.org"})
93      public void focus() throws Exception {
94          final String html = DOCTYPE_HTML
95              + "<html>\n"
96              + "<head>\n"
97              + "  <script>\n"
98              + LOG_TITLE_FUNCTION
99              + "    function test() {\n"
100             + "      var testNode = document.getElementById('myButton');\n"
101             + "      testNode.focus();\n"
102             + "      log(document.activeElement);\n"
103 
104             + "      testNode = document.getElementById('a1');\n"
105             + "      testNode.focus();\n"
106             + "      log(document.activeElement);\n"
107 
108             + "      testNode = document.getElementById('a2');\n"
109             + "      testNode.focus();\n"
110             + "      log(document.activeElement);\n"
111 
112             + "      testNode = document.getElementById('a3');\n"
113             + "      testNode.focus();\n"
114             + "      log(document.activeElement);\n"
115             + "    }\n"
116             + "  </script>\n"
117             + "</head>\n"
118             + "<body onload='test()'>\n"
119             + "  <button id='myButton'>Press</button>\n"
120             + "  <img usemap='#dot'"
121                     + " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
122                     + "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
123             + "  <map name='dot'>\n"
124             + "    <area id='a1' shape='rect' coords='0,0,1,1'/>\n"
125             + "    <area id='a2' href='' shape='rect' coords='0,0,1,1'/>\n"
126             + "    <area id='a3' href='http://srv/htmlunit.org' shape='rect' coords='0,0,1,1'/>\n"
127             + "  <map>\n"
128             + "</body>\n"
129             + "</html>";
130 
131         expandExpectedAlertsVariables(URL_FIRST);
132         loadPageVerifyTitle2(html);
133     }
134 
135     /**
136      * @throws Exception if the test fails
137      */
138     @Test
139     @Alerts({"a1 clicked", "a2 clicked"})
140     public void click() throws Exception {
141         final String html = DOCTYPE_HTML
142             + "<html><head>\n"
143             + "<script>\n"
144             + LOG_TITLE_FUNCTION
145             + "  function test() {\n"
146             + "    document.getElementById('a1').click();\n"
147             + "    document.getElementById('a2').click();\n"
148             + "  }\n"
149             + "</script>\n"
150             + "</head>\n"
151             + "<body onload='test()'>\n"
152             + "  <img usemap='#dot'"
153                         + " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
154                         + "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
155             + "  <map name='dot'>\n"
156             + "    <area id='a1' shape='rect' coords='0,0,1,1' onclick='log(\"a1 clicked\");'/>\n"
157             + "    <area id='a2' shape='rect' coords='0 0 2 2' onclick='log(\"a2 clicked\");'/>\n"
158             + "  <map>\n"
159             + "</body></html>";
160 
161         loadPageVerifyTitle2(html);
162     }
163 
164     /**
165      * @throws Exception if an error occurs
166      */
167     @Test
168     @Alerts({"", "alternate help", "prefetch", "prefetch", "not supported", "notsupported"})
169     public void readWriteRel() throws Exception {
170         final String html = DOCTYPE_HTML
171             + "<html><body><map><area id='a1'/><area id='a2' rel='alternate help'/></map><script>\n"
172             + LOG_TITLE_FUNCTION
173             + "var a1 = document.getElementById('a1'), a2 = document.getElementById('a2');\n"
174 
175             + "log(a1.rel);\n"
176             + "log(a2.rel);\n"
177 
178             + "a1.rel = 'prefetch';\n"
179             + "a2.rel = 'prefetch';\n"
180             + "log(a1.rel);\n"
181             + "log(a2.rel);\n"
182 
183             + "a1.rel = 'not supported';\n"
184             + "a2.rel = 'notsupported';\n"
185             + "log(a1.rel);\n"
186             + "log(a2.rel);\n"
187 
188             + "</script></body></html>";
189         loadPageVerifyTitle2(html);
190     }
191 
192     /**
193      * @throws Exception if an error occurs
194      */
195     @Test
196     @Alerts({"0", "2", "alternate", "help"})
197     public void relList() throws Exception {
198         final String html = DOCTYPE_HTML
199             + "<html><body><map><area id='a1'/><area id='a2' rel='alternate help'/></map><script>\n"
200             + LOG_TITLE_FUNCTION
201             + "var a1 = document.getElementById('a1'), a2 = document.getElementById('a2');\n"
202 
203             + "try {\n"
204             + "  log(a1.relList.length);\n"
205             + "  log(a2.relList.length);\n"
206 
207             + "  for (var i = 0; i < a2.relList.length; i++) {\n"
208             + "    log(a2.relList[i]);\n"
209             + "  }\n"
210             + "} catch(e) { logEx(e); }\n"
211 
212             + "</script></body></html>";
213         loadPageVerifyTitle2(html);
214     }
215 }