Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ClearGlobalFocusOwnerTest/ClearGlobalFocusOwnerTest.java
48104 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@bug 439055526@summary Synopsis: clearGlobalFocusOwner() is not trigerring permanent FOCUS_LOST event27@author [email protected], anton.tarasov: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main ClearGlobalFocusOwnerTest31*/3233import java.awt.*;34import java.awt.event.*;35import test.java.awt.regtesthelpers.Util;3637public class ClearGlobalFocusOwnerTest {38static volatile boolean isFocusLost = false;39static Frame frame = new Frame("Test frame");40static Button button = new Button("Test button");4142public static void main(String[] args) {43button.addFocusListener(new FocusAdapter() {44public void focusLost(FocusEvent fe) {45if (fe.isTemporary()) {46throw new TestFailedException("the FocusLost event is temporary: " + fe);47}48isFocusLost = true;49}50});5152frame.add(button);53frame.pack();54frame.setVisible(true);5556Util.waitForIdle(null);5758if (!button.hasFocus()) {59button.requestFocus();60Util.waitForIdle(null);61if (!button.hasFocus()) {62throw new TestErrorException("couldn't focus " + button);63}64}6566KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();6768Util.waitForIdle(null);6970if (!isFocusLost) {71throw new TestFailedException("no FocusLost event happened on clearGlobalFocusOwner");72}7374System.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