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