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