View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.webp;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.File;
25  import java.io.IOException;
26  
27  import org.apache.commons.imaging.ImageInfo;
28  import org.apache.commons.imaging.Imaging;
29  import org.apache.commons.imaging.ImagingException;
30  import org.apache.commons.imaging.bytesource.ByteSource;
31  import org.apache.commons.imaging.common.ImageMetadata;
32  import org.apache.commons.imaging.formats.webp.chunks.WebPChunkIccp;
33  import org.apache.commons.imaging.internal.Debug;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  import org.junit.jupiter.params.ParameterizedTest;
37  import org.junit.jupiter.params.provider.MethodSource;
38  
39  /**
40   * Tests that read WebP images.
41   */
42  public class WebPReadTest extends WebPBaseTest {
43  
44      /**
45       * Not implemented yet.
46       *
47       * @throws IOException if it failed to read the image.
48       */
49      @Test
50      public void testBufferedImageNotSupported() throws IOException {
51          final File emptyWebP = new File(WebPReadTest.class.getResource("/images/webp/empty/empty-100x100.webp").getFile());
52          final WebPImageParser parser = new WebPImageParser();
53          final ImagingException exception = assertThrows(ImagingException.class, () -> {
54              parser.getBufferedImage(ByteSource.file(emptyWebP), parser.getDefaultParameters());
55          });
56          assertTrue(exception.getMessage().contains("Reading WebP files is currently not supported"));
57      }
58  
59      /**
60       * Basic features of the parser.
61       */
62      @Test
63      public void testParser() {
64          final WebPImageParser parser = new WebPImageParser();
65          assertEquals("WebP-Custom", parser.getName());
66          assertEquals("webp", parser.getDefaultExtension());
67      }
68  
69      /**
70       * @param imageFile parameterized test image.
71       * @throws Exception if it cannot open the images.
72       */
73      @ParameterizedTest
74      @MethodSource("images")
75      public void testRead(final File imageFile) throws Exception {
76          Debug.debug("start");
77  
78          Debug.debug("imageFile", imageFile);
79  
80          final ImageMetadata metadata = Imaging.getMetadata(imageFile);
81          Assertions.assertFalse(metadata instanceof File); // Dummy check to avoid unused warning (it may be null)
82  
83          final ImageInfo imageInfo = Imaging.getImageInfo(imageFile);
84          assertNotNull(imageInfo);
85  
86          Debug.debug("ICC profile", Imaging.getIccProfileBytes(imageFile));
87      }
88  
89      /**
90       * Test that the given size, and the byte array length match.
91       */
92      @Test
93      public void testWebPChunkInvalidSizeBytes() {
94          final ImagingException exception = assertThrows(ImagingException.class, () -> {
95              new WebPChunkIccp(0, 10, new byte[] {});
96          });
97          assertEquals("Chunk size must match bytes length", exception.getMessage());
98      }
99  }