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 Verify that component's background matches wat was set using setBackground
  27  @summary com.apple.junit.java.awt.Component
  28  @library ../../regtesthelpers
  29  @build VisibilityValidator
  30  @run main TwoInOne
  31  */
  32 
  33 import junit.framework.*;
  34 
  35 import java.awt.*;
  36 import test.java.awt.regtesthelpers.VisibilityValidator;
  37 
  38 public class TwoInOne {
  39     static int width = 400, height = 100;
  40 
  41     static Color color0 = Color.red;
  42     static Color color1 = Color.green;
  43     static Color color2 = Color.blue;
  44     static Frame frame;
  45     static Robot robot;
  46 
  47     public static final void main(String[] args) throws Exception {
  48         try {
  49             init();
  50             VisibilityValidator.setVisibleAndConfirm(frame);
  51             Thread.sleep(200); // Let the user see things.  Not really needed, but original test was up for 2 secs...
  52 
  53             Color c1 = getPixel((width / 2), (height / 2));
  54             VisibilityValidator.assertColorEquals("North canvas should have gotten background color", c1 , color1);
  55 
  56             Color c2 = getPixel((width / 2), height + (height / 2));
  57             VisibilityValidator.assertColorEquals("North canvas should have gotten background color", c2, color2);
  58         }
  59         finally {
  60             frame.dispose();
  61         }
  62     }
  63 
  64     static private void init() throws Exception {
  65 
  66         frame = new Frame("CopyArea");
  67         frame.setBackground(color0);
  68         frame.add(createColoredCanvas(color1), "North");
  69         frame.add(createColoredCanvas(color2), "South");
  70         frame.pack();
  71         robot = new Robot();
  72         robot.setAutoWaitForIdle(true);
  73     }
  74 
  75 
  76     static public Color getPixel(int x, int y) {
  77         int locX = (int) (frame.getLocationOnScreen().getX()) + x;
  78         int locY = (int) (frame.getLocationOnScreen().getY()) + y;
  79         return robot.getPixelColor(locX, locY);
  80     }
  81 
  82     private static Canvas createColoredCanvas( Color color) {
  83         Canvas cCanvas = new Canvas();
  84 
  85         cCanvas.setPreferredSize(new Dimension(width, height));
  86         cCanvas.setMinimumSize(new Dimension(width, height));
  87         cCanvas.setBackground(color);
  88         return cCanvas;
  89     }
  90 }