1 /*
   2  * Copyright (c) 2015, 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 6980209
  28  * @summary Make tracking SecondaryLoop.enter/exit methods easier
  29  * @author Semyon Sadetsky
  30  */
  31 
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.awt.event.ActionEvent;
  36 import java.awt.event.ActionListener;
  37 import java.awt.event.KeyEvent;
  38 import java.awt.event.KeyListener;
  39 import java.util.logging.Logger;
  40 
  41 public class bug6980209 implements ActionListener {
  42     private final static Logger log =
  43             Logger.getLogger("java.awt.event.WaitDispatchSupport");
  44     public static final int ATTEMPTS = 100;
  45     public static final int EVENTS = 5;
  46 
  47     private static boolean runInEDT;
  48     private static JFrame frame;
  49     private static int disorderCounter = 0;
  50     private static Boolean enterReturn;
  51     private static Boolean exitReturn;
  52     private static int dispatchedEvents;
  53     private static JButton button;
  54     private static Point point;
  55 
  56     public static void main(String[] args) throws Exception {
  57         System.out.println(
  58                 "PLEASE DO NOT TOUCH KEYBOARD AND MOUSE DURING THE TEST RUN!");
  59         // log.setLevel(java.util.logging.Level.FINE);
  60         // log.setLevel(java.util.logging.Level.FINEST);
  61         try {
  62             SwingUtilities.invokeAndWait(new Runnable() {
  63                 public void run() {
  64                     frame = new JFrame();
  65                     frame.setUndecorated(true);
  66                     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67                     setup(frame);
  68                 }
  69             });
  70             final Robot robot = new Robot();
  71             robot.delay(100);
  72             robot.waitForIdle();
  73             robot.setAutoDelay(10);
  74             robot.setAutoWaitForIdle(true);
  75             SwingUtilities.invokeAndWait(new Runnable() {
  76                 @Override
  77                 public void run() {
  78                     point = button.getLocationOnScreen();
  79                 }
  80             });
  81             robot.mouseMove( point.x + 5, point.y + 5 );
  82             robot.mousePress(InputEvent.BUTTON1_MASK);
  83             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  84             robot.delay(100);
  85             robot.waitForIdle();
  86 
  87             testExitBeforeEnter();
  88             System.out.println("Run random test in EDT");
  89             runInEDT = true;
  90             testRandomly();
  91             System.out.println("Run random test in another thread");
  92             runInEDT = false;
  93             testRandomly();
  94             System.out.println("ok");
  95 
  96         } finally {
  97             SwingUtilities.invokeAndWait(new Runnable() {
  98                 @Override
  99                 public void run() {
 100                     frame.dispose();
 101                 }
 102             });
 103         }
 104     }
 105 
 106     private static void testExitBeforeEnter() throws Exception {
 107         final SecondaryLoop loop =
 108                 Toolkit.getDefaultToolkit().getSystemEventQueue()
 109                         .createSecondaryLoop();
 110         loop.exit();
 111         Robot robot = new Robot();
 112         robot.mouseWheel(1);
 113         robot.waitForIdle();
 114         SwingUtilities.invokeAndWait(new Runnable() {
 115             @Override
 116             public void run() {
 117                 if(loop.enter()) {
 118                     throw new RuntimeException("Wrong enter() return value");
 119                 }
 120             }
 121         });
 122     }
 123 
 124     private static void testRandomly() throws AWTException {
 125         disorderCounter = 0;
 126         final Robot robot = new Robot();
 127         robot.setAutoDelay(1);
 128         for (int i = 0; i < ATTEMPTS; i++) {
 129             enterReturn = null;
 130             exitReturn = null;
 131             dispatchedEvents = 0;
 132             synchronized (bug6980209.class) {
 133                 try {
 134                     for (int j = 0; j < EVENTS; j++) {
 135                         robot.keyPress(KeyEvent.VK_1);
 136                         robot.keyRelease(KeyEvent.VK_1);
 137                     }
 138 
 139                     // trigger the button action that starts secondary loop
 140                     robot.keyPress(KeyEvent.VK_SPACE);
 141                     robot.keyRelease(KeyEvent.VK_SPACE);
 142 
 143                     for (int j = 0; j < EVENTS; j++) {
 144                         robot.keyPress(KeyEvent.VK_1);
 145                         robot.keyRelease(KeyEvent.VK_1);
 146                     }
 147                     long time = System.nanoTime();
 148                     // wait for enter() returns
 149                     bug6980209.class.wait(1000);
 150                     if (enterReturn == null) {
 151                         System.out.println("wait time=" +
 152                                 ((System.nanoTime() - time) / 1E9) +
 153                                 " seconds");
 154                         throw new RuntimeException(
 155                                 "It seems the secondary loop will never end");
 156                     }
 157                     if (!enterReturn) disorderCounter++;
 158 
 159                     robot.waitForIdle();
 160                     if (dispatchedEvents <
 161                             2 * EVENTS) { //check that all events are dispatched
 162                         throw new RuntimeException(
 163                                 "KeyEvent.VK_1 has been lost!");
 164                     }
 165 
 166                 } catch (InterruptedException e) {
 167                     throw new RuntimeException("Interrupted!");
 168                 }
 169             }
 170         }
 171         if (disorderCounter == 0) {
 172             System.out.println(
 173                     "Zero disordered enter/exit caught. It is recommended to run scenario again");
 174         } else {
 175             System.out.println(
 176                     "Disordered calls is " + disorderCounter + " from " +
 177                             ATTEMPTS);
 178         }
 179     }
 180 
 181     private static void setup(final JFrame frame) {
 182         button = new JButton("Button");
 183         frame.getContentPane().add(button);
 184         button.addActionListener(new bug6980209());
 185         frame.pack();
 186         frame.setVisible(true);
 187         button.setFocusable(true);
 188         button.requestFocus();
 189         button.addKeyListener(new KeyListener() {
 190             @Override
 191             public void keyTyped(KeyEvent e) {
 192             }
 193 
 194             @Override
 195             public void keyPressed(KeyEvent e) {
 196                 if (e.getKeyChar() == '1') dispatchedEvents++;
 197             }
 198 
 199             @Override
 200             public void keyReleased(KeyEvent e) {
 201                 if (e.getKeyChar() == '1') dispatchedEvents++;
 202             }
 203         });
 204     }
 205 
 206 
 207     @Override
 208     public void actionPerformed(ActionEvent e) {
 209         if (runInEDT) {
 210             runSecondaryLoop();
 211             return;
 212         }
 213         new Thread("Secondary loop run thread") {
 214             @Override
 215             public void run() {
 216                 runSecondaryLoop();
 217             }
 218         }.start();
 219     }
 220 
 221     private static void runSecondaryLoop() {
 222         log.fine("\n---TEST START---");
 223 
 224         final SecondaryLoop loop =
 225                 Toolkit.getDefaultToolkit().getSystemEventQueue()
 226                         .createSecondaryLoop();
 227 
 228         final Object LOCK = new Object(); //lock to start simultaneously
 229         Thread exitThread = new Thread("Exit thread") {
 230             @Override
 231             public void run() {
 232                 synchronized (LOCK) {
 233                     LOCK.notify();
 234                 }
 235                 Thread.yield();
 236                 exitReturn = loop.exit();
 237                 log.fine("exit() returns " + exitReturn);
 238             }
 239         };
 240 
 241         synchronized (LOCK) {
 242             try {
 243                 exitThread.start();
 244                 LOCK.wait();
 245             } catch (InterruptedException e1) {
 246                 throw new RuntimeException("What?");
 247             }
 248         }
 249 
 250         enterReturn = loop.enter();
 251         log.fine("enter() returns " + enterReturn);
 252 
 253         try {
 254             exitThread.join();
 255         } catch (InterruptedException e) {
 256             throw new RuntimeException("What?");
 257         }
 258         synchronized (bug6980209.class) {
 259             bug6980209.class.notifyAll();
 260         }
 261         log.fine("\n---TEST END---");
 262     }
 263 }