1 /*
2  * Copyright (c) 2007, 2018, 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       6496958
28   @summary   Tests that breaking the proccess of clearing LW requests doesn't break focus.
29   @library    ../../regtesthelpers
30   @build      Util
31   @run       main ClearLwQueueBreakTest
32 */
33 
34 import java.awt.*;
35 import javax.swing.*;
36 import java.awt.event.*;
37 import test.java.awt.regtesthelpers.Util;
38 import java.util.concurrent.atomic.AtomicBoolean;
39 
40 public class ClearLwQueueBreakTest {
41     JFrame f1 = new JFrame("frame");
42     JFrame f2 = new JFrame("frame");
43     JButton b = new JButton("button");
44     JTextField tf1 = new JTextField("     ");
45     JTextField tf2 = new JTextField("     ");
46     JTextField tf3 = new JTextField("     ");
47     AtomicBoolean typed = new AtomicBoolean(false);
48     FocusListener listener1;
49     FocusListener listener2;
50 
51     Robot robot;
52 
main(String[] args)53     public static void main(String[] args) {
54         ClearLwQueueBreakTest app = new ClearLwQueueBreakTest();
55         app.init();
56         app.start();
57     }
58 
init()59     public void init() {
60         robot = Util.createRobot();
61     }
62 
start()63     public void start() {
64         b.addActionListener(new ActionListener() {
65                 public void actionPerformed(ActionEvent e) {
66                     f2.setVisible(true);
67                 }
68             });
69         tf2.addKeyListener(new KeyAdapter() {
70                 public void keyTyped(KeyEvent e) {
71                     if (e.getKeyChar() == '9') {
72                         synchronized (typed) {
73                             typed.set(true);
74                             typed.notifyAll();
75                         }
76                     }
77                 }
78             });
79         tf3.addKeyListener(new KeyAdapter() {
80                 public void keyTyped(KeyEvent e) {
81                     if (e.getKeyChar() == '8') {
82                         synchronized (typed) {
83                             typed.set(true);
84                             typed.notifyAll();
85                         }
86                     }
87                 }
88             });
89 
90         listener1 = new FocusAdapter() {
91                 public void focusGained(FocusEvent e) {
92                     b.requestFocus();
93                     tf1.requestFocus();
94                     tf1.setFocusable(false);
95                     tf2.requestFocus();
96                 }
97             };
98 
99         listener2 = new FocusAdapter() {
100                 public void focusGained(FocusEvent e) {
101                     b.requestFocus();
102                     tf1.requestFocus();
103                     tf2.requestFocus();
104                     tf2.setFocusable(false);
105                 }
106             };
107 
108         f1.add(b);
109         f1.add(tf1);
110         f1.add(tf2);
111         f1.add(tf3);
112         f1.setLayout(new FlowLayout());
113         f1.pack();
114         f1.setLocationRelativeTo(null);
115         f1.setVisible(true);
116         Util.waitForIdle(robot);
117 
118         /*
119          * Break the sequence of LW requests in the middle.
120          * Test that the last request succeeds
121          */
122         f2.addFocusListener(listener1);
123         System.out.println("Stage 1.");
124         test1();
125 
126 
127         /*
128          * Break the last LW request.
129          * Test that focus is restored correctly.
130          */
131         f2.removeFocusListener(listener1);
132         f2.addFocusListener(listener2);
133         System.out.println("Stage 2.");
134         test2();
135 
136         System.out.println("Test passed.");
137     }
138 
test1()139     void test1() {
140         Util.clickOnComp(b, robot);
141         Util.waitForIdle(robot);
142 
143         if (!tf2.hasFocus()) {
144             throw new TestFailedException("target component didn't get focus!");
145         }
146 
147         robot.keyPress(KeyEvent.VK_9);
148         robot.delay(50);
149         robot.keyRelease(KeyEvent.VK_9);
150 
151         synchronized (typed) {
152             if (!Util.waitForCondition(typed, 2000)) {
153                 throw new TestFailedException("key char couldn't be typed!");
154             }
155         }
156 
157         Util.clickOnComp(tf3, robot);
158         Util.waitForIdle(robot);
159 
160         if (!tf3.hasFocus()) {
161             throw new Error("a text field couldn't be focused.");
162         }
163 
164         typed.set(false);
165         robot.keyPress(KeyEvent.VK_8);
166         robot.delay(50);
167         robot.keyRelease(KeyEvent.VK_8);
168 
169         synchronized (typed) {
170             if (!Util.waitForCondition(typed, 2000)) {
171                 throw new TestFailedException("key char couldn't be typed!");
172             }
173         }
174     }
175 
test2()176     void test2() {
177         Util.clickOnComp(b, robot);
178         Util.waitForIdle(robot);
179 
180         if (!b.hasFocus()) {
181             throw new TestFailedException("focus wasn't restored correctly!");
182         }
183     }
184 }
185 
186 class TestFailedException extends RuntimeException {
TestFailedException(String msg)187     TestFailedException(String msg) {
188         super("Test failed: " + msg);
189     }
190 }
191