1 /*
2  * Copyright (c) 2016, 2020, 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 
24 
25 import java.awt.AWTException;
26 import java.awt.BorderLayout;
27 import java.awt.Point;
28 import java.awt.Robot;
29 import java.awt.event.InputEvent;
30 import javax.swing.JButton;
31 import javax.swing.JFrame;
32 import javax.swing.SwingUtilities;
33 
34 /*
35  * @test
36  * @key headful
37  * @bug  8049069
38  * @summary Tests whether right mouse click releases a pressed JButton
39  */
40 
41 public class PressedButtonRightClickTest {
42 
43     private static Robot testRobot;
44     private static JFrame myFrame;
45     private static JButton myButton;
46 
main(String[] args)47     public static void main(String[] args) throws Throwable {
48 
49         SwingUtilities.invokeAndWait(new Runnable() {
50 
51             @Override
52             public void run() {
53                 constructTestUI();
54             }
55         });
56 
57         try {
58             testRobot = new Robot();
59         } catch (AWTException ex) {
60             throw new RuntimeException("Exception in Robot creation");
61         }
62 
63         testRobot.waitForIdle();
64 
65         // Method performing auto test operation
66         test();
67 
68         disposeTestUI();
69     }
70 
test()71     private static void test() {
72         Point loc = myFrame.getLocationOnScreen();
73 
74         testRobot.mouseMove((loc.x + 100), (loc.y + 100));
75 
76         // Press the left mouse button
77         testRobot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
78         myButton.setText("Left button pressed");
79         testRobot.delay(1000);
80 
81         // Press the right mouse button
82         testRobot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
83         myButton.setText("Left button pressed + Right button pressed");
84         testRobot.delay(1000);
85 
86         // Release the right mouse button
87         testRobot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
88         myButton.setText("Right button released");
89         testRobot.delay(1000);
90 
91         // Test whether the button is still pressed
92         boolean pressed = myButton.getModel().isPressed();
93         testRobot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
94         if (!pressed) {
95             disposeTestUI();
96             throw new RuntimeException("Test Failed!");
97         }
98     }
99 
disposeTestUI()100     private static void disposeTestUI() {
101         myFrame.setVisible(false);
102         myFrame.dispose();
103     }
104 
constructTestUI()105     public static void constructTestUI() {
106         myFrame = new JFrame();
107         myFrame.setLayout(new BorderLayout());
108         myButton = new JButton("Whatever");
109         myFrame.add(myButton, BorderLayout.CENTER);
110         myFrame.setSize(400, 300);
111         myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
112         myFrame.setLocationRelativeTo(null);
113         myFrame.setVisible(true);
114     }
115 }
116 
117