1 /*
   2  * Copyright (c) 2007, 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   @bug       6496958
  27   @summary   Tests that breaking the proccess of clearing LW requests doesn't break focus.
  28   @author    anton.tarasov@...: area=awt-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 java.applet.Applet;
  38 import test.java.awt.regtesthelpers.Util;
  39 import java.util.concurrent.atomic.AtomicBoolean;
  40 
  41 public class ClearLwQueueBreakTest extends Applet {
  42     JFrame f1 = new JFrame("frame");
  43     JFrame f2 = new JFrame("frame");
  44     JButton b = new JButton("button");
  45     JTextField tf1 = new JTextField("     ");
  46     JTextField tf2 = new JTextField("     ");
  47     JTextField tf3 = new JTextField("     ");
  48     AtomicBoolean typed = new AtomicBoolean(false);
  49     FocusListener listener1;
  50     FocusListener listener2;
  51 
  52     Robot robot;
  53 
  54     public static void main(String[] args) {
  55         ClearLwQueueBreakTest app = new ClearLwQueueBreakTest();
  56         app.init();
  57         app.start();
  58     }
  59 
  60     public void init() {
  61         robot = Util.createRobot();
  62 
  63         // Create instructions for the user here, as well as set up
  64         // the environment -- set the layout manager, add buttons,
  65         // etc.
  66         this.setLayout (new BorderLayout ());
  67         Sysout.createDialogWithInstructions(new String[]
  68             {"This is an automatic test. Simply wait until it is done."
  69             });
  70     }
  71 
  72     public void start() {
  73         b.addActionListener(new ActionListener() {
  74                 public void actionPerformed(ActionEvent e) {
  75                     f2.setVisible(true);
  76                 }
  77             });
  78         tf2.addKeyListener(new KeyAdapter() {
  79                 public void keyTyped(KeyEvent e) {
  80                     if (e.getKeyChar() == '9') {
  81                         synchronized (typed) {
  82                             typed.set(true);
  83                             typed.notifyAll();
  84                         }
  85                     }
  86                 }
  87             });
  88         tf3.addKeyListener(new KeyAdapter() {
  89                 public void keyTyped(KeyEvent e) {
  90                     if (e.getKeyChar() == '8') {
  91                         synchronized (typed) {
  92                             typed.set(true);
  93                             typed.notifyAll();
  94                         }
  95                     }
  96                 }
  97             });
  98 
  99         listener1 = new FocusAdapter() {
 100                 public void focusGained(FocusEvent e) {
 101                     b.requestFocus();
 102                     tf1.requestFocus();
 103                     tf1.setFocusable(false);
 104                     tf2.requestFocus();
 105                 }
 106             };
 107 
 108         listener2 = new FocusAdapter() {
 109                 public void focusGained(FocusEvent e) {
 110                     b.requestFocus();
 111                     tf1.requestFocus();
 112                     tf2.requestFocus();
 113                     tf2.setFocusable(false);
 114                 }
 115             };
 116 
 117         f1.add(b);
 118         f1.add(tf1);
 119         f1.add(tf2);
 120         f1.add(tf3);
 121         f1.setLayout(new FlowLayout());
 122         f1.pack();
 123         f1.setVisible(true);
 124         Util.waitForIdle(robot);
 125 
 126         /*
 127          * Break the sequence of LW requests in the middle.
 128          * Test that the last request succeeds
 129          */
 130         f2.addFocusListener(listener1);
 131         Sysout.println("Stage 1.");
 132         test1();
 133 
 134 
 135         /*
 136          * Break the last LW request.
 137          * Test that focus is restored correctly.
 138          */
 139         f2.removeFocusListener(listener1);
 140         f2.addFocusListener(listener2);
 141         Sysout.println("Stage 2.");
 142         test2();
 143 
 144         Sysout.println("Test passed.");
 145     }
 146 
 147     void test1() {
 148         Util.clickOnComp(b, robot);
 149         Util.waitForIdle(robot);
 150 
 151         if (!tf2.hasFocus()) {
 152             throw new TestFailedException("target component didn't get focus!");
 153         }
 154 
 155         robot.keyPress(KeyEvent.VK_9);
 156         robot.delay(50);
 157         robot.keyRelease(KeyEvent.VK_9);
 158 
 159         synchronized (typed) {
 160             if (!Util.waitForCondition(typed, 2000)) {
 161                 throw new TestFailedException("key char couldn't be typed!");
 162             }
 163         }
 164 
 165         Util.clickOnComp(tf3, robot);
 166         Util.waitForIdle(robot);
 167 
 168         if (!tf3.hasFocus()) {
 169             throw new Error("a text field couldn't be focused.");
 170         }
 171 
 172         typed.set(false);
 173         robot.keyPress(KeyEvent.VK_8);
 174         robot.delay(50);
 175         robot.keyRelease(KeyEvent.VK_8);
 176 
 177         synchronized (typed) {
 178             if (!Util.waitForCondition(typed, 2000)) {
 179                 throw new TestFailedException("key char couldn't be typed!");
 180             }
 181         }
 182     }
 183 
 184     void test2() {
 185         Util.clickOnComp(b, robot);
 186         Util.waitForIdle(robot);
 187 
 188         if (!b.hasFocus()) {
 189             throw new TestFailedException("focus wasn't restored correctly!");
 190         }
 191     }
 192 }
 193 
 194 class TestFailedException extends RuntimeException {
 195     TestFailedException(String msg) {
 196         super("Test failed: " + msg);
 197     }
 198 }
 199 
 200 /****************************************************
 201  Standard Test Machinery
 202  DO NOT modify anything below -- it's a standard
 203   chunk of code whose purpose is to make user
 204   interaction uniform, and thereby make it simpler
 205   to read and understand someone else's test.
 206  ****************************************************/
 207 
 208 /**
 209  This is part of the standard test machinery.
 210  It creates a dialog (with the instructions), and is the interface
 211   for sending text messages to the user.
 212  To print the instructions, send an array of strings to Sysout.createDialog
 213   WithInstructions method.  Put one line of instructions per array entry.
 214  To display a message for the tester to see, simply call Sysout.println
 215   with the string to be displayed.
 216  This mimics System.out.println but works within the test harness as well
 217   as standalone.
 218  */
 219 
 220 class Sysout
 221 {
 222     static TestDialog dialog;
 223 
 224     public static void createDialogWithInstructions( String[] instructions )
 225     {
 226         dialog = new TestDialog( new Frame(), "Instructions" );
 227         dialog.printInstructions( instructions );
 228 //        dialog.setVisible(true);
 229         println( "Any messages for the tester will display here." );
 230     }
 231 
 232     public static void createDialog( )
 233     {
 234         dialog = new TestDialog( new Frame(), "Instructions" );
 235         String[] defInstr = { "Instructions will appear here. ", "" } ;
 236         dialog.printInstructions( defInstr );
 237 //        dialog.setVisible(true);
 238         println( "Any messages for the tester will display here." );
 239     }
 240 
 241 
 242     public static void printInstructions( String[] instructions )
 243     {
 244         dialog.printInstructions( instructions );
 245     }
 246 
 247 
 248     public static void println( String messageIn )
 249     {
 250         dialog.displayMessage( messageIn );
 251     }
 252 
 253 }// Sysout  class
 254 
 255 /**
 256   This is part of the standard test machinery.  It provides a place for the
 257    test instructions to be displayed, and a place for interactive messages
 258    to the user to be displayed.
 259   To have the test instructions displayed, see Sysout.
 260   To have a message to the user be displayed, see Sysout.
 261   Do not call anything in this dialog directly.
 262   */
 263 class TestDialog extends Dialog
 264 {
 265 
 266     TextArea instructionsText;
 267     TextArea messageText;
 268     int maxStringLength = 80;
 269 
 270     //DO NOT call this directly, go through Sysout
 271     public TestDialog( Frame frame, String name )
 272     {
 273         super( frame, name );
 274         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 275         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 276         add( "North", instructionsText );
 277 
 278         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 279         add("Center", messageText);
 280 
 281         pack();
 282 
 283 //        setVisible(true);
 284     }// TestDialog()
 285 
 286     //DO NOT call this directly, go through Sysout
 287     public void printInstructions( String[] instructions )
 288     {
 289         //Clear out any current instructions
 290         instructionsText.setText( "" );
 291 
 292         //Go down array of instruction strings
 293 
 294         String printStr, remainingStr;
 295         for( int i=0; i < instructions.length; i++ )
 296         {
 297             //chop up each into pieces maxSringLength long
 298             remainingStr = instructions[ i ];
 299             while( remainingStr.length() > 0 )
 300             {
 301                 //if longer than max then chop off first max chars to print
 302                 if( remainingStr.length() >= maxStringLength )
 303                 {
 304                     //Try to chop on a word boundary
 305                     int posOfSpace = remainingStr.
 306                         lastIndexOf( ' ', maxStringLength - 1 );
 307 
 308                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 309 
 310                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 311                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 312                 }
 313                 //else just print
 314                 else
 315                 {
 316                     printStr = remainingStr;
 317                     remainingStr = "";
 318                 }
 319 
 320                 instructionsText.append( printStr + "\n" );
 321 
 322             }// while
 323 
 324         }// for
 325 
 326     }//printInstructions()
 327 
 328     //DO NOT call this directly, go through Sysout
 329     public void displayMessage( String messageIn )
 330     {
 331         messageText.append( messageIn + "\n" );
 332         System.out.println(messageIn);
 333     }
 334 
 335 }// TestDialog  class