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 whether an exception is being thrown while trying to draw on AWT peer components
  27  * @summary com.apple.junit.java.graphics.drawable
  28  * @library ../regtesthelpers
  29  * @build VisibilityValidator
  30  * @run junit TestDrawables
  31  */
  32 
  33 import test.java.awt.regtesthelpers.VisibilityValidator;
  34 import junit.framework.*;
  35 
  36 import java.awt.*;
  37 import java.awt.image.BufferedImage;
  38 
  39 public class TestDrawables extends TestCase {
  40     private static final int TIMEOUT = 5000;
  41     public static final Color YELLOW_COLOR = Color.yellow;
  42 
  43     public static Test suite() {
  44         return new TestSuite(TestDrawables.class);
  45     }
  46 
  47     public static void main (String[] args) throws RuntimeException {
  48         TestResult tr = junit.textui.TestRunner.run(suite());
  49         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  50             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  51         }
  52     }
  53 
  54 
  55     protected void setUp() {
  56         initBaseImage();
  57 
  58         fFrame = new Frame("Test Frame");
  59         fFrame.setLayout( new GridLayout(4, 3, 0, 0) );
  60         addAWTComponents();
  61 
  62         fFrame.pack();
  63 
  64         fFrame.setSize(600, 600);
  65         fFrame.setVisible(true);
  66     }
  67 
  68 
  69     // Intialize the base image with a gradient fill.  We could use anything here.
  70     void initBaseImage() {
  71         Graphics2D ig = (Graphics2D) bi.createGraphics();
  72         GradientPaint gp = new GradientPaint( 0,0, Color.red, w, h, Color.cyan, false );
  73         ig.setPaint( gp );
  74         ig.fillRect( 0, 0, w, h);
  75         ig.dispose();
  76     }
  77 
  78 
  79     protected void tearDown() {
  80         fFrame.dispose();
  81     }
  82 
  83     public void testPainting() throws Exception {
  84         fFrame.invalidate();
  85         fFrame.paint(fFrame.getGraphics());
  86         fButton.paint(fButton.getGraphics());
  87         fCanvas.paint(fCanvas.getGraphics());
  88         fCheckbox.paint(fCheckbox.getGraphics());
  89         fChoice.paint(fChoice.getGraphics());
  90         fList.paint(fList.getGraphics());
  91         fScrollbar.paint(fScrollbar.getGraphics());
  92         fTextArea.paint(fTextArea.getGraphics());
  93         fScrollPane.paint(fScrollPane.getGraphics());
  94 
  95 
  96         Robot r = new Robot();
  97 
  98         int col1Center = 100;
  99         int row1Center = 100;
 100         int row2Center = 240;
 101         int row3Center = 380;
 102         int row4Center = 525;
 103 
 104 
 105         Point loc = fFrame.getLocationOnScreen();
 106         int buttonCenterX = loc.x + col1Center;
 107         int buttonCenterY = loc.y + row1Center;
 108 
 109         // We are willing to wait several seconds for contents of the frame to be flushed
 110         long endtime = System.currentTimeMillis() + TIMEOUT;
 111         Color buttonCenterHit = null;
 112         Color checkboxCenterColor = null;
 113         Color listCenterColor = null;
 114         Color textAreaCenterColor = null;
 115 
 116         // Hang out here until we either see what we expect, or we time out
 117         while ( System.currentTimeMillis() < endtime) {
 118             buttonCenterHit     = r.getPixelColor(buttonCenterX, buttonCenterY);
 119             checkboxCenterColor = r.getPixelColor(loc.x + col1Center, loc.y + row2Center);
 120             listCenterColor     = r.getPixelColor(loc.x + col1Center, loc.y + row3Center);
 121             textAreaCenterColor = r.getPixelColor(loc.x + col1Center, loc.y + row4Center);
 122 
 123             if (buttonCenterHit.equals(YELLOW_COLOR)
 124                 && checkboxCenterColor.equals(YELLOW_COLOR)
 125                 && listCenterColor.equals(YELLOW_COLOR)
 126                 && textAreaCenterColor.equals(YELLOW_COLOR)) {
 127                 // hey, contents of the frame have been flushed and look good
 128                 break;
 129             }
 130             else  {
 131                 // we may have looked to quickly, wait a bit and look again
 132                 Thread.sleep(200);
 133             }
 134         }
 135 
 136         VisibilityValidator.assertColorEquals(" Color should be yellow at the center of button",YELLOW_COLOR, buttonCenterHit);
 137         VisibilityValidator.assertColorEquals(" Color should be yellow at the center of the checkbox", YELLOW_COLOR, checkboxCenterColor);
 138         VisibilityValidator.assertColorEquals(" Color should be yellow at the center of the list", YELLOW_COLOR, listCenterColor);
 139         VisibilityValidator.assertColorEquals(" Color should be yellow at the center of the checkbox", YELLOW_COLOR, textAreaCenterColor);
 140     }
 141 
 142 
 143     private void addAWTComponents() {
 144 
 145         fButton  = new DrawableButton("Button");
 146         fButton.setBackground(Color.red);
 147         fButton.setForeground(Color.blue);
 148         fFrame.add(fButton);
 149 
 150         fCanvas = new DrawableCanvas();
 151         fCanvas.setBackground(Color.red);
 152         fCanvas.setForeground(Color.blue);
 153         fFrame.add(fCanvas);
 154 
 155         fCheckbox = new DrawableCheckbox("Checkbox");
 156         fCheckbox.setBackground(Color.red);
 157         fCheckbox.setForeground(Color.blue);
 158         fFrame.add(fCheckbox);
 159 
 160         CheckboxGroup radioButtons = new CheckboxGroup();
 161         Checkbox radiobutton = new DrawableCheckbox("CheckboxGroup 1", radioButtons, true);
 162         radiobutton.setBackground(Color.red);
 163         radiobutton.setForeground(Color.blue);
 164         Checkbox radiobutton2 = new DrawableCheckbox("CheckboxGroup 2", radioButtons, false);
 165         radiobutton2.setBackground(Color.red);
 166         radiobutton2.setForeground(Color.blue);
 167         fFrame.add(radiobutton);
 168         fFrame.add(radiobutton2);
 169 
 170         fChoice = new DrawableChoice();
 171         fChoice.add("Choice1");
 172         fChoice.add("Choice2");
 173         fChoice.add("Choice3");
 174         fChoice.add("Choice4");
 175         fChoice.setBackground(Color.red);
 176         fChoice.setForeground(Color.blue);
 177         fFrame.add(fChoice);
 178 
 179         fList = new DrawableList(4, true);
 180         fList.add("List1");
 181         fList.add("List2");
 182         fList.add("List3");
 183         fList.add("List4");
 184         fList.add("List5");
 185         fList.add("List6");
 186         fList.add("List7");
 187         fList.add("List8");
 188         fList.add("List9");
 189         fList.add("List10");
 190         fList.setBackground(Color.red);
 191         fList.setForeground(Color.blue);
 192         fFrame.add(fList);
 193 
 194         fScrollbar = new DrawableScrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
 195         fScrollbar.setBackground(Color.red);
 196         fScrollbar.setForeground(Color.blue);
 197         fFrame.add(fScrollbar);
 198 
 199         Scrollbar scrollbar2 = new DrawableScrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255);
 200         scrollbar2.setBackground(Color.red);
 201         scrollbar2.setForeground(Color.blue);
 202         fFrame.add(scrollbar2);
 203 
 204         String str = "Text Area\n";
 205         fTextArea = new DrawableTextArea(str+str+str+str+str+str+str+str+str+str+str+str+str+str+str+str+str+str+str+str);
 206         fTextArea.setBackground(Color.red);
 207         fTextArea.setForeground(Color.blue);
 208         fFrame.add(fTextArea);
 209 
 210         str = "Text Field";
 211         TextField textField = new DrawableTextField(str);
 212         textField.setBackground(Color.red);
 213         textField.setForeground(Color.blue);
 214         fFrame.add(textField);
 215 
 216         fScrollPane = new DrawableScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
 217         fScrollPane.setBackground(Color.red);
 218         fScrollPane.setForeground(Color.blue);
 219         ImageThingy imageThingy = new ImageThingy();
 220         fScrollPane.add(imageThingy);
 221         fFrame.add(fScrollPane);
 222     }
 223 
 224     class ImageThingy extends Panel {
 225         Image image;
 226         public ImageThingy() {
 227             super();
 228         }
 229         public void paint(Graphics g) {
 230             g.drawImage(bi, 0, 0, null);
 231         }
 232         public Dimension getPreferredSize() {
 233             return new Dimension(w, h);
 234         }
 235     }
 236 
 237 
 238     private class DrawableButton extends Button {
 239         public DrawableButton(String title) {
 240             super(title);
 241         }
 242 
 243         public void paint(Graphics g) {
 244             (new CompDrawer(this)).paint(g);
 245         }
 246     }
 247 
 248     private class DrawableCanvas extends Canvas {
 249         public void paint(Graphics g) {
 250             (new CompDrawer(this)).paint(g);
 251         }
 252     }
 253 
 254 
 255     private class DrawableChoice extends Choice {
 256         public void paint(Graphics g) {
 257             (new CompDrawer(this)).paint(g);
 258         }
 259     }
 260 
 261     private class DrawableCheckbox extends Checkbox {
 262         public DrawableCheckbox(String title) {
 263             super(title);
 264         }
 265 
 266         public DrawableCheckbox(String title, CheckboxGroup group, boolean b) {
 267             super(title, group, b);
 268         }
 269 
 270         public void paint(Graphics g) {
 271             (new CompDrawer(this)).paint(g);
 272         }
 273     }
 274 
 275     private class DrawableList extends List {
 276         public DrawableList(int i, boolean b) {
 277             super(i, b);
 278         }
 279 
 280         public void paint(Graphics g) {
 281             (new CompDrawer(this)).paint(g);
 282         }
 283     }
 284 
 285     private class DrawableScrollbar extends Scrollbar {
 286         public DrawableScrollbar(int orientation, int value, int visible, int minimum, int maximum) {
 287             super(orientation, value, visible, minimum, maximum);
 288         }
 289 
 290         public void paint(Graphics g) {
 291             (new CompDrawer(this)).paint(g);
 292         }
 293     }
 294 
 295 
 296     private class DrawableTextArea extends TextArea {
 297         public DrawableTextArea(String title) {
 298             super(title);
 299         }
 300 
 301         public void paint(Graphics g) {
 302             (new CompDrawer(this)).paint(g);
 303         }
 304     }
 305 
 306     private class DrawableTextField extends TextField {
 307         public DrawableTextField(String title) {
 308             super(title);
 309         }
 310 
 311         public void paint(Graphics g) {
 312             (new CompDrawer(this)).paint(g);
 313         }
 314     }
 315 
 316     private class DrawableScrollPane extends ScrollPane {
 317         public  DrawableScrollPane(int policy) {
 318             super(policy);
 319         }
 320 
 321         public void paint(Graphics g) {
 322             (new CompDrawer(this)).paint(g);
 323         }
 324     }
 325 
 326     private class CompDrawer {
 327         public CompDrawer(Component comp) {
 328             comp_ = comp;
 329         }
 330 
 331 
 332         public void paint(Graphics g) {
 333             g.setColor(Color.black);
 334             int ww = comp_.getWidth();
 335             int hh = comp_.getHeight();
 336             g.drawLine(0, 0, ww, hh);
 337             g.drawLine(ww, 0, 0, hh);
 338 
 339             Graphics g2 = comp_.getGraphics();
 340             g2.setColor(TestDrawables.YELLOW_COLOR);
 341             g2.fillOval(ww / 2 - 10, hh / 2 - 10, 20, 20);
 342         }
 343 
 344         private Component comp_ = null;
 345     }
 346 
 347     static final int        w = 400;
 348     static final int        h = 400;
 349     static final int        siw = w/ 4;
 350     static final int        sih = h / 4;
 351 
 352     GraphicsEnvironment     ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 353     GraphicsDevice          gd = ge.getDefaultScreenDevice();
 354     GraphicsConfiguration   gc = gd.getDefaultConfiguration();
 355     BufferedImage           bi = gc.createCompatibleImage( w, h );
 356 
 357     private Frame fFrame = null;
 358     private Button fButton = null;
 359     private Canvas fCanvas = null;
 360     private Checkbox fCheckbox = null;
 361     private Choice fChoice = null;
 362     private List fList = null;
 363     private Scrollbar fScrollbar = null;
 364     private TextArea fTextArea = null;
 365     private ScrollPane fScrollPane = null;
 366 }