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