1 /*
   2  * Copyright (c) 2003, 2017, 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 4897067 4920152
  27  * @summary Tests that IIOWriteProgressListener receives correct progress
  28  *          percentage. Also it tests problem described in 4920152: test fails
  29  *          if imageComplete() or imageStarted() was called twice.
  30  */
  31 
  32 import java.awt.image.BufferedImage;
  33 import java.io.ByteArrayOutputStream;
  34 import java.util.ArrayList;
  35 import java.util.Iterator;
  36 import java.util.List;
  37 
  38 import javax.imageio.IIOImage;
  39 import javax.imageio.ImageIO;
  40 import javax.imageio.ImageWriteParam;
  41 import javax.imageio.ImageWriter;
  42 import javax.imageio.event.IIOWriteProgressListener;
  43 import javax.imageio.stream.ImageOutputStream;
  44 
  45 public class WriteProgressListenerTest {
  46 
  47 
  48     protected static String format = "BMP";
  49 
  50         protected String compression_type;
  51         protected WriteProgressListener listener;
  52 
  53         public WriteProgressListenerTest(String compression_type) {
  54         this.compression_type = compression_type;
  55         listener = new WriteProgressListener();
  56     }
  57 
  58     public void doTest() {
  59         try {
  60             System.out.println("Progress test for " + compression_type);
  61             BufferedImage bi = new BufferedImage(20, 300, BufferedImage.TYPE_INT_RGB);
  62             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  63             ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
  64 
  65             Iterator iter = ImageIO.getImageWritersByFormatName(format);
  66             if (!iter.hasNext()) {
  67                 throw new RuntimeException("No available writer for " + format);
  68             }
  69             ImageWriter writer = (ImageWriter)iter.next();
  70 
  71             writer.setOutput(ios);
  72             writer.addIIOWriteProgressListener(listener);
  73 
  74             IIOImage iio_img = new IIOImage(bi, null, null);
  75 
  76             ImageWriteParam param = writer.getDefaultWriteParam();
  77 
  78             param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  79             param.setCompressionType(compression_type);
  80 
  81 
  82             writer.write(null, iio_img, param);
  83 
  84             if (!listener.isTestPassed) {
  85                 throw new RuntimeException("Test for " + compression_type + " does not finish correctly!");
  86             }
  87         } catch (Exception e) {
  88             throw new RuntimeException(e);
  89         }
  90     }
  91 
  92         public static void main(String args[]) {
  93         String[] compression_types = new String[] { "BI_RGB",
  94                                                     "BI_JPEG",
  95                                                     "BI_PNG"};
  96 
  97         for(int i=0; i<compression_types.length; i++) {
  98             WriteProgressListenerTest test = new
  99                 WriteProgressListenerTest(compression_types[i]);
 100             test.doTest();
 101         }
 102         }
 103 
 104     static class WriteProgressListener implements IIOWriteProgressListener {
 105         List progress;
 106         public boolean isTestPassed = false;
 107         private boolean isImageStarted = false;
 108         private boolean isImageComplete = false;
 109 
 110         public WriteProgressListener() {
 111             progress = new ArrayList();
 112         }
 113 
 114         public void imageComplete(ImageWriter source) {
 115             System.out.println("Image Completed");
 116             if (!isImageComplete) {
 117                 isImageComplete = true;
 118             } else {
 119                 throw new RuntimeException("The imageComplete() was called twice."
 120                                            + " Test failed.");
 121             }
 122 
 123             checkProgress();
 124         }
 125         public void imageProgress(ImageWriter source, float percentageDone) {
 126             System.out.println("Image Progress "+percentageDone);
 127             progress.add(new Float(percentageDone));
 128         }
 129 
 130         public void imageStarted(ImageWriter source, int imageIndex) {
 131             System.out.println("Image Started "+imageIndex);
 132             if (!isImageStarted) {
 133                 isImageStarted = true;
 134             } else {
 135                 throw new RuntimeException("The imageStarted() was called twice. "
 136                                            + " Test failed.");
 137             }
 138             progress.clear();
 139         }
 140 
 141         public void thumbnailComplete(ImageWriter source)  {
 142             System.out.println("Thubnail completed");
 143         }
 144 
 145         public void thumbnailProgress(ImageWriter source, float percentageDone) {
 146             System.out.println("Thubnail Progress " + percentageDone);
 147         }
 148 
 149         public void thumbnailStarted(ImageWriter source, int imageIndex, int thumbnailIndex) {
 150             System.out.println("Thubnail started " + imageIndex);
 151         }
 152 
 153         public void writeAborted(ImageWriter source) {
 154             System.out.println("Writing Aborted");
 155             checkProgress();
 156         }
 157 
 158         private void checkProgress() {
 159             Iterator i = progress.iterator();
 160             if (!i.hasNext()) {
 161                 throw new RuntimeException("progress values list is empty!");
 162             }
 163             float val = ((Float)i.next()).floatValue();
 164             while(i.hasNext()) {
 165                 float next = ((Float)i.next()).floatValue();
 166                 if (val >= next) {
 167                     throw new RuntimeException("progress values do not increase!");
 168                 }
 169                 val = next;
 170             }
 171             isTestPassed = true;
 172             System.out.println("Test passed.");
 173         }
 174     }
 175 
 176 }