jdk/test/java/awt/Focus/WindowIsFocusableAccessByThreadsTest/WindowIsFocusableAccessByThreadsTest.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 26008 ccab11700f07
child 45028 b0ea3c0bfb81
permissions -rw-r--r--
8159690: [TESTBUG] Mark headful tests with @key headful. Reviewed-by: simonis, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26008
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
     1
/*
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
     2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
     4
 *
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
     8
 *
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    13
 * accompanied this code).
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    14
 *
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    18
 *
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    21
 * questions.
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    22
 */
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    23
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    24
/*
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    25
  @test
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26008
diff changeset
    26
  @key headful
26008
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    27
  @bug      8047288
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    28
  @summary  Tests method isFocusable of Window component. It should be accessed only from EDT
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    29
  @author   artem.malinko@oracle.com
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    30
  @library  ../../regtesthelpers
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    31
  @build    Util
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    32
  @run      main WindowIsFocusableAccessByThreadsTest
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    33
*/
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    34
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    35
import test.java.awt.regtesthelpers.Util;
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    36
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    37
import javax.swing.*;
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    38
import java.awt.*;
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    39
import java.util.concurrent.atomic.AtomicBoolean;
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    40
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    41
public class WindowIsFocusableAccessByThreadsTest {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    42
    private static AtomicBoolean testPassed = new AtomicBoolean(true);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    43
    private static volatile TestFrame frame;
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    44
    private static volatile TestWindow window;
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    45
    private static volatile Button openWindowBtn;
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    46
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    47
    public static void main(String[] args) {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    48
        frame = new TestFrame("Test EDT access to Window components");
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    49
        window = new TestWindow(frame);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    50
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    51
        SwingUtilities.invokeLater(WindowIsFocusableAccessByThreadsTest::init);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    52
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    53
        Util.waitTillShown(frame);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    54
        Robot robot = Util.createRobot();
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    55
        Util.clickOnComp(frame, robot, 100);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    56
        Util.clickOnComp(openWindowBtn, robot, 100);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    57
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    58
        Util.waitTillShown(window);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    59
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    60
        if (!testPassed.get()) {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    61
            throw new RuntimeException("Window component methods has been accessed not " +
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    62
                    "from Event Dispatching Thread");
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    63
        }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    64
    }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    65
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    66
    private static void init() {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    67
        frame.setSize(400, 400);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    68
        frame.setLayout(new FlowLayout());
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    69
        openWindowBtn = new Button("open window");
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    70
        openWindowBtn.addActionListener(e -> {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    71
            window.setSize(100, 100);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    72
            window.setLocation(400, 100);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    73
            window.setVisible(true);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    74
        });
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    75
        frame.add(openWindowBtn);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    76
        frame.setVisible(true);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    77
    }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    78
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    79
    private static void testThread() {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    80
        if (!SwingUtilities.isEventDispatchThread()) {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    81
            testPassed.set(false);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    82
        }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    83
    }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    84
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    85
    private static class TestWindow extends Window {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    86
        public TestWindow(Frame owner) {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    87
            super(owner);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    88
        }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    89
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    90
        // isFocusable method is final and we can't add this test to it.
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    91
        // But it invokes getFocusableWindowState and here we can check
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    92
        // if thread is EDT.
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    93
        @Override
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    94
        public boolean getFocusableWindowState() {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    95
            testThread();
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    96
            return super.getFocusableWindowState();
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    97
        }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    98
    }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
    99
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   100
    private static class TestFrame extends Frame {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   101
        private TestFrame(String title) throws HeadlessException {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   102
            super(title);
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   103
        }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   104
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   105
        // isFocusable method is final and we can't add this test to it.
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   106
        // But it invokes getFocusableWindowState and here we can check
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   107
        // if thread is EDT.
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   108
        @Override
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   109
        public boolean getFocusableWindowState() {
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   110
            testThread();
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   111
            return super.getFocusableWindowState();
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   112
        }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   113
    }
ccab11700f07 8047288: Fixes endless loop on mac caused by invoking Windows.isFocusable() on Appkit thread.
mcherkas
parents:
diff changeset
   114
}