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 24import java.awt.*; 25import java.awt.image.*; 26import java.util.*; 27 28/* @test 29 * @bug 8147966 30 * @summary test multiresolution image properties 31 * @author a.stepanov 32 * 33 * @run main MultiResolutionImagePropertiesTest 34 */ 35 36public class MultiResolutionImagePropertiesTest { 37 38 private final static Map<String, String> PROPS; 39 static { 40 PROPS = new HashMap<>(); 41 PROPS.put("one", "ONE"); 42 PROPS.put("two", "TWO"); 43 PROPS.put("three", "THREE"); 44 PROPS.put("other", "OTHER"); 45 PROPS.put("test", "TEST"); 46 } 47 48 private final static int SZ = 100; 49 private final static Object UNDEF = Image.UndefinedProperty; 50 51 private static BufferedImage generateImage(int scale, Properties p) { 52 53 int x = (int) (SZ * scale); 54 BufferedImage tmp = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB); 55 56 return new BufferedImage(tmp.getColorModel(), 57 tmp.getRaster(), 58 tmp.isAlphaPremultiplied(), 59 p); 60 } 61 62 private static void checkProperties(BufferedImage img, 63 String keys[], 64 String undefined[]) { 65 boolean numOK = true; 66 67 if (keys.length == 0) { 68 numOK = (img.getPropertyNames() == null); 69 } else { 70 numOK = (img.getPropertyNames().length == keys.length); 71 } 72 73 if (!numOK) { 74 throw new RuntimeException("invalid number of properties"); 75 } 76 77 for (String k: keys) { 78 if (!img.getProperty(k).equals(PROPS.get(k))) { 79 throw new RuntimeException("invalid property for name " + k); 80 } 81 } 82 83 for (String k: undefined) { 84 if (!img.getProperty(k).equals(UNDEF)) { 85 throw new RuntimeException("property for name " + k + 86 " must be undefined"); 87 } 88 } 89 } 90 91 private static void checkProperties(BaseMultiResolutionImage img, 92 String keys[], 93 String undefined[]) { 94 for (String k: keys) { 95 if (!img.getProperty(k, null).equals(PROPS.get(k))) { 96 throw new RuntimeException("invalid property for name " + k); 97 } 98 } 99 100 for (String k: undefined) { 101 if (!img.getProperty(k, null).equals(UNDEF)) { 102 throw new RuntimeException("property for name " + k + 103 " must be undefined"); 104 } 105 } 106 } 107 108 109 public static void main(String[] args) throws Exception { 110 111 String keys[] = new String[]{"one", "two", "three"}; 112 String otherKeys[] = new String[]{"other", "test"}; 113 String empty[] = new String[]{}; 114 115 Properties props = new Properties(); 116 for (String k: keys) { props.setProperty(k, PROPS.get(k)); } 117 118 Properties otherProps = new Properties(); 119 for (String k: otherKeys) { otherProps.setProperty(k, PROPS.get(k)); } 120 121 Properties defaultProps = new Properties(); 122 123 124 // === check the default state === 125 BaseMultiResolutionImage image = 126 new BaseMultiResolutionImage(new BufferedImage[]{ 127 generateImage(1, defaultProps), 128 generateImage(2, defaultProps), 129 generateImage(3, defaultProps) 130 }); 131 132 for (Image var: image.getResolutionVariants()) { 133 if (((BufferedImage) var).getPropertyNames() != null) { 134 throw new RuntimeException("PropertyNames should be null"); 135 } 136 } 137 138 // === default: base image is the 1st one === 139 image = 140 new BaseMultiResolutionImage(new BufferedImage[]{ 141 generateImage(1, props), 142 generateImage(2, otherProps), 143 generateImage(3, defaultProps) 144 }); 145 146 checkProperties(image, keys, otherKeys); 147 148 BufferedImage var = (BufferedImage) image.getResolutionVariant(SZ, SZ); 149 checkProperties(var, keys, otherKeys); 150 151 var = (BufferedImage) image.getResolutionVariant(2 * SZ, 2 * SZ); 152 checkProperties(var, otherKeys, keys); 153 154 var = (BufferedImage) image.getResolutionVariant(3 * SZ, 3 * SZ); 155 checkProperties(var, empty, keys); 156 checkProperties(var, empty, otherKeys); 157 158 // === let the 2nd image be a base one === 159 image = 160 new BaseMultiResolutionImage(1, new BufferedImage[]{ 161 generateImage(1, props), 162 generateImage(2, otherProps), 163 generateImage(3, defaultProps) 164 }); 165 166 checkProperties(image, otherKeys, keys); 167 168 var = (BufferedImage) image.getResolutionVariant(SZ, SZ); 169 checkProperties(var, keys, otherKeys); 170 171 var = (BufferedImage) image.getResolutionVariant(2 * SZ, 2 * SZ); 172 checkProperties(var, otherKeys, keys); 173 174 var = (BufferedImage) image.getResolutionVariant(3 * SZ, 3 * SZ); 175 checkProperties(var, empty, keys); 176 checkProperties(var, empty, otherKeys); 177 178 // === let the 3rd image be a base one === 179 image = 180 new BaseMultiResolutionImage(2, new BufferedImage[]{ 181 generateImage(1, defaultProps), 182 generateImage(2, defaultProps), 183 generateImage(3, props) 184 }); 185 186 checkProperties(image, keys, otherKeys); 187 188 var = (BufferedImage) image.getResolutionVariant(SZ, SZ); 189 checkProperties(var, empty, keys); 190 checkProperties(var, empty, otherKeys); 191 192 var = (BufferedImage) image.getResolutionVariant(2 * SZ, 2 * SZ); 193 checkProperties(var, empty, keys); 194 checkProperties(var, empty, otherKeys); 195 196 var = (BufferedImage) image.getResolutionVariant(3 * SZ, 3 * SZ); 197 checkProperties(var, keys, otherKeys); 198 199 // === check the other properties don't affect base === 200 checkProperties( 201 new BaseMultiResolutionImage(new BufferedImage[]{ 202 generateImage(1, defaultProps), 203 generateImage(2, props), 204 generateImage(3, props) 205 }), 206 empty, keys); 207 208 checkProperties( 209 new BaseMultiResolutionImage(2, new BufferedImage[]{ 210 generateImage(1, props), 211 generateImage(2, props), 212 generateImage(3, defaultProps) 213 }), 214 empty, keys); 215 } 216} 217