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 WriteAbortTest(String format)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 } finally { 101 Files.delete(file.toPath()); 102 } 103 } 104 105 /* 106 * Abstract methods that we need to implement from 107 * IIOWriteProgressListener, and relevant for this test case. 108 */ 109 @Override imageStarted(ImageWriter source, int imageIndex)110 public void imageStarted(ImageWriter source, int imageIndex) { 111 System.out.println("imageStarted called"); 112 if (startAbort) { 113 source.abort(); 114 } 115 } 116 117 @Override imageProgress(ImageWriter source, float percentageDone)118 public void imageProgress(ImageWriter source, float percentageDone) { 119 System.out.println("imageProgress called"); 120 if (progressAbort) { 121 source.abort(); 122 } 123 } 124 125 @Override writeAborted(ImageWriter source)126 public void writeAborted(ImageWriter source) { 127 System.out.println("writeAborted called"); 128 // Verify IIOWriteProgressListener.imageStarted() abort request. 129 if (startAbort) { 130 System.out.println("imageStarted aborted "); 131 startAborted = true; 132 } 133 134 // Verify IIOWriteProgressListener.imageProgress() abort request. 135 if (progressAbort) { 136 System.out.println("imageProgress aborted "); 137 progressAborted = true; 138 } 139 } 140 main(String args[])141 public static void main(String args[]) throws Exception { 142 final String[] formats = {"bmp", "png", "gif", "jpg", "tif"}; 143 for (String format : formats) { 144 new WriteAbortTest(format); 145 } 146 } 147 148 /* 149 * Remaining abstract methods that we need to implement from 150 * IIOWriteProgressListener, but not relevant for this test case. 151 */ 152 @Override imageComplete(ImageWriter source)153 public void imageComplete(ImageWriter source) { 154 } 155 156 @Override thumbnailStarted(ImageWriter source, int imageIndex, int thumbnailIndex)157 public void thumbnailStarted(ImageWriter source, int imageIndex, 158 int thumbnailIndex) { 159 } 160 161 @Override thumbnailProgress(ImageWriter source, float percentageDone)162 public void thumbnailProgress(ImageWriter source, float percentageDone) { 163 } 164 165 @Override thumbnailComplete(ImageWriter source)166 public void thumbnailComplete(ImageWriter source) { 167 } 168 } 169 170