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