1 /*
   2  * Copyright (c) 2016, 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     4924727
  27  * @summary Test verifies that if we call ImageReader.abort() in
  28  *          IIOReadProgressListener.imageStarted() or
  29  *          IIOReadProgressListener.imageProgress() are we
  30  *          calling IIOReadProgressListener.readAborted() for all readers.
  31  * @run     main ReadAbortTest
  32  */
  33 import java.awt.image.BufferedImage;
  34 import java.io.File;
  35 import java.util.Iterator;
  36 import javax.imageio.ImageIO;
  37 import javax.imageio.ImageReader;
  38 import javax.imageio.event.IIOReadProgressListener;
  39 import javax.imageio.stream.ImageInputStream;
  40 import java.awt.Color;
  41 import java.awt.Graphics2D;
  42 import java.nio.file.Files;
  43 
  44 public class ReadAbortTest implements IIOReadProgressListener {
  45 
  46     ImageReader reader = null;
  47     ImageInputStream iis = null;
  48     BufferedImage bimg = null;
  49     File file;
  50     boolean startAbort = false;
  51     boolean startAborted = false;
  52     boolean progressAbort = false;
  53     boolean progressAborted = false;
  54     Color srccolor = Color.red;
  55     int width = 100;
  56     int heght = 100;
  57 
  58     public ReadAbortTest(String format) throws Exception {
  59         try {
  60             System.out.println("Test for format " + format);
  61             bimg = new BufferedImage(width, heght,
  62                     BufferedImage.TYPE_INT_RGB);
  63 
  64             Graphics2D g = bimg.createGraphics();
  65             g.setColor(srccolor);
  66             g.fillRect(0, 0, width, heght);
  67             g.dispose();
  68 
  69             file = File.createTempFile("src_", "." + format, new File("."));
  70             ImageIO.write(bimg, format, file);
  71             ImageInputStream iis = ImageIO.createImageInputStream(file);
  72 
  73             Iterator iter = ImageIO.getImageReaders(iis);
  74             while (iter.hasNext()) {
  75                 reader = (ImageReader) iter.next();
  76                 break;
  77             }
  78             reader.setInput(iis);
  79             reader.addIIOReadProgressListener(this);
  80 
  81             // Abort reading in IIOReadProgressListener.imageStarted().
  82             startAbort = true;
  83             bimg = reader.read(0);
  84             startAbort = false;
  85 
  86             // Abort reading in IIOReadProgressListener.imageProgress().
  87             progressAbort = true;
  88             bimg = reader.read(0);
  89             progressAbort = false;
  90 
  91             iis.close();
  92             /*
  93              * All abort requests from imageStarted,imageProgress and
  94              * imageComplete from IIOReadProgressListener should be reached
  95              * otherwise throw RuntimeException.
  96              */
  97             if (!(startAborted
  98                     && progressAborted)) {
  99                 throw new RuntimeException("All IIOReadProgressListener abort"
 100                         + " requests are not processed for format "
 101                         + format);
 102             }
 103         } catch (Exception e) {
 104             throw e;
 105         } finally {
 106             Files.delete(file.toPath());
 107         }
 108     }
 109 
 110     /*
 111      * Abstract methods that we need to implement from
 112      * IIOReadProgressListener, and relevant for this test case.
 113      */
 114     @Override
 115     public void imageStarted(ImageReader source, int imageIndex) {
 116         System.out.println("imageStarted called");
 117         if (startAbort) {
 118             source.abort();
 119         }
 120     }
 121 
 122     @Override
 123     public void imageProgress(ImageReader source, float percentageDone) {
 124         System.out.println("imageProgress called");
 125         if (progressAbort) {
 126             source.abort();
 127         }
 128     }
 129 
 130     @Override
 131     public void readAborted(ImageReader source) {
 132         System.out.println("readAborted called");
 133         // Verify IIOReadProgressListener.imageStarted() abort request.
 134         if (startAbort) {
 135             System.out.println("imageStarted aborted ");
 136             startAborted = true;
 137         }
 138 
 139         // Verify IIOReadProgressListener.imageProgress() abort request.
 140         if (progressAbort) {
 141             System.out.println("imageProgress aborted ");
 142             progressAborted = true;
 143         }
 144     }
 145 
 146     public static void main(String args[]) throws Exception {
 147         final String[] formats = {"bmp", "png", "gif", "jpg", "tif"};
 148         for (String format : formats) {
 149             new ReadAbortTest(format);
 150         }
 151     }
 152 
 153     /*
 154      * Remaining abstract methods that we need to implement from
 155      * IIOReadProgressListener, but not relevant for this test case.
 156      */
 157     @Override
 158     public void imageComplete(ImageReader source) {
 159     }
 160 
 161     @Override
 162     public void sequenceStarted(ImageReader reader, int i) {
 163     }
 164 
 165     @Override
 166     public void sequenceComplete(ImageReader reader) {
 167     }
 168 
 169     @Override
 170     public void thumbnailStarted(ImageReader reader, int i, int i1) {
 171     }
 172 
 173     @Override
 174     public void thumbnailProgress(ImageReader reader, float f) {
 175     }
 176 
 177     @Override
 178     public void thumbnailComplete(ImageReader reader) {
 179     }
 180 }
 181