test/jdk/java/awt/Focus/ClearLwQueueBreakTest/ClearLwQueueBreakTest.java
author serb
Wed, 27 Feb 2019 18:46:55 -0800
changeset 54231 e4813eded7cb
parent 50834 9cf279436b9d
permissions -rw-r--r--
8213110: Remove the use of applets in automatic tests Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50834
9cf279436b9d 8205153: Delete "standard instructions" machinery in the open automated tests
serb
parents: 47216
diff changeset
     2
 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
  @test
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 5506
diff changeset
    26
  @key headful
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
  @bug       6496958
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  @summary   Tests that breaking the proccess of clearing LW requests doesn't break focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  @library    ../../regtesthelpers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
  @build      Util
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
  @run       main ClearLwQueueBreakTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import test.java.awt.regtesthelpers.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.atomic.AtomicBoolean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
54231
e4813eded7cb 8213110: Remove the use of applets in automatic tests
serb
parents: 50834
diff changeset
    40
public class ClearLwQueueBreakTest {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    JFrame f1 = new JFrame("frame");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    JFrame f2 = new JFrame("frame");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    JButton b = new JButton("button");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    JTextField tf1 = new JTextField("     ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    JTextField tf2 = new JTextField("     ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    JTextField tf3 = new JTextField("     ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    AtomicBoolean typed = new AtomicBoolean(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    FocusListener listener1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    FocusListener listener2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    Robot robot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        ClearLwQueueBreakTest app = new ClearLwQueueBreakTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        app.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        app.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        robot = Util.createRobot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        b.addActionListener(new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    f2.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        tf2.addKeyListener(new KeyAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                public void keyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    if (e.getKeyChar() == '9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        synchronized (typed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                            typed.set(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                            typed.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        tf3.addKeyListener(new KeyAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                public void keyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    if (e.getKeyChar() == '8') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        synchronized (typed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                            typed.set(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                            typed.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        listener1 = new FocusAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    b.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    tf1.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    tf1.setFocusable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    tf2.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        listener2 = new FocusAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    b.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    tf1.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    tf2.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    tf2.setFocusable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        f1.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        f1.add(tf1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        f1.add(tf2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        f1.add(tf3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        f1.setLayout(new FlowLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        f1.pack();
54231
e4813eded7cb 8213110: Remove the use of applets in automatic tests
serb
parents: 50834
diff changeset
   114
        f1.setLocationRelativeTo(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        f1.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Util.waitForIdle(robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * Break the sequence of LW requests in the middle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * Test that the last request succeeds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        f2.addFocusListener(listener1);
50834
9cf279436b9d 8205153: Delete "standard instructions" machinery in the open automated tests
serb
parents: 47216
diff changeset
   123
        System.out.println("Stage 1.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        test1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * Break the last LW request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * Test that focus is restored correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        f2.removeFocusListener(listener1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        f2.addFocusListener(listener2);
50834
9cf279436b9d 8205153: Delete "standard instructions" machinery in the open automated tests
serb
parents: 47216
diff changeset
   133
        System.out.println("Stage 2.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        test2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
50834
9cf279436b9d 8205153: Delete "standard instructions" machinery in the open automated tests
serb
parents: 47216
diff changeset
   136
        System.out.println("Test passed.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    void test1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Util.clickOnComp(b, robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Util.waitForIdle(robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (!tf2.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new TestFailedException("target component didn't get focus!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        robot.keyPress(KeyEvent.VK_9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        robot.keyRelease(KeyEvent.VK_9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        synchronized (typed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (!Util.waitForCondition(typed, 2000)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw new TestFailedException("key char couldn't be typed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Util.clickOnComp(tf3, robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        Util.waitForIdle(robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (!tf3.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new Error("a text field couldn't be focused.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        typed.set(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        robot.keyPress(KeyEvent.VK_8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        robot.delay(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        robot.keyRelease(KeyEvent.VK_8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        synchronized (typed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (!Util.waitForCondition(typed, 2000)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                throw new TestFailedException("key char couldn't be typed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    void test2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        Util.clickOnComp(b, robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Util.waitForIdle(robot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (!b.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new TestFailedException("focus wasn't restored correctly!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
class TestFailedException extends RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    TestFailedException(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        super("Test failed: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}