Skip to content

Commit

Permalink
Backport 6f4fc82149b52dd91289fe42def7d1cacad31212
Browse files Browse the repository at this point in the history
  • Loading branch information
duke committed Feb 14, 2025
1 parent e3d6b0a commit cc33457
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 113 deletions.
89 changes: 54 additions & 35 deletions test/jdk/java/awt/TrayIcon/ActionCommand/ActionCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2025, 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
Expand All @@ -21,7 +21,14 @@
* questions.
*/

import java.awt.*;
import jdk.test.lib.Platform;
import jtreg.SkippedException;

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;

/*
Expand All @@ -30,12 +37,18 @@
* @summary Check the return value of the getActionCommand method
* of the ActionEvent triggered when TrayIcon is double clicked
* (single clicked, on Mac)
* @author Dmitriy Ermashov ([email protected])
* @modules java.desktop/java.awt:open
* @library /lib/client ../
* @library /java/awt/patchlib
* @build java.desktop/java.awt.Helper
* @build ExtendedRobot SystemTrayIconHelper
* @library
* /java/awt/patchlib
* /java/awt/TrayIcon
* /lib/client
* /test/lib
* @build
* java.desktop/java.awt.Helper
* jdk.test.lib.Platform
* jtreg.SkippedException
* ExtendedRobot
* SystemTrayIconHelper
* @run main ActionCommand
*/

