11/*
2- * Copyright (c) 2008, 2022 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2008, 2025 , 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
2222 */
2323
2424/*
25- @test
26- @key headful
27- @bug 6315717
28- @summary verifies that drag events are coming for every button if the property is set to true
29- @author Andrei Dmitriev : area=awt.mouse
30- @run main ExtraButtonDrag
25+ * @test
26+ * @key headful
27+ * @bug 6315717
28+ * @summary Verifies that the mouse drag events received for every button if the property is set to true
29+ * @run main ExtraButtonDrag
3130 */
3231
33- //events from standard should also come
34-
35- import java .awt .*;
36- import java .awt .event .*;
37-
38- public class ExtraButtonDrag extends Frame {
39- static String tk = Toolkit .getDefaultToolkit ().getClass ().getName ();
40- static Robot robot ;
41- static int [] buttonsPressed ;
42- static int [] buttonsReleased ;
43- static int [] buttonsClicked ;
44- volatile static boolean dragged = false ;
45- volatile static boolean moved = false ;
46-
47- public ExtraButtonDrag (){
48- super ("ExtraButtonDrag" );
49- }
50-
51- public static void main (String []s ){
52- Frame frame = new ExtraButtonDrag ();
53-
54- MouseAdapter ma = new MouseAdapter () {
55- public void mouseDragged (MouseEvent e ) {
56- System .out .println ("Dragged " +e );// +" : "+ e.getButton() + " : " +e.getButtonState(e.getButton()));
57- dragged = true ;
58- }
59- public void mouseMoved (MouseEvent e ) {
60- System .out .println ("Moved " +e );
61- moved = true ;
62- }
63- public void mousePressed (MouseEvent e ) {
64- System .out .println (">>> " +e );
65- }
66- public void mouseReleased (MouseEvent e ) {
67- System .out .println (">>> " +e );
68- }
32+ import java .awt .AWTException ;
33+ import java .awt .Dimension ;
34+ import java .awt .EventQueue ;
35+ import java .awt .Frame ;
36+ import java .awt .MouseInfo ;
37+ import java .awt .Point ;
38+ import java .awt .Robot ;
39+ import java .awt .Toolkit ;
40+ import java .awt .event .InputEvent ;
41+ import java .awt .event .MouseAdapter ;
42+ import java .awt .event .MouseEvent ;
43+ import java .lang .reflect .InvocationTargetException ;
44+
45+ public class ExtraButtonDrag {
46+
47+ private static Frame frame ;
48+ private static Robot robot ;
49+ private static volatile boolean dragged = false ;
50+ private static volatile boolean moved = false ;
51+ private static volatile Point centerFrame ;
52+ private static volatile Point outboundsFrame ;
53+ private static final String OS_NAME = System .getProperty ("os.name" );
54+ private static MouseAdapter mAdapter = new MouseAdapter () {
55+ @ Override
56+ public void mouseDragged (MouseEvent e ) {
57+ dragged = true ;
58+ }
6959
70- };
60+ @ Override
61+ public void mouseMoved (MouseEvent e ) {
62+ moved = true ;
63+ }
64+ };
7165
72- frame .addMouseMotionListener (ma );
73- frame .addMouseListener (ma );
66+ public static void initializeGUI () {
67+ frame = new Frame ("ExtraButtonDrag" );
68+ frame .addMouseMotionListener (mAdapter );
69+ frame .addMouseListener (mAdapter );
7470
7571 frame .setSize (300 , 300 );
72+ frame .setLocationRelativeTo (null );
7673 frame .setVisible (true );
74+ }
7775
78- int [] buttonMask = new int [MouseInfo .getNumberOfButtons ()]; //InputEvent.getButtonMasks();
79-
80- for (int i = 0 ; i < MouseInfo .getNumberOfButtons (); i ++){
81- buttonMask [i ] = InputEvent .getMaskForButton (i +1 );
82- // System.out.println("TEST: "+tmp[i]);
83- }
76+ public static void doTest ()
77+ throws InvocationTargetException , InterruptedException {
8478
85- try {
86- robot = new Robot ();
87- robot .delay (1000 );
88- Point centerFrame = new Point (frame .getLocationOnScreen ().x + frame .getWidth ()/2 , frame .getLocationOnScreen ().y + frame .getHeight ()/2 );
89- Point outboundsFrame = new Point (frame .getLocationOnScreen ().x + frame .getWidth ()*3 /2 , frame .getLocationOnScreen ().y + frame .getHeight ()/2 );
79+ int [] buttonMask = new int [MouseInfo .getNumberOfButtons ()];
9080
91- System .out .println ("areExtraMouseButtonsEnabled() == " + Toolkit .getDefaultToolkit ().areExtraMouseButtonsEnabled () );
92-
93- for (int i = 0 ; i < MouseInfo .getNumberOfButtons (); i ++){
94- System .out .println ("button to drag = " +(i +1 ) + " : value passed to robot = " +buttonMask [i ]);
81+ for (int i = 0 ; i < MouseInfo .getNumberOfButtons (); i ++) {
82+ buttonMask [i ] = InputEvent .getMaskForButton (i + 1 );
83+ }
9584
96- try {
97- dragMouse (buttonMask [i ], centerFrame .x , centerFrame .y , outboundsFrame .x , outboundsFrame .y );
98- } catch (IllegalArgumentException e ){
99- throw new RuntimeException ("Test failed. Exception occured." , e );
100- }
85+ EventQueue .invokeAndWait (() -> {
86+ Point location = frame .getLocationOnScreen ();
87+ Dimension size = frame .getSize ();
88+ centerFrame = new Point (location .x + size .width / 2 ,
89+ location .y + size .height / 2 );
90+ outboundsFrame = new Point (location .x + size .width * 3 / 2 ,
91+ location .y + size .height / 2 );
92+ });
93+
94+ System .out .println ("areExtraMouseButtonsEnabled() == "
95+ + Toolkit .getDefaultToolkit ().areExtraMouseButtonsEnabled ());
96+
97+ for (int i = 0 ; i < MouseInfo .getNumberOfButtons (); i ++) {
98+ System .out .println ("button to drag = " + (i + 1 )
99+ + " : value passed to robot = " + buttonMask [i ]);
100+
101+ try {
102+ dragMouse (buttonMask [i ], centerFrame .x , centerFrame .y ,
103+ outboundsFrame .x , outboundsFrame .y );
104+ } catch (IllegalArgumentException e ) {
105+ throw new RuntimeException ("Test failed. Exception occured." ,
106+ e );
107+ }
101108
102- robot .delay (500 );
103- //this is a choice-case for X protocol issue: native events from extra buttons doesn't contain
104- // the correct state so it's unable to decide if there is a drag or move. By default we send MOVED event.
105- //XToolkit: extra buttons should report MOVED events only
106- //WToolkit: extra buttons should report DRAGGED events only
107- if (i > 2 ){ //extra buttons only
108- if (tk .equals ("sun.awt.X11.XToolkit" )) {
109- if (!moved || dragged ) {
110- throw new RuntimeException ("Test failed." + tk +" Button = " +(i +1 ) + " moved = " +moved +" : dragged = " +dragged );
111- }
112- } else { //WToolkit
113- if (moved || !dragged ) {
114- throw new RuntimeException ("Test failed." + tk +" Button = " +(i +1 ) + " moved = " +moved +" : dragged = " +dragged );
115- }
109+ // this is a choice-case for X protocol issue: native events from
110+ // extra buttons doesn't contain
111+ // the correct state so it's unable to decide if there is a drag or
112+ // move. By default we send MOVED event.
113+ // XToolkit: extra buttons should report MOVED events only
114+ // WToolkit: extra buttons should report DRAGGED events only
115+ if (i > 2 ) { // extra buttons only
116+ if (OS_NAME .equals ("Linux" )) {
117+ if (!moved || dragged ) {
118+ throw new RuntimeException ("Test failed." + OS_NAME
119+ + " Button = " + (i + 1 ) + " moved = " + moved
120+ + " : dragged = " + dragged );
116121 }
117- } else {
118- if (moved || !dragged ){
119- throw new RuntimeException ("Test failed. Button = " +(i +1 ) + " not dragged." );
122+ } else { // WToolkit
123+ if (moved || !dragged ) {
124+ throw new RuntimeException ("Test failed." + OS_NAME
125+ + " Button = " + (i + 1 ) + " moved = " + moved
126+ + " : dragged = " + dragged );
120127 }
121128 }
129+ } else {
130+ if (moved || !dragged ) {
131+ throw new RuntimeException (
132+ "Test failed. Button = " + (i + 1 ) + " not dragged." );
133+ }
122134 }
123- } catch (Exception e ){
124- throw new RuntimeException ("" , e );
125135 }
126136 }
127137
128- public static void dragMouse (int button , int x0 , int y0 , int x1 , int y1 ){
138+ public static void dragMouse (int button , int x0 , int y0 , int x1 , int y1 ) {
129139 int curX = x0 ;
130140 int curY = y0 ;
131141 int dx = x0 < x1 ? 1 : -1 ;
@@ -138,17 +148,43 @@ public static void dragMouse(int button, int x0, int y0, int x1, int y1){
138148
139149 robot .mousePress (button );
140150
141- while (curX != x1 ){
151+ while (curX != x1 ) {
142152 curX += dx ;
143153 robot .mouseMove (curX , curY );
144154 robot .delay (5 );
145155 }
146- while (curY != y1 ) {
156+ while (curY != y1 ) {
147157 curY += dy ;
148158 robot .mouseMove (curX , curY );
149159 robot .delay (5 );
150160 }
151161 robot .mouseRelease (button );
152162 }
153163
164+ public static void main (String [] s )
165+ throws InvocationTargetException , InterruptedException , AWTException {
166+ try {
167+ robot = new Robot ();
168+ robot .setAutoDelay (10 );
169+ robot .setAutoWaitForIdle (true );
170+
171+ EventQueue .invokeAndWait (ExtraButtonDrag ::initializeGUI );
172+ robot .waitForIdle ();
173+ robot .delay (100 );
174+
175+ doTest ();
176+
177+ System .out .println ("Test Passed" );
178+ } finally {
179+ EventQueue .invokeAndWait (ExtraButtonDrag ::disposeFrame );
180+ }
181+ }
182+
183+ public static void disposeFrame () {
184+ if (frame != null ) {
185+ frame .dispose ();
186+ frame = null ;
187+ }
188+ }
189+
154190}
0 commit comments