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