Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/6981400/Test1.java
48452 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* @bug 698140026* @summary Tabbing between textfiled do not work properly when ALT+TAB27* @author anton.tarasov28* @library ../../regtesthelpers29* @build Util30* @run main Test131*/3233// This test shows a frame with four focusable components: b0, b1, b2, b3.34// Then it presses Tab three times. EDT is freezed for a while on the first FOCUS_LOST event.35// Meantime, the test clicks in a component of another frame and then clicks in the title36// of the original frame. When EDT awakes and all the queued events get processed,37// the other frame should ones gain focus and then pass it to the original frame.38// The b3 component of the orinial frame should finally become a focus owner.39// The FOCUS_LOST/FOCUS_GAINED events order in the original frame is tracked and should be:40// b0 -> b1 -> b2 -> b3.4142import java.awt.*;43import java.awt.event.*;44import java.util.ArrayList;45import java.util.Arrays;46import java.util.List;47import javax.swing.*;48import test.java.awt.regtesthelpers.Util;4950public class Test1 {51static JFrame f0 = new JFrame("base_frame") { public String getName() {return "base_frame";} };52static JButton f0b0 = new JB("b0");53static JButton f0b1 = new JB("b1");54static JButton f0b2 = new JB("b2");55static JButton f0b3 = new JB("b3");5657static JFrame f1 = new JFrame("swing_frame") { public String getName() {return "swing_frame";} };58static JButton f1b0 = new JButton("button");5960static Frame f2 = new Frame("awt_frame") { public String getName() {return "awt_frame";} };61static Button f2b0 = new Button("button");6263static Robot robot;6465static List<Component> gainedList = new ArrayList<Component>();66static List<Component> lostList = new ArrayList<Component>();6768static Component[] refGainedList = new Component[] {f0b1, f0b2, f0b3, f0b3};69static Component[] refLostList = new Component[] {f0b0, f0b1, f0b2, f0b3};7071static boolean tracking;7273public static void main(String[] args) {74Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {75public void eventDispatched(AWTEvent e) {76System.out.println(e);77}78}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);7980try {81robot = new Robot();82} catch (AWTException ex) {83throw new RuntimeException("Error: can't create Robot");84}8586f0.add(f0b0);87f0.add(f0b1);88f0.add(f0b2);89f0.add(f0b3);90f0.setLayout(new FlowLayout());91f0.setBounds(0, 100, 400, 200);9293f1.add(f1b0);94f1.setBounds(0, 400, 400, 200);9596f2.add(f2b0);97f2.setBounds(0, 400, 400, 200);9899f0b0.addFocusListener(new FocusAdapter() {100@Override101public void focusLost(FocusEvent e) {102try {103Thread.sleep(1000);104} catch (Exception ex) {}105}106});107108//109// Case 1. Test against swing JFrame.110//111112f1.setVisible(true);113f0.setVisible(true);114115Util.waitForIdle(robot);116117if (!f0b0.isFocusOwner()) {118Util.clickOnComp(f0b0, robot);119Util.waitForIdle(robot);120if (!f0b0.isFocusOwner()) {121throw new RuntimeException("Error: can't focus the component " + f0b0);122}123}124125System.out.println("\nTest case 1: swing frame\n");126test(f1b0);127128//129// Case 2. Test against awt Frame.130//131132tracking = false;133gainedList.clear();134lostList.clear();135136f1.dispose();137f2.setAutoRequestFocus(false);138f2.setVisible(true);139Util.waitForIdle(robot);140141Util.clickOnComp(f0b0, robot);142Util.waitForIdle(robot);143if (!f0b0.isFocusOwner()) {144throw new RuntimeException("Error: can't focus the component " + f0b0);145}146147System.out.println("\nTest case 2: awt frame\n");148test(f2b0);149150System.out.println("\nTest passed.");151}152153public static void test(Component compToClick) {154tracking = true;155156robot.keyPress(KeyEvent.VK_TAB);157robot.delay(50);158robot.keyRelease(KeyEvent.VK_TAB);159robot.delay(50);160161robot.keyPress(KeyEvent.VK_TAB);162robot.delay(50);163robot.keyRelease(KeyEvent.VK_TAB);164robot.delay(50);165166robot.keyPress(KeyEvent.VK_TAB);167robot.delay(50);168robot.keyRelease(KeyEvent.VK_TAB);169170robot.delay(50);171Util.clickOnComp(compToClick, robot);172173robot.delay(50);174Util.clickOnTitle(f0, robot);175176Util.waitForIdle(robot);177178if (!f0b3.isFocusOwner()) {179throw new RuntimeException("Test failed: f0b3 is not a focus owner");180}181182if (!"sun.awt.X11.XToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {183184if (!Arrays.asList(refGainedList).equals(gainedList)) {185System.out.println("gained list: " + gainedList);186throw new RuntimeException("Test failed: wrong FOCUS_GAINED events order");187}188if (!Arrays.asList(refLostList).equals(lostList)) {189System.out.println("lost list: " + lostList);190throw new RuntimeException("Test failed: wrong FOCUS_LOST events order");191}192}193}194}195196class JB extends JButton {197String name;198199public JB(String name) {200super(name);201this.name = name;202203addFocusListener(new FocusListener() {204public void focusGained(FocusEvent e) {205if (Test1.tracking)206Test1.gainedList.add(e.getComponent());207}208209public void focusLost(FocusEvent e) {210if (Test1.tracking)211Test1.lostList.add(e.getComponent());212}213});214}215216public String toString() {217return "[" + name + "]";218}219}220221222