test/jdk/javax/imageio/plugins/gif/GIFCharCellDimensionTest.java
changeset 49306 fe898e8bd271
equal deleted inserted replaced
49305:2f2b37908f91 49306:fe898e8bd271
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6686236
       
    27  * @summary Checks whether GIF native metadata format returns appropriate max
       
    28  *          value that could be set for characterCellWidth and
       
    29  *          characterCellHeight attributes of PlainTextExtension node. Besides,
       
    30  *          the test also checks whether IIOInvalidTreeException is thrown when
       
    31  *          incorrect value is set on these two attributes.
       
    32  * @run main GIFCharCellDimensionTest
       
    33  */
       
    34 import javax.imageio.ImageIO;
       
    35 import javax.imageio.ImageWriter;
       
    36 import javax.imageio.ImageTypeSpecifier;
       
    37 import javax.imageio.metadata.IIOInvalidTreeException;
       
    38 import javax.imageio.metadata.IIOMetadata;
       
    39 import javax.imageio.metadata.IIOMetadataNode;
       
    40 import javax.imageio.metadata.IIOMetadataFormat;
       
    41 import java.util.Iterator;
       
    42 import java.awt.image.BufferedImage;
       
    43 import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR;
       
    44 
       
    45 public class GIFCharCellDimensionTest {
       
    46     // GIF image metadata and native metadata format.
       
    47     private static IIOMetadata imageMetadata = null;
       
    48     private static IIOMetadataFormat metadataFormat = null;
       
    49     private static String formatName = null;
       
    50 
       
    51     private static void initializeTest() {
       
    52         // Initialize the test case by querying GIF image writer for default
       
    53         // image metadata and native metadata format.
       
    54         ImageWriter gifWriter = null;
       
    55         Iterator<ImageWriter> iterGifWriter = null;
       
    56         BufferedImage bufImage = null;
       
    57         ImageTypeSpecifier imageType = null;
       
    58 
       
    59         try {
       
    60             iterGifWriter = ImageIO.getImageWritersBySuffix("GIF");
       
    61             if (iterGifWriter.hasNext()) {
       
    62                 gifWriter = iterGifWriter.next();
       
    63                 bufImage = new BufferedImage(32, 32, TYPE_4BYTE_ABGR);
       
    64 
       
    65                 // Get GIF image metadata
       
    66                 imageMetadata = gifWriter.getDefaultImageMetadata(
       
    67                         ImageTypeSpecifier.createFromRenderedImage(bufImage),
       
    68                         gifWriter.getDefaultWriteParam());
       
    69                 if (imageMetadata == null) {
       
    70                     reportException("Test Failed. Could not get image" +
       
    71                             " metadata.");
       
    72                 }
       
    73 
       
    74                 // Get GIF native metadata format.
       
    75                 formatName = imageMetadata.getNativeMetadataFormatName();
       
    76                 metadataFormat = imageMetadata.getMetadataFormat(formatName);
       
    77                 if (metadataFormat == null) {
       
    78                     reportException("Test Failed. Could not get native" +
       
    79                             " metadata format.");
       
    80                 }
       
    81             } else {
       
    82                 reportException("Test Failed. No GIF image writer found.");
       
    83             }
       
    84         } finally {
       
    85             gifWriter.dispose();
       
    86         }
       
    87     }
       
    88 
       
    89     private static IIOMetadataNode createPlainTextExtensionNode(String value) {
       
    90         // Create a PlainTextExtensionNode with required values
       
    91         IIOMetadataNode rootNode = null;
       
    92 
       
    93         if (imageMetadata != null && formatName != null) {
       
    94             IIOMetadataNode plainTextNode = null;
       
    95 
       
    96             rootNode = new IIOMetadataNode(formatName);
       
    97             plainTextNode = new IIOMetadataNode("PlainTextExtension");
       
    98             plainTextNode.setAttribute("textGridLeft", "0");
       
    99             plainTextNode.setAttribute("textGridTop", "0");
       
   100             plainTextNode.setAttribute("textGridWidth", "32");
       
   101             plainTextNode.setAttribute("textGridHeight", "32");
       
   102             // Set required values for cell width and cell height
       
   103             plainTextNode.setAttribute("characterCellWidth", value);
       
   104             plainTextNode.setAttribute("characterCellHeight", value);
       
   105             plainTextNode.setAttribute("textForegroundColor", "0");
       
   106             plainTextNode.setAttribute("textBackgroundColor", "1");
       
   107             rootNode.appendChild(plainTextNode);
       
   108         } else {
       
   109             reportException("Test Failed. Un-initialized image metadata.");
       
   110         }
       
   111 
       
   112         return rootNode;
       
   113     }
       
   114 
       
   115     private static void testCharacterCellDimensions() {
       
   116         if (imageMetadata != null && metadataFormat != null) {
       
   117             // Get max permissible value that could be set on characterCellWidth
       
   118             // and characterCellHeight attributes of plain text node.
       
   119             String cellWidth = metadataFormat.getAttributeMaxValue(
       
   120                     "PlainTextExtension",
       
   121                     "characterCellWidth");
       
   122             String cellHeight = metadataFormat.getAttributeMaxValue(
       
   123                     "PlainTextExtension",
       
   124                     "characterCellHeight");
       
   125 
       
   126             // Test fails if the max permissible value is larger than 255
       
   127             int maxCharCellWidth = Integer.parseInt(cellWidth);
       
   128             int maxCharCellHeight = Integer.parseInt(cellHeight);
       
   129             if (maxCharCellWidth > 255 || maxCharCellHeight > 255) {
       
   130                 reportException("Test Failed. Invalid max range for" +
       
   131                         " character cell width or character cell height.");
       
   132             }
       
   133 
       
   134             try {
       
   135                 // Merge a plain text node with incorrect value set on
       
   136                 // characterCellWidth and characterCellHeight attributes.
       
   137                 IIOMetadataNode root = createPlainTextExtensionNode("256");
       
   138                 imageMetadata.setFromTree(formatName, root);
       
   139             } catch (IIOInvalidTreeException exception) {
       
   140                 // No-op. Expected exception was thrown.
       
   141             }
       
   142         } else {
       
   143             reportException("Test Failed. Un-initialized image metadata or" +
       
   144                     " metadata format.");
       
   145         }
       
   146     }
       
   147 
       
   148     private static void reportException(String message) {
       
   149         // Report an exception with the required message.
       
   150         throw new RuntimeException(message);
       
   151     }
       
   152 
       
   153     public static void main(String[] args) {
       
   154         // Initialize the test
       
   155         initializeTest();
       
   156 
       
   157         // Test plain text extension node.
       
   158         testCharacterCellDimensions();
       
   159     }
       
   160 }