Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ClearLwQueueBreakTest/ClearLwQueueBreakTest.java
47867 views
/*1* Copyright (c) 2007, 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@bug 649695826@summary Tests that breaking the proccess of clearing LW requests doesn't break focus.27@author anton.tarasov@...: area=awt-focus28@library ../../regtesthelpers29@build Util30@run main ClearLwQueueBreakTest31*/3233import java.awt.*;34import javax.swing.*;35import java.awt.event.*;36import java.applet.Applet;37import test.java.awt.regtesthelpers.Util;38import java.util.concurrent.atomic.AtomicBoolean;3940public class ClearLwQueueBreakTest extends Applet {41JFrame f1 = new JFrame("frame");42JFrame f2 = new JFrame("frame");43JButton b = new JButton("button");44JTextField tf1 = new JTextField(" ");45JTextField tf2 = new JTextField(" ");46JTextField tf3 = new JTextField(" ");47AtomicBoolean typed = new AtomicBoolean(false);48FocusListener listener1;49FocusListener listener2;5051Robot robot;5253public static void main(String[] args) {54ClearLwQueueBreakTest app = new ClearLwQueueBreakTest();55app.init();56app.start();57}5859public void init() {60robot = Util.createRobot();6162// Create instructions for the user here, as well as set up63// the environment -- set the layout manager, add buttons,64// etc.65this.setLayout (new BorderLayout ());66Sysout.createDialogWithInstructions(new String[]67{"This is an automatic test. Simply wait until it is done."68});69}7071public void start() {72b.addActionListener(new ActionListener() {73public void actionPerformed(ActionEvent e) {74f2.setVisible(true);75}76});77tf2.addKeyListener(new KeyAdapter() {78public void keyTyped(KeyEvent e) {79if (e.getKeyChar() == '9') {80synchronized (typed) {81typed.set(true);82typed.notifyAll();83}84}85}86});87tf3.addKeyListener(new KeyAdapter() {88public void keyTyped(KeyEvent e) {89if (e.getKeyChar() == '8') {90synchronized (typed) {91typed.set(true);92typed.notifyAll();93}94}95}96});9798listener1 = new FocusAdapter() {99public void focusGained(FocusEvent e) {100b.requestFocus();101tf1.requestFocus();102tf1.setFocusable(false);103tf2.requestFocus();104}105};106107listener2 = new FocusAdapter() {108public void focusGained(FocusEvent e) {109b.requestFocus();110tf1.requestFocus();111tf2.requestFocus();112tf2.setFocusable(false);113}114};115116f1.add(b);117f1.add(tf1);118f1.add(tf2);119f1.add(tf3);120f1.setLayout(new FlowLayout());121f1.pack();122f1.setVisible(true);123Util.waitForIdle(robot);124125/*126* Break the sequence of LW requests in the middle.127* Test that the last request succeeds128*/129f2.addFocusListener(listener1);130Sysout.println("Stage 1.");131test1();132133134/*135* Break the last LW request.136* Test that focus is restored correctly.137*/138f2.removeFocusListener(listener1);139f2.addFocusListener(listener2);140Sysout.println("Stage 2.");141test2();142143Sysout.println("Test passed.");144}145146void test1() {147Util.clickOnComp(b, robot);148Util.waitForIdle(robot);149150if (!tf2.hasFocus()) {151throw new TestFailedException("target component didn't get focus!");152}153154robot.keyPress(KeyEvent.VK_9);155robot.delay(50);156robot.keyRelease(KeyEvent.VK_9);157158synchronized (typed) {159if (!Util.waitForCondition(typed, 2000)) {160throw new TestFailedException("key char couldn't be typed!");161}162}163164Util.clickOnComp(tf3, robot);165Util.waitForIdle(robot);166167if (!tf3.hasFocus()) {168throw new Error("a text field couldn't be focused.");169}170171typed.set(false);172robot.keyPress(KeyEvent.VK_8);173robot.delay(50);174robot.keyRelease(KeyEvent.VK_8);175176synchronized (typed) {177if (!Util.waitForCondition(typed, 2000)) {178throw new TestFailedException("key char couldn't be typed!");179}180}181}182183void test2() {184Util.clickOnComp(b, robot);185Util.waitForIdle(robot);186187if (!b.hasFocus()) {188throw new TestFailedException("focus wasn't restored correctly!");189}190}191}192193class TestFailedException extends RuntimeException {194TestFailedException(String msg) {195super("Test failed: " + msg);196}197}198199/****************************************************200Standard Test Machinery201DO NOT modify anything below -- it's a standard202chunk of code whose purpose is to make user203interaction uniform, and thereby make it simpler204to read and understand someone else's test.205****************************************************/206207/**208This is part of the standard test machinery.209It creates a dialog (with the instructions), and is the interface210for sending text messages to the user.211To print the instructions, send an array of strings to Sysout.createDialog212WithInstructions method. Put one line of instructions per array entry.213To display a message for the tester to see, simply call Sysout.println214with the string to be displayed.215This mimics System.out.println but works within the test harness as well216as standalone.217*/218219class Sysout220{221static TestDialog dialog;222223public static void createDialogWithInstructions( String[] instructions )224{225dialog = new TestDialog( new Frame(), "Instructions" );226dialog.printInstructions( instructions );227// dialog.setVisible(true);228println( "Any messages for the tester will display here." );229}230231public static void createDialog( )232{233dialog = new TestDialog( new Frame(), "Instructions" );234String[] defInstr = { "Instructions will appear here. ", "" } ;235dialog.printInstructions( defInstr );236// dialog.setVisible(true);237println( "Any messages for the tester will display here." );238}239240241public static void printInstructions( String[] instructions )242{243dialog.printInstructions( instructions );244}245246247public static void println( String messageIn )248{249dialog.displayMessage( messageIn );250}251252}// Sysout class253254/**255This is part of the standard test machinery. It provides a place for the256test instructions to be displayed, and a place for interactive messages257to the user to be displayed.258To have the test instructions displayed, see Sysout.259To have a message to the user be displayed, see Sysout.260Do not call anything in this dialog directly.261*/262class TestDialog extends Dialog263{264265TextArea instructionsText;266TextArea messageText;267int maxStringLength = 80;268269//DO NOT call this directly, go through Sysout270public TestDialog( Frame frame, String name )271{272super( frame, name );273int scrollBoth = TextArea.SCROLLBARS_BOTH;274instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );275add( "North", instructionsText );276277messageText = new TextArea( "", 5, maxStringLength, scrollBoth );278add("Center", messageText);279280pack();281282// setVisible(true);283}// TestDialog()284285//DO NOT call this directly, go through Sysout286public void printInstructions( String[] instructions )287{288//Clear out any current instructions289instructionsText.setText( "" );290291//Go down array of instruction strings292293String printStr, remainingStr;294for( int i=0; i < instructions.length; i++ )295{296//chop up each into pieces maxSringLength long297remainingStr = instructions[ i ];298while( remainingStr.length() > 0 )299{300//if longer than max then chop off first max chars to print301if( remainingStr.length() >= maxStringLength )302{303//Try to chop on a word boundary304int posOfSpace = remainingStr.305lastIndexOf( ' ', maxStringLength - 1 );306307if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;308309printStr = remainingStr.substring( 0, posOfSpace + 1 );310remainingStr = remainingStr.substring( posOfSpace + 1 );311}312//else just print313else314{315printStr = remainingStr;316remainingStr = "";317}318319instructionsText.append( printStr + "\n" );320321}// while322323}// for324325}//printInstructions()326327//DO NOT call this directly, go through Sysout328public void displayMessage( String messageIn )329{330messageText.append( messageIn + "\n" );331System.out.println(messageIn);332}333334}// TestDialog class335336337