|
1 /* |
|
2 * Copyright (c) 2001, 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 4420342 4421831 |
|
27 * @summary Checks that IIOWriteProgressListener methods are called in proper |
|
28 * sequence for the JPEG and PNG writers |
|
29 */ |
|
30 |
|
31 import java.awt.image.BufferedImage; |
|
32 import java.io.File; |
|
33 import java.io.IOException; |
|
34 import java.util.Iterator; |
|
35 import javax.imageio.ImageIO; |
|
36 import javax.imageio.ImageWriter; |
|
37 import javax.imageio.event.IIOWriteProgressListener; |
|
38 import javax.imageio.stream.ImageOutputStream; |
|
39 |
|
40 public class WriteProgressListenerTest implements IIOWriteProgressListener { |
|
41 |
|
42 final static int UNINITIALIZED = 0; |
|
43 final static int IMAGE_STARTED = 1; |
|
44 final static int IMAGE_COMPLETE = 2; |
|
45 |
|
46 int state = UNINITIALIZED; |
|
47 float prevPercentageDone = 0.0F; |
|
48 File tempFile = null; |
|
49 |
|
50 public WriteProgressListenerTest(String format) throws IOException { |
|
51 ImageWriter writer = null; |
|
52 Iterator witer = ImageIO.getImageWritersByFormatName(format); |
|
53 if (!witer.hasNext()) { |
|
54 error("No writer for format " + format + "!"); |
|
55 } |
|
56 writer = (ImageWriter)witer.next(); |
|
57 |
|
58 System.out.println("Got writer " + writer); |
|
59 writer.addIIOWriteProgressListener(this); |
|
60 |
|
61 this.tempFile = File.createTempFile("imageio", ".tmp"); |
|
62 tempFile.deleteOnExit(); |
|
63 ImageOutputStream stream = ImageIO.createImageOutputStream(tempFile); |
|
64 writer.setOutput(stream); |
|
65 |
|
66 BufferedImage im = |
|
67 new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR); |
|
68 |
|
69 this.state = UNINITIALIZED; |
|
70 |
|
71 writer.write(im); |
|
72 |
|
73 if (this.state == UNINITIALIZED) { |
|
74 error("imageStarted never called!"); |
|
75 } |
|
76 if (this.state != IMAGE_COMPLETE) { |
|
77 error("imageComplete not called!"); |
|
78 } |
|
79 |
|
80 print("Passed!"); |
|
81 } |
|
82 |
|
83 private void error(String s) { |
|
84 if (tempFile != null) { |
|
85 tempFile.delete(); |
|
86 } |
|
87 throw new RuntimeException(s); |
|
88 } |
|
89 |
|
90 private void print(String s) { |
|
91 System.out.println(s); |
|
92 } |
|
93 |
|
94 public void sequenceStarted(ImageWriter source) { |
|
95 error("Obsolete method sequenceStarted was called!"); |
|
96 } |
|
97 |
|
98 public void sequenceComplete(ImageWriter source) { |
|
99 error("Obsolete method sequenceComplete was called!"); |
|
100 } |
|
101 |
|
102 public void imageStarted(ImageWriter source, int imageIndex) { |
|
103 print("imageStarted: imageIndex = " + imageIndex); |
|
104 |
|
105 if (state != UNINITIALIZED) { |
|
106 error("imageStarted not called first!"); |
|
107 } |
|
108 state = IMAGE_STARTED; |
|
109 prevPercentageDone = 0.0F; |
|
110 } |
|
111 |
|
112 public void imageProgress(ImageWriter source, |
|
113 float percentageDone) { |
|
114 print("imageProgress: percentageDone = " + percentageDone); |
|
115 |
|
116 if (state != IMAGE_STARTED) { |
|
117 error("imageProgress called without prior imageStarted!"); |
|
118 } |
|
119 if (percentageDone < prevPercentageDone) { |
|
120 error("percentageDone did not increase!"); |
|
121 } |
|
122 prevPercentageDone = percentageDone; |
|
123 } |
|
124 |
|
125 public void imageComplete(ImageWriter source) { |
|
126 print("imageComplete"); |
|
127 |
|
128 if (state != IMAGE_STARTED) { |
|
129 error("imageComplete called without imageStarted!"); |
|
130 } |
|
131 if (prevPercentageDone == 0.0F) { |
|
132 error("percentageDone was never updated!"); |
|
133 } |
|
134 state = IMAGE_COMPLETE; |
|
135 } |
|
136 |
|
137 public void thumbnailStarted(ImageWriter source, |
|
138 int imageIndex, int thumbnailIndex) { |
|
139 } |
|
140 |
|
141 public void thumbnailProgress(ImageWriter source, float percentageDone) { |
|
142 } |
|
143 |
|
144 public void thumbnailComplete(ImageWriter source) { |
|
145 } |
|
146 |
|
147 public void writeAborted(ImageWriter source) { |
|
148 } |
|
149 |
|
150 public static void main(String[] args) throws IOException { |
|
151 new WriteProgressListenerTest("jpeg"); |
|
152 new WriteProgressListenerTest("png"); |
|
153 } |
|
154 } |