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