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