Path: blob/master/test/jdk/java/awt/Focus/6981400/Test3.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 Test332*/3334// A menu item in a frame should not be auto-selected when switching by Alt+TAB back and forth.3536import java.awt.*;37import javax.swing.*;38import java.awt.event.*;39import test.java.awt.regtesthelpers.Util;4041public class Test3 {42static JFrame f = new JFrame("Frame");43static JMenuBar bar = new JMenuBar();44static JMenu menu = new JMenu("File");45static JMenuItem item = new JMenuItem("Save");4647static JButton b0 = new JButton("b0");48static JButton b1 = new JButton("b1");4950static Robot robot;5152public static void main(String[] args) {53Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {54public void eventDispatched(AWTEvent e) {55System.err.println(e);56}57}, KeyEvent.KEY_EVENT_MASK);5859try {60robot = new Robot();61} catch (AWTException ex) {62throw new RuntimeException("Error: can't create Robot");63}6465try {66UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");67} catch (Exception e) {}6869b0.addFocusListener(new FocusAdapter() {70public void focusLost(FocusEvent f) {71try {72Thread.sleep(2000);73} catch (Exception e) {}74}75});7677menu.add(item);78bar.add(menu);79f.setJMenuBar(bar);8081f.add(b0);82f.add(b1);8384f.setLayout(new FlowLayout());85f.setSize(400, 100);86f.setVisible(true);87Util.waitForIdle(robot);8889if (!b0.hasFocus()) {90Util.clickOnComp(b0, robot);91Util.waitForIdle(robot);92if (!b0.hasFocus()) {93throw new RuntimeException("Error: can't focus " + b0);94}95}9697test();9899System.out.println("Test passed.");100}101102public static void test() {103robot.keyPress(KeyEvent.VK_TAB);104robot.delay(50);105robot.keyRelease(KeyEvent.VK_TAB);106robot.delay(50);107108robot.keyPress(KeyEvent.VK_ALT);109robot.delay(50);110robot.keyPress(KeyEvent.VK_TAB);111robot.delay(50);112robot.keyRelease(KeyEvent.VK_ALT);113robot.delay(50);114robot.keyRelease(KeyEvent.VK_TAB);115116robot.delay(500);117118robot.keyPress(KeyEvent.VK_ALT);119robot.delay(50);120robot.keyPress(KeyEvent.VK_TAB);121robot.delay(50);122robot.keyRelease(KeyEvent.VK_ALT);123robot.delay(50);124robot.keyRelease(KeyEvent.VK_TAB);125126// Control shot.127Util.clickOnTitle(f, robot);128Util.waitForIdle(robot);129130if (menu.isSelected()) {131throw new RuntimeException("Test failed: the menu gets selected");132}133if (!b1.hasFocus()) {134throw new RuntimeException("Test failed: the button is not a focus owner " + b1);135}136}137}138139140