1 /*
   2  * Copyright (c) 2014 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   @bug      8047288
  27   @summary  Tests method isFocusable of Window component. It should be accessed only from EDT
  28   @author   artem.malinko@oracle.com
  29   @library  ../../regtesthelpers
  30   @build    Util
  31   @run      main WindowIsFocusableAccessByThreadsTest
  32 */
  33 
  34 import test.java.awt.regtesthelpers.Util;
  35 
  36 import javax.swing.*;
  37 import java.awt.*;
  38 import java.util.concurrent.atomic.AtomicBoolean;
  39 
  40 public class WindowIsFocusableAccessByThreadsTest {
  41     private static AtomicBoolean testPassed = new AtomicBoolean(true);
  42     private static volatile TestFrame frame;
  43     private static volatile TestWindow window;
  44     private static volatile Button openWindowBtn;
  45 
  46     public static void main(String[] args) {
  47         frame = new TestFrame("Test EDT access to Window components");
  48         window = new TestWindow(frame);
  49 
  50         SwingUtilities.invokeLater(WindowIsFocusableAccessByThreadsTest::init);
  51 
  52         Util.waitTillShown(frame);
  53         Robot robot = Util.createRobot();
  54         Util.clickOnComp(frame, robot, 100);
  55         Util.clickOnComp(openWindowBtn, robot, 100);
  56 
  57         Util.waitTillShown(window);
  58 
  59         if (!testPassed.get()) {
  60             throw new RuntimeException("Window component methods has been accessed not " +
  61                     "from Event Dispatching Thread");
  62         }
  63     }
  64 
  65     private static void init() {
  66         frame.setSize(400, 400);
  67         frame.setLayout(new FlowLayout());
  68         openWindowBtn = new Button("open window");
  69         openWindowBtn.addActionListener(e -> {
  70             window.setSize(100, 100);
  71             window.setLocation(400, 100);
  72             window.setVisible(true);
  73         });
  74         frame.add(openWindowBtn);
  75         frame.setVisible(true);
  76     }
  77 
  78     private static void testThread() {
  79         if (!SwingUtilities.isEventDispatchThread()) {
  80             testPassed.set(false);
  81         }
  82     }
  83 
  84     private static class TestWindow extends Window {
  85         public TestWindow(Frame owner) {
  86             super(owner);
  87         }
  88 
  89         // isFocusable method is final and we can't add this test to it.
  90         // But it invokes getFocusableWindowState and here we can check
  91         // if thread is EDT.
  92         @Override
  93         public boolean getFocusableWindowState() {
  94             testThread();
  95             return super.getFocusableWindowState();
  96         }
  97     }
  98 
  99     private static class TestFrame extends Frame {
 100         private TestFrame(String title) throws HeadlessException {
 101             super(title);
 102         }
 103 
 104         // isFocusable method is final and we can't add this test to it.
 105         // But it invokes getFocusableWindowState and here we can check
 106         // if thread is EDT.
 107         @Override
 108         public boolean getFocusableWindowState() {
 109             testThread();
 110             return super.getFocusableWindowState();
 111         }
 112     }
 113 }