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.io.IOException;
18  import java.io.InputStream;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.commons.io.IOUtils;
25  import org.htmlunit.CollectingAlertHandler;
26  import org.htmlunit.MockWebConnection;
27  import org.htmlunit.SimpleWebTestCase;
28  import org.htmlunit.WebClient;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests for {@link HtmlArea}.
33   *
34   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
35   * @author David K. Taylor
36   * @author Ahmed Ashour
37   * @author Ronald Brill
38   */
39  public class HtmlArea2Test extends SimpleWebTestCase {
40  
41      private WebClient createWebClient(final String onClick) throws IOException {
42          final MockWebConnection webConnection = new MockWebConnection();
43  
44          final URL urlImage = new URL(URL_FIRST, "img.jpg");
45          try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
46              final byte[] directBytes = IOUtils.toByteArray(is);
47              webConnection.setResponse(urlImage, directBytes, 200, "ok", "image/jpg", Collections.emptyList());
48          }
49  
50          final String firstContent = DOCTYPE_HTML
51              + "<html><head><title>first</title></head><body>\n"
52              + "<img src='" + urlImage + "' width='145' height='126' usemap='#planetmap'>\n"
53              + "<map id='planetmap' name='planetmap'>\n"
54              + "<area shape='rect' onClick=\"" + onClick + "\" coords='0,0,82,126' id='second' "
55              + "href='" + URL_SECOND + "'>\n"
56              + "<area shape='circle' coords='90,58,3' id='third' href='" + URL_THIRD + "'>\n"
57              + "</map></body></html>";
58          final String secondContent = DOCTYPE_HTML + "<html><head><title>second</title></head><body></body></html>";
59          final String thirdContent = DOCTYPE_HTML + "<html><head><title>third</title></head><body></body></html>";
60  
61          final WebClient client = getWebClient();
62  
63          webConnection.setResponse(URL_FIRST, firstContent);
64          webConnection.setResponse(URL_SECOND, secondContent);
65          webConnection.setResponse(URL_THIRD, thirdContent);
66  
67          client.setWebConnection(webConnection);
68  
69          return client;
70      }
71  
72      /**
73       * @throws Exception if the test fails
74       */
75      @Test
76      public void click() throws Exception {
77          final WebClient client = createWebClient("");
78  
79          final HtmlPage page = client.getPage(URL_FIRST);
80          final HtmlArea area = page.getHtmlElementById("third");
81  
82          // Test that the correct value is being passed back up to the server
83          final HtmlPage thirdPage = area.click();
84          assertEquals("third", thirdPage.getTitleText());
85      }
86  
87      /**
88       * @throws Exception if the test fails
89       */
90      @Test
91      public void click_onclickReturnsFalse() throws Exception {
92          final WebClient client = createWebClient("alert('foo');return false;");
93          final List<String> collectedAlerts = new ArrayList<>();
94          client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
95  
96          final HtmlPage page = client.getPage(URL_FIRST);
97          final HtmlArea area = page.getHtmlElementById("second");
98  
99          final HtmlPage thirdPage = area.click();
100         assertEquals(new String[] {"foo"}, collectedAlerts);
101         assertEquals("first", thirdPage.getTitleText());
102     }
103 
104     /**
105      * @throws Exception if the test fails
106      */
107     @Test
108     public void click_onclickReturnsTrue() throws Exception {
109         final WebClient client = createWebClient("alert('foo');return true;");
110         final List<String> collectedAlerts = new ArrayList<>();
111         client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
112 
113         final HtmlPage page = client.getPage(URL_FIRST);
114         final HtmlArea area = page.getHtmlElementById("second");
115 
116         final HtmlPage thirdPage = area.click();
117         assertEquals(new String[] {"foo"}, collectedAlerts);
118         assertEquals("second", thirdPage.getTitleText());
119     }
120 
121     /**
122      * @throws Exception if the test fails
123      */
124     @Test
125     public void click_javascriptUrl_javascriptDisabled() throws Exception {
126         final String htmlContent = DOCTYPE_HTML
127             + "<html><head><title>foo</title></head><body><map>\n"
128             + "<area href='javascript:alert(\"clicked\")' id='a2' coords='0,0,10,10'/>\n"
129             + "</map></body></html>";
130         final WebClient client = getWebClient();
131         client.getOptions().setJavaScriptEnabled(false);
132 
133         final List<String> collectedAlerts = new ArrayList<>();
134         client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
135 
136         final MockWebConnection webConnection = new MockWebConnection();
137         webConnection.setDefaultResponse(htmlContent);
138         client.setWebConnection(webConnection);
139 
140         final HtmlPage page = client.getPage(URL_FIRST);
141         final HtmlArea area = page.getHtmlElementById("a2");
142 
143         assertEquals(Collections.EMPTY_LIST, collectedAlerts);
144 
145         final HtmlPage secondPage = area.click();
146 
147         assertEquals(Collections.EMPTY_LIST, collectedAlerts);
148         assertSame(page, secondPage);
149     }
150 
151 }