Path: blob/master/test/jdk/java/awt/Focus/RequestFocusByCause/RequestFocusByCauseTest.java
41152 views
/*1* Copyright (c) 2016, 2017, 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 815443427* @summary Open the request focus methods of the java.awt.Component which accept28* FocusEvent.Cause29* @run main RequestFocusByCauseTest30*/3132import java.awt.*;33import java.awt.event.FocusEvent;34import java.awt.event.FocusListener;3536public class RequestFocusByCauseTest {37static boolean success;3839public static void main(String[] args) throws Exception {40testRequestFocusCause();41testRequestFocusTemporaryCause();42testRequestFocusInWindowCause();43System.out.println("ok");44}4546private static void testRequestFocusCause() throws AWTException {47Frame frame = new Frame();48Component c = new Button();49frame.add(new Button());50frame.add(c);51c.addFocusListener(new FocusListener() {52@Override53public void focusGained(FocusEvent e) {54success = e.getCause() == FocusEvent.Cause.UNEXPECTED;55}5657@Override58public void focusLost(FocusEvent e) {}59});60Robot robot = new Robot();6162try {63frame.setVisible(true);64robot.waitForIdle();65robot.delay(200);66success = false;6768c.requestFocus(FocusEvent.Cause.UNEXPECTED);69robot.waitForIdle();70robot.delay(200);71if(!success) {72throw new RuntimeException("request failed");73}74} finally {75frame.dispose();76}77}7879private static void testRequestFocusTemporaryCause() throws AWTException {80Frame frame = new Frame();81frame.add(new Button() {82@Override83protected boolean requestFocus(boolean temporary,84FocusEvent.Cause cause) {85success = cause == FocusEvent.Cause.ROLLBACK;86return super.requestFocus(temporary, cause);87}88});89Component c = new Button() {90@Override91public void requestFocus() {92super.requestFocus();93setFocusable(false);94}95};96frame.add(c);97Robot robot = new Robot();9899try {100frame.setVisible(true);101robot.waitForIdle();102robot.delay(200);103104success = false;105c.requestFocus();106robot.waitForIdle();107robot.delay(200);108109110if(!success) {111throw new RuntimeException("rollback request is not triggered");112}113} finally {114frame.dispose();115}116}117118private static void testRequestFocusInWindowCause() throws AWTException {119Frame frame = new Frame();120Component c = new Button();121frame.add(new Button());122frame.add(c);123c.addFocusListener(new FocusListener() {124@Override125public void focusGained(FocusEvent e) {126success = e.getCause() == FocusEvent.Cause.UNEXPECTED;127}128129@Override130public void focusLost(FocusEvent e) {131}132});133Robot robot = new Robot();134135try {136frame.setVisible(true);137robot.waitForIdle();138robot.delay(200);139success = false;140141c.requestFocusInWindow(FocusEvent.Cause.UNEXPECTED);142robot.waitForIdle();143robot.delay(200);144if (!success) {145throw new RuntimeException("request in window failed");146}147} finally {148frame.dispose();149}150}151}152153154155