Path: blob/master/test/jdk/javax/imageio/plugins/gif/GIFCharCellDimensionTest.java
41155 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 668623626* @summary Checks whether GIF native metadata format returns appropriate max27* value that could be set for characterCellWidth and28* characterCellHeight attributes of PlainTextExtension node. Besides,29* the test also checks whether IIOInvalidTreeException is thrown when30* incorrect value is set on these two attributes.31* @run main GIFCharCellDimensionTest32*/33import javax.imageio.ImageIO;34import javax.imageio.ImageWriter;35import javax.imageio.ImageTypeSpecifier;36import javax.imageio.metadata.IIOInvalidTreeException;37import javax.imageio.metadata.IIOMetadata;38import javax.imageio.metadata.IIOMetadataNode;39import javax.imageio.metadata.IIOMetadataFormat;40import java.util.Iterator;41import java.awt.image.BufferedImage;42import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR;4344public class GIFCharCellDimensionTest {45// GIF image metadata and native metadata format.46private static IIOMetadata imageMetadata = null;47private static IIOMetadataFormat metadataFormat = null;48private static String formatName = null;4950private static void initializeTest() {51// Initialize the test case by querying GIF image writer for default52// image metadata and native metadata format.53ImageWriter gifWriter = null;54Iterator<ImageWriter> iterGifWriter = null;55BufferedImage bufImage = null;56ImageTypeSpecifier imageType = null;5758try {59iterGifWriter = ImageIO.getImageWritersBySuffix("GIF");60if (iterGifWriter.hasNext()) {61gifWriter = iterGifWriter.next();62bufImage = new BufferedImage(32, 32, TYPE_4BYTE_ABGR);6364// Get GIF image metadata65imageMetadata = gifWriter.getDefaultImageMetadata(66ImageTypeSpecifier.createFromRenderedImage(bufImage),67gifWriter.getDefaultWriteParam());68if (imageMetadata == null) {69reportException("Test Failed. Could not get image" +70" metadata.");71}7273// Get GIF native metadata format.74formatName = imageMetadata.getNativeMetadataFormatName();75metadataFormat = imageMetadata.getMetadataFormat(formatName);76if (metadataFormat == null) {77reportException("Test Failed. Could not get native" +78" metadata format.");79}80} else {81reportException("Test Failed. No GIF image writer found.");82}83} finally {84gifWriter.dispose();85}86}8788private static IIOMetadataNode createPlainTextExtensionNode(String value) {89// Create a PlainTextExtensionNode with required values90IIOMetadataNode rootNode = null;9192if (imageMetadata != null && formatName != null) {93IIOMetadataNode plainTextNode = null;9495rootNode = new IIOMetadataNode(formatName);96plainTextNode = new IIOMetadataNode("PlainTextExtension");97plainTextNode.setAttribute("textGridLeft", "0");98plainTextNode.setAttribute("textGridTop", "0");99plainTextNode.setAttribute("textGridWidth", "32");100plainTextNode.setAttribute("textGridHeight", "32");101// Set required values for cell width and cell height102plainTextNode.setAttribute("characterCellWidth", value);103plainTextNode.setAttribute("characterCellHeight", value);104plainTextNode.setAttribute("textForegroundColor", "0");105plainTextNode.setAttribute("textBackgroundColor", "1");106rootNode.appendChild(plainTextNode);107} else {108reportException("Test Failed. Un-initialized image metadata.");109}110111return rootNode;112}113114private static void testCharacterCellDimensions() {115if (imageMetadata != null && metadataFormat != null) {116// Get max permissible value that could be set on characterCellWidth117// and characterCellHeight attributes of plain text node.118String cellWidth = metadataFormat.getAttributeMaxValue(119"PlainTextExtension",120"characterCellWidth");121String cellHeight = metadataFormat.getAttributeMaxValue(122"PlainTextExtension",123"characterCellHeight");124125// Test fails if the max permissible value is larger than 255126int maxCharCellWidth = Integer.parseInt(cellWidth);127int maxCharCellHeight = Integer.parseInt(cellHeight);128if (maxCharCellWidth > 255 || maxCharCellHeight > 255) {129reportException("Test Failed. Invalid max range for" +130" character cell width or character cell height.");131}132133try {134// Merge a plain text node with incorrect value set on135// characterCellWidth and characterCellHeight attributes.136IIOMetadataNode root = createPlainTextExtensionNode("256");137imageMetadata.setFromTree(formatName, root);138} catch (IIOInvalidTreeException exception) {139// No-op. Expected exception was thrown.140}141} else {142reportException("Test Failed. Un-initialized image metadata or" +143" metadata format.");144}145}146147private static void reportException(String message) {148// Report an exception with the required message.149throw new RuntimeException(message);150}151152public static void main(String[] args) {153// Initialize the test154initializeTest();155156// Test plain text extension node.157testCharacterCellDimensions();158}159}160161