Skip to content

Commit

Permalink
8306072: Open source several AWT MouseInfo related tests
Browse files Browse the repository at this point in the history
Backport-of: 44d9f55d0b3c469988be6f1c47f0cfbc433c4490
  • Loading branch information
Amos Shi authored and GoeLin committed Dec 15, 2023
1 parent 50a1dc7 commit 1ed87b6
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/jdk/java/awt/MouseInfo/ButtonsNumber.java
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
@test
@bug 4908137
@summary tests that non-zero number of mouse buttons is returned
@key headful
*/

import java.awt.MouseInfo;

public class ButtonsNumber {

public static void main(String[] args) {

if (MouseInfo.getNumberOfButtons() == 0) {
throw new RuntimeException("Zero returned by getNumberOfButtons(). Test failed.");
}
}
}
172 changes: 172 additions & 0 deletions test/jdk/java/awt/MouseInfo/ContainerMousePositionTest.java
@@ -0,0 +1,172 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
@test
@summary unit test for a new method in Container class: getMousePosition(boolean)
@bug 4009555
@key headful
*/

import java.awt.Button;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;

public class ContainerMousePositionTest {
private Button button;
private Frame frame;
private Panel panel;
private static Dimension BUTTON_DIMENSION = new Dimension(100, 100);
private static Dimension FRAME_DIMENSION = new Dimension(200, 200);
private static Point POINT_WITHOUT_COMPONENTS = new Point(10, 10);
private static Point FIRST_BUTTON_LOCATION = new Point(20, 20);
private static int DELAY = 1000;
Robot robot;
volatile int xPos = 0;
volatile int yPos = 0;
Point pMousePosition;

public static void main(String[] args) throws Exception {
ContainerMousePositionTest containerObj = new ContainerMousePositionTest();
containerObj.init();
containerObj.start();
}

public void init() throws Exception {
robot = new Robot();
EventQueue.invokeAndWait(() -> {
button = new Button("Button");
frame = new Frame("Testing Component.getMousePosition()");
panel = new Panel();

button.setSize(BUTTON_DIMENSION);
button.setLocation(FIRST_BUTTON_LOCATION);

panel.setLayout(null);

panel.add(button);
frame.add(panel);
frame.setSize(FRAME_DIMENSION);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}

public void start() throws Exception {
try {
robot.delay(DELAY);
robot.waitForIdle();

EventQueue.invokeAndWait(() -> {
Point p = button.getLocationOnScreen();
xPos = p.x + button.getWidth() / 2;
yPos = p.y + button.getHeight() / 2;
});
robot.mouseMove(xPos,yPos);
robot.delay(DELAY);

EventQueue.invokeAndWait(() -> {
pMousePosition = frame.getMousePosition(false);
if (pMousePosition != null) {
throw new RuntimeException("Test failed: Container is " +
"overlapped by " + " child and it should be taken " +
"into account");
}
System.out.println("Test stage completed: Container is " +
"overlapped by " + " child and it was taken into " +
"account");

pMousePosition = frame.getMousePosition(true);
if (pMousePosition == null) {
throw new RuntimeException("Test failed: Container is " +
"overlapped by " + " child and it should not be " +
"taken into account");
}
System.out.println("Test stage completed: Container is " +
"overlapped by " + " child and it should not be " +
"taken into account");
xPos = panel.getLocationOnScreen().x + POINT_WITHOUT_COMPONENTS.x;
yPos = panel.getLocationOnScreen().y + POINT_WITHOUT_COMPONENTS.y;
});

robot.mouseMove(xPos, yPos);

robot.delay(DELAY);

EventQueue.invokeAndWait(() -> {
pMousePosition = panel.getMousePosition(true);
if (pMousePosition == null) {
throw new RuntimeException("Test failed: Pointer was " +
"outside of " + "the component so getMousePosition()" +
" should not return null");
}
System.out.println("Test stage completed: Pointer was outside of " +
"the component and getMousePosition() has not returned null");

pMousePosition = panel.getMousePosition(false);
if (pMousePosition == null) {
throw new RuntimeException("Test failed: Pointer was outside of " +
"the component so getMousePosition() should not return null");
}
System.out.println("Test stage completed: Pointer was outside of " +
"the component and getMousePosition() has not returned null");
xPos = frame.getLocationOnScreen().x + frame.getWidth() + POINT_WITHOUT_COMPONENTS.x;
yPos = frame.getLocationOnScreen().y + frame.getHeight() + POINT_WITHOUT_COMPONENTS.y;
});
robot.mouseMove(xPos, yPos);

robot.delay(DELAY);

EventQueue.invokeAndWait(() -> {
pMousePosition = frame.getMousePosition(true);
if (pMousePosition != null) {
throw new RuntimeException("Test failed: Pointer was outside of " +
"the Frame widow and getMousePosition() should return null");
}
System.out.println("Test stage completed: Pointer was outside of " +
"the Frame widow and getMousePosition() returned null");

pMousePosition = frame.getMousePosition(false);
if (pMousePosition != null) {
throw new RuntimeException("Test failed: Pointer was outside of " +
"the Frame widow and getMousePosition() should return null");
}
System.out.println("Test stage completed: Pointer was outside of " +
"the Frame widow and getMousePosition() returned null");
});
robot.delay(DELAY);

System.out.println("ComponentMousePositionTest PASSED.");
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
}

1 comment on commit 1ed87b6

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.