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