Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ContainerFocusAutoTransferTest/ContainerFocusAutoTransferTest.java
47525 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 660717026@summary Tests for focus-auto-transfer.27@author Anton Tarasov: area=awt-focus28@library ../../regtesthelpers29@build Util30@run main ContainerFocusAutoTransferTest31*/3233import java.applet.Applet;34import java.awt.AWTEvent;35import java.awt.Component;36import java.awt.ComponentOrientation;37import java.awt.DefaultKeyboardFocusManager;38import java.awt.KeyboardFocusManager;39import java.awt.Robot;40import java.awt.Color;41import java.awt.FlowLayout;42import java.awt.Toolkit;43import java.awt.event.AWTEventListener;44import java.awt.event.FocusEvent;45import java.awt.event.WindowEvent;46import javax.swing.JButton;47import javax.swing.JFrame;48import javax.swing.JPanel;49import test.java.awt.regtesthelpers.Util;5051public class ContainerFocusAutoTransferTest extends Applet {52Robot robot;53TestFrame frame;54KeyboardFocusManager kfm;55enum TestCase {56REMOVAL { public String toString() { return "removal"; } },57HIDING { public String toString() { return "hiding"; } },58DISABLING { public String toString() { return "disabling"; } },59DEFOCUSING { public String toString() { return "defocusing"; } };60public abstract String toString();61};6263public static void main(String[] args) {64ContainerFocusAutoTransferTest app = new ContainerFocusAutoTransferTest();65app.init();66app.start();67}6869public void init() {70robot = Util.createRobot();71kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();72Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {73public void eventDispatched(AWTEvent event) {74System.out.println("--> " + event);75}76}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);77}7879public void start() {80System.out.println("*** TEST #1 ***");81test(TestCase.HIDING);8283System.out.println("*** TEST #2 ***");84test(TestCase.REMOVAL);8586System.out.println("*** TEST #3 ***");87test3(TestCase.DISABLING);8889System.out.println("*** TEST #4 ***");90test3(TestCase.DEFOCUSING);9192System.out.println("*** TEST #5 ***");93test4();9495System.out.println("Test passed.");96}9798void test(final TestCase t) {99showFrame();100test1(t); // Test for correct auto-transfer101test2(t); // Test for clearing focus102}103104void test1(final TestCase t) {105Runnable action = new Runnable() {106public void run() {107KeyboardFocusManager.setCurrentKeyboardFocusManager(new TestKFM());108if (t == TestCase.REMOVAL) {109frame.remove(frame.panel0);110111} else if (t == TestCase.HIDING) {112frame.panel0.setVisible(false);113}114frame.repaint();115}116};117if (!Util.trackFocusGained(frame.b3, action, 2000, false)) {118throw new TestFailedException(t + ": focus wasn't transfered as expected!");119}120KeyboardFocusManager.setCurrentKeyboardFocusManager(kfm);121}122123void test2(TestCase t) {124frame.setFocusable(false); // exclude it from the focus cycle125if (t == TestCase.REMOVAL) {126frame.remove(frame.panel1);127128} else if (t == TestCase.HIDING) {129frame.panel1.setVisible(false);130}131frame.repaint();132Util.waitForIdle(robot);133if (kfm.getFocusOwner() != null) {134throw new TestFailedException(t + ": focus wasn't cleared!");135}136}137138void test3(final TestCase t) {139showFrame();140Runnable action = new Runnable() {141public void run() {142if (t == TestCase.DISABLING) {143frame.b0.setEnabled(false);144145} else if (t == TestCase.DEFOCUSING) {146frame.b0.setFocusable(false);147}148}};149if (!Util.trackFocusGained(frame.b1, action, 2000, false)) {150throw new TestFailedException(t + ": focus wasn't transfered as expected!");151}152}153154void test4() {155showFrame();156frame.setFocusableWindowState(false);157Util.waitForIdle(robot);158if (kfm.getFocusOwner() != null) {159throw new TestFailedException("defocusing the frame: focus wasn't cleared!");160}161}162163void showFrame() {164if (frame != null) {165frame.dispose();166Util.waitForIdle(robot);167}168frame = new TestFrame();169frame.setVisible(true);170Util.waitTillShown(frame);171172if (!frame.b0.hasFocus()) {173Util.clickOnComp(frame.b0, robot);174Util.waitForIdle(robot);175if (!frame.b0.hasFocus()) {176throw new TestErrorException("couldn't set focus on " + frame.b2);177}178}179}180181class TestKFM extends DefaultKeyboardFocusManager {182public boolean dispatchEvent(AWTEvent e) {183if (e.getID() == FocusEvent.FOCUS_GAINED) {184System.out.println(e);185Component src = (Component)e.getSource();186if (src == frame.b1 || src == frame.b2) {187throw new TestFailedException("wrong focus transfer on removal!");188}189}190return super.dispatchEvent(e);191}192}193}194195class TestFrame extends JFrame {196public JPanel panel0 = new JPanel();197public JPanel panel1 = new JPanel();198public JButton b0 = new JButton("b0");199public JButton b1 = new JButton("b1");200public JButton b2 = new JButton("b2");201public JButton b3 = new JButton("b3");202public JButton b4 = new JButton("b4");203204public TestFrame() {205super("TestFrame");206207// The change of the orientation and the reverse order of208// adding the buttons to the panel is because in Container.removeNotify()209// the child components are removed in the reverse order.210// We want that the focus owner (b0) would be removed first and211// that the next traversable component would be b1.212panel0.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);213panel0.add(b2);214panel0.add(b1);215panel0.add(b0);216217panel1.add(b3);218panel1.add(b4);219220setLayout(new FlowLayout());221add(panel0);222add(panel1);223pack();224225panel0.setBackground(Color.red);226panel1.setBackground(Color.blue);227}228}229230// Thrown when the behavior being verified is found wrong.231class TestFailedException extends RuntimeException {232TestFailedException(String msg) {233super("Test failed: " + msg);234}235}236237// Thrown when an error not related to the behavior being verified is encountered.238class TestErrorException extends RuntimeException {239TestErrorException(String msg) {240super("Unexpected error: " + msg);241}242}243244245