Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/NoAutotransferToDisabledCompTest/NoAutotransferToDisabledCompTest.java
47517 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@bug 468576826@summary Tests that auto-transfering focus doesn't stuck on a disabled component.27@author Anton Tarasov: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main NoAutotransferToDisabledCompTest31*/3233import java.awt.Robot;34import javax.swing.*;35import java.awt.*;36import java.awt.event.*;37import java.applet.Applet;38import test.java.awt.regtesthelpers.Util;3940public class NoAutotransferToDisabledCompTest extends Applet {41Robot robot;42JFrame frame = new JFrame("Frame");43JButton b0 = new JButton("b0");44JButton b1 = new JButton("b1");45JButton b2 = new JButton("b2");4647public static void main(String[] args) {48NoAutotransferToDisabledCompTest app = new NoAutotransferToDisabledCompTest();49app.init();50app.start();51}5253public void init() {54robot = Util.createRobot();55frame.add(b0);56frame.add(b1);57frame.add(b2);58frame.setLayout(new FlowLayout());59frame.pack();6061b1.addActionListener(new ActionListener() {62public void actionPerformed(ActionEvent e) {63b1.setEnabled(false);64b2.setEnabled(false);65}66});67}6869public void start() {70Util.showWindowWait(frame);7172// Request focus on b1.73if (!Util.focusComponent(b1, 2000)) {74throw new TestErrorException("couldn't focus " + b1);75}7677// Activate b1.78robot.keyPress(KeyEvent.VK_SPACE);79robot.delay(50);80robot.keyRelease(KeyEvent.VK_SPACE);81Util.waitForIdle(robot);8283// Check that focus has been transfered to b0.84if (!b0.hasFocus()) {85throw new TestFailedException("focus wasn't auto-transfered properly!");86}87System.out.println("Test passed.");88}89}9091/**92* Thrown when the behavior being verified is found wrong.93*/94class TestFailedException extends RuntimeException {95TestFailedException(String msg) {96super("Test failed: " + msg);97}98}99100/**101* Thrown when an error not related to the behavior being verified is encountered.102*/103class TestErrorException extends RuntimeException {104TestErrorException(String msg) {105super("Unexpected error: " + msg);106}107}108109110111