test/jdk/java/awt/Focus/ConsumeNextKeyTypedOnModalShowTest/ConsumeNextKeyTypedOnModalShowTest.java
author serb
Wed, 27 Feb 2019 18:46:55 -0800
changeset 54231 e4813eded7cb
parent 47216 71c04702a3d5
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:
419
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
     1
/*
54231
e4813eded7cb 8213110: Remove the use of applets in automatic tests
serb
parents: 47216
diff changeset
     2
 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
419
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
     4
 *
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
     8
 *
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    13
 * accompanied this code).
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    14
 *
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 419
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 419
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 419
diff changeset
    21
 * questions.
419
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    22
 */
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    23
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    24
/*
54231
e4813eded7cb 8213110: Remove the use of applets in automatic tests
serb
parents: 47216
diff changeset
    25
  @test
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 5506
diff changeset
    26
  @key headful
419
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    27
  @bug       6637607
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    28
  @summary   Showing a modal dlg on TAB KEY_PRESS shouldn't consume inappropriate KEY_TYPED.
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    29
  @library   ../../regtesthelpers
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    30
  @build     Util
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    31
  @run       main ConsumeNextKeyTypedOnModalShowTest
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    32
*/
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    33
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    34
import java.awt.*;
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    35
import java.awt.event.*;
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    36
import test.java.awt.regtesthelpers.Util;
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    37
54231
e4813eded7cb 8213110: Remove the use of applets in automatic tests
serb
parents: 47216
diff changeset
    38
public class ConsumeNextKeyTypedOnModalShowTest {
419
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    39
    Robot robot;
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    40
    Frame frame = new Frame("Frame");
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    41
    Dialog dialog = new Dialog(frame, "Dialog", true);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    42
    TextField tf0 = new TextField();
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    43
    TextField tf1 = new TextField();
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    44
    Button button = new Button("Button");
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    45
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    46
    public static void main(String[] args) {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    47
        ConsumeNextKeyTypedOnModalShowTest app = new ConsumeNextKeyTypedOnModalShowTest();
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    48
        app.init();
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    49
        app.start();
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    50
    }
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    51
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    52
    public void init() {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    53
        robot = Util.createRobot();
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    54
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    55
        tf0.setPreferredSize(new Dimension(50, 30));
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    56
        tf1.setPreferredSize(new Dimension(50, 30));
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    57
        frame.setLayout(new FlowLayout());
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    58
        frame.add(tf0);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    59
        frame.add(tf1);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    60
        frame.pack();
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    61
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    62
        dialog.add(button);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    63
        dialog.pack();
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    64
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    65
        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    66
            public void eventDispatched(AWTEvent e) {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    67
                if (e.getID() == KeyEvent.KEY_PRESSED && e.getSource() == tf0) {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    68
                    dialog.setVisible(true);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    69
                }
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    70
            }
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    71
        }, KeyEvent.KEY_EVENT_MASK);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    72
    }
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    73
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    74
    public void start() {
54231
e4813eded7cb 8213110: Remove the use of applets in automatic tests
serb
parents: 47216
diff changeset
    75
        frame.setLocationRelativeTo(null);
419
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    76
        frame.setVisible(true);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    77
        Util.waitTillShown(frame);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    78
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    79
        // Show the dialog.
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    80
        robot.keyPress(KeyEvent.VK_TAB);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    81
        robot.delay(50);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    82
        robot.keyRelease(KeyEvent.VK_TAB);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    83
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    84
        Util.waitForIdle(robot);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    85
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    86
        // Dispose the dialog.
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    87
        Runnable action = new Runnable() {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    88
            public void run() {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    89
                dialog.dispose();
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    90
            }
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    91
        };
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    92
        if (!Util.trackFocusGained(tf1, action, 2000, false)) {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    93
            throw new RuntimeException("Test failed: TAB was processed incorrectly!");
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    94
        }
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    95
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    96
        // Check for type-ability.
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    97
        robot.keyPress(KeyEvent.VK_A);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    98
        robot.delay(50);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
    99
        robot.keyRelease(KeyEvent.VK_A);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
   100
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
   101
        Util.waitForIdle(robot);
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
   102
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
   103
        if (tf1.getText().equals("")) {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
   104
            throw new RuntimeException("Test failed: couldn't type a char!");
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
   105
        }
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
   106
        System.out.println("Test passed.");
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
   107
    }
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents:
diff changeset
   108
}