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