1 /*
   2  * Copyright (c) 2003, 2014, 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 4799136
  27 @summary Tests that type-ahead for dialog works and doesn't block program
  28 @author Dmitry.Cherepanov@SUN.COM area=awt.focus
  29 @run main EnqueueWithDialogTest
  30 */
  31 
  32 import java.awt.*;
  33 import java.lang.reflect.InvocationTargetException;
  34 import java.awt.event.*;
  35 import java.util.concurrent.CountDownLatch;
  36 import java.util.concurrent.TimeUnit;
  37 
  38 /*
  39  * The purpose of this test is check that the type-head
  40  * works correctly on Windows. That means that the key
  41  * events are not delivered until a focus transfer is
  42  * completed. Another regression test EnqueueWithDialogButton
  43  * doesn't work on Windows because of the bug 6347235.
  44  * This test workaround the bug by means of the removing
  45  * button from the dialog.
  46  */
  47 
  48 public class EnqueueWithDialogTest
  49 {
  50     static Frame f;
  51     static Button b;
  52     static Dialog d;
  53     static CountDownLatch pressLatch = new CountDownLatch(1);
  54     static CountDownLatch robotLatch = new CountDownLatch(1);
  55     static volatile boolean gotFocus = false;
  56     static Robot robot;
  57     public static void main(String args[]) throws Exception {
  58         EnqueueWithDialogTest test = new EnqueueWithDialogTest();
  59         test.init();
  60         test.start();
  61     }
  62     public void init()
  63     {
  64         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  65                 public void eventDispatched(AWTEvent e) {
  66                     if (e instanceof InputEvent){
  67                         System.err.println(e.toString()+","+((InputEvent)e).getWhen());
  68                     }else{
  69                         System.err.println(e.toString());
  70                     }
  71                  }
  72             }, AWTEvent.KEY_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
  73 
  74 
  75         f = new Frame("frame");
  76         f.setPreferredSize(new Dimension(100,100));
  77         f.setLocation(100,50);
  78         b = new Button("press");
  79         d = new Dialog(f, "dialog", true);
  80         d.setPreferredSize(new Dimension(70,70));
  81         d.pack();
  82         d.addKeyListener(new KeyAdapter() {
  83                 public void keyPressed(KeyEvent e) {
  84                     System.err.println("DIALOG pressed: should arrive after got focus");
  85                     d.dispose();
  86                     f.dispose();
  87                     // Typed-ahead key events should only be accepted if
  88                     // they arrive after FOCUS_GAINED
  89                     if (gotFocus) {
  90                         pressLatch.countDown();
  91                     }
  92                 }
  93             });
  94         d.addFocusListener(new FocusAdapter() {
  95                 public void focusGained(FocusEvent e) {
  96                     gotFocus = true;
  97                     System.err.println("DIALOG got focus");
  98                 }
  99             });
 100         f.add(b);
 101         f.pack();
 102         b.addActionListener(new ActionListener() {
 103                 public void actionPerformed(ActionEvent e) {
 104                     System.err.println(e.toString()+","+e.getWhen());
 105                     System.err.println("B pressed");
 106                     robotLatch.countDown();
 107 
 108                     EventQueue.invokeLater(new Runnable() {
 109                             public void run() {
 110                                 waitTillShown(d);
 111                                 EnqueueWithDialogTest.this.d.toFront();
 112                                 EnqueueWithDialogTest.this.moveMouseOver(d);
 113                             }
 114                         });
 115 
 116                     // This will cause enqueue the following key events
 117                     d.setVisible(true);
 118                 }
 119             });
 120 
 121     }//End  init()
 122 
 123     public void start () throws Exception
 124     {
 125         try {
 126             robot = new Robot();
 127             //robot.setAutoDelay(50);
 128         } catch (Exception e) {
 129             throw new RuntimeException("Can't create robot:" + e);
 130         }
 131 
 132         f.setVisible(true);
 133         waitTillShown(b);
 134         System.err.println("b is shown");
 135         f.toFront();
 136         moveMouseOver(f);
 137         robot.waitForIdle();
 138         robot.delay(100);
 139         makeFocused(b);
 140         robot.waitForIdle();
 141         robot.delay(100);
 142         System.err.println("b is focused");
 143 
 144         robot.keyPress(KeyEvent.VK_SPACE);
 145         robot.keyRelease(KeyEvent.VK_SPACE);
 146         System.err.println("space typed once");
 147         boolean ok = robotLatch.await(1, TimeUnit.SECONDS);
 148         if(!ok) {
 149             throw new RuntimeException("Was B button pressed?");
 150         }
 151 
 152         robot.keyPress(KeyEvent.VK_SPACE);
 153         robot.keyRelease(KeyEvent.VK_SPACE);
 154         System.err.println("space typed twice");
 155         robot.delay(500);
 156         ok = pressLatch.await(3, TimeUnit.SECONDS);
 157         if(!ok) {
 158             throw new RuntimeException("Type-ahead doesn't work");
 159         }
 160 
 161     }// start()
 162 
 163     private void moveMouseOver(Container c) {
 164         Point p = c.getLocationOnScreen();
 165         Dimension d = c.getSize();
 166         robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 167     }
 168 
 169     private void waitTillShown(Component c) {
 170         while (true) {
 171             try {
 172                 Thread.sleep(100);
 173                 c.getLocationOnScreen();
 174                 break;
 175             } catch (InterruptedException ie) {
 176                 ie.printStackTrace();
 177                 break;
 178             } catch (Exception e) {
 179             }
 180         }
 181     }
 182     private void makeFocused(Component comp) {
 183         if (comp.isFocusOwner()) {
 184             return;
 185         }
 186         final Semaphore sema = new Semaphore();
 187         final FocusAdapter fa = new FocusAdapter() {
 188                 public void focusGained(FocusEvent fe) {
 189                     sema.raise();
 190                 }
 191             };
 192         comp.addFocusListener(fa);
 193         comp.requestFocusInWindow();
 194         if (comp.isFocusOwner()) {
 195             return;
 196         }
 197         try {
 198             sema.doWait(3000);
 199         } catch (InterruptedException ie) {
 200             ie.printStackTrace();
 201         }
 202         comp.removeFocusListener(fa);
 203         if (!comp.isFocusOwner()) {
 204             throw new RuntimeException("Can't make " + comp + " focused, current owner is " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
 205         }
 206     }
 207 
 208 static class Semaphore {
 209     boolean state = false;
 210     int waiting = 0;
 211     public Semaphore() {
 212     }
 213     public synchronized void doWait() throws InterruptedException {
 214         if (state) {
 215             return;
 216         }
 217         waiting++;
 218         wait();
 219         waiting--;
 220     }
 221     public synchronized void doWait(int timeout) throws InterruptedException {
 222         if (state) {
 223             return;
 224         }
 225         waiting++;
 226         wait(timeout);
 227         waiting--;
 228     }
 229     public synchronized void raise() {
 230         state = true;
 231         if (waiting > 0) {
 232             notifyAll();
 233         }
 234     }
 235     public synchronized boolean getState() {
 236         return state;
 237     }
 238 }
 239 }