1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import static java.awt.image.ImageObserver.ALLBITS;
import java.io.File;
import javax.imageio.ImageIO;
import sun.awt.OSInfo;
import sun.awt.SunToolkit;
import sun.awt.image.MultiResolutionToolkitImage;
/**
* @test
* @key headful
* @bug 8040291
* @author Alexander Scherbatiy
* @summary [macosx] Http-Images are not fully loaded when using ImageIcon
* @modules java.desktop/sun.awt
* java.desktop/sun.awt.image
* @run main MultiResolutionToolkitImageTest
*/
public class MultiResolutionToolkitImageTest {
private static final int IMAGE_WIDTH = 300;
private static final int IMAGE_HEIGHT = 200;
private static final Color COLOR_1X = Color.GREEN;
private static final Color COLOR_2X = Color.BLUE;
private static final String IMAGE_NAME_1X = "image.png";
private static final String IMAGE_NAME_2X = "image@2x.png";
private static final int WAIT_TIME = 400;
private static volatile boolean isImageLoaded = false;
private static volatile boolean isRVObserverCalled = false;
public static void main(String[] args) throws Exception {
if (!checkOS()) {
return;
}
generateImages();
testToolkitMultiResolutionImageLoad();
}
static void testToolkitMultiResolutionImageLoad() throws Exception {
File imageFile = new File(IMAGE_NAME_1X);
String fileName = imageFile.getAbsolutePath();
Image image = Toolkit.getDefaultToolkit().getImage(fileName);
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
toolkit.prepareImage(image, -1, -1, new LoadImageObserver());
final long time = WAIT_TIME + System.currentTimeMillis();
while ((!isImageLoaded || !isRVObserverCalled)
&& System.currentTimeMillis() < time) {
Thread.sleep(50);
}
if(!isImageLoaded){
throw new RuntimeException("Image is not loaded!");
}
if(!isRVObserverCalled){
throw new RuntimeException("Resolution Variant observer is not called!");
}
}
static void generateImages() throws Exception {
if (!new File(IMAGE_NAME_1X).exists()) {
generateImage(1);
}
if (!new File(IMAGE_NAME_2X).exists()) {
generateImage(2);
}
}
static void generateImage(int scale) throws Exception {
BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(scale == 1 ? COLOR_1X : COLOR_2X);
g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT);
File file = new File(scale == 1 ? IMAGE_NAME_1X : IMAGE_NAME_2X);
ImageIO.write(image, "png", file);
}
static boolean checkOS() {
return OSInfo.getOSType() == OSInfo.OSType.MACOSX;
}
static class LoadImageObserver implements ImageObserver {
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
if (isRVObserver()) {
isRVObserverCalled = true;
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Image resolutionVariant = getResolutionVariant(img);
int rvFlags = toolkit.checkImage(resolutionVariant, width, height,
new IdleImageObserver());
if (rvFlags < infoflags) {
throw new RuntimeException("Info flags are greater than"
+ " resolution varint info flags");
}
} else if ((infoflags & ALLBITS) != 0) {
isImageLoaded = true;
}
return (infoflags & ALLBITS) == 0;
}
}
static boolean isRVObserver() {
Exception e = new Exception();
for (StackTraceElement elem : e.getStackTrace()) {
if (elem.getClassName().endsWith("MultiResolutionToolkitImage")) {
return true;
}
}
return false;
}
static class IdleImageObserver implements ImageObserver {
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y,
int width, int height) {
return false;
}
}
static Image getResolutionVariant(Image image) {
return ((MultiResolutionToolkitImage) image).getResolutionVariant();
}
}
|