Path: blob/master/test/jdk/javax/swing/JButton/PressedButtonRightClickTest.java
41149 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324import java.awt.AWTException;25import java.awt.BorderLayout;26import java.awt.Point;27import java.awt.Robot;28import java.awt.event.InputEvent;29import javax.swing.JButton;30import javax.swing.JFrame;31import javax.swing.SwingUtilities;3233/*34* @test35* @key headful36* @bug 804906937* @summary Tests whether right mouse click releases a pressed JButton38*/3940public class PressedButtonRightClickTest {4142private static Robot testRobot;43private static JFrame myFrame;44private static JButton myButton;4546public static void main(String[] args) throws Throwable {4748SwingUtilities.invokeAndWait(new Runnable() {4950@Override51public void run() {52constructTestUI();53}54});5556try {57testRobot = new Robot();58} catch (AWTException ex) {59throw new RuntimeException("Exception in Robot creation");60}6162testRobot.waitForIdle();6364// Method performing auto test operation65test();6667disposeTestUI();68}6970private static void test() {71Point loc = myFrame.getLocationOnScreen();7273testRobot.mouseMove((loc.x + 100), (loc.y + 100));7475// Press the left mouse button76testRobot.mousePress(InputEvent.BUTTON1_DOWN_MASK);77myButton.setText("Left button pressed");78testRobot.delay(1000);7980// Press the right mouse button81testRobot.mousePress(InputEvent.BUTTON3_DOWN_MASK);82myButton.setText("Left button pressed + Right button pressed");83testRobot.delay(1000);8485// Release the right mouse button86testRobot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);87myButton.setText("Right button released");88testRobot.delay(1000);8990// Test whether the button is still pressed91boolean pressed = myButton.getModel().isPressed();92testRobot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);93if (!pressed) {94disposeTestUI();95throw new RuntimeException("Test Failed!");96}97}9899private static void disposeTestUI() {100myFrame.setVisible(false);101myFrame.dispose();102}103104public static void constructTestUI() {105myFrame = new JFrame();106myFrame.setLayout(new BorderLayout());107myButton = new JButton("Whatever");108myFrame.add(myButton, BorderLayout.CENTER);109myFrame.setSize(400, 300);110myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);111myFrame.setLocationRelativeTo(null);112myFrame.setVisible(true);113}114}115116117118