1 /*
   2  * Copyright (c) 2020, 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 8212226
  27  * @summary Check that base image gets selected during painting
  28  *          if other resolution variants are not ready
  29  * @run main MultiResolutionImageSelectionTest
  30  */
  31 
  32 import java.awt.Color;
  33 import java.awt.Graphics;
  34 import java.awt.Graphics2D;
  35 import java.awt.Image;
  36 import java.awt.image.AbstractMultiResolutionImage;
  37 import java.awt.image.BufferedImage;
  38 import java.awt.image.ImageObserver;
  39 import java.awt.image.ImageProducer;
  40 import java.awt.image.MultiResolutionImage;
  41 import java.util.Arrays;
  42 import java.util.List;
  43 
  44 import static java.awt.image.BufferedImage.TYPE_INT_RGB;
  45 
  46 public class MultiResolutionImageSelectionTest {
  47     final static BufferedImage GOOD_IMAGE = new BufferedImage(200, 200,
  48             TYPE_INT_RGB);
  49     
  50     final static Image BAD_IMAGE= new Image() {
  51         @Override
  52         public int getWidth(ImageObserver observer) {
  53             return -1;
  54         }
  55 
  56         @Override
  57         public int getHeight(ImageObserver observer) {
  58             return 1;
  59         }
  60 
  61         @Override
  62         public ImageProducer getSource() {
  63             return null;
  64         }
  65 
  66         @Override
  67         public Graphics getGraphics() {
  68             return null;
  69         }
  70 
  71         @Override
  72         public Object getProperty(String name, ImageObserver observer) {
  73             return null;
  74         }
  75     };
  76 
  77     public static void main(String[] args) {
  78         Graphics g = GOOD_IMAGE.createGraphics();
  79         g.setColor(Color.RED);
  80         g.fillRect(0, 0, GOOD_IMAGE.getWidth(), GOOD_IMAGE.getHeight());
  81         g.dispose();
  82 
  83         MultiResolutionImage mri = new AbstractMultiResolutionImage() {
  84             @Override
  85             protected Image getBaseImage() {
  86                 return GOOD_IMAGE;
  87             }
  88 
  89             @Override
  90             public Image getResolutionVariant(double destImageWidth, double destImageHeight) {
  91                 if ((int)destImageHeight == 200) {
  92                     return GOOD_IMAGE;
  93                 } else {
  94                     return BAD_IMAGE;
  95                 }
  96             }
  97 
  98             @Override
  99             public List<Image> getResolutionVariants() {
 100                 return Arrays.asList(BAD_IMAGE, GOOD_IMAGE, BAD_IMAGE);
 101             }
 102         };
 103 
 104         BufferedImage target = new BufferedImage(500, 500, TYPE_INT_RGB);
 105         Graphics2D g2d = target.createGraphics();
 106         g2d.drawImage((Image) mri, 0, 0, 500, 500, null);
 107         g2d.dispose();
 108         if (Color.RED.getRGB() != target.getRGB(1, 1)) {
 109             throw new RuntimeException("Wrong resolution variant was used");
 110         }
 111     }
 112 }