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