1
2
3
4
5
6
7
8
9
10
11
12
13
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
33
34
35
36
37
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
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
83 final HtmlPage thirdPage = area.click();
84 assertEquals("third", thirdPage.getTitleText());
85 }
86
87
88
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
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
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 }