Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/6981400/Test3.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 Test331*/3233// A menu item in a frame should not be auto-selected when switching by Alt+TAB back and forth.3435import java.awt.*;36import javax.swing.*;37import java.awt.event.*;38import test.java.awt.regtesthelpers.Util;3940public class Test3 {41static JFrame f = new JFrame("Frame");42static JMenuBar bar = new JMenuBar();43static JMenu menu = new JMenu("File");44static JMenuItem item = new JMenuItem("Save");4546static JButton b0 = new JButton("b0");47static JButton b1 = new JButton("b1");4849static Robot robot;5051public static void main(String[] args) {52Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {53public void eventDispatched(AWTEvent e) {54System.err.println(e);55}56}, KeyEvent.KEY_EVENT_MASK);5758try {59robot = new Robot();60} catch (AWTException ex) {61throw new RuntimeException("Error: can't create Robot");62}6364try {65UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");66} catch (Exception e) {}6768b0.addFocusListener(new FocusAdapter() {69public void focusLost(FocusEvent f) {70try {71Thread.sleep(2000);72} catch (Exception e) {}73}74});7576menu.add(item);77bar.add(menu);78f.setJMenuBar(bar);7980f.add(b0);81f.add(b1);8283f.setLayout(new FlowLayout());84f.setSize(400, 100);85f.setVisible(true);86Util.waitForIdle(robot);8788if (!b0.hasFocus()) {89Util.clickOnComp(b0, robot);90Util.waitForIdle(robot);91if (!b0.hasFocus()) {92throw new RuntimeException("Error: can't focus " + b0);93}94}9596test();9798System.out.println("Test passed.");99}100101public static void test() {102robot.keyPress(KeyEvent.VK_TAB);103robot.delay(50);104robot.keyRelease(KeyEvent.VK_TAB);105robot.delay(50);106107robot.keyPress(KeyEvent.VK_ALT);108robot.delay(50);109robot.keyPress(KeyEvent.VK_TAB);110robot.delay(50);111robot.keyRelease(KeyEvent.VK_ALT);112robot.delay(50);113robot.keyRelease(KeyEvent.VK_TAB);114115robot.delay(500);116117robot.keyPress(KeyEvent.VK_ALT);118robot.delay(50);119robot.keyPress(KeyEvent.VK_TAB);120robot.delay(50);121robot.keyRelease(KeyEvent.VK_ALT);122robot.delay(50);123robot.keyRelease(KeyEvent.VK_TAB);124125// Control shot.126Util.clickOnTitle(f, robot);127Util.waitForIdle(robot);128129if (menu.isSelected()) {130throw new RuntimeException("Test failed: the menu gets selected");131}132if (!b1.hasFocus()) {133throw new RuntimeException("Test failed: the button is not a focus owner " + b1);134}135}136}137138139140141