Expand All @@ -44,32 +57,38 @@ public class ActionCommand {
TrayIcon icon;
ExtendedRobot robot;

boolean actionPerformed = false;
Object actionLock = new Object();
String actionCommand = null;
volatile boolean actionPerformed = false;
volatile String actionCommand = null;
final Object actionLock = new Object();

static boolean isMacOS = false;

public static void main(String[] args) throws Exception {
if (! SystemTray.isSupported()) {
System.out.println("SystemTray not supported on the platform under test. " +
"Marking the test passed");
} else {
if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
System.err.println("Test can fail if the icon hides to a tray icons pool " +
"in Windows 7, which is behavior by default.\n" +
"Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
"\"Always show all icons and notifications on the taskbar\" true to " +
"avoid this problem. Or change behavior only for Java SE tray icon " +
"and rerun test.");
} else if (System.getProperty("os.name").toLowerCase().startsWith("mac")){
isMacOS = true;
} else if (SystemTrayIconHelper.isOel7orLater()) {
System.out.println("OEL 7 doesn't support double click in " +
"systray. Skipped");
return;
}
new ActionCommand().doTest();
if (Platform.isOnWayland()) {
// The current robot implementation does not support
// clicking in the system tray area.
throw new SkippedException("Skipped on Wayland");
}

if (!SystemTray.isSupported()) {
throw new SkippedException("SystemTray is not supported on this platform.");
}

if (Platform.isWindows()) {
System.err.println("Test can fail if the icon hides to a tray icons pool " +
"in Windows 7, which is behavior by default.\n" +
"Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
"\"Always show all icons and notifications on the taskbar\" true to " +
"avoid this problem. Or change behavior only for Java SE tray icon " +
"and rerun test.");
} else if (Platform.isOSX()){
isMacOS = true;
} else if (SystemTrayIconHelper.isOel7orLater()) {
System.out.println("OEL 7 doesn't support double click in " +
"systray. Skipped");
throw new SkippedException("Skipped on OEL 7+");
}
new ActionCommand().doTest();
}

void doTest() throws Exception {
Expand All @@ -95,7 +114,7 @@ void doTest() throws Exception {

icon.setActionCommand("Sample Command");

if (! "Sample Command".equals(icon.getActionCommand()))
if (!"Sample Command".equals(icon.getActionCommand()))
throw new RuntimeException("FAIL: getActionCommand did not return the correct value. " +
icon.getActionCommand() + " Expected: Sample Command");

Expand All @@ -117,15 +136,15 @@ void doTest() throws Exception {
actionPerformed = false;
SystemTrayIconHelper.doubleClick(robot);

if (! actionPerformed) {
if (!actionPerformed) {
synchronized (actionLock) {
try {
actionLock.wait(3000);
} catch (Exception e) {
}
}
}
if (! actionPerformed) {
if (!actionPerformed) {
throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is "+(isMacOS? "" : "double ")+"clicked");
} else if (! "Sample Command".equals(actionCommand)) {
throw new RuntimeException("FAIL: ActionEvent.getActionCommand did not return the correct " +
Expand All @@ -140,7 +159,7 @@ void doTest() throws Exception {
}
});

robot.mouseMove(0, 0);
robot.mouseMove(100, 0);
robot.waitForIdle();
robot.mouseMove(iconPosition.x, iconPosition.y);
robot.waitForIdle();
Expand All @@ -149,15 +168,15 @@ void doTest() throws Exception {
actionCommand = null;
SystemTrayIconHelper.doubleClick(robot);

if (! actionPerformed) {
if (!actionPerformed) {
synchronized (actionLock) {
try {
actionLock.wait(3000);
} catch (Exception e) {
}
}
}
if (! actionPerformed) {
if (!actionPerformed) {
throw new RuntimeException("FAIL: ActionEvent not triggered when ActionCommand set to " +
"null and then TrayIcon is "+(isMacOS? "" : "double ")+ "clicked");
} else if (actionCommand != null) {
Expand Down
58 changes: 37 additions & 21 deletions test/jdk/java/awt/TrayIcon/TrayIconMouseTest/TrayIconMouseTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2025, 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
Expand All @@ -21,6 +21,9 @@
* questions.
*/

import jdk.test.lib.Platform;
import jtreg.SkippedException;

import java.awt.EventQueue;
import java.awt.Point;
import java.awt.SystemTray;
Expand All @@ -37,27 +40,36 @@
* or single clicked with button 3 on Mac OS X
* or single clicked with button 1 on rest.
* @modules java.desktop/java.awt:open
* @library /java/awt/patchlib
* @library /lib/client ../
* @build java.desktop/java.awt.Helper
* @build ExtendedRobot SystemTrayIconHelper
* @library
* /java/awt/patchlib
* /java/awt/TrayIcon
* /lib/client
* /test/lib
* @build
* java.desktop/java.awt.Helper
* jdk.test.lib.Platform
* jtreg.SkippedException
* ExtendedRobot
* SystemTrayIconHelper
* @run main TrayIconMouseTest
*/

public class TrayIconMouseTest {

TrayIcon icon;
ExtendedRobot robot;
boolean actionPerformed = false;
Object actionLock = new Object();

volatile boolean actionPerformed = false;
final Object actionLock = new Object();

static boolean isMacOS = false;
static boolean isWinOS = false;
static boolean isOelOS = false;
String caption = "Sample Icon";
int[] buttonTypes = {
InputEvent.BUTTON1_MASK,
InputEvent.BUTTON2_MASK,
InputEvent.BUTTON3_MASK
InputEvent.BUTTON1_DOWN_MASK,
InputEvent.BUTTON2_DOWN_MASK,
InputEvent.BUTTON3_DOWN_MASK
};
String[] buttonNames = {
"BUTTON1",
Expand All @@ -66,20 +78,24 @@ public class TrayIconMouseTest {
};

public static void main(String[] args) throws Exception {
if (Platform.isOnWayland()) {
// The current robot implementation does not support
// clicking in the system tray area.
throw new SkippedException("Skipped on Wayland");
}

if (!SystemTray.isSupported()) {
System.out.println("SystemTray not supported on the platform "
+ "under test. Marking the test passed");
throw new SkippedException("SystemTray is not supported on this platform.");
}

if (Platform.isOSX()) {
isMacOS = true;
} else if (Platform.isWindows()) {
isWinOS = true;
} else {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.startsWith("mac")) {
isMacOS = true;
} else if (osName.startsWith("win")) {
isWinOS = true;
} else {
isOelOS = SystemTrayIconHelper.isOel7orLater();
}
new TrayIconMouseTest().doTest();
isOelOS = SystemTrayIconHelper.isOel7orLater();
}
new TrayIconMouseTest().doTest();
}

TrayIconMouseTest() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, 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
Expand All @@ -20,14 +20,15 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.test.lib.Platform;
import jtreg.SkippedException;

import java.awt.TrayIcon;
import java.awt.SystemTray;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.AWTException;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferedImage;
Expand All @@ -37,35 +38,48 @@
* @key headful
* @summary Check if a action performed event is received when TrayIcon display
* message is clicked on.
* @author Shashidhara Veerabhadraiah ([email protected])
* @modules java.desktop/java.awt:open
* @library /java/awt/patchlib
* @library /lib/client ../
* @build java.desktop/java.awt.Helper
* @build ExtendedRobot SystemTrayIconHelper
* @library
* /java/awt/patchlib
* /java/awt/TrayIcon
* /lib/client
* /test/lib
* @build
* java.desktop/java.awt.Helper
* jdk.test.lib.Platform
* jtreg.SkippedException
* ExtendedRobot
* SystemTrayIconHelper
* @run main TrayIconPopupClickTest
*/

public class TrayIconPopupClickTest {

TrayIcon icon;
ExtendedRobot robot;
boolean actionPerformed = false;
volatile boolean actionPerformed = false;

public static void main(String[] args) throws Exception {
if (Platform.isOnWayland()) {
// The current robot implementation does not support
// clicking in the system tray area.
throw new SkippedException("Skipped on Wayland");
}

if (!SystemTray.isSupported()) {
System.out.println("SystemTray not supported on the platform under test. " +
"Marking the test passed");
} else {
if (System.getProperty("os.name").toLowerCase().startsWith("win"))
System.err.println("Test can fail if the icon hides to a tray icons pool " +
"in Windows 7/10, which is behavior by default.\n" +
"Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
"\"Always show all icons and notifications on the taskbar\" true " +
"to avoid this problem. Or change behavior only for Java SE " +
"tray icon.");
new TrayIconPopupClickTest().doTest();
throw new SkippedException("SystemTray is not supported on this platform.");
}

if (Platform.isWindows()) {
System.err.println("Test can fail if the icon hides to a tray icons pool " +
"in Windows 7/10, which is behavior by default.\n" +
"Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
"\"Always show all icons and notifications on the taskbar\" true " +
"to avoid this problem. Or change behavior only for Java SE " +
"tray icon.");
}

new TrayIconPopupClickTest().doTest();
}

TrayIconPopupClickTest() throws Exception {
Expand All @@ -89,33 +103,26 @@ public void mousePressed(MouseEvent event) {
throw new RuntimeException(e);
}

icon.getActionCommand();
icon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
actionPerformed = true;
}
});
icon.addActionListener(e -> actionPerformed = true);
}

void doTest() throws Exception {

Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
if (iconPosition == null)
throw new RuntimeException("Unable to find the icon location!");

robot.mouseMove(iconPosition.x, iconPosition.y);
robot.waitForIdle();
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(50);

robot.mouseMove(iconPosition.x, iconPosition.y + 10);
robot.waitForIdle();
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(50);

if (!actionPerformed)
Expand Down
Loading

0 comments on commit cc33457

Please sign in to comment.