1 /*
2  * Copyright (c) 2007, 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     4614845
27  * @summary Test drawImage(bgcolor) gets correct RGB from SystemColor objects.
28  * @run     main SystemBgColorTest
29  */
30 
31 import java.awt.AlphaComposite;
32 import java.awt.Color;
33 import java.awt.Graphics2D;
34 import java.awt.Image;
35 import java.awt.SystemColor;
36 import java.awt.image.BufferedImage;
37 import java.awt.image.DataBuffer;
38 import java.awt.image.IndexColorModel;
39 
40 public class SystemBgColorTest {
41     public static final int TESTW = 10;
42     public static final int TESTH = 10;
43 
44     static SystemColor systemColorObjects [] = {
45         SystemColor.desktop,
46         SystemColor.activeCaption,
47         SystemColor.activeCaptionText,
48         SystemColor.activeCaptionBorder,
49         SystemColor.inactiveCaption,
50         SystemColor.inactiveCaptionText,
51         SystemColor.inactiveCaptionBorder,
52         SystemColor.window,
53         SystemColor.windowBorder,
54         SystemColor.windowText,
55         SystemColor.menu,
56         SystemColor.menuText,
57         SystemColor.text,
58         SystemColor.textText,
59         SystemColor.textHighlight,
60         SystemColor.textHighlightText,
61         SystemColor.textInactiveText,
62         SystemColor.control,
63         SystemColor.controlText,
64         SystemColor.controlHighlight,
65         SystemColor.controlLtHighlight,
66         SystemColor.controlShadow,
67         SystemColor.controlDkShadow,
68         SystemColor.scrollbar,
69         SystemColor.info,
70         SystemColor.infoText
71     };
72 
73     static boolean counterrors;
74     static int errcount;
75 
error(String problem)76     public static void error(String problem) {
77         if (counterrors) {
78             errcount++;
79         } else {
80             throw new RuntimeException(problem);
81         }
82     }
83 
main(String argv[])84     public static void main(String argv[]) {
85         counterrors = (argv.length > 0);
86         test(BufferedImage.TYPE_INT_ARGB);
87         test(BufferedImage.TYPE_INT_RGB);
88         if (errcount > 0) {
89             throw new RuntimeException(errcount+" errors");
90         }
91     }
92 
93     static int cmap[] = {
94         0x00000000,
95         0xffffffff,
96     };
97 
test(int dsttype)98     public static void test(int dsttype) {
99         BufferedImage src =
100             new BufferedImage(TESTW, TESTH, BufferedImage.TYPE_INT_ARGB);
101         test(src, dsttype);
102         IndexColorModel icm = new IndexColorModel(8, 2, cmap, 0, true, 0,
103                                                   DataBuffer.TYPE_BYTE);
104         src = new BufferedImage(TESTW, TESTH,
105                                 BufferedImage.TYPE_BYTE_INDEXED, icm);
106         test(src, dsttype);
107     }
108 
test(Image src, int dsttype)109     public static void test(Image src, int dsttype) {
110         BufferedImage dst =
111             new BufferedImage(TESTW, TESTH, dsttype);
112         for (int i = 0; i < systemColorObjects.length; i++) {
113             test(src, dst, systemColorObjects[i]);
114         }
115     }
116 
test(Image src, BufferedImage dst, Color bg)117     public static void test(Image src, BufferedImage dst, Color bg) {
118         Graphics2D g = (Graphics2D) dst.getGraphics();
119         g.setComposite(AlphaComposite.Src);
120         g.setColor(Color.white);
121         g.fillRect(0, 0, TESTW, TESTH);
122         g.drawImage(src, 0, 0, bg, null);
123         int dstRGB = dst.getRGB(0, 0);
124         int bgRGB = bg.getRGB();
125         if (!dst.getColorModel().hasAlpha()) {
126             bgRGB |= 0xFF000000;
127         }
128         if (dstRGB != bgRGB) {
129             System.err.println("Actual: " + Integer.toHexString(dstRGB));
130             System.err.println("Expected: " + Integer.toHexString(bgRGB));
131             error("bad bg pixel for: " + bg);
132         }
133     }
134 }