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