Path: blob/master/test/jdk/java/awt/Focus/ContainerFocusAutoTransferTest/ContainerFocusAutoTransferTest.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 660717027@summary Tests for focus-auto-transfer.28@library ../../regtesthelpers29@build Util30@run main ContainerFocusAutoTransferTest31*/3233import java.awt.AWTEvent;34import java.awt.Component;35import java.awt.ComponentOrientation;36import java.awt.DefaultKeyboardFocusManager;37import java.awt.KeyboardFocusManager;38import java.awt.Robot;39import java.awt.Color;40import java.awt.FlowLayout;41import java.awt.Toolkit;42import java.awt.event.AWTEventListener;43import java.awt.event.FocusEvent;44import java.awt.event.WindowEvent;45import javax.swing.JButton;46import javax.swing.JFrame;47import javax.swing.JPanel;48import test.java.awt.regtesthelpers.Util;4950public class ContainerFocusAutoTransferTest {51Robot robot;52TestFrame frame;53KeyboardFocusManager kfm;54enum TestCase {55REMOVAL { public String toString() { return "removal"; } },56HIDING { public String toString() { return "hiding"; } },57DISABLING { public String toString() { return "disabling"; } },58DEFOCUSING { public String toString() { return "defocusing"; } };59public abstract String toString();60};6162public static void main(String[] args) {63ContainerFocusAutoTransferTest app = new ContainerFocusAutoTransferTest();64app.init();65app.start();66}6768public void init() {69robot = Util.createRobot();70kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();71Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {72public void eventDispatched(AWTEvent event) {73System.out.println("--> " + event);74}75}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);76}7778public void start() {79System.out.println("*** TEST #1 ***");80test(TestCase.HIDING);8182System.out.println("*** TEST #2 ***");83test(TestCase.REMOVAL);8485System.out.println("*** TEST #3 ***");86test3(TestCase.DISABLING);8788System.out.println("*** TEST #4 ***");89test3(TestCase.DEFOCUSING);9091System.out.println("*** TEST #5 ***");92test4();9394System.out.println("Test passed.");95}9697void test(final TestCase t) {98showFrame();99test1(t); // Test for correct auto-transfer100test2(t); // Test for clearing focus101}102103void test1(final TestCase t) {104Runnable action = new Runnable() {105public void run() {106KeyboardFocusManager.setCurrentKeyboardFocusManager(new TestKFM());107if (t == TestCase.REMOVAL) {108frame.remove(frame.panel0);109110} else if (t == TestCase.HIDING) {111frame.panel0.setVisible(false);112}113frame.repaint();114}115};116if (!Util.trackFocusGained(frame.b3, action, 2000, false)) {117throw new TestFailedException(t + ": focus wasn't transfered as expected!");118}119KeyboardFocusManager.setCurrentKeyboardFocusManager(kfm);120}121122void test2(TestCase t) {123frame.setFocusable(false); // exclude it from the focus cycle124if (t == TestCase.REMOVAL) {125frame.remove(frame.panel1);126127} else if (t == TestCase.HIDING) {128frame.panel1.setVisible(false);129}130frame.repaint();131Util.waitForIdle(robot);132if (kfm.getFocusOwner() != null) {133throw new TestFailedException(t + ": focus wasn't cleared!");134}135}136137void test3(final TestCase t) {138showFrame();139Runnable action = new Runnable() {140public void run() {141if (t == TestCase.DISABLING) {142frame.b0.setEnabled(false);143144} else if (t == TestCase.DEFOCUSING) {145frame.b0.setFocusable(false);146}147}};148if (!Util.trackFocusGained(frame.b1, action, 2000, false)) {149throw new TestFailedException(t + ": focus wasn't transfered as expected!");150}151}152153void test4() {154showFrame();155frame.setFocusableWindowState(false);156Util.waitForIdle(robot);157if (kfm.getFocusOwner() != null) {158throw new TestFailedException("defocusing the frame: focus wasn't cleared!");159}160}161162void showFrame() {163if (frame != null) {164frame.dispose();165Util.waitForIdle(robot);166}167frame = new TestFrame();168frame.setVisible(true);169Util.waitTillShown(frame);170171if (!frame.b0.hasFocus()) {172Util.clickOnComp(frame.b0, robot);173Util.waitForIdle(robot);174if (!frame.b0.hasFocus()) {175throw new TestErrorException("couldn't set focus on " + frame.b2);176}177}178}179180class TestKFM extends DefaultKeyboardFocusManager {181public boolean dispatchEvent(AWTEvent e) {182if (e.getID() == FocusEvent.FOCUS_GAINED) {183System.out.println(e);184Component src = (Component)e.getSource();185if (src == frame.b1 || src == frame.b2) {186throw new TestFailedException("wrong focus transfer on removal!");187}188}189return super.dispatchEvent(e);190}191}192}193194class TestFrame extends JFrame {195public JPanel panel0 = new JPanel();196public JPanel panel1 = new JPanel();197public JButton b0 = new JButton("b0");198public JButton b1 = new JButton("b1");199public JButton b2 = new JButton("b2");200public JButton b3 = new JButton("b3");201public JButton b4 = new JButton("b4");202203public TestFrame() {204super("TestFrame");205206// The change of the orientation and the reverse order of207// adding the buttons to the panel is because in Container.removeNotify()208// the child components are removed in the reverse order.209// We want that the focus owner (b0) would be removed first and210// that the next traversable component would be b1.211panel0.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);212panel0.add(b2);213panel0.add(b1);214panel0.add(b0);215216panel1.add(b3);217panel1.add(b4);218219setLayout(new FlowLayout());220add(panel0);221add(panel1);222pack();223224panel0.setBackground(Color.red);225panel1.setBackground(Color.blue);226}227}228229// Thrown when the behavior being verified is found wrong.230class TestFailedException extends RuntimeException {231TestFailedException(String msg) {232super("Test failed: " + msg);233}234}235236// Thrown when an error not related to the behavior being verified is encountered.237class TestErrorException extends RuntimeException {238TestErrorException(String msg) {239super("Unexpected error: " + msg);240}241}242243244