1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @key headful
  27  * @bug     6981400
  28  * @summary Tabbing between textfiled do not work properly when ALT+TAB
  29  * @author  anton.tarasov
  30  * @library ../../regtesthelpers
  31  * @build   Util
  32  * @run     main Test3
  33  */
  34 
  35 // A menu item in a frame should not be auto-selected when switching by Alt+TAB back and forth.
  36 
  37 import java.awt.*;
  38 import javax.swing.*;
  39 import java.awt.event.*;
  40 import test.java.awt.regtesthelpers.Util;
  41 
  42 public class Test3 {
  43     static JFrame f = new JFrame("Frame");
  44     static JMenuBar bar = new JMenuBar();
  45     static JMenu menu = new JMenu("File");
  46     static JMenuItem item = new JMenuItem("Save");
  47 
  48     static JButton b0 = new JButton("b0");
  49     static JButton b1 = new JButton("b1");
  50 
  51     static Robot robot;
  52 
  53     public static void main(String[] args) {
  54         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  55             public void eventDispatched(AWTEvent e) {
  56                 System.err.println(e);
  57             }
  58         }, KeyEvent.KEY_EVENT_MASK);
  59 
  60         try {
  61             robot = new Robot();
  62         } catch (AWTException ex) {
  63             throw new RuntimeException("Error: can't create Robot");
  64         }
  65 
  66         try {
  67             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  68         } catch (Exception e) {}
  69 
  70         b0.addFocusListener(new FocusAdapter() {
  71             public void focusLost(FocusEvent f) {
  72                 try {
  73                     Thread.sleep(2000);
  74                 } catch (Exception e) {}
  75             }
  76         });
  77 
  78         menu.add(item);
  79         bar.add(menu);
  80         f.setJMenuBar(bar);
  81 
  82         f.add(b0);
  83         f.add(b1);
  84 
  85         f.setLayout(new FlowLayout());
  86         f.setSize(400, 100);
  87         f.setVisible(true);
  88         Util.waitForIdle(robot);
  89 
  90         if (!b0.hasFocus()) {
  91             Util.clickOnComp(b0, robot);
  92             Util.waitForIdle(robot);
  93             if (!b0.hasFocus()) {
  94                 throw new RuntimeException("Error: can't focus " + b0);
  95             }
  96         }
  97 
  98         test();
  99 
 100         System.out.println("Test passed.");
 101     }
 102 
 103     public static void test() {
 104         robot.keyPress(KeyEvent.VK_TAB);
 105         robot.delay(50);
 106         robot.keyRelease(KeyEvent.VK_TAB);
 107         robot.delay(50);
 108 
 109         robot.keyPress(KeyEvent.VK_ALT);
 110         robot.delay(50);
 111         robot.keyPress(KeyEvent.VK_TAB);
 112         robot.delay(50);
 113         robot.keyRelease(KeyEvent.VK_ALT);
 114         robot.delay(50);
 115         robot.keyRelease(KeyEvent.VK_TAB);
 116 
 117         robot.delay(500);
 118 
 119         robot.keyPress(KeyEvent.VK_ALT);
 120         robot.delay(50);
 121         robot.keyPress(KeyEvent.VK_TAB);
 122         robot.delay(50);
 123         robot.keyRelease(KeyEvent.VK_ALT);
 124         robot.delay(50);
 125         robot.keyRelease(KeyEvent.VK_TAB);
 126 
 127         // Control shot.
 128         Util.clickOnTitle(f, robot);
 129         Util.waitForIdle(robot);
 130 
 131         if (menu.isSelected()) {
 132             throw new RuntimeException("Test failed: the menu gets selected");
 133         }
 134         if (!b1.hasFocus()) {
 135             throw new RuntimeException("Test failed: the button is not a focus owner " + b1);
 136         }
 137     }
 138 }
 139 
 140