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