EventWhenTest.java revision 14851:980da45565c8
1/* 2 * Copyright (c) 2014, 2016, 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 24import java.awt.*; 25import java.awt.event.AWTEventListener; 26import java.awt.event.InputEvent; 27import java.awt.event.KeyEvent; 28import java.awt.event.MouseEvent; 29 30/* 31 * @test 32 * @key headful 33 * @bug 8046495 34 * @summary Verifies that mouse/key events has always increasing 'when' timestamps 35 * @author Anton Nashatyrev 36 * @run main EventWhenTest 37 */ 38public class EventWhenTest { 39 40 private static volatile int eventsCount = 0; 41 private static volatile boolean failed = false; 42 43 static { 44 Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { 45 long lastWhen = 0; 46 47 @Override 48 public void eventDispatched(AWTEvent event) { 49 long curWhen; 50 if (event instanceof KeyEvent) { 51 curWhen = ((KeyEvent) event).getWhen(); 52 } else if (event instanceof MouseEvent) { 53 curWhen = ((MouseEvent) event).getWhen(); 54 } else { 55 return; 56 } 57 58 eventsCount++; 59 60 if (curWhen < lastWhen) { 61 System.err.println("FAILED: " + curWhen + " < " + lastWhen + 62 " for " + event); 63 failed = true; 64 } else { 65 lastWhen = curWhen; 66 } 67 } 68 }, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK); 69 } 70 71 public static void main(String[] args) throws Exception { 72 73 Frame frame = new Frame(); 74 75 try { 76 Button b = new Button("Button"); 77 frame.setBounds(300, 300, 300, 300); 78 frame.add(b); 79 frame.setVisible(true); 80 81 Robot robot = new Robot(); 82 robot.waitForIdle(); 83 robot.mouseMove((int)frame.getLocationOnScreen().getX() + 150, 84 (int)frame.getLocationOnScreen().getY() + 150); 85 86 eventsCount = 0; 87 System.out.println("Clicking mouse..."); 88 for (int i = 0; i < 300 && !failed; i++) { 89 robot.mousePress(InputEvent.BUTTON1_MASK); 90 robot.mouseRelease(InputEvent.BUTTON1_MASK); 91 Thread.sleep(10); 92 b.setLabel("Click: " + i); 93 } 94 95 if (eventsCount == 0) { 96 throw new RuntimeException("No events were received"); 97 } 98 99 if (failed) { 100 throw new RuntimeException("Test failed."); 101 } 102 System.out.println("Clicking mouse done: " + eventsCount + " events."); 103 104 b.requestFocusInWindow(); 105 robot.waitForIdle(); 106 107 eventsCount = 0; 108 System.out.println("Typing a key..."); 109 for (int i = 0; i < 300 && !failed; i++) { 110 robot.keyPress(KeyEvent.VK_A); 111 robot.keyRelease(KeyEvent.VK_A); 112 Thread.sleep(10); 113 b.setLabel("Type: " + i); 114 } 115 System.out.println("Key typing done: " + eventsCount + " events."); 116 117 if (eventsCount == 0) { 118 throw new RuntimeException("No events were received"); 119 } 120 121 if (failed) { 122 throw new RuntimeException("Test failed."); 123 } 124 125 System.out.println("Success!"); 126 } finally { 127 frame.dispose(); 128 } 129 } 130} 131