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(); 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) { | 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.*; 33 import java.awt.event.ActionEvent; 34 import java.awt.event.ActionListener; 35 import java.awt.event.KeyEvent; 36 import java.awt.event.KeyListener; 37 import java.util.logging.Logger; 38 39 public class bug6980209 implements ActionListener { 40 private final static Logger log = 41 Logger.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 private static JButton button; 52 private static Point point; 53 54 public static void main(String[] args) throws Exception { 55 System.out.println( 56 "PLEASE DO NOT TOUCH KEYBOARD AND MOUSE DURING THE TEST RUN!"); 57 // log.setLevel(java.util.logging.Level.FINE); 58 // log.setLevel(java.util.logging.Level.FINEST); 59 try { 60 SwingUtilities.invokeAndWait(new Runnable() { 61 public void run() { 62 frame = new JFrame(); 63 frame.setUndecorated(true); 64 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 65 setup(frame); 66 } 67 }); 68 final Robot robot = new Robot(); 69 robot.delay(100); 70 robot.waitForIdle(); 71 robot.setAutoDelay(10); 72 robot.setAutoWaitForIdle(true); 73 SwingUtilities.invokeAndWait(new Runnable() { 74 @Override 75 public void run() { 76 point = button.getLocationOnScreen(); 77 } 78 }); 79 robot.mouseMove( point.x + 5, point.y + 5 ); 80 robot.mousePress(InputEvent.BUTTON1_MASK); 81 robot.mouseRelease(InputEvent.BUTTON1_MASK); 82 robot.delay(100); 83 robot.waitForIdle(); 84 85 testExitBeforeEnter(); 86 System.out.println("Run random test in EDT"); 87 runInEDT = true; 88 testRandomly(); 89 System.out.println("Run random test in another thread"); 90 runInEDT = false; 91 testRandomly(); 92 System.out.println("ok"); 93 94 } finally { 95 SwingUtilities.invokeAndWait(new Runnable() { 96 @Override 97 public void run() { 98 frame.dispose(); 99 } 100 }); 101 } 102 } 103 104 private static void testExitBeforeEnter() throws Exception { 105 final SecondaryLoop loop = 106 Toolkit.getDefaultToolkit().getSystemEventQueue() 107 .createSecondaryLoop(); 108 loop.exit(); 109 Robot robot = new Robot(); 110 robot.mouseWheel(1); 111 robot.waitForIdle(); 112 SwingUtilities.invokeAndWait(new Runnable() { 113 @Override 114 public void run() { 115 if(loop.enter()) { 116 throw new RuntimeException("Wrong enter() return value"); 117 } 118 } 119 }); 120 } 121 122 private static void testRandomly() throws AWTException { 123 disorderCounter = 0; 124 final Robot robot = new Robot(); 125 robot.setAutoDelay(1); 126 for (int i = 0; i < ATTEMPTS; i++) { 127 enterReturn = null; 128 exitReturn = null; 129 dispatchedEvents = 0; 130 synchronized (bug6980209.class) { 131 try { 132 for (int j = 0; j < EVENTS; j++) { 133 robot.keyPress(KeyEvent.VK_1); 134 robot.keyRelease(KeyEvent.VK_1); 135 } 136 137 // trigger the button action that starts secondary loop 138 robot.keyPress(KeyEvent.VK_SPACE); 139 robot.keyRelease(KeyEvent.VK_SPACE); 140 141 for (int j = 0; j < EVENTS; j++) { 142 robot.keyPress(KeyEvent.VK_1); 143 robot.keyRelease(KeyEvent.VK_1); 144 } 145 long time = System.nanoTime(); 160 throw new RuntimeException( 161 "KeyEvent.VK_1 has been lost!"); 162 } 163 164 } catch (InterruptedException e) { 165 throw new RuntimeException("Interrupted!"); 166 } 167 } 168 } 169 if (disorderCounter == 0) { 170 System.out.println( 171 "Zero disordered enter/exit caught. It is recommended to run scenario again"); 172 } else { 173 System.out.println( 174 "Disordered calls is " + disorderCounter + " from " + 175 ATTEMPTS); 176 } 177 } 178 179 private static void setup(final JFrame frame) { 180 button = new JButton("Button"); 181 frame.getContentPane().add(button); 182 button.addActionListener(new bug6980209()); 183 frame.pack(); 184 frame.setVisible(true); 185 button.setFocusable(true); 186 button.requestFocus(); 187 button.addKeyListener(new KeyListener() { 188 @Override 189 public void keyTyped(KeyEvent e) { 190 } 191 192 @Override 193 public void keyPressed(KeyEvent e) { 194 if (e.getKeyChar() == '1') dispatchedEvents++; 195 } 196 197 @Override 198 public void keyReleased(KeyEvent e) { 199 if (e.getKeyChar() == '1') dispatchedEvents++; 200 } 201 }); 202 } 203 204 205 @Override 206 public void actionPerformed(ActionEvent e) { 207 if (runInEDT) { |