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