1 /*
   2  * Copyright (c) 2011, 2013, 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  * @summary Tests the behavior of IndexColorModel via two rectangular images on the screen with different Color models.
  27  * @summary com.apple.junit.java.graphics.color
  28  * @library ../regtesthelpers
  29  * @build VisibilityValidator
  30  * @run junit IndexColorModelTest
  31  */
  32 
  33 import test.java.awt.regtesthelpers.VisibilityValidator;
  34 import junit.framework.*;
  35 import javax.swing.*;
  36 import java.awt.*;
  37 import java.awt.image.BufferedImage;
  38 import java.awt.image.IndexColorModel;
  39 
  40 public class IndexColorModelTest extends TestCase {
  41     private static BufferedImage image1;
  42     private static BufferedImage image2;
  43 
  44     private static byte[] reds = {(byte)0, (byte)0};
  45     private static byte[] greens = {(byte)0, (byte)0};
  46     private static byte[] blues = {(byte)255, (byte)255};
  47 
  48     private JFrame frame;
  49 
  50     public void testColor() throws Exception {
  51         VisibilityValidator.setVisibleAndConfirm( frame);
  52 
  53         Thread.sleep(125);   // let humans see it too...
  54 
  55         assertTrue("Timed out without seeing our blue image", VisibilityValidator.waitForColor(frame ,50,50, Color.blue));
  56         assertTrue("Timed out without seeing our IndexColorModel image", VisibilityValidator.waitForColor(frame ,150,50, new Color(204, 204, 255)));
  57 
  58     }
  59 
  60     public static Test suite() {
  61         return new TestSuite(IndexColorModelTest.class);
  62     }
  63 
  64     protected void tearDown() {
  65            frame.dispose();
  66     }
  67 
  68     public static void main (String[] args) throws RuntimeException {
  69         TestResult tr = junit.textui.TestRunner.run(suite());
  70         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  71             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  72         }
  73     }
  74 
  75     public void setUp() {
  76         frame = new JFrame();
  77         frame.setSize(new Dimension(200, 100));
  78         JPanel panel = new JPanel() {
  79             public void paint(Graphics g) {
  80                 super.paint(g);
  81                 paintOntoPanel((Graphics2D)g);
  82             }
  83         };
  84         panel.setBackground(Color.WHITE);
  85         panel.setPreferredSize(new Dimension(200, 100));
  86         frame.getContentPane().add(panel);
  87 
  88         IndexColorModel solidFirstModel = createColorModelWithSolidBlueFirst();
  89         IndexColorModel alphaFirstModel = createColorModelWithAlphaBlueFirst();
  90         image1 = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_INDEXED, solidFirstModel);
  91         image2 = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_INDEXED, alphaFirstModel);
  92         paintWithColorMap(image1);
  93         paintWithColorMap(image2);
  94 
  95         frame.pack();
  96     }
  97 
  98     private static void paintWithColorMap(BufferedImage image) {
  99         Graphics2D indexGraphics = image.createGraphics();
 100         paintAlphaRect(indexGraphics);
 101     }
 102 
 103     // solid blue comes before alpha blue in color map
 104     private static IndexColorModel createColorModelWithSolidBlueFirst() {
 105         // alpha value of 51 corresponds to .2f as set in alpha composite below
 106         byte[] alphas = {(byte)255, (byte)51};
 107         return new IndexColorModel(1, 2, reds, greens, blues, alphas);
 108     }
 109 
 110     // alpha blue comes before solid blue in color map
 111     private static IndexColorModel createColorModelWithAlphaBlueFirst() {
 112         byte[] alphas = {(byte)51, (byte)255};
 113         return new IndexColorModel(1, 2, reds, greens, blues, alphas);
 114     }
 115 
 116     private static void paintAlphaRect(Graphics2D graphics) {
 117         AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .2f);
 118         graphics.setComposite(alpha);
 119         graphics.setColor(Color.BLUE);
 120         graphics.fillRect(0, 0, 100, 100);
 121     }
 122 
 123     private static void paintOntoPanel(Graphics2D graphics) {
 124         graphics.drawImage(image1, 0, 0, null);
 125         graphics.drawImage(image2, 100, 0, null);
 126     }
 127 }