11/*
2- * Copyright (c) 1998, 2016 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1998, 2024 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2121 * questions.
2222 */
2323
24+ import java .awt .EventQueue ;
25+ import java .awt .Frame ;
26+ import java .awt .Robot ;
27+ import java .awt .event .InputEvent ;
28+ import java .awt .event .KeyEvent ;
29+ import java .awt .event .MouseEvent ;
30+ import java .awt .event .MouseListener ;
31+ import java .awt .event .WindowAdapter ;
32+ import java .awt .event .WindowEvent ;
33+
2434/*
25- @test 1.2 98/08/05
35+ @test
2636 @key headful
2737 @bug 4515763
2838 @summary Tests that clicking mouse and pressing keys generates correct amount of click-counts
29- @author andrei.dmitriev: area=awt.mouse
3039 @run main ClickDuringKeypress
3140*/
3241
33- /**
34- * ClickDuringKeypress.java
35- *
36- * summary:
37- */
38-
39- import java .applet .Applet ;
40- import java .awt .*;
41- import java .awt .event .*;
42+ public class ClickDuringKeypress implements MouseListener {
4243
43- public class ClickDuringKeypress implements MouseListener
44- {
45- //Declare things used in the test, like buttons and labels here
4644 final static int CLICKCOUNT = 10 ;
47- final static int DOUBLE_CLICK_AUTO_DELAY = 10 ;
48- volatile int lastClickCount = 0 ;
49- volatile boolean clicked = false ;
50- volatile boolean ready = false ;
51-
52- Frame frame ;
53- Robot robot ;
54-
55- public void init ()
56- {
57- //Create instructions for the user here, as well as set up
58- // the environment -- set the layout manager, add buttons,
59- // etc.
60-
45+ final static int DOUBLE_CLICK_AUTO_DELAY = 20 ;
46+ static volatile int lastClickCount = 0 ;
47+ static volatile boolean clicked = false ;
48+ static volatile boolean ready = false ;
49+
50+ static volatile Frame frame ;
51+ static volatile Robot robot ;
52+ static final ClickDuringKeypress clicker = new ClickDuringKeypress ();
53+
54+ public static void main (String [] args ) throws Exception {
55+ try {
56+ EventQueue .invokeAndWait (ClickDuringKeypress ::createUI );
57+ robot = new Robot ();
58+ robot .setAutoWaitForIdle (true );
59+ robot .delay (2000 );
60+ robot .mouseMove (200 , 200 );
61+ robot .delay (2000 );
62+ EventQueue .invokeAndWait (() -> frame .setVisible (true ));
63+ doTest ();
64+ } finally {
65+ if (frame != null ) {
66+ EventQueue .invokeAndWait (frame ::dispose );
67+ }
68+ }
69+ }
70+
71+ static void createUI () {
6172 frame = new Frame ("ClickDuringKeypress" );
62- frame .addMouseListener (this );
73+ frame .addMouseListener (clicker );
6374 frame .addWindowListener (new WindowAdapter () {
6475 public void windowActivated (WindowEvent e ) {
65- synchronized (ClickDuringKeypress .this ) {
6676 ready = true ;
67- ClickDuringKeypress .this .notifyAll ();
68- }
6977 }
7078 });
7179 frame .setBounds (0 , 0 , 400 , 400 );
80+ }
7281
73- start ();
74-
75- }//End init()
76-
77- public void start ()
78- {
79- try {
80- robot = new Robot ();
81- } catch (AWTException e ) {
82- System .out .println ("Could not create Robot." );
83- throw new RuntimeException ("Couldn't create Robot. Test fails" );
84- }
85-
86- robot .mouseMove (200 , 200 );
87- frame .show ();
88-
89- synchronized (this ) {
90- try {
91- if (!ready ) {
92- wait (10000 );
93- }
94- } catch (InterruptedException ex ) {
95- }
96- if (!ready ) {
97- System .out .println ("Not Activated. Test fails" );
98- throw new RuntimeException ("Not Activated. Test fails" );
99- }
82+ static void doTest () throws Exception {
83+ robot .waitForIdle ();
84+ robot .delay (1000 );
85+ if (!ready ) {
86+ System .out .println ("Not Activated. Test fails" );
87+ throw new RuntimeException ("Not Activated. Test fails" );
10088 }
101-
102- doTest ();
103-
104- //What would normally go into main() will probably go here.
105- //Use System.out.println for diagnostic messages that you want
106- //to read after the test is done.
107- //Use Sysout.println for messages you want the tester to read.
108-
109- }// start()
110-
111- // Mouse should be over the Frame by this point
112- private void doTest () {
89+ // Mouse should be over the Frame by this point
11390 robot .setAutoDelay (2000 );
11491 robot .waitForIdle ();
11592 robot .keyPress (KeyEvent .VK_B );
116- robot .mousePress (InputEvent .BUTTON1_MASK );
117- robot .delay (10 );
118- robot .mouseRelease (InputEvent .BUTTON1_MASK );
93+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
94+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
11995 // Should trigger mouseClicked
12096 robot .keyRelease (KeyEvent .VK_B );
12197 robot .delay (1000 );
12298
12399 robot .setAutoDelay (DOUBLE_CLICK_AUTO_DELAY );
124100 for (int i = 0 ; i < CLICKCOUNT / 2 ; i ++) {
125- robot .mousePress (InputEvent .BUTTON1_MASK );
126- robot .delay (10 );
127- robot .mouseRelease (InputEvent .BUTTON1_MASK );
101+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
102+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
128103 robot .keyPress (KeyEvent .VK_B );
129- robot .delay (10 );
130104 robot .keyRelease (KeyEvent .VK_B );
131- robot .mousePress (InputEvent .BUTTON1_MASK );
132- robot .delay (10 );
133- robot .mouseRelease (InputEvent .BUTTON1_MASK );
105+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
106+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
134107 }
135108 robot .waitForIdle ();
136109 // check results
@@ -156,9 +129,4 @@ public void mouseClicked(MouseEvent e) {
156129 clicked = true ;
157130 lastClickCount = e .getClickCount ();
158131 }
159-
160- public static void main (String [] args ) {
161- new ClickDuringKeypress ().init ();
162- }
163-
164- }// class ClickDuringKeypress
132+ }
0 commit comments