1 /*
   2  * Copyright (c) 2014, 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 import java.awt.Color;
  25 import java.awt.Graphics;
  26 import java.awt.Image;
  27 import java.awt.Toolkit;
  28 import java.awt.image.BufferedImage;
  29 import java.awt.image.ImageObserver;
  30 import static java.awt.image.ImageObserver.ALLBITS;
  31 import java.io.File;
  32 import javax.imageio.ImageIO;
  33 import sun.awt.OSInfo;
  34 import sun.awt.SunToolkit;
  35 import sun.awt.image.MultiResolutionToolkitImage;
  36 
  37 /**
  38  * @test
  39  * @bug 8040291
  40  * @author Alexander Scherbatiy
  41  * @summary [macosx] Http-Images are not fully loaded when using ImageIcon
  42  * @run main MultiResolutionToolkitImageTest
  43  */
  44 public class MultiResolutionToolkitImageTest {
  45 
  46     private static final int IMAGE_WIDTH = 300;
  47     private static final int IMAGE_HEIGHT = 200;
  48     private static final Color COLOR_1X = Color.GREEN;
  49     private static final Color COLOR_2X = Color.BLUE;
  50     private static final String IMAGE_NAME_1X = "image.png";
  51     private static final String IMAGE_NAME_2X = "image@2x.png";
  52 
  53     public static void main(String[] args) throws Exception {
  54 
  55         if (!checkOS()) {
  56             return;
  57         }
  58         generateImages();
  59         testToolkitMultiResolutionImageLoad();
  60     }
  61 
  62     static void testToolkitMultiResolutionImageLoad() throws Exception {
  63         File imageFile = new File(IMAGE_NAME_1X);
  64         String fileName = imageFile.getAbsolutePath();
  65         Image image = Toolkit.getDefaultToolkit().getImage(fileName);
  66         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  67         toolkit.prepareImage(image, -1, -1, new LoadImageObserver());
  68     }
  69 
  70     static void generateImages() throws Exception {
  71         if (!new File(IMAGE_NAME_1X).exists()) {
  72             generateImage(1);
  73         }
  74 
  75         if (!new File(IMAGE_NAME_2X).exists()) {
  76             generateImage(2);
  77         }
  78     }
  79 
  80     static void generateImage(int scale) throws Exception {
  81         BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT,
  82                 BufferedImage.TYPE_INT_RGB);
  83         Graphics g = image.getGraphics();
  84         g.setColor(scale == 1 ? COLOR_1X : COLOR_2X);
  85         g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT);
  86         File file = new File(scale == 1 ? IMAGE_NAME_1X : IMAGE_NAME_2X);
  87         ImageIO.write(image, "png", file);
  88     }
  89 
  90     static boolean checkOS() {
  91         return OSInfo.getOSType() == OSInfo.OSType.MACOSX;
  92     }
  93 
  94     static class LoadImageObserver implements ImageObserver {
  95 
  96         @Override
  97         public boolean imageUpdate(Image img, int infoflags, int x, int y,
  98                 int width, int height) {
  99 
 100             if (isRVObserevr()) {
 101                 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
 102                 Image resolutionVariant = getResolutionVariant(img);
 103                 int rvFlags = toolkit.checkImage(resolutionVariant, width, height,
 104                         new IdleImageObserver());
 105                 if (rvFlags < infoflags) {
 106                     throw new RuntimeException("Info flags are greater than"
 107                             + " resolution varint info flags");
 108                 }
 109             }
 110             return (infoflags & ALLBITS) == 0;
 111         }
 112     }
 113 
 114     static boolean isRVObserevr() {
 115         Exception e = new Exception();
 116 
 117         for (StackTraceElement elem : e.getStackTrace()) {
 118             if (elem.getClassName().endsWith("MultiResolutionToolkitImage")) {
 119                 return true;
 120             }
 121         }
 122         return false;
 123     }
 124 
 125     static class IdleImageObserver implements ImageObserver {
 126 
 127         @Override
 128         public boolean imageUpdate(Image img, int infoflags, int x, int y,
 129                 int width, int height) {
 130             return false;
 131         }
 132     }
 133 
 134     static Image getResolutionVariant(Image image) {
 135         return ((MultiResolutionToolkitImage) image).getResolutionVariant();
 136     }
 137 }