1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.io.InputStream;
18 import java.net.URL;
19 import java.util.Collections;
20
21 import org.apache.commons.io.IOUtils;
22 import org.htmlunit.HttpMethod;
23 import org.htmlunit.MockWebConnection;
24 import org.htmlunit.WebDriverTestCase;
25 import org.htmlunit.junit.annotation.Alerts;
26 import org.htmlunit.junit.annotation.BuggyWebDriver;
27 import org.junit.jupiter.api.Test;
28 import org.openqa.selenium.By;
29 import org.openqa.selenium.WebDriver;
30 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
31
32
33
34
35
36
37
38
39 public class HtmlMapTest extends WebDriverTestCase {
40
41
42
43
44 @Test
45 @Alerts("[object HTMLMapElement]")
46 public void simpleScriptable() throws Exception {
47 final String html = DOCTYPE_HTML
48 + "<html><head>\n"
49 + "<script>\n"
50 + LOG_TITLE_FUNCTION
51 + " function test() {\n"
52 + " log(document.getElementById('myId'));\n"
53 + " }\n"
54 + "</script>\n"
55 + "</head><body onload='test()'>\n"
56 + " <map id='myId'/>\n"
57 + "</body></html>";
58
59 final WebDriver driver = loadPageVerifyTitle2(html);
60 if (driver instanceof HtmlUnitDriver) {
61 final HtmlPage page = (HtmlPage) getEnclosedPage();
62 assertTrue(HtmlMap.class.isInstance(page.getHtmlElementById("myId")));
63 }
64 }
65
66
67
68
69 @Test
70 @Alerts("§§URL§§a.html")
71 @BuggyWebDriver(FF = "Element <area id=\"tester\" href=\"a.html\"> could not be scrolled into view",
72 FF_ESR = "Element <area id=\"tester\" href=\"a.html\"> could not be scrolled into view")
73 public void mapClick() throws Exception {
74 final URL urlImage = new URL(URL_FIRST, "img.jpg");
75 try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/4x7.jpg")) {
76 final byte[] directBytes = IOUtils.toByteArray(is);
77 getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", Collections.emptyList());
78 }
79
80 final String html = DOCTYPE_HTML
81 + "<html>\n"
82 + "<head></head>"
83 + "<body>\n"
84 + " <img id='myImg' src='" + urlImage + "' usemap='#map1'>\n"
85 + " <map name='map1'>\n"
86 + " <area id='tester' href='a.html' shape='rect' coords='0,0,4,4'>\n"
87 + " </map>\n"
88 + "</body></html>";
89
90 final String secondContent = DOCTYPE_HTML
91 + "<html><head><title>Second</title></head><body></body></html>";
92
93 final MockWebConnection webConnection = getMockWebConnection();
94 webConnection.setDefaultResponse(secondContent);
95
96
97 final WebDriver driver = loadPage2(html);
98 expandExpectedAlertsVariables(URL_FIRST);
99
100 try {
101 driver.findElement(By.id("tester")).click();
102
103 assertEquals(getExpectedAlerts()[0], driver.getCurrentUrl());
104 assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
105 }
106 catch (final Exception e) {
107 assertEquals(getExpectedAlerts()[0], e.getMessage().substring(0, getExpectedAlerts()[0].length()));
108 }
109 }
110
111
112
113
114 @Test
115 public void isDisplayed() throws Exception {
116 final String html = DOCTYPE_HTML
117 + "<html><head><title>Page A</title></head>\n"
118 + "<body>\n"
119 + " <img id='myImg' usemap='#imgmap'"
120 + " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
121 + "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
122 + " <map id='myMap' name='imgmap'>\n"
123 + " <area id='myArea' shape='rect' coords='0,0,1,1'>\n"
124 + " </map>\n"
125 + "</body></html>";
126
127 final WebDriver driver = loadPage2(html);
128
129 boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
130 assertTrue(displayed);
131
132 displayed = driver.findElement(By.id("myMap")).isDisplayed();
133 assertTrue(displayed);
134
135 displayed = driver.findElement(By.id("myArea")).isDisplayed();
136 assertTrue(displayed);
137 }
138
139
140
141
142 @Test
143 public void isDisplayedHiddenImage() throws Exception {
144 final String html = DOCTYPE_HTML
145 + "<html><head><title>Page A</title></head>\n"
146 + "<body>\n"
147 + " <img id='myImg' usemap='#imgmap' style='display: none'"
148 + " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
149 + "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
150 + " <map id='myMap' name='imgmap'>\n"
151 + " <area id='myArea' shape='rect' coords='0,0,1,1'>\n"
152 + " </map>\n"
153 + "</body></html>";
154
155 final WebDriver driver = loadPage2(html);
156
157 boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
158 assertFalse(displayed);
159
160 displayed = driver.findElement(By.id("myMap")).isDisplayed();
161 assertFalse(displayed);
162 }
163
164
165
166
167 @Test
168 public void isDisplayedHiddenMap() throws Exception {
169 final String html = DOCTYPE_HTML
170 + "<html><head><title>Page A</title></head>\n"
171 + "<body>\n"
172 + " <img id='myImg' usemap='#imgmap'"
173 + " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
174 + "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
175 + " <map id='myMap' name='imgmap' style='display: none'>\n"
176 + " <area id='myArea' shape='rect' coords='0,0,1,1'>\n"
177 + " </map>\n"
178 + "</body></html>";
179
180 final WebDriver driver = loadPage2(html);
181
182 boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
183 assertTrue(displayed);
184
185 displayed = driver.findElement(By.id("myMap")).isDisplayed();
186 assertTrue(displayed);
187 }
188
189
190
191
192 @Test
193 public void isDisplayedMissingImage() throws Exception {
194 final String html = DOCTYPE_HTML
195 + "<html><head><title>Page A</title></head>\n"
196 + "<body>\n"
197 + " <map id='myMap' name='imgmap' style='display: none'>\n"
198 + " <area id='myArea' shape='rect' coords='0,0,1,1'>\n"
199 + " </map>\n"
200 + "</body></html>";
201
202 final WebDriver driver = loadPage2(html);
203
204 final boolean displayed = driver.findElement(By.id("myMap")).isDisplayed();
205 assertFalse(displayed);
206 }
207 }