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  * @bug     6981400
  27  * @summary Tabbing between textfiled do not work properly when ALT+TAB
  28  * @author  anton.tarasov
  29  * @library ../../regtesthelpers
  30  * @build   Util
  31  * @run     main Test3
  32  */
  33 
  34 // A menu item in a frame should not be auto-selected when switching by Alt+TAB back and forth.
  35 
  36 import java.awt.*;
  37 import javax.swing.*;
  38 import java.awt.event.*;
  39 import test.java.awt.regtesthelpers.Util;
  40 
  41 public class Test3 {
  42     static JFrame f = new JFrame("Frame");
  43     static JMenuBar bar = new JMenuBar();
  44     static JMenu menu = new JMenu("File");
  45     static JMenuItem item = new JMenuItem("Save");
  46 
  47     static JButton b0 = new JButton("b0");
  48     static JButton b1 = new JButton("b1");
  49 
  50     static Robot robot;
  51 
  52     public static void main(String[] args) {
  53         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  54             public void eventDispatched(AWTEvent e) {
  55                 System.err.println(e);
  56             }
  57         }, KeyEvent.KEY_EVENT_MASK);
  58 
  59         try {
  60             robot = new Robot();
  61         } catch (AWTException ex) {
  62             throw new RuntimeException("Error: can't create Robot");
  63         }
  64 
  65         try {
  66             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  67         } catch (Exception e) {}
  68 
  69         b0.addFocusListener(new FocusAdapter() {
  70             public void focusLost(FocusEvent f) {
  71                 try {
  72                     Thread.sleep(2000);
  73                 } catch (Exception e) {}
  74             }
  75         });
  76 
  77         menu.add(item);
  78         bar.add(menu);
  79         f.setJMenuBar(bar);
  80 
  81         f.add(b0);
  82         f.add(b1);
  83 
  84         f.setLayout(new FlowLayout());
  85         f.setSize(400, 100);
  86         f.setVisible(true);
  87         Util.waitForIdle(robot);
  88 
  89         if (!b0.hasFocus()) {
  90             Util.clickOnComp(b0, robot);
  91             Util.waitForIdle(robot);
  92             if (!b0.hasFocus()) {
  93                 throw new RuntimeException("Error: can't focus " + b0);
  94             }
  95         }
  96 
  97         test();
  98 
  99         System.out.println("Test passed.");
 100     }
 101 
 102     public static void test() {
 103         robot.keyPress(KeyEvent.VK_TAB);
 104         robot.delay(50);
 105         robot.keyRelease(KeyEvent.VK_TAB);
 106         robot.delay(50);
 107 
 108         robot.keyPress(KeyEvent.VK_ALT);
 109         robot.delay(50);
 110         robot.keyPress(KeyEvent.VK_TAB);
 111         robot.delay(50);
 112         robot.keyRelease(KeyEvent.VK_ALT);
 113         robot.delay(50);
 114         robot.keyRelease(KeyEvent.VK_TAB);
 115 
 116         robot.delay(500);
 117 
 118         robot.keyPress(KeyEvent.VK_ALT);
 119         robot.delay(50);
 120         robot.keyPress(KeyEvent.VK_TAB);
 121         robot.delay(50);
 122         robot.keyRelease(KeyEvent.VK_ALT);
 123         robot.delay(50);
 124         robot.keyRelease(KeyEvent.VK_TAB);
 125 
 126         // Control shot.
 127         Util.clickOnTitle(f, robot);
 128         Util.waitForIdle(robot);
 129 
 130         if (menu.isSelected()) {
 131             throw new RuntimeException("Test failed: the menu gets selected");
 132         }
 133         if (!b1.hasFocus()) {
 134             throw new RuntimeException("Test failed: the button is not a focus owner " + b1);
 135         }
 136     }
 137 }
 138 
 139