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