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     8164931
  27  * @summary Test verifies that if we call ImageWriter.abort() in
  28  *          IIOWriteProgressListener.imageStarted() or
  29  *          IIOWriteProgressListener.imageProgress() are we
  30  *          calling IIOWriteProgressListener.readAborted() for all readers.
  31  * @run     main WriteAbortTest
  32  */
  33 import java.awt.image.BufferedImage;
  34 import java.io.File;
  35 import javax.imageio.ImageIO;
  36 import javax.imageio.stream.ImageInputStream;
  37 import java.awt.Color;
  38 import java.awt.Graphics2D;
  39 import java.nio.file.Files;
  40 import javax.imageio.ImageWriter;
  41 import javax.imageio.event.IIOWriteProgressListener;
  42 import javax.imageio.stream.ImageOutputStream;
  43 
  44 public class WriteAbortTest implements IIOWriteProgressListener {
  45 
  46     ImageWriter writer = null;
  47     ImageOutputStream ios = 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 WriteAbortTest(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             ImageInputStream ios = ImageIO.createImageOutputStream(file);
  71 
  72             ImageWriter writer =
  73                     ImageIO.getImageWritersByFormatName(format).next();
  74 
  75             writer.setOutput(ios);
  76             writer.addIIOWriteProgressListener(this);
  77 
  78             // Abort writing in IIOWriteProgressListener.imageStarted().
  79             startAbort = true;
  80             writer.write(bimg);
  81             startAbort = false;
  82 
  83             // Abort writing in IIOWriteProgressListener.imageProgress().
  84             progressAbort = true;
  85             writer.write(bimg);
  86             progressAbort = false;
  87 
  88             ios.close();
  89             /*
  90              * All abort requests from imageStarted,imageProgress
  91              * from IIOWriteProgressListener should be reached
  92              * otherwise throw RuntimeException.
  93              */
  94             if (!(startAborted
  95                     && progressAborted)) {
  96                 throw new RuntimeException("All IIOWriteProgressListener abort"
  97                         + " requests are not processed for format "
  98                         + format);
  99             }
 100         } catch (Exception e) {
 101             throw e;
 102         } finally {
 103             Files.delete(file.toPath());
 104         }
 105     }
 106 
 107     /*
 108      * Abstract methods that we need to implement from
 109      * IIOWriteProgressListener, and relevant for this test case.
 110      */
 111     @Override
 112     public void imageStarted(ImageWriter source, int imageIndex) {
 113         System.out.println("imageStarted called");
 114         if (startAbort) {
 115             source.abort();
 116         }
 117     }
 118 
 119     @Override
 120     public void imageProgress(ImageWriter source, float percentageDone) {
 121         System.out.println("imageProgress called");
 122         if (progressAbort) {
 123             source.abort();
 124         }
 125     }
 126 
 127     @Override
 128     public void writeAborted(ImageWriter source) {
 129         System.out.println("writeAborted called");
 130         // Verify IIOWriteProgressListener.imageStarted() abort request.
 131         if (startAbort) {
 132             System.out.println("imageStarted aborted ");
 133             startAborted = true;
 134         }
 135 
 136         // Verify IIOWriteProgressListener.imageProgress() abort request.
 137         if (progressAbort) {
 138             System.out.println("imageProgress aborted ");
 139             progressAborted = true;
 140         }
 141     }
 142 
 143     public static void main(String args[]) throws Exception {
 144         final String[] formats = {"bmp", "png", "gif", "jpg", "tif"};
 145         for (String format : formats) {
 146             new WriteAbortTest(format);
 147         }
 148     }
 149 
 150     /*
 151      * Remaining abstract methods that we need to implement from
 152      * IIOWriteProgressListener, but not relevant for this test case.
 153      */
 154     @Override
 155     public void imageComplete(ImageWriter source) {
 156     }
 157 
 158     @Override
 159     public void thumbnailStarted(ImageWriter source, int imageIndex,
 160                                  int thumbnailIndex) {
 161     }
 162 
 163     @Override
 164     public void thumbnailProgress(ImageWriter source, float percentageDone) {
 165     }
 166 
 167     @Override
 168     public void thumbnailComplete(ImageWriter source) {
 169     }
 170 }
 171