View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for {@link HtmlImage}.
39   *
40   * @author Knut Johannes Dahle
41   * @author Ahmed Ashour
42   * @author Marc Guillemot
43   * @author Lukas Botsch
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       * Constructor.
51       * @throws Exception if an exception occurs
52       */
53      public HtmlImageDownloadTest() throws Exception {
54          startWebServer(BASE_FILE_PATH);
55      }
56  
57      /**
58       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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      * @throws Exception if the test fails
115      */
116     @Test
117     public void getWebResponse() throws Exception {
118         final HtmlImage htmlImage = getHtmlElementToTest("image1");
119         final URL url = htmlImage.getPage().getUrl();
120         // per default images are not downloaded
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      * The image should be redownloaded when the src attribute changes.
129      * @throws Exception if the test fails
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      * Common code for the tests to load the test page and fetch the HtmlImage object.
145      * @param id value of image id attribute
146      * @return the found HtmlImage
147      * @throws Exception if an error occurs
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      * {@inheritDoc}
158      */
159     @Override
160     @After
161     public void tearDown() throws Exception {
162         Thread.sleep(100);
163         super.tearDown();
164     }
165 
166     /**
167      * @throws Exception if the test fails
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 }