From c494770ca0662d95ed35f9244a7a9e012aab61a7 Mon Sep 17 00:00:00 2001
From: Tejesh R
Date: Fri, 26 May 2023 10:50:49 +0000
Subject: [PATCH] 8306812: Open source several AWT Miscellaneous tests
Reviewed-by: psadhukhan
---
.../jdk/java/awt/Panel/SetForegroundTest.java | 55 ++++++
.../java/awt/PopupMenu/PopupMenuStayOpen.java | 115 ++++++++++++
.../java/awt/Robot/RobotMoveMultiscreen.java | 95 ++++++++++
.../java/awt/Scrollbar/PageIncrementTest.java | 82 +++++++++
.../Scrollbar/ScrollbarKeyControlTest.java | 168 ++++++++++++++++++
5 files changed, 515 insertions(+)
create mode 100644 test/jdk/java/awt/Panel/SetForegroundTest.java
create mode 100644 test/jdk/java/awt/PopupMenu/PopupMenuStayOpen.java
create mode 100644 test/jdk/java/awt/Robot/RobotMoveMultiscreen.java
create mode 100644 test/jdk/java/awt/Scrollbar/PageIncrementTest.java
create mode 100644 test/jdk/java/awt/Scrollbar/ScrollbarKeyControlTest.java
diff --git a/test/jdk/java/awt/Panel/SetForegroundTest.java b/test/jdk/java/awt/Panel/SetForegroundTest.java
new file mode 100644
index 00000000000..8601efc526b
--- /dev/null
+++ b/test/jdk/java/awt/Panel/SetForegroundTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2004, 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 4994151
+ @summary REGRESSION: Bug when setting the foreground of a JWindow
+ @key headful
+*/
+
+import java.awt.EventQueue;
+import java.awt.Color;
+
+import javax.swing.JWindow;
+
+public class SetForegroundTest {
+ static JWindow jwindow;
+
+ public static void main(String[] args) throws Exception {
+ try {
+ EventQueue.invokeAndWait(() -> {
+ jwindow = new JWindow();
+ jwindow.pack();
+ jwindow.setForeground(Color.BLACK);
+ System.out.println("TEST PASSED");
+ });
+ } finally {
+ EventQueue.invokeAndWait(() -> {
+ if (jwindow != null) {
+ jwindow.dispose();
+ }
+ });
+ }
+ }
+}
diff --git a/test/jdk/java/awt/PopupMenu/PopupMenuStayOpen.java b/test/jdk/java/awt/PopupMenu/PopupMenuStayOpen.java
new file mode 100644
index 00000000000..334a3e4ea5f
--- /dev/null
+++ b/test/jdk/java/awt/PopupMenu/PopupMenuStayOpen.java
@@ -0,0 +1,115 @@
+/*
+ * 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 4791953
+ @requires (os.family == "linux" | os.family == "mac")
+ @summary Checks that popup menu stay open after a triggering click.
+ @key headful
+ @run main/othervm -Dsun.java2d.uiScale=1 PopupMenuStayOpen
+*/
+
+import java.awt.Component;
+import java.awt.EventQueue;
+import java.awt.Frame;
+import java.awt.Point;
+import java.awt.PopupMenu;
+import java.awt.Robot;
+import java.awt.Toolkit;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.awt.event.InputEvent;
+
+public class PopupMenuStayOpen {
+ public static final int MAX_COUNT = 100;
+ public volatile static boolean wasActionFired = false;
+ static Frame frame;
+ static PopupMenu pom;
+ volatile static Point point;
+
+ public static void main(String[] args) throws Exception {
+
+ String nm = Toolkit.getDefaultToolkit().getClass().getName();
+
+ try {
+ EventQueue.invokeAndWait(() -> {
+ frame = new Frame("Click-to-see-Popup");
+ pom = new PopupMenu();
+ frame.setTitle(nm);
+ frame.setSize(300, 300);
+ frame.setLocationRelativeTo(null);
+ frame.setVisible(true);
+ pom.add("A long enough line");
+
+ pom.getItem(0).addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent ae) {
+ wasActionFired = true;
+ }
+ });
+
+ frame.add(pom);
+ frame.addMouseListener(new MouseAdapter() {
+ public void mousePressed(MouseEvent me) {
+ pom.show(frame, me.getX(), me.getY());
+ }
+ });
+ });
+
+ Robot robot = new Robot();
+ robot.delay(1000);
+ robot.waitForIdle();
+
+ EventQueue.invokeAndWait(() -> {
+ point = frame.getLocationOnScreen();
+ });
+
+ robot.mouseMove(point.x + 50, point.y + 100);
+ robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
+ robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
+
+ robot.delay(1000);
+ robot.waitForIdle();
+
+ robot.mouseMove(point.x + 50 + 30, point.y + 100 + 15);
+ robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
+ robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
+ robot.delay(500);
+
+
+ if (!wasActionFired) {
+ throw new RuntimeException("Popup not visible or has no focus");
+ }
+ System.out.println("Test Pass!!");
+ } finally {
+ EventQueue.invokeAndWait(() -> {
+ if (frame != null) {
+ frame.dispose();
+ }
+ });
+ }
+ }
+}
diff --git a/test/jdk/java/awt/Robot/RobotMoveMultiscreen.java b/test/jdk/java/awt/Robot/RobotMoveMultiscreen.java
new file mode 100644
index 00000000000..05de2e1e0ad
--- /dev/null
+++ b/test/jdk/java/awt/Robot/RobotMoveMultiscreen.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 1999, 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 4288230
+ @summary Tests that Robot can move mouse to another screen
+ @key headful
+*/
+
+import java.awt.EventQueue;
+import java.awt.Frame;
+import java.awt.GraphicsConfiguration;
+import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
+import java.awt.Rectangle;
+import java.awt.Robot;
+
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseMotionAdapter;
+
+public class RobotMoveMultiscreen {
+ static volatile int x_dest = 20;
+ static volatile int y_dest = 20;
+ static Frame frame;
+ static volatile Boolean testCondition = false;
+
+ public static void main(String[] args) throws Exception {
+ GraphicsDevice[] devs =
+ GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
+
+ if (devs.length <= 1) {
+ System.out.println("Minimum 2 display screens are required" +
+ " for the test, Found " + devs.length);
+ return;
+ }
+ try {
+ EventQueue.invokeAndWait(() -> {
+ GraphicsDevice workDev = devs[devs.length - 1];
+ GraphicsConfiguration config = workDev.getDefaultConfiguration();
+ Rectangle bounds = config.getBounds();
+ x_dest = bounds.x + bounds.width / 2;
+ y_dest = bounds.y + bounds.height / 2;
+ frame = new Frame("Listening frame");
+ frame.addMouseMotionListener(new MouseMotionAdapter() {
+ public void mouseMoved(MouseEvent e) {
+ testCondition = true;
+ }
+ });
+ frame.setLocation(x_dest,y_dest);
+ frame.setSize(100,100);
+ frame.setVisible(true);
+ });
+
+ Robot robot = new Robot();
+ robot.delay(1000);
+ robot.waitForIdle();
+ robot.mouseMove(x_dest+50, y_dest+50);
+ robot.waitForIdle();
+ EventQueue.invokeAndWait(() -> {
+ if (testCondition == false) {
+ throw new RuntimeException("Can't move to another display");
+ }
+ });
+
+ System.out.println("Test Pass!!");
+ } finally {
+ EventQueue.invokeAndWait(() -> {
+ if (frame != null) {
+ frame.dispose();
+ }
+ });
+ }
+ }
+}
diff --git a/test/jdk/java/awt/Scrollbar/PageIncrementTest.java b/test/jdk/java/awt/Scrollbar/PageIncrementTest.java
new file mode 100644
index 00000000000..6bc7c955ed1
--- /dev/null
+++ b/test/jdk/java/awt/Scrollbar/PageIncrementTest.java
@@ -0,0 +1,82 @@
+/*
+ * 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 4677084
+ @summary Tests that the PageIncrement (BlockIncrement) and
+ LineIncrement (UnitIncrement) cannot be < 1
+ @key headful
+*/
+
+import java.awt.Scrollbar;
+
+public class PageIncrementTest {
+ static Scrollbar sb;
+
+ public static void main(String[] args) {
+ sb = new Scrollbar();
+ sb.setBlockIncrement(0);
+ sb.setUnitIncrement(0);
+
+ if (sb.getBlockIncrement() < 1) {
+ String msg = "Failed: getBlockIncrement() == " + sb.getBlockIncrement();
+ System.out.println(msg);
+ throw new RuntimeException(msg);
+ }
+ if (sb.getUnitIncrement() < 1) {
+ String msg = "Failed: getLineIncrement() == " + sb.getUnitIncrement();
+ System.out.println(msg);
+ throw new RuntimeException(msg);
+ }
+
+ sb.setBlockIncrement(-1);
+ sb.setUnitIncrement(-1);
+
+ if (sb.getBlockIncrement() < 1) {
+ String msg = "Failed: getBlockIncrement() == " + sb.getBlockIncrement();
+ System.out.println(msg);
+ throw new RuntimeException(msg);
+ }
+ if (sb.getUnitIncrement() < 1) {
+ String msg = "Failed: getLineIncrement() == " + sb.getUnitIncrement();
+ System.out.println(msg);
+ throw new RuntimeException(msg);
+ }
+
+ sb.setBlockIncrement(2);
+ sb.setUnitIncrement(2);
+
+ if (sb.getBlockIncrement() != 2) {
+ String msg = "Failed: getBlockIncrement() == " + sb.getBlockIncrement();
+ System.out.println(msg);
+ throw new RuntimeException(msg);
+ }
+ if (sb.getUnitIncrement() != 2) {
+ String msg = "Failed: getLineIncrement() == " + sb.getUnitIncrement();
+ System.out.println(msg);
+ throw new RuntimeException(msg);
+ }
+ System.out.println("Test Pass!!");
+ }
+}
diff --git a/test/jdk/java/awt/Scrollbar/ScrollbarKeyControlTest.java b/test/jdk/java/awt/Scrollbar/ScrollbarKeyControlTest.java
new file mode 100644
index 00000000000..06c0b87e9d7
--- /dev/null
+++ b/test/jdk/java/awt/Scrollbar/ScrollbarKeyControlTest.java
@@ -0,0 +1,168 @@
+/*
+ * 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 4943277
+ @requires (os.family == "linux")
+ @summary XAWT: Scrollbar can't be controlled by keyboard
+ @key headful
+*/
+
+import java.awt.AWTException;
+import java.awt.EventQueue;
+import java.awt.Frame;
+import java.awt.Robot;
+import java.awt.Scrollbar;
+import java.awt.Toolkit;
+
+import java.awt.event.AdjustmentListener;
+import java.awt.event.AdjustmentEvent;
+import java.awt.event.InputEvent;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+public class ScrollbarKeyControlTest implements AdjustmentListener, KeyListener {
+ Scrollbar scrollbarV;
+ Scrollbar scrollbarH;
+ volatile int changesTotal = 0;
+ Robot robot;
+ Object LOCK = new Object();
+ Frame frame;
+
+ public static void main(String[] args) throws Exception {
+ if (!System.getProperty("os.name").startsWith("Linux")) {
+ System.out.println("This test is for XAWT only.");
+ return;
+ }
+ ScrollbarKeyControlTest scrollbarKeyControlTest = new ScrollbarKeyControlTest();
+ scrollbarKeyControlTest.init();
+ }
+
+ public void init() throws Exception {
+ try {
+ EventQueue.invokeAndWait(() -> {
+ frame = new Frame("Scrollbar Test");
+
+ scrollbarV = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
+ scrollbarH = new Scrollbar(Scrollbar.HORIZONTAL, 0, 60, 0, 300);
+
+ scrollbarH.addAdjustmentListener(this);
+ scrollbarH.addKeyListener(this);
+ scrollbarV.addAdjustmentListener(this);
+ scrollbarV.addKeyListener(this);
+
+ frame.add("South", scrollbarH);
+ frame.add("East", scrollbarV);
+
+ frame.setSize(200, 200);
+ frame.setVisible(true);
+ frame.validate();
+ frame.toFront();
+ });
+ robot = new Robot();
+ robot.delay(1000);
+ robot.waitForIdle();
+
+ testOneScrollbar(scrollbarV);
+ if (changesTotal != 9) { //one by mouse click and six by keys
+ throw new RuntimeException("Test failed. Not all adjustment " +
+ "events received by vertical scrollbar (" + changesTotal + " of 9)");
+ }
+ changesTotal = 0;
+ testOneScrollbar(scrollbarH);
+ if (changesTotal != 9) { //one by mouse click and six by keys
+ throw new RuntimeException("Test failed. Not all adjustment " +
+ "events received by horizontal scrollbar (" + changesTotal + " of 9)");
+ }
+ System.out.println("Test passed. Adjustment Event called "
+ + changesTotal + " times for each scrollbar");
+
+ } finally {
+ EventQueue.invokeAndWait(() -> {
+ if (frame != null) {
+ frame.dispose();
+ }
+ });
+ }
+ }
+
+ public void testOneScrollbar(Scrollbar sb) {
+ robot.waitForIdle();
+ robot.mouseMove(sb.getLocationOnScreen().x + sb.getWidth() / 2,
+ sb.getLocationOnScreen().y + sb.getHeight() / 2);
+ try {
+ synchronized (LOCK) {
+ robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
+ robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
+ LOCK.wait(2000);
+ robot.keyPress(KeyEvent.VK_DOWN);
+ robot.keyRelease(KeyEvent.VK_DOWN);
+ LOCK.wait(2000);
+ robot.keyPress(KeyEvent.VK_PAGE_DOWN);
+ robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
+ LOCK.wait(2000);
+ robot.keyPress(KeyEvent.VK_UP);
+ robot.keyRelease(KeyEvent.VK_UP);
+ LOCK.wait(2000);
+ robot.keyPress(KeyEvent.VK_PAGE_UP);
+ robot.keyRelease(KeyEvent.VK_PAGE_UP);
+ LOCK.wait(2000);
+ robot.keyPress(KeyEvent.VK_RIGHT);
+ robot.keyRelease(KeyEvent.VK_RIGHT);
+ LOCK.wait(2000);
+ robot.keyPress(KeyEvent.VK_LEFT);
+ robot.keyRelease(KeyEvent.VK_LEFT);
+ LOCK.wait(2000);
+ robot.keyPress(KeyEvent.VK_HOME);
+ robot.keyRelease(KeyEvent.VK_HOME);
+ LOCK.wait(2000);
+ robot.keyPress(KeyEvent.VK_END);
+ robot.keyRelease(KeyEvent.VK_END);
+ LOCK.wait(2000);
+ }
+ } catch (InterruptedException e) {
+ throw new RuntimeException("Test interrupted while keys being pressed.", e);
+ }
+ }
+
+ public void adjustmentValueChanged(AdjustmentEvent e) {
+ changesTotal++;
+ synchronized (LOCK) {
+ LOCK.notify();
+ }
+ System.out.println("Adjustment Event called ");
+ }
+
+ public void keyPressed(KeyEvent e) {
+ System.out.println("KeyPressed called");
+ }
+
+ public void keyReleased(KeyEvent e) {
+ System.out.println("in keyReleased");
+ }
+
+ public void keyTyped(KeyEvent e) {
+ System.out.println("in keyTyped");
+ }
+}