Path: blob/master/test/jdk/java/awt/Focus/ClearLwQueueBreakTest/ClearLwQueueBreakTest.java
41152 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 649695827@summary Tests that breaking the proccess of clearing LW requests doesn't break focus.28@library ../../regtesthelpers29@build Util30@run main ClearLwQueueBreakTest31*/3233import java.awt.*;34import javax.swing.*;35import java.awt.event.*;36import test.java.awt.regtesthelpers.Util;37import java.util.concurrent.atomic.AtomicBoolean;3839public class ClearLwQueueBreakTest {40JFrame f1 = new JFrame("frame");41JFrame f2 = new JFrame("frame");42JButton b = new JButton("button");43JTextField tf1 = new JTextField(" ");44JTextField tf2 = new JTextField(" ");45JTextField tf3 = new JTextField(" ");46AtomicBoolean typed = new AtomicBoolean(false);47FocusListener listener1;48FocusListener listener2;4950Robot robot;5152public static void main(String[] args) {53ClearLwQueueBreakTest app = new ClearLwQueueBreakTest();54app.init();55app.start();56}5758public void init() {59robot = Util.createRobot();60}6162public void start() {63b.addActionListener(new ActionListener() {64public void actionPerformed(ActionEvent e) {65f2.setVisible(true);66}67});68tf2.addKeyListener(new KeyAdapter() {69public void keyTyped(KeyEvent e) {70if (e.getKeyChar() == '9') {71synchronized (typed) {72typed.set(true);73typed.notifyAll();74}75}76}77});78tf3.addKeyListener(new KeyAdapter() {79public void keyTyped(KeyEvent e) {80if (e.getKeyChar() == '8') {81synchronized (typed) {82typed.set(true);83typed.notifyAll();84}85}86}87});8889listener1 = new FocusAdapter() {90public void focusGained(FocusEvent e) {91b.requestFocus();92tf1.requestFocus();93tf1.setFocusable(false);94tf2.requestFocus();95}96};9798listener2 = new FocusAdapter() {99public void focusGained(FocusEvent e) {100b.requestFocus();101tf1.requestFocus();102tf2.requestFocus();103tf2.setFocusable(false);104}105};106107f1.add(b);108f1.add(tf1);109f1.add(tf2);110f1.add(tf3);111f1.setLayout(new FlowLayout());112f1.pack();113f1.setLocationRelativeTo(null);114f1.setVisible(true);115Util.waitForIdle(robot);116117/*118* Break the sequence of LW requests in the middle.119* Test that the last request succeeds120*/121f2.addFocusListener(listener1);122System.out.println("Stage 1.");123test1();124125126/*127* Break the last LW request.128* Test that focus is restored correctly.129*/130f2.removeFocusListener(listener1);131f2.addFocusListener(listener2);132System.out.println("Stage 2.");133test2();134135System.out.println("Test passed.");136}137138void test1() {139Util.clickOnComp(b, robot);140Util.waitForIdle(robot);141142if (!tf2.hasFocus()) {143throw new TestFailedException("target component didn't get focus!");144}145146robot.keyPress(KeyEvent.VK_9);147robot.delay(50);148robot.keyRelease(KeyEvent.VK_9);149150synchronized (typed) {151if (!Util.waitForCondition(typed, 2000)) {152throw new TestFailedException("key char couldn't be typed!");153}154}155156Util.clickOnComp(tf3, robot);157Util.waitForIdle(robot);158159if (!tf3.hasFocus()) {160throw new Error("a text field couldn't be focused.");161}162163typed.set(false);164robot.keyPress(KeyEvent.VK_8);165robot.delay(50);166robot.keyRelease(KeyEvent.VK_8);167168synchronized (typed) {169if (!Util.waitForCondition(typed, 2000)) {170throw new TestFailedException("key char couldn't be typed!");171}172}173}174175void test2() {176Util.clickOnComp(b, robot);177Util.waitForIdle(robot);178179if (!b.hasFocus()) {180throw new TestFailedException("focus wasn't restored correctly!");181}182}183}184185class TestFailedException extends RuntimeException {186TestFailedException(String msg) {187super("Test failed: " + msg);188}189}190191192