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.
   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 Test2
  32  */
  33 
  34 // A focus request made after a char is typed ahead shouldn't affect the char's target component.
  35 
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 import test.java.awt.regtesthelpers.Util;
  39 
  40 public class Test2 {
  41     static Frame f = new Frame("frame");
  42     static TextArea t0 = new TextArea(1, 10) { public String toString() { return "[TA-0]";} };
  43     static TextArea t1 = new TextArea(1, 10) { public String toString() { return "[TA-1]";} };
  44     static TextArea t2 = new TextArea(1, 10) { public String toString() { return "[TA-2]";} };
  45 
  46     static volatile boolean passed = true;
  47 
  48     static Robot robot;
  49 
  50     public static void main(String[] args) {
  51         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  52             public void eventDispatched(AWTEvent e) {
  53                 System.out.println(e);
  54                 if (e.getID() == KeyEvent.KEY_TYPED) {
  55                     if (e.getSource() != t1) {
  56                         passed = false;
  57                         throw new RuntimeException("Test failed: the key event has wrong source: " + e);
  58                     }
  59                 }
  60             }
  61         }, FocusEvent.FOCUS_EVENT_MASK | KeyEvent.KEY_EVENT_MASK);
  62 
  63         try {
  64             robot = new Robot();
  65         } catch (AWTException ex) {
  66             throw new RuntimeException("Error: can't create Robot");
  67         }
  68 
  69         f.add(t0);
  70         f.add(t1);
  71         f.add(t2);
  72 
  73         f.setLayout(new FlowLayout());
  74         f.pack();
  75 
  76         t0.addFocusListener(new FocusAdapter() {
  77             public void focusLost(FocusEvent e) {
  78                 try {
  79                     Thread.sleep(3000);
  80                 } catch (Exception ex) {}
  81             }
  82         });
  83 
  84         // The request shouldn't affect the key event delivery.
  85         new Thread(new Runnable() {
  86             public void run() {
  87                 try {
  88                     Thread.sleep(2000);
  89                 } catch (Exception ex) {}
  90                 System.out.println("requesting focus to " + t2);
  91                 t2.requestFocus();
  92             }
  93         }).start();
  94 
  95 
  96         f.setVisible(true);
  97         Util.waitForIdle(robot);
  98 
  99         test();
 100 
 101         if (passed) System.out.println("\nTest passed.");
 102     }
 103 
 104     static void test() {
 105         Util.clickOnComp(t1, robot);
 106 
 107         // The key event should be eventually delivered to t1.
 108         robot.delay(50);
 109         robot.keyPress(KeyEvent.VK_A);
 110         robot.delay(50);
 111         robot.keyRelease(KeyEvent.VK_A);
 112 
 113         Util.waitForIdle(robot);
 114     }
 115 }
 116