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