MultiResolutionCachedImageTest.java revision 12563:9d2b7575edf8
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
24import java.awt.Color;
25import java.awt.Dimension;
26import java.awt.Graphics;
27import java.awt.Image;
28import java.awt.geom.Dimension2D;
29import java.awt.image.BufferedImage;
30import sun.awt.image.MultiResolutionCachedImage;
31
32/**
33 * @test
34 * @bug 8132123
35 * @author Alexander Scherbatiy
36 * @summary MultiResolutionCachedImage unnecessarily creates base image to get
37 *          its size
38 * @modules java.desktop/sun.awt.image
39 * @run main MultiResolutionCachedImageTest
40 */
41public class MultiResolutionCachedImageTest {
42
43    private static final Color TEST_COLOR = Color.BLUE;
44
45    public static void main(String[] args) {
46
47        Image image = new TestMultiResolutionCachedImage(100);
48
49        image.getWidth(null);
50        image.getHeight(null);
51        image.getProperty("comment", null);
52
53        int scaledSize = 50;
54        Image scaledImage = image.getScaledInstance(scaledSize, scaledSize,
55                Image.SCALE_SMOOTH);
56
57        if (!(scaledImage instanceof BufferedImage)) {
58            throw new RuntimeException("Wrong scaled image!");
59        }
60
61        BufferedImage buffScaledImage = (BufferedImage) scaledImage;
62
63        if (buffScaledImage.getWidth() != scaledSize
64                || buffScaledImage.getHeight() != scaledSize) {
65            throw new RuntimeException("Wrong scaled image!");
66        }
67
68        if (buffScaledImage.getRGB(scaledSize / 2, scaledSize / 2) != TEST_COLOR.getRGB()) {
69            throw new RuntimeException("Wrong scaled image!");
70        }
71    }
72
73    private static Dimension2D getDimension(int size) {
74        return new Dimension(size, size);
75    }
76
77    private static Dimension2D[] getSizes(int size) {
78        return new Dimension2D[]{getDimension(size), getDimension(2 * size)};
79    }
80
81    private static Image createImage(int width, int height) {
82        BufferedImage buffImage = new BufferedImage(width, height,
83                BufferedImage.TYPE_INT_RGB);
84        Graphics g = buffImage.createGraphics();
85        g.setColor(TEST_COLOR);
86        g.fillRect(0, 0, width, height);
87        return buffImage;
88    }
89
90    private static class TestMultiResolutionCachedImage
91            extends MultiResolutionCachedImage {
92
93        private final int size;
94
95        public TestMultiResolutionCachedImage(int size) {
96            super(size, size, getSizes(size), (w, h) -> createImage(w, h));
97            this.size = size;
98        }
99
100        @Override
101        public Image getResolutionVariant(int width, int height) {
102            if (width == size || height == size) {
103                throw new RuntimeException("Base image is requested!");
104            }
105            return super.getResolutionVariant(width, height);
106        }
107
108        @Override
109        protected Image getBaseImage() {
110            throw new RuntimeException("Base image is used");
111        }
112    }
113}
114