Path: blob/master/test/jdk/java/awt/Focus/RequestFocusToDisabledCompTest/RequestFocusToDisabledCompTest.java
41152 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 it's possible to manually request focus on a disabled component.28@library ../../regtesthelpers29@build Util30@run main RequestFocusToDisabledCompTest31*/3233import java.awt.Robot;34import javax.swing.*;35import java.awt.*;36import test.java.awt.regtesthelpers.Util;3738public class RequestFocusToDisabledCompTest {39Robot robot;40JFrame frame = new JFrame("Frame");41JButton b0 = new JButton("b0");42JButton b1 = new JButton("b1");4344public static void main(String[] args) {45RequestFocusToDisabledCompTest app = new RequestFocusToDisabledCompTest();46app.init();47app.start();48}4950public void init() {51robot = Util.createRobot();52frame.add(b0);53frame.add(b1);54frame.setLayout(new FlowLayout());55frame.pack();5657b1.setEnabled(false);58}5960public void start() {61Util.showWindowWait(frame);6263if (!b0.hasFocus()) {64// Request focus on b0.65if (!Util.focusComponent(b0, 2000)) {66throw new TestErrorException("couldn't focus " + b0);67}68}6970// Try to request focus on b1.71if (!Util.focusComponent(b1, 2000)) {72throw new TestFailedException("focus wasn't requested on disabled " + b1);73}74System.out.println("Test passed.");75}76}7778/**79* Thrown when the behavior being verified is found wrong.80*/81class TestFailedException extends RuntimeException {82TestFailedException(String msg) {83super("Test failed: " + msg);84}85}8687/**88* Thrown when an error not related to the behavior being verified is encountered.89*/90class TestErrorException extends RuntimeException {91TestErrorException(String msg) {92super("Unexpected error: " + msg);93}94}959697