Path: blob/master/test/jdk/java/awt/Focus/NoAutotransferToDisabledCompTest/NoAutotransferToDisabledCompTest.java
41153 views
/*1* Copyright (c) 2008, 2018, 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@key headful26@bug 468576827@summary Tests that auto-transfering focus doesn't stuck on a disabled component.28@library ../../regtesthelpers29@build Util30@run main NoAutotransferToDisabledCompTest31*/3233import java.awt.Robot;34import javax.swing.*;35import java.awt.*;36import java.awt.event.*;37import test.java.awt.regtesthelpers.Util;3839public class NoAutotransferToDisabledCompTest {40Robot robot;41JFrame frame = new JFrame("Frame");42JButton b0 = new JButton("b0");43JButton b1 = new JButton("b1");44JButton b2 = new JButton("b2");4546public static void main(String[] args) {47NoAutotransferToDisabledCompTest app = new NoAutotransferToDisabledCompTest();48app.init();49app.start();50}5152public void init() {53robot = Util.createRobot();54frame.add(b0);55frame.add(b1);56frame.add(b2);57frame.setLayout(new FlowLayout());58frame.pack();5960b1.addActionListener(new ActionListener() {61public void actionPerformed(ActionEvent e) {62b1.setEnabled(false);63b2.setEnabled(false);64}65});66}6768public void start() {69Util.showWindowWait(frame);7071// Request focus on b1.72if (!Util.focusComponent(b1, 2000)) {73throw new TestErrorException("couldn't focus " + b1);74}7576// Activate b1.77robot.keyPress(KeyEvent.VK_SPACE);78robot.delay(50);79robot.keyRelease(KeyEvent.VK_SPACE);80Util.waitForIdle(robot);81robot.delay(2000);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