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(); 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) { | 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(); 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) { |