From 5fe6cf88bafc57f34f59a1cb32a98d6bdb79791f Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier <goetz@openjdk.org> Date: Thu, 31 Oct 2024 09:34:08 +0000 Subject: [PATCH] 8332901: Select{Current,New}ItemTest.java for Choice don't open popup on macOS Move SelectCurrentItemTest.java to java/awt/Choice/SelectItem/. Move SelectNewItemTest.java to java/awt/Choice/SelectItem/. Use latches to control test flow instead of delays. Encapsulate the common logic in SelectCurrentItemTest. Provide overridable checkXXX() methods to modify conditions. Provide an overridable method which defines where to click in the choice popup to select an item. Backport-of: ef96a7b014795f366af3a90ef8f474cfb621197f --- test/jdk/ProblemList.txt | 3 - .../SelectCurrentItemTest.java | 134 ------------ .../SelectItem/SelectCurrentItemTest.java | 207 ++++++++++++++++++ .../Choice/SelectItem/SelectNewItemTest.java | 69 ++++++ .../SelectNewItemTest/SelectNewItemTest.java | 180 --------------- 5 files changed, 276 insertions(+), 317 deletions(-) delete mode 100644 test/jdk/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java create mode 100644 test/jdk/java/awt/Choice/SelectItem/SelectCurrentItemTest.java create mode 100644 test/jdk/java/awt/Choice/SelectItem/SelectNewItemTest.java delete mode 100644 test/jdk/java/awt/Choice/SelectNewItemTest/SelectNewItemTest.java diff --git a/test/jdk/ProblemList.txt b/test/jdk/ProblemList.txt index 7c6878fc19f..b1ea7cea3dd 100644 --- a/test/jdk/ProblemList.txt +++ b/test/jdk/ProblemList.txt @@ -509,9 +509,6 @@ java/awt/KeyboardFocusmanager/ConsumeNextMnemonicKeyTypedTest/ConsumeNextMnemoni java/awt/Window/GetScreenLocation/GetScreenLocationTest.java 8225787 linux-x64 java/awt/Dialog/MakeWindowAlwaysOnTop/MakeWindowAlwaysOnTop.java 8266243 macosx-aarch64 -# This test fails on macOS 14 -java/awt/Choice/SelectNewItemTest/SelectNewItemTest.java 8324782 macosx-all - # Wayland related java/awt/FullScreen/FullscreenWindowProps/FullscreenWindowProps.java 8280991 linux-x64 diff --git a/test/jdk/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java b/test/jdk/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java deleted file mode 100644 index 3d3f8c80793..00000000000 --- a/test/jdk/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (c) 2002, 2018, 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 4902933 8197810 - @summary Test that selecting the current item doesnot send an ItemEvent - @key headful - @run main SelectCurrentItemTest -*/ - -import java.awt.Choice; -import java.awt.Robot; -import java.awt.Frame; -import java.awt.BorderLayout; -import java.awt.AWTException; -import java.awt.Point; -import java.awt.Dimension; -import java.awt.event.InputEvent; -import java.awt.event.ItemListener; -import java.awt.event.WindowListener; -import java.awt.event.ItemEvent; -import java.awt.event.WindowEvent; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -public class SelectCurrentItemTest implements ItemListener, WindowListener { - //Declare things used in the test, like buttons and labels here - private Frame frame; - private Choice theChoice; - private Robot robot; - - private CountDownLatch latch = new CountDownLatch(1); - private volatile boolean passed = true; - - private void init() - { - try { - robot = new Robot(); - robot.setAutoDelay(500); - } catch (AWTException e) { - throw new RuntimeException("Unable to create Robot. Test fails."); - } - - frame = new Frame("SelectCurrentItemTest"); - frame.setLayout(new BorderLayout()); - theChoice = new Choice(); - for (int i = 0; i < 10; i++) { - theChoice.add(new String("Choice Item " + i)); - } - theChoice.addItemListener(this); - frame.add(theChoice); - frame.addWindowListener(this); - - frame.setLocation(1,20); - robot.mouseMove(10, 30); - frame.pack(); - frame.setVisible(true); - } - - public static void main(String... args) { - SelectCurrentItemTest test = new SelectCurrentItemTest(); - test.init(); - try { - test.latch.await(12000, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) {} - test.robot.waitForIdle(); - - try { - if (!test.passed) { - throw new RuntimeException("TEST FAILED."); - } - } finally { - test.frame.dispose(); - } - } - - private void run() { - try {Thread.sleep(1000);} catch (InterruptedException e){} - // get loc of Choice on screen - Point loc = theChoice.getLocationOnScreen(); - // get bounds of Choice - Dimension size = theChoice.getSize(); - robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2); - - robot.setAutoDelay(250); - robot.mousePress(InputEvent.BUTTON1_MASK); - robot.mouseRelease(InputEvent.BUTTON1_MASK); - - robot.delay(1000); - - robot.mouseMove(loc.x + size.width / 2, loc.y + size.height); - robot.mousePress(InputEvent.BUTTON1_MASK); - robot.mouseRelease(InputEvent.BUTTON1_MASK); - robot.waitForIdle(); - latch.countDown(); - } - - @Override public void itemStateChanged(ItemEvent e) { - System.out.println("ItemEvent received. Test fails"); - passed = false; - } - - @Override public void windowOpened(WindowEvent e) { - System.out.println("windowActivated()"); - (new Thread(this::run)).start(); - } - - @Override public void windowActivated(WindowEvent e) {} - @Override public void windowDeactivated(WindowEvent e) {} - @Override public void windowClosed(WindowEvent e) {} - @Override public void windowClosing(WindowEvent e) {} - @Override public void windowIconified(WindowEvent e) {} - @Override public void windowDeiconified(WindowEvent e) {} -} diff --git a/test/jdk/java/awt/Choice/SelectItem/SelectCurrentItemTest.java b/test/jdk/java/awt/Choice/SelectItem/SelectCurrentItemTest.java new file mode 100644 index 00000000000..9bfcfeb9a97 --- /dev/null +++ b/test/jdk/java/awt/Choice/SelectItem/SelectCurrentItemTest.java @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2002, 2024, 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. + */ + +import java.awt.AWTException; +import java.awt.BorderLayout; +import java.awt.Choice; +import java.awt.EventQueue; +import java.awt.Frame; +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.Robot; +import java.awt.event.InputEvent; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +/* + * @test + * @bug 4902933 8197810 + * @summary Test that selecting the current item does not send an ItemEvent + * @key headful + * @run main SelectCurrentItemTest +*/ +public class SelectCurrentItemTest + extends WindowAdapter + implements ItemListener { + private static Frame frame; + private static Choice choice; + + private final Robot robot; + + private final CountDownLatch windowOpened = new CountDownLatch(1); + private final CountDownLatch mouseClicked = new CountDownLatch(1); + + protected final CountDownLatch itemStateChanged = new CountDownLatch(1); + + protected SelectCurrentItemTest() throws AWTException { + robot = new Robot(); + robot.setAutoDelay(250); + } + + private void createUI() { + frame = new Frame(getClass().getName()); + frame.setLayout(new BorderLayout()); + + choice = new Choice(); + for (int i = 0; i < 10; i++) { + choice.add("Choice Item " + i); + } + choice.addItemListener(this); + choice.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + System.out.println("mouseClicked()"); + mouseClicked.countDown(); + } + }); + + frame.add(choice, BorderLayout.CENTER); + + frame.addWindowListener(this); + + frame.setLocationRelativeTo(null); + frame.setResizable(false); + frame.pack(); + frame.setVisible(true); + } + + protected final void runTest() + throws InterruptedException, InvocationTargetException { + try { + doTest(); + } finally { + EventQueue.invokeAndWait(this::dispose); + } + } + + private void doTest() + throws InterruptedException, InvocationTargetException { + EventQueue.invokeAndWait(this::createUI); + + if (!windowOpened.await(2, TimeUnit.SECONDS)) { + throw new RuntimeException("Frame is not open in time"); + } + robot.waitForIdle(); + + final int initialIndex = getSelectedIndex(); + + final Rectangle choiceRect = getChoiceRect(); + + // Open the choice popup + robot.mouseMove(choiceRect.x + choiceRect.width - 10, + choiceRect.y + choiceRect.height / 2); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + + if (!mouseClicked.await(500, TimeUnit.MILLISECONDS)) { + throw new RuntimeException("Mouse is not clicked in time"); + } + robot.waitForIdle(); + + // Click an item in the choice popup + final Point pt = getClickLocation(choiceRect); + robot.mouseMove(pt.x, pt.y); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + + robot.waitForIdle(); + + checkItemStateChanged(); + + final int currentIndex = getSelectedIndex(); + System.out.println("initialIndex = " + initialIndex); + System.out.println("currentIndex = " + currentIndex); + checkSelectedIndex(initialIndex, currentIndex); + } + + protected void checkItemStateChanged() throws InterruptedException { + if (itemStateChanged.await(500, TimeUnit.MILLISECONDS)) { + throw new RuntimeException("ItemEvent is received but unexpected"); + } + } + + protected void checkSelectedIndex(final int initialIndex, + final int currentIndex) { + if (initialIndex != currentIndex) { + throw new RuntimeException("Selected index in Choice should not change"); + } + } + + /** + * {@return the location for clicking choice popup to select an item} + * @param choiceRect the bounds of the Choice component + */ + protected Point getClickLocation(final Rectangle choiceRect) { + // Click on the first item in the popup, it's the selected item + return new Point(choiceRect.x + choiceRect.width / 2, + choiceRect.y + choiceRect.height + 3); + } + + private int getSelectedIndex() + throws InterruptedException, InvocationTargetException { + AtomicInteger index = new AtomicInteger(); + EventQueue.invokeAndWait(() -> index.set(choice.getSelectedIndex())); + return index.get(); + } + + private Rectangle getChoiceRect() + throws InterruptedException, InvocationTargetException { + AtomicReference<Rectangle> rect = new AtomicReference<>(); + EventQueue.invokeAndWait( + () -> rect.set(new Rectangle(choice.getLocationOnScreen(), + choice.getSize()))); + return rect.get(); + } + + public static void main(String... args) throws Exception { + new SelectCurrentItemTest().runTest(); + } + + private void dispose() { + if (frame != null) { + frame.dispose(); + } + } + + @Override + public final void itemStateChanged(ItemEvent e) { + System.out.println("itemStateChanged: " + e); + itemStateChanged.countDown(); + } + + @Override + public final void windowOpened(WindowEvent e) { + System.out.println("windowActivated()"); + windowOpened.countDown(); + } + +} diff --git a/test/jdk/java/awt/Choice/SelectItem/SelectNewItemTest.java b/test/jdk/java/awt/Choice/SelectItem/SelectNewItemTest.java new file mode 100644 index 00000000000..1dba3fd905b --- /dev/null +++ b/test/jdk/java/awt/Choice/SelectItem/SelectNewItemTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2019, 2024, 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. + */ + +import java.awt.AWTException; +import java.awt.Point; +import java.awt.Rectangle; +import java.util.concurrent.TimeUnit; + +/* + * @test + * @bug 8215921 + * @summary Test that selecting a different item does send an ItemEvent + * @key headful + * @run main SelectNewItemTest +*/ +public final class SelectNewItemTest + extends SelectCurrentItemTest { + + private SelectNewItemTest() throws AWTException { + super(); + } + + @Override + protected void checkItemStateChanged() throws InterruptedException { + if (!itemStateChanged.await(500, TimeUnit.MILLISECONDS)) { + throw new RuntimeException("ItemEvent is not received"); + } + } + + @Override + protected void checkSelectedIndex(final int initialIndex, + final int currentIndex) { + if (initialIndex == currentIndex) { + throw new RuntimeException("Selected index in Choice should've changed"); + } + } + + @Override + protected Point getClickLocation(final Rectangle choiceRect) { + // Click a different item the popup, not the first one + return new Point(choiceRect.x + choiceRect.width / 2, + choiceRect.y + choiceRect.height * 3); + } + + public static void main(String... args) throws Exception { + new SelectNewItemTest().runTest(); + } + +} diff --git a/test/jdk/java/awt/Choice/SelectNewItemTest/SelectNewItemTest.java b/test/jdk/java/awt/Choice/SelectNewItemTest/SelectNewItemTest.java deleted file mode 100644 index 502c5a161df..00000000000 --- a/test/jdk/java/awt/Choice/SelectNewItemTest/SelectNewItemTest.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright (c) 2019, 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 8215921 - @summary Test that selecting a different item does send an ItemEvent - @key headful - @run main SelectNewItemTest -*/ - -import java.awt.Choice; -import java.awt.Robot; -import java.awt.Frame; -import java.awt.BorderLayout; -import java.awt.AWTException; -import java.awt.Point; -import java.awt.Dimension; -import java.awt.event.InputEvent; -import java.awt.event.ItemListener; -import java.awt.event.WindowListener; -import java.awt.event.ItemEvent; -import java.awt.event.WindowEvent; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -public class SelectNewItemTest implements ItemListener, WindowListener { - //Declare things used in the test, like buttons and labels here - private Frame frame; - private Choice theChoice; - private Robot robot; - - private CountDownLatch latch = new CountDownLatch(1); - private volatile boolean passed = false; - - private void init() - { - try { - robot = new Robot(); - robot.setAutoDelay(500); - } catch (AWTException e) { - throw new RuntimeException("Unable to create Robot. Test fails."); - } - - frame = new Frame("SelectNewItemTest"); - frame.setLayout(new BorderLayout()); - theChoice = new Choice(); - for (int i = 0; i < 10; i++) { - theChoice.add(new String("Choice Item " + i)); - } - theChoice.addItemListener(this); - frame.add(theChoice); - frame.addWindowListener(this); - - frame.setLocation(1,20); - frame.setSize(200, 50); - robot.mouseMove(10, 30); - frame.pack(); - frame.setVisible(true); - } - - public static void main(String... args) { - SelectNewItemTest test = new SelectNewItemTest(); - test.init(); - try { - test.latch.await(12000, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) {} - test.robot.waitForIdle(); - - try { - if (!test.passed) { - throw new RuntimeException("TEST FAILED."); - } - } finally { - test.frame.dispose(); - } - } - - private void run() { - try { - Thread.sleep(1000); - - Point loc = theChoice.getLocationOnScreen(); - int selectedIndex = theChoice.getSelectedIndex(); - Dimension size = theChoice.getSize(); - - robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2); - - robot.setAutoDelay(250); - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - - robot.delay(1000); - - //make sure that the mouse moves to a different item, so that - //itemStateChanged is called. - robot.mouseMove(loc.x + size.width / 2, loc.y + 3 * size.height); - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - robot.waitForIdle(); - - if (selectedIndex == theChoice.getSelectedIndex()) - throw new RuntimeException("Test case failed - expected to select" + - " a different item than " + selectedIndex); - - selectedIndex = theChoice.getSelectedIndex(); - //now click on the same item and make sure that item event is - //not generated. - robot.delay(1000); - robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2); - - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - //Make sure that the popup menu scrolls back to show the index from - //beginning, so that the second mouse click happens on the previously - //selected item. - //For example, on windows, it automatically scrolls the list to show - //the currently selected item just below the choice, which can - //throw off the test. - if (System.getProperty("os.name").toLowerCase().startsWith("win")) { - robot.mouseWheel(-100); - } - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - - robot.delay(1000); - robot.mouseMove(loc.x + size.width / 2, loc.y + 3 * size.height); - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - robot.waitForIdle(); - - if (selectedIndex != theChoice.getSelectedIndex()) - throw new RuntimeException("Test failed. Expected to select the same item " + - "located at: " + selectedIndex + " but got an item selected at: " + theChoice.getSelectedIndex()); - } catch(InterruptedException e) { - throw new RuntimeException(e.getCause()); - } finally { - latch.countDown(); - } - } - - @Override public void itemStateChanged(ItemEvent e) { - if (!passed) { - System.out.println("ItemEvent received. Test passes"); - passed = true; - } else { - System.out.println("ItemEvent received for second click. Test fails"); - passed = false; - } - } - - @Override public void windowOpened(WindowEvent e) { - System.out.println("windowActivated()"); - (new Thread(this::run)).start(); - } - - @Override public void windowActivated(WindowEvent e) {} - @Override public void windowDeactivated(WindowEvent e) {} - @Override public void windowClosed(WindowEvent e) {} - @Override public void windowClosing(WindowEvent e) {} - @Override public void windowIconified(WindowEvent e) {} - @Override public void windowDeiconified(WindowEvent e) {} -} -- GitLab