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