1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.io.File;
18 import java.io.InputStream;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.io.IOUtils;
27 import org.htmlunit.MockWebConnection;
28 import org.htmlunit.SimpleWebTestCase;
29 import org.htmlunit.junit.BrowserRunner;
30 import org.htmlunit.junit.annotation.Alerts;
31 import org.htmlunit.util.NameValuePair;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34
35
36
37
38
39
40
41
42
43 @RunWith(BrowserRunner.class)
44 public class HtmlImageInput2Test extends SimpleWebTestCase {
45
46
47
48
49 @Test
50 @Alerts({"button.x#100", "button.y#200"})
51 public void click_WithPosition() throws Exception {
52 final String html = DOCTYPE_HTML
53 + "<html><head><title>foo</title></head><body>\n"
54 + "<form id='form1' method='post'>\n"
55 + "<input type='image' name='aButton' value='foo'/>\n"
56 + "<input type='image' name='button' value='foo'/>\n"
57 + "<input type='image' name='anotherButton' value='foo'/>\n"
58 + "</form></body></html>";
59 final HtmlPage page = loadPage(html);
60 final MockWebConnection webConnection = getMockConnection(page);
61
62 final HtmlForm form = page.getHtmlElementById("form1");
63
64 final HtmlImageInput imageInput = form.getInputByName("button");
65 final HtmlPage secondPage = imageInput.click(100, 200);
66 assertNotNull(secondPage);
67
68 final List<String> params = new ArrayList<>();
69 for (final NameValuePair nameValuePair : webConnection.getLastParameters()) {
70 params.add(nameValuePair.getName() + "#" + nameValuePair.getValue());
71 }
72 assertEquals(getExpectedAlerts(), params);
73 }
74
75
76
77
78
79
80 @Test
81 public void noNameClick_WithPosition() throws Exception {
82 final String html = DOCTYPE_HTML
83 + "<html><head><title>foo</title></head><body>\n"
84 + "<form id='form1' method='post'>\n"
85 + "<input type='image' value='foo'/>\n"
86 + "</form></body></html>";
87 final HtmlPage page = loadPageWithAlerts(html);
88 final MockWebConnection webConnection = getMockConnection(page);
89
90 final HtmlForm form = page.getHtmlElementById("form1");
91
92 final HtmlImageInput imageInput = form.getInputByValue("foo");
93 final HtmlPage secondPage = imageInput.click(100, 200);
94 assertNotNull(secondPage);
95
96 final List<NameValuePair> expectedPairs = Arrays.asList(new NameValuePair[]{
97 new NameValuePair("x", "100"),
98 new NameValuePair("y", "200")
99 });
100
101 assertEquals(expectedPairs, webConnection.getLastParameters());
102 }
103
104
105
106
107 @Test
108 public void saveAs() throws Exception {
109 try (InputStream is = getClass().getClassLoader().
110 getResourceAsStream("testfiles/tiny-jpg.img")) {
111 final byte[] directBytes = IOUtils.toByteArray(is);
112 final URL urlImage = new URL(URL_FIRST, "img.jpg");
113 getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", Collections.emptyList());
114 }
115
116 final String html = DOCTYPE_HTML
117 + "<html><head>\n"
118 + "</head>\n"
119 + "<body>\n"
120 + " <input type='image' src='img.jpg' >\n"
121 + "</body></html>";
122
123 final HtmlPage page = loadPage(html);
124
125 final HtmlImageInput input = page.querySelector("input");
126 final File tempFile = File.createTempFile("img", ".tmp");
127 input.saveAs(tempFile);
128 FileUtils.deleteQuietly(tempFile);
129 }
130
131
132
133
134 @Test
135 @Alerts({"4x7.jpg", "§§URL§§4x7.jpg"})
136 public void src() throws Exception {
137 final String html = DOCTYPE_HTML
138 + "<html>\n"
139 + "<head></head>\n"
140 + "<body>\n"
141 + " <input type='image' id='myImage' src='4x7.jpg' >\n"
142 + "</body></html>";
143
144 final HtmlPage page = loadPage(html);
145 final HtmlImageInput img = page.getHtmlElementById("myImage");
146
147 expandExpectedAlertsVariables(URL_FIRST);
148 assertEquals(getExpectedAlerts()[0], img.getSrcAttribute());
149 assertEquals(getExpectedAlerts()[1], img.getSrc());
150 }
151 }