Path: blob/master/test/jdk/java/awt/Focus/6981400/Test1.java
41153 views
/*1* Copyright (c) 2012, 2013, 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 698140027* @summary Tabbing between textfiled do not work properly when ALT+TAB28* @author anton.tarasov29* @library ../../regtesthelpers30* @build Util31* @run main Test132*/3334// This test shows a frame with four focusable components: b0, b1, b2, b3.35// Then it presses Tab three times. EDT is freezed for a while on the first FOCUS_LOST event.36// Meantime, the test clicks in a component of another frame and then clicks in the title37// of the original frame. When EDT awakes and all the queued events get processed,38// the other frame should ones gain focus and then pass it to the original frame.39// The b3 component of the orinial frame should finally become a focus owner.40// The FOCUS_LOST/FOCUS_GAINED events order in the original frame is tracked and should be:41// b0 -> b1 -> b2 -> b3.4243import java.awt.*;44import java.awt.event.*;45import java.util.ArrayList;46import java.util.Arrays;47import java.util.List;48import javax.swing.*;49import test.java.awt.regtesthelpers.Util;5051public class Test1 {52static JFrame f0 = new JFrame("base_frame") { public String getName() {return "base_frame";} };53static JButton f0b0 = new JB("b0");54static JButton f0b1 = new JB("b1");55static JButton f0b2 = new JB("b2");56static JButton f0b3 = new JB("b3");5758static JFrame f1 = new JFrame("swing_frame") { public String getName() {return "swing_frame";} };59static JButton f1b0 = new JButton("button");6061static Frame f2 = new Frame("awt_frame") { public String getName() {return "awt_frame";} };62static Button f2b0 = new Button("button");6364static Robot robot;6566static List<Component> gainedList = new ArrayList<Component>();67static List<Component> lostList = new ArrayList<Component>();6869static Component[] refGainedList = new Component[] {f0b1, f0b2, f0b3, f0b3};70static Component[] refLostList = new Component[] {f0b0, f0b1, f0b2, f0b3};7172static boolean tracking;7374public static void main(String[] args) {75Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {76public void eventDispatched(AWTEvent e) {77System.out.println(e);78}79}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);8081try {82robot = new Robot();83} catch (AWTException ex) {84throw new RuntimeException("Error: can't create Robot");85}8687f0.add(f0b0);88f0.add(f0b1);89f0.add(f0b2);90f0.add(f0b3);91f0.setLayout(new FlowLayout());92f0.setBounds(0, 100, 400, 200);9394f1.add(f1b0);95f1.setBounds(0, 400, 400, 200);9697f2.add(f2b0);98f2.setBounds(0, 400, 400, 200);99100f0b0.addFocusListener(new FocusAdapter() {101@Override102public void focusLost(FocusEvent e) {103try {104Thread.sleep(1000);105} catch (Exception ex) {}106}107});108109//110// Case 1. Test against swing JFrame.111//112113f1.setVisible(true);114f0.setVisible(true);115116Util.waitForIdle(robot);117118if (!f0b0.isFocusOwner()) {119Util.clickOnComp(f0b0, robot);120Util.waitForIdle(robot);121if (!f0b0.isFocusOwner()) {122throw new RuntimeException("Error: can't focus the component " + f0b0);123}124}125126System.out.println("\nTest case 1: swing frame\n");127test(f1b0);128129//130// Case 2. Test against awt Frame.131//132133tracking = false;134gainedList.clear();135lostList.clear();136137f1.dispose();138f2.setAutoRequestFocus(false);139f2.setVisible(true);140Util.waitForIdle(robot);141142Util.clickOnComp(f0b0, robot);143Util.waitForIdle(robot);144if (!f0b0.isFocusOwner()) {145throw new RuntimeException("Error: can't focus the component " + f0b0);146}147148System.out.println("\nTest case 2: awt frame\n");149test(f2b0);150151System.out.println("\nTest passed.");152}153154public static void test(Component compToClick) {155tracking = true;156157robot.keyPress(KeyEvent.VK_TAB);158robot.delay(50);159robot.keyRelease(KeyEvent.VK_TAB);160robot.delay(50);161162robot.keyPress(KeyEvent.VK_TAB);163robot.delay(50);164robot.keyRelease(KeyEvent.VK_TAB);165robot.delay(50);166167robot.keyPress(KeyEvent.VK_TAB);168robot.delay(50);169robot.keyRelease(KeyEvent.VK_TAB);170171robot.delay(50);172Util.clickOnComp(compToClick, robot);173174robot.delay(50);175Util.clickOnTitle(f0, robot);176177Util.waitForIdle(robot);178179if (!f0b3.isFocusOwner()) {180throw new RuntimeException("Test failed: f0b3 is not a focus owner");181}182183if (!"sun.awt.X11.XToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {184185if (!Arrays.asList(refGainedList).equals(gainedList)) {186System.out.println("gained list: " + gainedList);187throw new RuntimeException("Test failed: wrong FOCUS_GAINED events order");188}189if (!Arrays.asList(refLostList).equals(lostList)) {190System.out.println("lost list: " + lostList);191throw new RuntimeException("Test failed: wrong FOCUS_LOST events order");192}193}194}195}196197class JB extends JButton {198String name;199200public JB(String name) {201super(name);202this.name = name;203204addFocusListener(new FocusListener() {205public void focusGained(FocusEvent e) {206if (Test1.tracking)207Test1.gainedList.add(e.getComponent());208}209210public void focusLost(FocusEvent e) {211if (Test1.tracking)212Test1.lostList.add(e.getComponent());213}214});215}216217public String toString() {218return "[" + name + "]";219}220}221222223