Path: blob/master/test/jdk/javax/imageio/ReadAbortTest.java
41144 views
/*1* Copyright (c) 2016, 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 492472726* @summary Test verifies that if we call ImageReader.abort() in27* IIOReadProgressListener.imageStarted() or28* IIOReadProgressListener.imageProgress() are we29* calling IIOReadProgressListener.readAborted() for all readers.30* @run main ReadAbortTest31*/32import java.awt.image.BufferedImage;33import java.io.File;34import java.util.Iterator;35import javax.imageio.ImageIO;36import javax.imageio.ImageReader;37import javax.imageio.event.IIOReadProgressListener;38import javax.imageio.stream.ImageInputStream;39import java.awt.Color;40import java.awt.Graphics2D;41import java.nio.file.Files;4243public class ReadAbortTest implements IIOReadProgressListener {4445ImageReader reader = null;46ImageInputStream iis = null;47BufferedImage bimg = null;48File file;49boolean startAbort = false;50boolean startAborted = false;51boolean progressAbort = false;52boolean progressAborted = false;53Color srccolor = Color.red;54int width = 100;55int heght = 100;5657public ReadAbortTest(String format) throws Exception {58try {59System.out.println("Test for format " + format);60bimg = new BufferedImage(width, heght,61BufferedImage.TYPE_INT_RGB);6263Graphics2D g = bimg.createGraphics();64g.setColor(srccolor);65g.fillRect(0, 0, width, heght);66g.dispose();6768file = File.createTempFile("src_", "." + format, new File("."));69ImageIO.write(bimg, format, file);70ImageInputStream iis = ImageIO.createImageInputStream(file);7172Iterator iter = ImageIO.getImageReaders(iis);73while (iter.hasNext()) {74reader = (ImageReader) iter.next();75break;76}77reader.setInput(iis);78reader.addIIOReadProgressListener(this);7980// Abort reading in IIOReadProgressListener.imageStarted().81startAbort = true;82bimg = reader.read(0);83startAbort = false;8485// Abort reading in IIOReadProgressListener.imageProgress().86progressAbort = true;87bimg = reader.read(0);88progressAbort = false;8990iis.close();91/*92* All abort requests from imageStarted,imageProgress and93* imageComplete from IIOReadProgressListener should be reached94* otherwise throw RuntimeException.95*/96if (!(startAborted97&& progressAborted)) {98throw new RuntimeException("All IIOReadProgressListener abort"99+ " requests are not processed for format "100+ format);101}102} catch (Exception e) {103throw e;104} finally {105Files.delete(file.toPath());106}107}108109/*110* Abstract methods that we need to implement from111* IIOReadProgressListener, and relevant for this test case.112*/113@Override114public void imageStarted(ImageReader source, int imageIndex) {115System.out.println("imageStarted called");116if (startAbort) {117source.abort();118}119}120121@Override122public void imageProgress(ImageReader source, float percentageDone) {123System.out.println("imageProgress called");124if (progressAbort) {125source.abort();126}127}128129@Override130public void readAborted(ImageReader source) {131System.out.println("readAborted called");132// Verify IIOReadProgressListener.imageStarted() abort request.133if (startAbort) {134System.out.println("imageStarted aborted ");135startAborted = true;136}137138// Verify IIOReadProgressListener.imageProgress() abort request.139if (progressAbort) {140System.out.println("imageProgress aborted ");141progressAborted = true;142}143}144145public static void main(String args[]) throws Exception {146final String[] formats = {"bmp", "png", "gif", "jpg", "tif"};147for (String format : formats) {148new ReadAbortTest(format);149}150}151152/*153* Remaining abstract methods that we need to implement from154* IIOReadProgressListener, but not relevant for this test case.155*/156@Override157public void imageComplete(ImageReader source) {158}159160@Override161public void sequenceStarted(ImageReader reader, int i) {162}163164@Override165public void sequenceComplete(ImageReader reader) {166}167168@Override169public void thumbnailStarted(ImageReader reader, int i, int i1) {170}171172@Override173public void thumbnailProgress(ImageReader reader, float f) {174}175176@Override177public void thumbnailComplete(ImageReader reader) {178}179}180181182183