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