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 Test1
  32  */
  33 
  34 // This test shows a frame with four focusable components: b0, b1, b2, b3.
  35 // Then it presses Tab three times. EDT is freezed for a while on the first FOCUS_LOST event.
  36 // Meantime, the test clicks in a component of another frame and then clicks in the title
  37 // of the original frame. When EDT awakes and all the queued events get processed,
  38 // the other frame should ones gain focus and then pass it to the original frame.
  39 // The b3 component of the orinial frame should finally become a focus owner.
  40 // The FOCUS_LOST/FOCUS_GAINED events order in the original frame is tracked and should be:
  41 // b0 -> b1 -> b2 -> b3.
  42 
  43 import java.awt.*;
  44 import java.awt.event.*;
  45 import java.util.ArrayList;
  46 import java.util.Arrays;
  47 import java.util.List;
  48 import javax.swing.*;
  49 import test.java.awt.regtesthelpers.Util;
  50 
  51 public class Test1 {
  52     static JFrame f0 = new JFrame("base_frame") { public String getName() {return "base_frame";} };
  53     static JButton f0b0 = new JB("b0");
  54     static JButton f0b1 = new JB("b1");
  55     static JButton f0b2 = new JB("b2");
  56     static JButton f0b3 = new JB("b3");
  57 
  58     static JFrame f1 = new JFrame("swing_frame") { public String getName() {return "swing_frame";} };
  59     static JButton f1b0 = new JButton("button");
  60 
  61     static Frame f2 = new Frame("awt_frame") { public String getName() {return "awt_frame";} };
  62     static Button f2b0 = new Button("button");
  63 
  64     static Robot robot;
  65 
  66     static List<Component> gainedList = new ArrayList<Component>();
  67     static List<Component> lostList = new ArrayList<Component>();
  68 
  69     static Component[] refGainedList = new Component[] {f0b1, f0b2, f0b3, f0b3};
  70     static Component[] refLostList = new Component[] {f0b0, f0b1, f0b2, f0b3};
  71 
  72     static boolean tracking;
  73 
  74     public static void main(String[] args) {
  75         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  76             public void eventDispatched(AWTEvent e) {
  77                 System.out.println(e);
  78             }
  79         }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);
  80 
  81         try {
  82             robot = new Robot();
  83         } catch (AWTException ex) {
  84             throw new RuntimeException("Error: can't create Robot");
  85         }
  86 
  87         f0.add(f0b0);
  88         f0.add(f0b1);
  89         f0.add(f0b2);
  90         f0.add(f0b3);
  91         f0.setLayout(new FlowLayout());
  92         f0.setBounds(0, 100, 400, 200);
  93 
  94         f1.add(f1b0);
  95         f1.setBounds(0, 400, 400, 200);
  96 
  97         f2.add(f2b0);
  98         f2.setBounds(0, 400, 400, 200);
  99 
 100         f0b0.addFocusListener(new FocusAdapter() {
 101             @Override
 102             public void focusLost(FocusEvent e) {
 103                 try {
 104                     Thread.sleep(1000);
 105                 } catch (Exception ex) {}
 106             }
 107         });
 108 
 109         //
 110         // Case 1. Test against swing JFrame.
 111         //
 112 
 113         f1.setVisible(true);
 114         f0.setVisible(true);
 115 
 116         Util.waitForIdle(robot);
 117 
 118         if (!f0b0.isFocusOwner()) {
 119             Util.clickOnComp(f0b0, robot);
 120             Util.waitForIdle(robot);
 121             if (!f0b0.isFocusOwner()) {
 122                 throw new RuntimeException("Error: can't focus the component " + f0b0);
 123             }
 124         }
 125 
 126         System.out.println("\nTest case 1: swing frame\n");
 127         test(f1b0);
 128 
 129         //
 130         // Case 2. Test against awt Frame.
 131         //
 132 
 133         tracking = false;
 134         gainedList.clear();
 135         lostList.clear();
 136 
 137         f1.dispose();
 138         f2.setAutoRequestFocus(false);
 139         f2.setVisible(true);
 140         Util.waitForIdle(robot);
 141 
 142         Util.clickOnComp(f0b0, robot);
 143         Util.waitForIdle(robot);
 144         if (!f0b0.isFocusOwner()) {
 145             throw new RuntimeException("Error: can't focus the component " + f0b0);
 146         }
 147 
 148         System.out.println("\nTest case 2: awt frame\n");
 149         test(f2b0);
 150 
 151         System.out.println("\nTest passed.");
 152     }
 153 
 154     public static void test(Component compToClick) {
 155         tracking = true;
 156 
 157         robot.keyPress(KeyEvent.VK_TAB);
 158         robot.delay(50);
 159         robot.keyRelease(KeyEvent.VK_TAB);
 160         robot.delay(50);
 161 
 162         robot.keyPress(KeyEvent.VK_TAB);
 163         robot.delay(50);
 164         robot.keyRelease(KeyEvent.VK_TAB);
 165         robot.delay(50);
 166 
 167         robot.keyPress(KeyEvent.VK_TAB);
 168         robot.delay(50);
 169         robot.keyRelease(KeyEvent.VK_TAB);
 170 
 171         robot.delay(50);
 172         Util.clickOnComp(compToClick, robot);
 173 
 174         robot.delay(50);
 175         Util.clickOnTitle(f0, robot);
 176 
 177         Util.waitForIdle(robot);
 178 
 179         if (!f0b3.isFocusOwner()) {
 180             throw new RuntimeException("Test failed: f0b3 is not a focus owner");
 181         }
 182 
 183         if (!"sun.awt.X11.XToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
 184 
 185             if (!Arrays.asList(refGainedList).equals(gainedList)) {
 186                 System.out.println("gained list: " + gainedList);
 187                 throw new RuntimeException("Test failed: wrong FOCUS_GAINED events order");
 188             }
 189             if (!Arrays.asList(refLostList).equals(lostList)) {
 190                 System.out.println("lost list: " + lostList);
 191                 throw new RuntimeException("Test failed: wrong FOCUS_LOST events order");
 192             }
 193         }
 194     }
 195 }
 196 
 197 class JB extends JButton {
 198     String name;
 199 
 200     public JB(String name) {
 201         super(name);
 202         this.name = name;
 203 
 204         addFocusListener(new FocusListener() {
 205             public void focusGained(FocusEvent e) {
 206                 if (Test1.tracking)
 207                     Test1.gainedList.add(e.getComponent());
 208             }
 209 
 210             public void focusLost(FocusEvent e) {
 211                 if (Test1.tracking)
 212                     Test1.lostList.add(e.getComponent());
 213             }
 214         });
 215     }
 216 
 217     public String toString() {
 218         return "[" + name + "]";
 219     }
 220 }