1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import static org.junit.Assert.fail;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.ObjectOutputStream;
23 import java.net.URL;
24
25 import org.apache.commons.io.IOUtils;
26 import org.htmlunit.HttpHeader;
27 import org.htmlunit.WebClient;
28 import org.htmlunit.WebResponse;
29 import org.htmlunit.WebServerTestCase;
30 import org.htmlunit.junit.BrowserRunner;
31 import org.htmlunit.platform.image.ImageData;
32 import org.htmlunit.platform.image.ImageIOImageData;
33 import org.junit.After;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36
37
38
39
40
41
42
43
44
45 @RunWith(BrowserRunner.class)
46 public class HtmlImageDownloadTest extends WebServerTestCase {
47 private static final String BASE_FILE_PATH = "src/test/resources/org/htmlunit/html";
48
49
50
51
52
53 public HtmlImageDownloadTest() throws Exception {
54 startWebServer(BASE_FILE_PATH);
55 }
56
57
58
59
60 @Test
61 public void imageHeight() throws Exception {
62 final HtmlImage htmlImage = getHtmlElementToTest("image1");
63 assertEquals("Image height", 612, htmlImage.getHeight());
64 }
65
66
67
68
69 @Test
70 public void imageWidth() throws Exception {
71 final HtmlImage htmlImage = getHtmlElementToTest("image1");
72 assertEquals("Image width", 879, htmlImage.getWidth());
73 }
74
75
76
77
78 @Test
79 public void imageFileSize() throws Exception {
80 final HtmlImage htmlImage = getHtmlElementToTest("image1");
81 try (InputStream in = htmlImage.getWebResponse(true).getContentAsStream()) {
82 assertEquals("Image filesize", 140144,
83 IOUtils.toByteArray(in).length);
84 }
85 }
86
87
88
89
90 @Test
91 public void getImageData() throws Exception {
92 final HtmlImage htmlImage = getHtmlElementToTest("image1");
93 assertNotNull("ImageData should not be null", htmlImage.getImageData());
94 assertNotNull("ImageReader should not be null", ((ImageIOImageData) htmlImage.getImageData()).getImageReader());
95 }
96
97
98
99
100 @Test
101 public void getImageDataNoneSupportedImage() throws Exception {
102 final HtmlImage htmlImage = getHtmlElementToTest("image1");
103 final String url = "/HtmlImageDownloadTest.html";
104 htmlImage.setAttribute("src", url);
105 try {
106 htmlImage.getImageData();
107 fail("it was not an image!");
108 }
109 catch (final IOException expected) {
110 }
111 }
112
113
114
115
116 @Test
117 public void getWebResponse() throws Exception {
118 final HtmlImage htmlImage = getHtmlElementToTest("image1");
119 final URL url = htmlImage.getPage().getUrl();
120
121 assertNull(htmlImage.getWebResponse(false));
122 final WebResponse resp = htmlImage.getWebResponse(true);
123 assertNotNull(resp);
124 assertEquals(url.toExternalForm(), resp.getWebRequest().getAdditionalHeaders().get(HttpHeader.REFERER));
125 }
126
127
128
129
130
131 @Test
132 public void redownloadOnSrcAttributeChanged() throws Exception {
133 final HtmlImage htmlImage = getHtmlElementToTest("image1");
134
135 final ImageData imageData = htmlImage.getImageData();
136
137 htmlImage.setAttribute("src", htmlImage.getSrcAttribute() + "#changed");
138
139 assertFalse("Src attribute changed but ImageData was not reloaded",
140 imageData.equals(htmlImage.getImageData()));
141 }
142
143
144
145
146
147
148
149 private HtmlImage getHtmlElementToTest(final String id) throws Exception {
150 final String url = URL_FIRST + "HtmlImageDownloadTest.html";
151 final WebClient client = getWebClient();
152 final HtmlPage page = client.getPage(url);
153 return (HtmlImage) page.getElementById(id);
154 }
155
156
157
158
159 @Override
160 @After
161 public void tearDown() throws Exception {
162 Thread.sleep(100);
163 super.tearDown();
164 }
165
166
167
168
169 @Test
170 public void serialize() throws Exception {
171 final HtmlImage htmlImage = getHtmlElementToTest("image1");
172 htmlImage.getImageData();
173
174 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
175 try (ObjectOutputStream out = new ObjectOutputStream(baos)) {
176 out.writeObject(htmlImage);
177 }
178 }
179 }
180
181 }