Path: blob/master/test/jdk/java/awt/Focus/ClearGlobalFocusOwnerTest/ClearGlobalFocusOwnerTest.java
41152 views
/*1* Copyright (c) 2009, 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 439055527@summary Synopsis: clearGlobalFocusOwner() is not trigerring permanent FOCUS_LOST event28@author [email protected], anton.tarasov: area=awt.focus29@library ../../regtesthelpers30@build Util31@run main ClearGlobalFocusOwnerTest32*/3334import java.awt.*;35import java.awt.event.*;36import test.java.awt.regtesthelpers.Util;3738public class ClearGlobalFocusOwnerTest {39static volatile boolean isFocusLost = false;40static Frame frame = new Frame("Test frame");41static Button button = new Button("Test button");4243public static void main(String[] args) {44button.addFocusListener(new FocusAdapter() {45public void focusLost(FocusEvent fe) {46if (fe.isTemporary()) {47throw new TestFailedException("the FocusLost event is temporary: " + fe);48}49isFocusLost = true;50}51});5253frame.add(button);54frame.pack();55frame.setVisible(true);5657Util.waitForIdle(null);5859if (!button.hasFocus()) {60button.requestFocus();61Util.waitForIdle(null);62if (!button.hasFocus()) {63throw new TestErrorException("couldn't focus " + button);64}65}6667KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();6869Util.waitForIdle(null);7071if (!isFocusLost) {72throw new TestFailedException("no FocusLost event happened on clearGlobalFocusOwner");73}7475System.out.println("Test passed.");76}77}7879/**80* Thrown when the behavior being verified is found wrong.81*/82class TestFailedException extends RuntimeException {83TestFailedException(String msg) {84super("Test failed: " + msg);85}86}8788/**89* Thrown when an error not related to the behavior being verified is encountered.90*/91class TestErrorException extends RuntimeException {92TestErrorException(String msg) {93super("Unexpected error: " + msg);94}95}969798