1 /* 2 * Copyright (c) 2015, 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 import java.awt.Dimension; 24 import java.awt.Image; 25 import java.awt.image.BufferedImage; 26 import java.awt.image.BaseMultiResolutionImage; 27 import java.awt.image.MultiResolutionImage; 28 import java.util.List; 29 30 /** 31 * @test 32 * @bug 8029339 33 * @author Alexander Scherbatiy 34 * @summary Custom MultiResolution image support on HiDPI displays 35 * @run main BaseMultiResolutionImageTest 36 */ 37 public class BaseMultiResolutionImageTest { 38 39 public static void main(String[] args) { 40 testZeroRVIMages(); 41 testNullRVIMages(); 42 testNullRVIMage(); 43 testIOOBException(); 44 testRVSizes(); 45 testBaseMRImage(); 46 } 47 48 static void testZeroRVIMages() { 49 try { 50 new BaseMultiResolutionImage(); 51 } catch (IllegalArgumentException ignored) { 52 return; 53 } 54 throw new RuntimeException("IllegalArgumentException is not thrown!"); 55 } 56 57 static void testNullRVIMages() { 58 try { 59 new BaseMultiResolutionImage(null); 60 } catch (IllegalArgumentException ignored) { 61 return; 62 } 63 throw new RuntimeException("IllegalArgumentException is not thrown!"); 64 } 65 66 static void testNullRVIMage() { 67 68 Image baseImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); 69 70 try { 71 new BaseMultiResolutionImage(baseImage, null); 72 } catch (NullPointerException ignored) { 73 return; 74 } 75 throw new RuntimeException("NullPointerException is not thrown!"); 76 } 77 78 static void testIOOBException() { 79 80 for (int baseImageIndex : new int[]{-3, 2, 4}) { 81 try { 82 new BaseMultiResolutionImage(baseImageIndex, 83 createRVImage(0), createRVImage(1)); 84 } catch (IndexOutOfBoundsException ignored) { 85 continue; 86 } 87 88 throw new RuntimeException("IndexOutOfBoundsException is not thrown!"); 89 } 90 } 91 92 static void testRVSizes() { 93 94 int imageSize = getSize(1); 95 96 double[][] sizeArray = { 97 {-imageSize, imageSize}, 98 {2 * imageSize, -2 * imageSize}, 99 {Double.POSITIVE_INFINITY, imageSize}, 100 {Double.POSITIVE_INFINITY, -imageSize}, 101 {imageSize, Double.NEGATIVE_INFINITY}, 102 {-imageSize, Double.NEGATIVE_INFINITY}, 103 {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}, 104 {Double.NaN, imageSize}, 105 {imageSize, Double.NaN}, 106 {Double.NaN, Double.NaN}, 107 {Double.POSITIVE_INFINITY, Double.NaN} 108 }; 109 110 for (double[] sizes : sizeArray) { 111 try { 112 MultiResolutionImage mrImage = new BaseMultiResolutionImage( 113 0, createRVImage(0), createRVImage(1)); 114 mrImage.getResolutionVariant(sizes[0], sizes[1]); 115 } catch (IllegalArgumentException ignored) { 116 continue; 117 } 118 119 throw new RuntimeException("IllegalArgumentException is not thrown!"); 120 } 121 } 122 123 static void testBaseMRImage() { 124 int baseIndex = 1; 125 int length = 3; 126 BufferedImage[] resolutionVariants = new BufferedImage[length]; 127 for (int i = 0; i < length; i++) { 128 resolutionVariants[i] = createRVImage(i); 129 } 130 131 BaseMultiResolutionImage mrImage = new BaseMultiResolutionImage(baseIndex, 132 resolutionVariants); 133 134 List<Image> rvImageList = mrImage.getResolutionVariants(); 135 if (rvImageList.size() != length) { 136 throw new RuntimeException("Wrong size of resolution variants list!"); 137 } 138 139 for (int i = 0; i < length; i++) { 140 int imageSize = getSize(i); 141 Image testRVImage = mrImage.getResolutionVariant(imageSize, imageSize); 142 143 if (testRVImage != resolutionVariants[i]) { 144 throw new RuntimeException("Wrong resolution variant!"); 145 } 146 147 if (rvImageList.get(i) != resolutionVariants[i]) { 148 throw new RuntimeException("Wrong resolution variant!"); 149 } 150 } 151 152 BufferedImage baseImage = resolutionVariants[baseIndex]; 153 154 if (baseImage.getWidth() != mrImage.getWidth(null) 155 || baseImage.getHeight() != mrImage.getHeight(null)) { 156 throw new RuntimeException("Base image is wrong!"); 157 } 158 159 boolean passed = false; 160 161 try { 162 rvImageList.set(0, createRVImage(10)); 163 } catch (Exception e) { 164 passed = true; 165 } 166 167 if (!passed) { 168 throw new RuntimeException("Resolution variants list is modifiable!"); 169 } 170 171 passed = false; 172 173 try { 174 rvImageList.remove(0); 175 } catch (Exception e) { 176 passed = true; 177 } 178 179 if (!passed) { 180 throw new RuntimeException("Resolution variants list is modifiable!"); 181 } 182 183 passed = false; 184 185 try { 186 rvImageList.add(0, createRVImage(10)); 187 } catch (Exception e) { 188 passed = true; 189 } 190 191 if (!passed) { 192 throw new RuntimeException("Resolution variants list is modifiable!"); 193 } 194 } 195 196 private static int getSize(int i) { 197 return 8 * (i + 1); 198 } 199 200 private static BufferedImage createRVImage(int i) { 201 return new BufferedImage(getSize(i), getSize(i), 202 BufferedImage.TYPE_INT_RGB); 203 } 204 }