1 /*
   2  * Copyright (c) 2011, 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 import sun.awt.SunToolkit;
  25 
  26 import java.awt.Button;
  27 import java.awt.Canvas;
  28 import java.awt.Checkbox;
  29 import java.awt.Choice;
  30 import java.awt.Component;
  31 import java.awt.Container;
  32 import java.awt.Dimension;
  33 import java.awt.Frame;
  34 import java.awt.Graphics;
  35 import java.awt.GridLayout;
  36 import java.awt.Label;
  37 import java.awt.List;
  38 import java.awt.Panel;
  39 import java.awt.ScrollPane;
  40 import java.awt.Scrollbar;
  41 import java.awt.TextArea;
  42 import java.awt.TextField;
  43 import java.awt.Toolkit;
  44 import java.awt.image.BufferedImage;
  45 
  46 /*
  47   @test
  48   @key headful
  49   @bug 6596915
  50   @summary Test Component.paintAll() method
  51   @author sergey.bylokhov@oracle.com: area=awt.component
  52   @run main PaintAll
  53 */
  54 public class PaintAll {
  55 
  56     private static volatile boolean lwPainted;
  57     private static volatile boolean buttonPainted;
  58     private static volatile boolean canvasPainted;
  59     private static volatile boolean checkboxPainted;
  60     private static volatile boolean choicePainted;
  61     private static volatile boolean containerPainted;
  62     private static volatile boolean framePainted;
  63     private static volatile boolean labelPainted;
  64     private static volatile boolean listPainted;
  65     private static volatile boolean panelPainted;
  66     private static volatile boolean scrollbarPainted;
  67     private static volatile boolean scrollPanePainted;
  68     private static volatile boolean textAreaPainted;
  69     private static volatile boolean textFieldPainted;
  70 
  71     private static final Button buttonStub = new Button() {
  72         @Override
  73         public void paint(final Graphics g) {
  74             buttonPainted = true;
  75         }
  76     };
  77 
  78     private static final Canvas canvasStub = new Canvas() {
  79         @Override
  80         public void paint(final Graphics g) {
  81             canvasPainted = true;
  82         }
  83     };
  84 
  85     private static final Checkbox checkboxStub = new Checkbox() {
  86         @Override
  87         public void paint(final Graphics g) {
  88             checkboxPainted = true;
  89         }
  90     };
  91 
  92     private static final Choice choiceStub = new Choice() {
  93         @Override
  94         public void paint(final Graphics g) {
  95             choicePainted = true;
  96         }
  97     };
  98 
  99     private static final Component lwComponentStub = new Component() {
 100         @Override
 101         public void paint(final Graphics g) {
 102             lwPainted = true;
 103         }
 104     };
 105 
 106     private static final Container containerStub = new Container() {
 107         @Override
 108         public void paint(final Graphics g) {
 109             containerPainted = true;
 110         }
 111     };
 112 
 113     private static final Frame frame = new Frame() {
 114         @Override
 115         public void paint(final Graphics g) {
 116             super.paint(g);
 117             framePainted = true;
 118         }
 119     };
 120 
 121     private static final Label labelStub = new Label() {
 122         @Override
 123         public void paint(final Graphics g) {
 124             labelPainted = true;
 125         }
 126     };
 127 
 128     private static final List listStub = new List() {
 129         @Override
 130         public void paint(final Graphics g) {
 131             listPainted = true;
 132         }
 133     };
 134 
 135     private static final Panel panelStub = new Panel() {
 136         @Override
 137         public void paint(final Graphics g) {
 138             panelPainted = true;
 139         }
 140     };
 141 
 142     private static final Scrollbar scrollbarStub = new Scrollbar() {
 143         @Override
 144         public void paint(final Graphics g) {
 145             scrollbarPainted = true;
 146         }
 147     };
 148 
 149     private static final ScrollPane scrollPaneStub = new ScrollPane() {
 150         @Override
 151         public void paint(final Graphics g) {
 152             scrollPanePainted = true;
 153         }
 154     };
 155 
 156     private static final TextArea textAreaStub = new TextArea() {
 157         @Override
 158         public void paint(final Graphics g) {
 159             textAreaPainted = true;
 160         }
 161     };
 162 
 163     private static final TextField textFieldStub = new TextField() {
 164         @Override
 165         public void paint(final Graphics g) {
 166             textFieldPainted = true;
 167         }
 168     };
 169 
 170     public static void main(final String[] args) throws Exception {
 171         //Frame initialisation
 172         final BufferedImage graphicsProducer =
 173                 new BufferedImage(BufferedImage.TYPE_INT_ARGB, 1, 1);
 174 
 175         final Graphics g = graphicsProducer.getGraphics();
 176 
 177         frame.setLayout(new GridLayout());
 178         frame.add(buttonStub);
 179         frame.add(canvasStub);
 180         frame.add(checkboxStub);
 181         frame.add(choiceStub);
 182         frame.add(lwComponentStub);
 183         frame.add(containerStub);
 184         frame.add(labelStub);
 185         frame.add(listStub);
 186         frame.add(panelStub);
 187         frame.add(scrollbarStub);
 188         frame.add(scrollPaneStub);
 189         frame.add(textAreaStub);
 190         frame.add(textFieldStub);
 191         frame.setSize(new Dimension(500, 500));
 192         frame.setLocationRelativeTo(null);
 193         frame.setVisible(true);
 194         sleep();
 195 
 196         //Check results.
 197         validation();
 198 
 199         //Reset all flags to 'false'.
 200         initPaintedFlags();
 201 
 202         //Tested method.
 203         frame.paintAll(g);
 204         sleep();
 205 
 206         //Check results.
 207         validation();
 208         cleanup();
 209     }
 210 
 211     private static void initPaintedFlags() {
 212         lwPainted = false;
 213         buttonPainted = false;
 214         canvasPainted = false;
 215         checkboxPainted = false;
 216         choicePainted = false;
 217         containerPainted = false;
 218         framePainted = false;
 219         labelPainted = false;
 220         listPainted = false;
 221         panelPainted = false;
 222         scrollbarPainted = false;
 223         scrollPanePainted = false;
 224         textAreaPainted = false;
 225         textFieldPainted = false;
 226     }
 227 
 228     private static void validation() {
 229         if (!buttonPainted) {
 230             fail("Paint is not called a Button "
 231                  + "when paintAll() invoked on a parent");
 232         }
 233         if (!canvasPainted) {
 234             fail("Paint is not called a Canvas "
 235                  + "when paintAll() invoked on a parent");
 236         }
 237         if (!checkboxPainted) {
 238             fail("Paint is not called a Checkbox "
 239                  + "when paintAll() invoked on a parent");
 240         }
 241         if (!choicePainted) {
 242             fail("Paint is not called a Choice "
 243                  + "when paintAll() invoked on a parent");
 244         }
 245         if (!lwPainted) {
 246             fail("Paint is not called on a lightweight"
 247                  + " subcomponent when paintAll() invoked on a parent");
 248         }
 249         if (!containerPainted) {
 250             fail("Paint is not called on a Container"
 251                  + " subcomponent when paintAll() invoked on a parent");
 252         }
 253         if (!labelPainted) {
 254             fail("Paint is not called on a Label"
 255                  + " subcomponent when paintAll() invoked on a parent");
 256         }
 257         if (!listPainted) {
 258             fail("Paint is not called on a List"
 259                  + " subcomponent when paintAll() invoked on a parent");
 260         }
 261         if (!panelPainted) {
 262             fail("Paint is not called on a Panel"
 263                  + " subcomponent when paintAll() invoked on a parent");
 264         }
 265         if (!scrollbarPainted) {
 266             fail("Paint is not called on a Scrollbar"
 267                  + " subcomponent when paintAll() invoked on a parent");
 268         }
 269         if (!scrollPanePainted) {
 270             fail("Paint is not called on a ScrollPane"
 271                  + " subcomponent when paintAll() invoked on a parent");
 272         }
 273         if (!textAreaPainted) {
 274             fail("Paint is not called on a TextArea"
 275                  + " subcomponent when paintAll() invoked on a parent");
 276         }
 277         if (!textFieldPainted) {
 278             fail("Paint is not called on a TextField"
 279                  + " subcomponent when paintAll() invoked on a parent");
 280         }
 281         if (!framePainted) {
 282             fail("Paint is not called on a Frame when paintAll()");
 283         }
 284     }
 285 
 286     private static void sleep() {
 287         ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
 288         try {
 289             Thread.sleep(500L);
 290         } catch (InterruptedException ignored) {
 291         }
 292     }
 293 
 294     private static void fail(final String message) {
 295         cleanup();
 296         throw new RuntimeException(message);
 297     }
 298 
 299     private static void cleanup() {
 300         frame.dispose();
 301     }
 302 }