mirror of
https://github.com/openjdk/jdk.git
synced 2026-02-13 03:45:19 +00:00
8044765: Move functional tests AWT_SystemTray/Automated to openjdk repository
Reviewed-by: pchelko
This commit is contained in:
parent
b7b41fb089
commit
5ecd91e84a
162
jdk/test/java/awt/TrayIcon/ActionCommand/ActionCommand.java
Normal file
162
jdk/test/java/awt/TrayIcon/ActionCommand/ActionCommand.java
Normal file
@ -0,0 +1,162 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @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 (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../lib/testlibrary ../
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
* @run main ActionCommand
|
||||
*/
|
||||
|
||||
public class ActionCommand {
|
||||
|
||||
TrayIcon icon;
|
||||
ExtendedRobot robot;
|
||||
|
||||
boolean actionPerformed = false;
|
||||
Object actionLock = new Object();
|
||||
String actionCommand = null;
|
||||
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;
|
||||
}
|
||||
|
||||
new ActionCommand().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
void doTest() throws Exception {
|
||||
robot = new ExtendedRobot();
|
||||
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
|
||||
icon.addActionListener((event) -> {
|
||||
actionPerformed = true;
|
||||
actionCommand = event.getActionCommand();
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (icon.getActionCommand() != null)
|
||||
throw new RuntimeException("FAIL: getActionCommand did not return null " +
|
||||
"when no action command set " + icon.getActionCommand());
|
||||
|
||||
icon.setActionCommand("Sample Command");
|
||||
|
||||
if (! "Sample Command".equals(icon.getActionCommand()))
|
||||
throw new RuntimeException("FAIL: getActionCommand did not return the correct value. " +
|
||||
icon.getActionCommand() + " Expected: Sample Command");
|
||||
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (AWTException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
robot.waitForIdle();
|
||||
|
||||
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(2000);
|
||||
actionPerformed = false;
|
||||
SystemTrayIconHelper.doubleClick(robot);
|
||||
|
||||
if (! actionPerformed) {
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
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 " +
|
||||
"value. Returned: " + actionCommand + " ; Expected: Sample Command");
|
||||
}
|
||||
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
icon.setActionCommand(null);
|
||||
if (icon.getActionCommand() != null) {
|
||||
throw new RuntimeException("FAIL: ActionCommand set to null. getActionCommand did " +
|
||||
"not return null " + icon.getActionCommand());
|
||||
}
|
||||
});
|
||||
|
||||
robot.mouseMove(0, 0);
|
||||
robot.waitForIdle();
|
||||
robot.mouseMove(iconPosition.x, iconPosition.y);
|
||||
robot.waitForIdle();
|
||||
|
||||
actionPerformed = false;
|
||||
actionCommand = null;
|
||||
SystemTrayIconHelper.doubleClick(robot);
|
||||
|
||||
if (! actionPerformed) {
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
throw new RuntimeException("FAIL: ActionEvent.getActionCommand did not return null " +
|
||||
"when ActionCommand is set to null " + actionCommand);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
252
jdk/test/java/awt/TrayIcon/ActionEventMask/ActionEventMask.java
Normal file
252
jdk/test/java/awt/TrayIcon/ActionEventMask/ActionEventMask.java
Normal file
@ -0,0 +1,252 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check if ActionEvent triggered when a TrayIcon is double
|
||||
* (single, on Mac) clicked is visible by an AWTEventListener
|
||||
* added to the Toolkit. It also checks if all listeners are
|
||||
* triggered when multiple AWTEventListeners and ActionListeners
|
||||
* are added.
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../lib/testlibrary ../
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
* @run main ActionEventMask
|
||||
*/
|
||||
|
||||
public class ActionEventMask {
|
||||
|
||||
private Image image;
|
||||
|
||||
TrayIcon icon;
|
||||
ExtendedRobot robot;
|
||||
|
||||
boolean actionPerformed = false;
|
||||
boolean listenersInvoked = false;
|
||||
Object actionLock = new Object();
|
||||
Object listenersLock = new Object();
|
||||
static boolean isMacOS = false;
|
||||
static final int clickDelay = 50;
|
||||
|
||||
ActionListener[] listeners;
|
||||
boolean[] listenerStatus;
|
||||
|
||||
Object lLock = new Object();
|
||||
boolean doTest, listenerAdded;
|
||||
Button b1;
|
||||
|
||||
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("mac")) {
|
||||
isMacOS = true;
|
||||
}
|
||||
new ActionEventMask().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
public ActionEventMask() throws Exception {
|
||||
EventQueue.invokeAndWait(this::initializeGUI);
|
||||
}
|
||||
|
||||
void initializeGUI() {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
|
||||
|
||||
Toolkit.getDefaultToolkit().addAWTEventListener(event -> {
|
||||
if (doTest) {
|
||||
System.out.println("ActionListener AWTEventListener");
|
||||
if (! icon.equals(event.getSource())) {
|
||||
throw new RuntimeException("FAIL: ActionEvent not triggered for icon");
|
||||
}
|
||||
actionPerformed = true;
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}, AWTEvent.ACTION_EVENT_MASK);
|
||||
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (AWTException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
listeners = new ActionListener[3];
|
||||
listenerStatus = new boolean[3];
|
||||
for (int i = 0; i < listeners.length; i++) {
|
||||
final int index = i;
|
||||
listeners[i] = event -> {
|
||||
listenerStatus[index] = true;
|
||||
System.out.println("ActionListener listeners[" + index + "]");
|
||||
for (int j = 0; j < listenerStatus.length; j++) {
|
||||
if (! listenerStatus[j]) {
|
||||
break;
|
||||
}
|
||||
listenersInvoked = true;
|
||||
synchronized (listenersLock) {
|
||||
try {
|
||||
listenersLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Frame frame = new Frame("Test frame");
|
||||
b1 = new Button("Add ActionListener");
|
||||
b1.addActionListener(event -> {
|
||||
for (int i = 0; i < listeners.length; i++) {
|
||||
icon.addActionListener(listeners[i]);
|
||||
}
|
||||
listenerAdded = true;
|
||||
synchronized (lLock) {
|
||||
try {
|
||||
lLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
frame.setLayout(new FlowLayout());
|
||||
frame.add(b1);
|
||||
frame.setSize(200, 200);
|
||||
frame.addWindowListener(new WindowAdapter() {
|
||||
public void windowClosing(WindowEvent event) {
|
||||
System.err.println("User closed the window");
|
||||
System.exit(1);
|
||||
}
|
||||
});
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
|
||||
robot = new ExtendedRobot();
|
||||
|
||||
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();
|
||||
|
||||
actionPerformed = false;
|
||||
doTest = true;
|
||||
|
||||
if(isMacOS) {
|
||||
robot.click(InputEvent.BUTTON3_MASK);
|
||||
}else{
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(clickDelay);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(clickDelay);
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(clickDelay);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||
}
|
||||
|
||||
if (! actionPerformed) {
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! actionPerformed)
|
||||
throw new RuntimeException("FAIL: AWTEventListener not notified when TrayIcon " +
|
||||
"is "+(isMacOS ? "" :"double ")+ "clicked");
|
||||
|
||||
doTest = false;
|
||||
listenerAdded = false;
|
||||
robot.mouseMove(b1.getLocationOnScreen().x + b1.getSize().width / 2,
|
||||
b1.getLocationOnScreen().y + b1.getSize().height / 2);
|
||||
robot.waitForIdle();
|
||||
robot.click();
|
||||
|
||||
if (! listenerAdded) {
|
||||
synchronized (lLock) {
|
||||
try {
|
||||
lLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! listenerAdded)
|
||||
throw new RuntimeException("FAIL: ActionListener could not be added at runtime. " +
|
||||
"b1 did not trigger ActionEvent");
|
||||
|
||||
doTest = true;
|
||||
actionPerformed = false;
|
||||
robot.mouseMove(iconPosition.x, iconPosition.y);
|
||||
robot.waitForIdle();
|
||||
|
||||
if(isMacOS) {
|
||||
robot.click(InputEvent.BUTTON3_MASK);
|
||||
}else{
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(clickDelay);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(clickDelay);
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(clickDelay);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||
}
|
||||
|
||||
if (! listenersInvoked) {
|
||||
synchronized (listenersLock) {
|
||||
try {
|
||||
listenersLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! listenersInvoked) {
|
||||
System.err.println("FAIL: All the listeners not invoked!");
|
||||
for (int i = 0; i < listenerStatus.length; i++)
|
||||
throw new RuntimeException("Listener[" + i + "] invoked: " + listenerStatus[i]);
|
||||
}
|
||||
if (! actionPerformed) {
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! actionPerformed)
|
||||
throw new RuntimeException("FAIL: AWTEventListener not notified when TrayIcon " +
|
||||
"is "+(isMacOS? "" : "double ")+ "clicked. A set of listeners were added after it");
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check the getTrayIcons method of the SystemTray
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @run main GetTrayIcons
|
||||
*/
|
||||
|
||||
public class GetTrayIcons {
|
||||
|
||||
Image image;
|
||||
|
||||
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
|
||||
new GetTrayIcons().doTest();
|
||||
}
|
||||
|
||||
GetTrayIcons() {
|
||||
image = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
|
||||
}
|
||||
|
||||
void doTest() throws Exception {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
TrayIcon[] icons = tray.getTrayIcons();
|
||||
if (icons == null || icons.length > 0)
|
||||
throw new RuntimeException("FAIL: getTrayIcons() returned incorrect " +
|
||||
"value when no icons are added " + icons);
|
||||
|
||||
TrayIcon icon = new TrayIcon(image);
|
||||
tray.add(icon);
|
||||
|
||||
icons = tray.getTrayIcons();
|
||||
if (icons == null || icons.length != 1)
|
||||
throw new RuntimeException("FAIL: getTrayIcons() returned incorrect value " +
|
||||
"when one icon present " + icons);
|
||||
|
||||
icon = new TrayIcon(image);
|
||||
tray.add(icon);
|
||||
|
||||
icons = tray.getTrayIcons();
|
||||
if (icons == null || icons.length != 2)
|
||||
throw new RuntimeException("FAIL: getTrayIcons() returned incorrect value " +
|
||||
"when two icons present " + icons);
|
||||
|
||||
icons = tray.getTrayIcons();
|
||||
if (icons != null) {
|
||||
for (int i = 0; i < icons.length; i++) {
|
||||
tray.remove(icons[i]);
|
||||
}
|
||||
|
||||
TrayIcon[] newList = tray.getTrayIcons();
|
||||
|
||||
if (newList == null || newList.length != 0)
|
||||
throw new RuntimeException("FAIL: Incorrect value returned by getTrayIcons " +
|
||||
"after icons are added and then removed " + newList);
|
||||
}
|
||||
}
|
||||
}
|
||||
86
jdk/test/java/awt/TrayIcon/InterJVMTest/InterJVM.java
Normal file
86
jdk/test/java/awt/TrayIcon/InterJVMTest/InterJVM.java
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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 Check if TrayIcon added by a JVM is not visible
|
||||
* in another JVM
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../regtesthelpers/process/
|
||||
* @build ProcessResults ProcessCommunicator
|
||||
* @run main InterJVM
|
||||
*/
|
||||
|
||||
import test.java.awt.regtesthelpers.process.ProcessCommunicator;
|
||||
import test.java.awt.regtesthelpers.process.ProcessResults;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class InterJVM {
|
||||
|
||||
static String NEW_JVM = "-doTest";
|
||||
|
||||
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 (args == null || args.length == 0) {
|
||||
new InterJVM().addTrayIcon();
|
||||
} else {
|
||||
if (args.length == 1 && NEW_JVM.equals(args[0]))
|
||||
new InterJVM().doTest();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void doTest() throws Exception {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
try {
|
||||
TrayIcon[] icons = tray.getTrayIcons();
|
||||
System.out.println(icons.length);
|
||||
if (icons == null || icons.length != 0)
|
||||
throw new RuntimeException("FAIL: getTrayIcons() returned incorrect " +
|
||||
"value when two icons are added by a " +
|
||||
"separate JVM: " + icons.length);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
void addTrayIcon() throws Exception {
|
||||
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
TrayIcon icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB));
|
||||
tray.add(icon);
|
||||
|
||||
new Robot().delay(2000);
|
||||
|
||||
ProcessResults processResults =
|
||||
ProcessCommunicator.executeChildProcess(this.getClass(), new String[]{NEW_JVM});
|
||||
|
||||
if (processResults.getExitValue() != 0)
|
||||
throw new RuntimeException("\n"+processResults.getStdErr());
|
||||
}
|
||||
}
|
||||
302
jdk/test/java/awt/TrayIcon/ModalityTest/ModalityTest.java
Normal file
302
jdk/test/java/awt/TrayIcon/ModalityTest/ModalityTest.java
Normal file
@ -0,0 +1,302 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check for MouseEvents with all mouse buttons
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../lib/testlibrary ../
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
* @run main ModalityTest
|
||||
*/
|
||||
public class ModalityTest {
|
||||
|
||||
TrayIcon icon;
|
||||
ExtendedRobot robot;
|
||||
Dialog d;
|
||||
|
||||
boolean actionPerformed = false;
|
||||
|
||||
private boolean dialogVisible = false;
|
||||
private Object dialogLock = new Object();
|
||||
|
||||
Object actionLock = new Object();
|
||||
Object pressLock = new Object();
|
||||
Object releaseLock = new Object();
|
||||
Object clickLock = new Object();
|
||||
Object moveLock = new Object();
|
||||
static final int clickDelay = 50;
|
||||
|
||||
boolean mousePressed = false;
|
||||
boolean mouseReleased = false;
|
||||
boolean mouseClicked = false;
|
||||
boolean mouseMoved = false;
|
||||
|
||||
int[] buttonTypes = {
|
||||
InputEvent.BUTTON1_MASK,
|
||||
InputEvent.BUTTON2_MASK,
|
||||
InputEvent.BUTTON3_MASK
|
||||
};
|
||||
|
||||
String[] buttonNames = {
|
||||
"BUTTON1",
|
||||
"BUTTON2",
|
||||
"BUTTON3"
|
||||
};
|
||||
|
||||
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.");
|
||||
|
||||
new ModalityTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
public ModalityTest() throws Exception {
|
||||
robot = new ExtendedRobot();
|
||||
EventQueue.invokeLater(this::initializeGUI);
|
||||
}
|
||||
|
||||
void initializeGUI() {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
|
||||
icon.addActionListener(event -> {
|
||||
actionPerformed = true;
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
icon.addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent event) {
|
||||
mousePressed = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mousePressed");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (pressLock) {
|
||||
try {
|
||||
pressLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseReleased(MouseEvent event) {
|
||||
mouseReleased = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mouseReleased");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (releaseLock) {
|
||||
try {
|
||||
releaseLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseClicked(MouseEvent event) {
|
||||
mouseClicked = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mouseClicked");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (clickLock) {
|
||||
try {
|
||||
clickLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
icon.addMouseMotionListener(new MouseMotionAdapter() {
|
||||
public void mouseMoved(MouseEvent event) {
|
||||
mouseMoved = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mouseMoved");
|
||||
|
||||
if (!icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseMoved: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (moveLock) {
|
||||
try {
|
||||
moveLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (AWTException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
d = new Dialog((Frame) null, "Modal Dialog");
|
||||
d.setLocation(200, 200);
|
||||
d.setSize(100, 100);
|
||||
d.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL);
|
||||
|
||||
dialogVisible = true;
|
||||
synchronized (dialogLock) {
|
||||
try {
|
||||
dialogLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
d.setVisible(true);
|
||||
}
|
||||
|
||||
void doTest() throws Exception {
|
||||
|
||||
if (! dialogVisible) {
|
||||
synchronized (dialogLock) {
|
||||
try {
|
||||
dialogLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (! dialogVisible)
|
||||
throw new RuntimeException("ERROR: TIMEOUT: The thread in EDT not yet complete");
|
||||
|
||||
Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
|
||||
if (iconPosition == null)
|
||||
throw new RuntimeException("Unable to find the icon location!");
|
||||
|
||||
if (! d.isVisible())
|
||||
throw new RuntimeException("FAIL: The modal dialog is not yet visible");
|
||||
|
||||
robot.mouseMove(iconPosition.x, iconPosition.y);
|
||||
robot.waitForIdle(2000);
|
||||
|
||||
SystemTrayIconHelper.doubleClick(robot);
|
||||
|
||||
if (! actionPerformed) {
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! actionPerformed)
|
||||
throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");
|
||||
|
||||
for (int i = 0; i < buttonTypes.length; i++) {
|
||||
mousePressed = false;
|
||||
robot.mousePress(buttonTypes[i]);
|
||||
|
||||
if (! mousePressed) {
|
||||
synchronized (pressLock) {
|
||||
try {
|
||||
pressLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mousePressed)
|
||||
if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
|
||||
throw new RuntimeException("FAIL: mousePressed not triggered when " +
|
||||
buttonNames[i] + " pressed");
|
||||
|
||||
mouseReleased = false;
|
||||
mouseClicked = false;
|
||||
robot.mouseRelease(buttonTypes[i]);
|
||||
|
||||
if (! mouseReleased) {
|
||||
synchronized (releaseLock) {
|
||||
try {
|
||||
releaseLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mouseReleased)
|
||||
throw new RuntimeException("FAIL: mouseReleased not triggered when " +
|
||||
buttonNames[i] + " released");
|
||||
|
||||
if (! mouseClicked) {
|
||||
synchronized (clickLock) {
|
||||
try {
|
||||
clickLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mouseClicked)
|
||||
throw new RuntimeException("FAIL: mouseClicked not triggered when " +
|
||||
buttonNames[i] + " pressed & released");
|
||||
}
|
||||
|
||||
mouseMoved = false;
|
||||
robot.mouseMove(iconPosition.x, iconPosition.y);
|
||||
robot.glide(iconPosition.x + 100, iconPosition.y);
|
||||
|
||||
if (! mouseMoved)
|
||||
if (! SystemTrayIconHelper.skip(0) )
|
||||
throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,195 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check if MouseEvents triggered by TrayIcon are visible
|
||||
* by an AWTEventListener added to the Toolkit. It also
|
||||
* checks if all listeners are triggered when AWTEventListeners
|
||||
* and MouseListeners are added.
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../lib/testlibrary ../
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
* @run main MouseEventMaskTest
|
||||
*/
|
||||
|
||||
public class MouseEventMaskTest {
|
||||
|
||||
TrayIcon icon;
|
||||
Robot robot;
|
||||
int[] buttonTypes = {
|
||||
InputEvent.BUTTON1_MASK,
|
||||
InputEvent.BUTTON2_MASK,
|
||||
InputEvent.BUTTON3_MASK
|
||||
};
|
||||
|
||||
String[] buttonNames = {
|
||||
"BUTTON1",
|
||||
"BUTTON2",
|
||||
"BUTTON3"
|
||||
};
|
||||
|
||||
boolean mouseEventTriggered = false;
|
||||
boolean mouseMotionEventTriggered = false;
|
||||
Object mouseEventLock = new Object();
|
||||
Object mouseMotionEventLock = new Object();
|
||||
boolean mouseMotionTest, mouseTest;
|
||||
|
||||
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.");
|
||||
}
|
||||
new MouseEventMaskTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
public MouseEventMaskTest() throws Exception{
|
||||
EventQueue.invokeAndWait(this::initializeGUI);
|
||||
}
|
||||
|
||||
void initializeGUI() {
|
||||
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
|
||||
|
||||
Toolkit.getDefaultToolkit().addAWTEventListener(event -> {
|
||||
if (mouseTest) {
|
||||
if (! event.getSource().getClass().getName().contains("Canvas")) {
|
||||
if (!icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: MouseEvent not triggered for icon " + event);
|
||||
|
||||
mouseEventTriggered = true;
|
||||
synchronized (mouseEventLock) {
|
||||
try {
|
||||
mouseEventLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, AWTEvent.MOUSE_EVENT_MASK);
|
||||
Toolkit.getDefaultToolkit().addAWTEventListener(event -> {
|
||||
if (mouseMotionTest) {
|
||||
if (! event.getSource().getClass().getName().contains("Canvas")) {
|
||||
if (!icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: MouseMotionEvent not triggered for icon " + event);
|
||||
|
||||
mouseMotionEventTriggered = true;
|
||||
synchronized (mouseMotionEventLock) {
|
||||
try {
|
||||
mouseMotionEventLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, AWTEvent.MOUSE_MOTION_EVENT_MASK);
|
||||
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (AWTException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
void doTest() throws Exception {
|
||||
|
||||
robot = new Robot();
|
||||
|
||||
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();
|
||||
|
||||
for (int i = 0; i < buttonTypes.length; i++) {
|
||||
System.out.println("Verify button "+buttonTypes[i]);
|
||||
mouseTest = true;
|
||||
mouseEventTriggered = false;
|
||||
robot.mousePress(buttonTypes[i]);
|
||||
|
||||
if (! mouseEventTriggered) {
|
||||
synchronized (mouseEventLock) {
|
||||
try {
|
||||
mouseEventLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mouseEventTriggered)
|
||||
if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
|
||||
throw new RuntimeException("FAIL: AWTEventListener not notified when " +
|
||||
buttonNames[i] + " pressed on TrayIcon");
|
||||
|
||||
mouseEventTriggered = false;
|
||||
robot.mouseRelease(buttonTypes[i]);
|
||||
if (! mouseEventTriggered) {
|
||||
synchronized (mouseEventLock) {
|
||||
try {
|
||||
mouseEventLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (! mouseEventTriggered)
|
||||
throw new RuntimeException("FAIL: AWTEventListener not notified when " +
|
||||
buttonNames[i] + " released on TrayIcon");
|
||||
}
|
||||
|
||||
mouseMotionTest = true;
|
||||
mouseTest = false;
|
||||
mouseMotionEventTriggered = false;
|
||||
|
||||
for (int i = 0; i < 20; i++) {
|
||||
robot.mouseMove(iconPosition.x + i, iconPosition.y);
|
||||
robot.delay(25);
|
||||
}
|
||||
if (! mouseMotionEventTriggered) {
|
||||
synchronized (mouseMotionEventLock) {
|
||||
try {
|
||||
mouseMotionEventLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mouseMotionEventTriggered)
|
||||
if (! SystemTrayIconHelper.skip(0) )
|
||||
throw new RuntimeException("FAIL: AWTEventListener not notified when " +
|
||||
"mouse moved");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2014, 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.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7153700
|
||||
* @summary Check for mouseMoved event for java.awt.TrayIcon
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../lib/testlibrary
|
||||
* @build ExtendedRobot
|
||||
* @run main MouseMovedTest
|
||||
*/
|
||||
|
||||
public class MouseMovedTest {
|
||||
static volatile boolean moved;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
moved = false;
|
||||
|
||||
TrayIcon icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Test icon");
|
||||
icon.addMouseMotionListener(new MouseMotionAdapter() {
|
||||
public void mouseMoved(MouseEvent event) {
|
||||
moved = true;
|
||||
System.out.println("Mouse moved");
|
||||
}
|
||||
});
|
||||
SystemTray.getSystemTray().add(icon);
|
||||
|
||||
ExtendedRobot robot = new ExtendedRobot();
|
||||
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
if (System.getProperty("os.name").toLowerCase().startsWith("win"))
|
||||
robot.glide(size.width / 2, size.height-15, size.width, size.height-15, 1, 3);
|
||||
else
|
||||
robot.glide(size.width / 2, 13, size.width, 13, 1, 3);
|
||||
robot.mouseMove(size.width/2, size.height/2);
|
||||
|
||||
if (!moved)
|
||||
throw new RuntimeException("Mouse moved action did not trigger");
|
||||
}
|
||||
}
|
||||
201
jdk/test/java/awt/TrayIcon/PropertyChangeListenerTest.java
Normal file
201
jdk/test/java/awt/TrayIcon/PropertyChangeListenerTest.java
Normal file
@ -0,0 +1,201 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check if custom property change listener added
|
||||
* to system tray works correctly
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @run main PropertyChangeListenerTest
|
||||
*/
|
||||
|
||||
public class PropertyChangeListenerTest implements PropertyChangeListener {
|
||||
|
||||
Object property;
|
||||
Object lock = new Object();
|
||||
boolean propertyChanged = 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 {
|
||||
new PropertyChangeListenerTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
public void propertyChange(PropertyChangeEvent event) {
|
||||
if (! "trayIcons".equals(event.getPropertyName()))
|
||||
throw new RuntimeException("ERROR: PropertyName not matching. Event " +
|
||||
"triggered for a different property\n"+
|
||||
"Property: " + event.getPropertyName());
|
||||
property = event.getNewValue();
|
||||
propertyChanged = true;
|
||||
synchronized (lock) {
|
||||
try {
|
||||
lock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void doTest() throws Exception {
|
||||
propertyChanged = false;
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
|
||||
tray.addPropertyChangeListener(null, null);
|
||||
tray.addPropertyChangeListener("trayIcons", null);
|
||||
tray.addPropertyChangeListener("trayIcons", this);
|
||||
|
||||
BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics g = img.createGraphics();
|
||||
g.setColor(Color.WHITE);
|
||||
g.fillRect(0, 0, 32, 32);
|
||||
g.setColor(Color.RED);
|
||||
g.fillRect(6, 6, 20, 20);
|
||||
g.dispose();
|
||||
|
||||
TrayIcon icon = new TrayIcon(img);
|
||||
if (propertyChanged)
|
||||
throw new RuntimeException("FAIL: spurious property events triggered");
|
||||
|
||||
propertyChanged = false;
|
||||
tray.add(icon);
|
||||
|
||||
if (! propertyChanged) {
|
||||
synchronized (lock) {
|
||||
try {
|
||||
lock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! propertyChanged) {
|
||||
throw new RuntimeException("FAIL: property event did not get triggered when tray icon added");
|
||||
} else {
|
||||
if (! (property instanceof TrayIcon[])) {
|
||||
throw new RuntimeException("FAIL: property is not TrayIcon[]. TrayIcon added.");
|
||||
} else {
|
||||
TrayIcon[] icons = (TrayIcon[]) property;
|
||||
if (icons.length != 1 || ! icon.equals(icons[0])) {
|
||||
throw new RuntimeException("FAIL: TrayIcon[] returned by the " +
|
||||
"PropertyChangeEvent is incorrect. TrayIcon added.\n"+
|
||||
"icon[] length: " + icons.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
propertyChanged = false;
|
||||
tray.remove(icon);
|
||||
|
||||
if (! propertyChanged) {
|
||||
synchronized (lock) {
|
||||
try {
|
||||
lock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! propertyChanged) {
|
||||
throw new RuntimeException("FAIL: property event did not get triggered when tray icon removed");
|
||||
} else {
|
||||
if (! (property instanceof TrayIcon[])) {
|
||||
throw new RuntimeException("FAIL: property is not TrayIcon[]. TrayIcon removed.");
|
||||
} else {
|
||||
TrayIcon[] icons = (TrayIcon[]) property;
|
||||
if (icons.length != 0) {
|
||||
throw new RuntimeException("FAIL: TrayIcon[] returned by the " +
|
||||
"PropertyChangeEvent is incorrect. TrayIcon removed.\n"+
|
||||
"icon[] length: " + icons.length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tray.removePropertyChangeListener("trayIcons", null);
|
||||
tray.removePropertyChangeListener("trayIcons", this);
|
||||
|
||||
propertyChanged = false;
|
||||
tray.add(icon);
|
||||
|
||||
Thread.sleep(3000);
|
||||
if (propertyChanged)
|
||||
throw new RuntimeException("FAIL: property listener notified even after " +
|
||||
"removing the listener from SystemTray. TrayIcon added.");
|
||||
|
||||
propertyChanged = false;
|
||||
tray.remove(icon);
|
||||
|
||||
Thread.sleep(3000);
|
||||
if (propertyChanged)
|
||||
throw new RuntimeException("FAIL: property listener notified even after " +
|
||||
"removing the listener from SystemTray. TrayIcon removed.");
|
||||
|
||||
tray.addPropertyChangeListener("someName", this);
|
||||
|
||||
propertyChanged = false;
|
||||
tray.add(icon);
|
||||
|
||||
Thread.sleep(3000);
|
||||
if (propertyChanged)
|
||||
throw new RuntimeException("FAIL: property listener notified when " +
|
||||
"listener added for a different property. TrayIcon added.");
|
||||
|
||||
propertyChanged = false;
|
||||
tray.remove(icon);
|
||||
|
||||
Thread.sleep(3000);
|
||||
if (propertyChanged)
|
||||
throw new RuntimeException("FAIL: property listener notified when " +
|
||||
"listener added for a different property. TrayIcon removed.");
|
||||
|
||||
tray.addPropertyChangeListener("trayIcons", this);
|
||||
tray.addPropertyChangeListener("trayIcons", this);
|
||||
PropertyChangeListener listener = event -> { };
|
||||
tray.addPropertyChangeListener("trayIcons", listener);
|
||||
tray.addPropertyChangeListener("sampleProp", event -> {});
|
||||
|
||||
if (tray.getPropertyChangeListeners("trayIcons").length != 3) {
|
||||
throw new RuntimeException("FAIL: getPropertyChangeListeners did not return the " +
|
||||
"correct value for trayIcons property. Expected: 3, " +
|
||||
"Actual: " + tray.getPropertyChangeListeners("trayIcons").length);
|
||||
} else if (! this.equals(tray.getPropertyChangeListeners("trayIcons")[0]) ||
|
||||
! this.equals(tray.getPropertyChangeListeners("trayIcons")[1]) ||
|
||||
! listener.equals(tray.getPropertyChangeListeners("trayIcons")[2])) {
|
||||
throw new RuntimeException("FAIL: getPropertyChangeListeners did not return the " +
|
||||
"expected listeners\n" +
|
||||
"tray.getPropertyChangeListeners('trayIcons')[0] " + tray.getPropertyChangeListeners("trayIcons")[0]+"\n"+
|
||||
"tray.getPropertyChangeListeners('trayIcons')[1] " + tray.getPropertyChangeListeners("trayIcons")[1]+"\n"+
|
||||
"tray.getPropertyChangeListeners('trayIcons')[2] " + tray.getPropertyChangeListeners("trayIcons")[2]);
|
||||
}
|
||||
|
||||
if (tray.getPropertyChangeListeners("sampleProp").length != 1)
|
||||
throw new RuntimeException("FAIL: getPropertyChangeListeners did not return the " +
|
||||
"expected listener for 'sampleProp'");
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,263 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check for MouseEvents with all mouse buttons
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../../lib/testlibrary ../../
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
* @run main/othervm/policy=tray.policy -Djava.security.manager FunctionalityCheck
|
||||
*/
|
||||
|
||||
public class FunctionalityCheck {
|
||||
|
||||
TrayIcon icon;
|
||||
ExtendedRobot robot;
|
||||
|
||||
boolean actionPerformed = false;
|
||||
Object actionLock = new Object();
|
||||
Object pressLock = new Object();
|
||||
Object releaseLock = new Object();
|
||||
Object clickLock = new Object();
|
||||
Object moveLock = new Object();
|
||||
|
||||
String caption = "Sample Icon";
|
||||
boolean mousePressed = false;
|
||||
boolean mouseReleased = false;
|
||||
boolean mouseClicked = false;
|
||||
boolean mouseMoved = false;
|
||||
|
||||
static final int[] buttonTypes = {
|
||||
InputEvent.BUTTON1_MASK,
|
||||
InputEvent.BUTTON2_MASK,
|
||||
InputEvent.BUTTON3_MASK
|
||||
};
|
||||
|
||||
static final String[] buttonNames = {
|
||||
"BUTTON1",
|
||||
"BUTTON2",
|
||||
"BUTTON3"
|
||||
};
|
||||
|
||||
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 {
|
||||
new FunctionalityCheck().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
FunctionalityCheck() throws Exception {
|
||||
robot = new ExtendedRobot();
|
||||
EventQueue.invokeAndWait(this::initializeGUI);
|
||||
}
|
||||
|
||||
void initializeGUI() {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
|
||||
icon.addActionListener(event -> {
|
||||
actionPerformed = true;
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
icon.addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent event) {
|
||||
mousePressed = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mousePressed");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (pressLock) {
|
||||
try {
|
||||
pressLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseReleased(MouseEvent event) {
|
||||
mouseReleased = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mouseReleased");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (releaseLock) {
|
||||
try {
|
||||
releaseLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseClicked(MouseEvent event) {
|
||||
mouseClicked = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mouseClicked");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (clickLock) {
|
||||
try {
|
||||
clickLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
icon.addMouseMotionListener(new MouseMotionAdapter() {
|
||||
public void mouseMoved(MouseEvent event) {
|
||||
mouseMoved = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mouseMoved");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseMoved: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (moveLock) {
|
||||
try {
|
||||
moveLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private 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(2000);
|
||||
|
||||
SystemTrayIconHelper.doubleClick(robot);
|
||||
|
||||
if (! actionPerformed) {
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! actionPerformed)
|
||||
throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");
|
||||
|
||||
for (int i = 0; i < buttonTypes.length; i++) {
|
||||
mousePressed = false;
|
||||
robot.mousePress(buttonTypes[i]);
|
||||
|
||||
if (! mousePressed) {
|
||||
synchronized (pressLock) {
|
||||
try {
|
||||
pressLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mousePressed)
|
||||
if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
|
||||
throw new RuntimeException("FAIL: mousePressed not triggered when " +
|
||||
buttonNames[i] + " pressed");
|
||||
|
||||
mouseReleased = false;
|
||||
mouseClicked = false;
|
||||
robot.mouseRelease(buttonTypes[i]);
|
||||
|
||||
if (! mouseReleased) {
|
||||
synchronized (releaseLock) {
|
||||
try {
|
||||
releaseLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mouseReleased)
|
||||
if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
|
||||
throw new RuntimeException("FAIL: mouseReleased not triggered when " +
|
||||
buttonNames[i] + " released");
|
||||
|
||||
if (! mouseClicked) {
|
||||
synchronized (clickLock) {
|
||||
try {
|
||||
clickLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mouseClicked)
|
||||
throw new RuntimeException("FAIL: mouseClicked not triggered when " +
|
||||
buttonNames[i] + " pressed & released");
|
||||
}
|
||||
|
||||
mouseMoved = false;
|
||||
robot.mouseMove(iconPosition.x + 100, iconPosition.y);
|
||||
robot.glide(iconPosition.x, iconPosition.y);
|
||||
|
||||
if (! mouseMoved)
|
||||
if (! SystemTrayIconHelper.skip(0) )
|
||||
throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
grant {
|
||||
permission java.io.FilePermission "<<ALL FILES>>", "read, write, delete, execute";
|
||||
permission java.awt.AWTPermission "accessSystemTray";
|
||||
permission java.awt.AWTPermission "createRobot";
|
||||
permission java.util.PropertyPermission "resultsDir", "read";
|
||||
permission java.util.PropertyPermission "user.home", "read";
|
||||
permission java.util.PropertyPermission "os.name", "read";
|
||||
permission java.awt.AWTPermission "accessEventQueue";
|
||||
permission java.lang.RuntimePermission "setIO";
|
||||
permission java.lang.RuntimePermission "accessDeclaredMembers";
|
||||
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
|
||||
permission java.lang.RuntimePermission "accessClassInPackage.sun.lwawt.macosx";
|
||||
permission java.lang.RuntimePermission "accessClassInPackage.sun.awt.X11";
|
||||
permission java.lang.RuntimePermission "accessClassInPackage.sun.awt";
|
||||
permission java.lang.RuntimePermission "loadLibrary.stl";
|
||||
permission java.util.PropertyPermission "java.home", "read";
|
||||
permission java.util.PropertyPermission "java.class.path", "read";
|
||||
permission java.awt.AWTPermission "readDisplayPixels";
|
||||
permission java.awt.AWTPermission "watchMousePointer";
|
||||
};
|
||||
|
||||
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check for SecurityException occurrence if no permissions for system tray granted
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @run main/othervm/policy=tray.policy -Djava.security.manager NoPermissionTest
|
||||
*/
|
||||
|
||||
public class NoPermissionTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (! SystemTray.isSupported()) {
|
||||
System.out.println("SystemTray is not supported on this platform. Marking the test passed");
|
||||
} else {
|
||||
|
||||
BufferedImage im = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics gr = im.createGraphics();
|
||||
gr.setColor(Color.white);
|
||||
gr.fillRect(0, 0, 16, 16);
|
||||
|
||||
try {
|
||||
SystemTray.getSystemTray();
|
||||
throw new RuntimeException("FAIL: SecurityException not thrown by getSystemTray method");
|
||||
} catch (SecurityException ex) {
|
||||
if (!ex.getMessage().matches(".+java.awt.AWTPermission.+accessSystemTray.*"))
|
||||
throw new RuntimeException("FAIL: Security exception thrown due to unexpected reason");
|
||||
}
|
||||
|
||||
try {
|
||||
TrayIcon icon = new TrayIcon(im, "Caption");
|
||||
throw new RuntimeException("FAIL: SecurityException not thrown by TrayIcon constructor");
|
||||
} catch (SecurityException ex) {
|
||||
if (!ex.getMessage().matches(".+java.awt.AWTPermission.+accessSystemTray.*"))
|
||||
throw new RuntimeException("FAIL: Security exception thrown due to unexpected reason");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check for no Exception occurrence if permissions for system tray granted
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @run main/othervm/policy=tray.policy -Djava.security.manager PermissionTest
|
||||
*/
|
||||
|
||||
public class PermissionTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
if (!SystemTray.isSupported()) {
|
||||
System.out.println("SystemTray is not supported on this platform. Marking the test passed");
|
||||
} else {
|
||||
BufferedImage im = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics gr = im.createGraphics();
|
||||
gr.setColor(Color.white);
|
||||
gr.fillRect(0, 0, 16, 16);
|
||||
|
||||
SystemTray.getSystemTray();
|
||||
TrayIcon icon = new TrayIcon(im, "Caption");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
grant {
|
||||
permission java.awt.AWTPermission "accessSystemTray";
|
||||
};
|
||||
172
jdk/test/java/awt/TrayIcon/SystemTrayIconHelper.java
Normal file
172
jdk/test/java/awt/TrayIcon/SystemTrayIconHelper.java
Normal file
@ -0,0 +1,172 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.geom.Point2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedActionException;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
|
||||
/*
|
||||
* @summary This is a helper class to find the location of a system tray icon,
|
||||
* and skip some OS specific cases in tests.
|
||||
* @library ../../../../../lib/testlibrary
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
*/
|
||||
public class SystemTrayIconHelper {
|
||||
|
||||
static Frame frame;
|
||||
|
||||
/**
|
||||
* Call this method if the tray icon need to be followed in an automated manner
|
||||
* This method will be called by automated testcases
|
||||
*/
|
||||
static Point getTrayIconLocation(TrayIcon icon) throws Exception {
|
||||
if (icon == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//This is added just in case the tray's native menu is visible.
|
||||
//It has to be hidden if visible. For that, we are showing a Frame
|
||||
//and clicking on it - the assumption is, the menu will
|
||||
//be closed if another window is clicked
|
||||
ExtendedRobot robot = new ExtendedRobot();
|
||||
try {
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
frame = new Frame();
|
||||
frame.setSize(100, 100);
|
||||
frame.setVisible(true);
|
||||
});
|
||||
robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth() / 2,
|
||||
frame.getLocationOnScreen().y + frame.getHeight() / 2);
|
||||
robot.waitForIdle();
|
||||
robot.click();
|
||||
EventQueue.invokeAndWait(frame::dispose);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (System.getProperty("os.name").startsWith("Win")) {
|
||||
try {
|
||||
// sun.awt.windows.WTrayIconPeer
|
||||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
Dimension iconSize = icon.getSize();
|
||||
|
||||
int width = (int) iconSize.getWidth();
|
||||
int height = (int) iconSize.getHeight();
|
||||
|
||||
// Some previously created icons may not be removed
|
||||
// from tray until mouse move on it. So we glide
|
||||
// through the whole tray bar.
|
||||
robot.glide((int) screenSize.getWidth(), (int) (screenSize.getHeight()-15), 0, (int) (screenSize.getHeight() - 15), 1, 2);
|
||||
|
||||
BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));
|
||||
|
||||
for (int x = (int) (screenSize.getWidth()-width); x > 0; x--) {
|
||||
for (int y = (int) (screenSize.getHeight()-height); y > (screenSize.getHeight()-50); y--) {
|
||||
if (imagesEquals(((BufferedImage)icon.getImage()).getSubimage(0, 0, width, height), screen.getSubimage(x, y, width, height))) {
|
||||
return new Point(x+5, y+5);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
} else if (System.getProperty("os.name").startsWith("Mac")) {
|
||||
Point2D point2d;
|
||||
try {
|
||||
// sun.lwawt.macosx.CTrayIcon
|
||||
Field f_peer = getField( java.awt.TrayIcon.class, "peer");
|
||||
|
||||
Object peer = f_peer.get(icon);
|
||||
Method m_getModel = peer.getClass().getDeclaredMethod(
|
||||
"getModel");
|
||||
m_getModel.setAccessible(true);
|
||||
long model = (Long) (m_getModel.invoke(peer, new Object[]{}));
|
||||
Method m_getLocation = peer.getClass().getDeclaredMethod(
|
||||
"nativeGetIconLocation", new Class[]{Long.TYPE});
|
||||
m_getLocation.setAccessible(true);
|
||||
point2d = (Point2D)m_getLocation.invoke(peer, new Object[]{model});
|
||||
Point po = new Point((int)(point2d.getX()), (int)(point2d.getY()));
|
||||
po.translate(10, -5);
|
||||
return po;
|
||||
}catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
// sun.awt.X11.XTrayIconPeer
|
||||
Field f_peer = getField(java.awt.TrayIcon.class, "peer");
|
||||
|
||||
Object peer = f_peer.get(icon);
|
||||
Method m_getLOS = peer.getClass().getDeclaredMethod(
|
||||
"getLocationOnScreen", new Class[]{});
|
||||
m_getLOS.setAccessible(true);
|
||||
Point point = (Point)m_getLOS.invoke(peer, new Object[]{});
|
||||
point.translate(5, 5);
|
||||
return point;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static Field getField(final Class clz, final String fieldName) {
|
||||
Field res = null;
|
||||
try {
|
||||
res = (Field)AccessController.doPrivileged((PrivilegedExceptionAction) () -> {
|
||||
Field f = clz.getDeclaredField(fieldName);
|
||||
f.setAccessible(true);
|
||||
return f;
|
||||
});
|
||||
} catch (PrivilegedActionException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static boolean imagesEquals(BufferedImage img1, BufferedImage img2) {
|
||||
for (int x = 0; x < img1.getWidth(); x++) {
|
||||
for (int y = 0; y < img1.getHeight(); y++) {
|
||||
if (img1.getRGB(x, y) != img2.getRGB(x, y))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void doubleClick(Robot robot) {
|
||||
if (System.getProperty("os.name").startsWith("Mac")) {
|
||||
robot.mousePress(InputEvent.BUTTON3_MASK);
|
||||
robot.delay(50);
|
||||
robot.mouseRelease(InputEvent.BUTTON3_MASK);
|
||||
} else {
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(50);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(50);
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
robot.delay(50);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||
}
|
||||
}
|
||||
|
||||
// Method for skipping some OS specific cases
|
||||
static boolean skip(int button) {
|
||||
if (System.getProperty("os.name").toLowerCase().startsWith("win")){
|
||||
if (button == InputEvent.BUTTON1_MASK){
|
||||
// See JDK-6827035
|
||||
return true;
|
||||
}
|
||||
} else if (System.getProperty("os.name").toLowerCase().contains("os x")){
|
||||
// See JDK-7153700
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check the getSystemTray method of the SystemTray. Checks if
|
||||
* a proper instance is returned in supported platforms and a proper
|
||||
* exception is thrown in unsupported platforms
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @run main/othervm -DSystemTraySupport=TRUE SystemTrayInstanceTest
|
||||
*/
|
||||
|
||||
public class SystemTrayInstanceTest {
|
||||
|
||||
private static boolean supported = false;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String sysTraySupport = System.getProperty("SystemTraySupport");
|
||||
if (sysTraySupport == null)
|
||||
throw new RuntimeException("SystemTray support status unknown!");
|
||||
|
||||
if ("TRUE".equals(sysTraySupport)) {
|
||||
System.out.println("System tray is supported on the platform under test");
|
||||
supported = true;
|
||||
}
|
||||
|
||||
new SystemTrayInstanceTest().doTest();
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
boolean flag = SystemTray.isSupported();
|
||||
if (supported != flag)
|
||||
throw new RuntimeException("FAIL: isSupported did not return the correct value"+
|
||||
(supported ?
|
||||
"SystemTray is supported on the platform under test" :
|
||||
"SystemTray is not supported on the platform under test") +
|
||||
"SystemTray.isSupported() method returned " + flag);
|
||||
|
||||
if (supported) {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
} else {
|
||||
try {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
} catch (UnsupportedOperationException uoe) {
|
||||
System.out.println("UnsupportedOperationException thrown correctly");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Tests the add method of the SystemTray. Checks if it
|
||||
* throws proper exceptions in case of invalid arguments and adds the
|
||||
* TrayIcon correctly in case of a proper argument
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @run main TrayIconAddTest
|
||||
*/
|
||||
|
||||
public class TrayIconAddTest {
|
||||
|
||||
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 {
|
||||
new TrayIconAddTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
void doTest() throws Exception {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
try {
|
||||
tray.add(null);
|
||||
} catch (NullPointerException npe) {
|
||||
System.out.println("NullPointerException thrown correctly when add(null) called");
|
||||
}
|
||||
|
||||
TrayIcon icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB));
|
||||
|
||||
tray.add(icon);
|
||||
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.out.println("IllegalArgumentException rightly thrown when tray icon is added twice");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,275 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check if MouseEvent has the proper modifiers when
|
||||
* TrayIcon is clicked pressing the modifier keys
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../lib/testlibrary ../
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
* @run main TrayIconEventModifiersTest
|
||||
*/
|
||||
|
||||
public class TrayIconEventModifiersTest {
|
||||
|
||||
Image image;
|
||||
|
||||
TrayIcon icon;
|
||||
ExtendedRobot robot;
|
||||
|
||||
Object mouseLock = new Object();
|
||||
|
||||
String caption = "Sample Icon";
|
||||
boolean mousePressed = false;
|
||||
boolean mouseReleased = false;
|
||||
boolean mouseClicked = false;
|
||||
int modifiers, releaseModifiers, clickModifiers;
|
||||
|
||||
int[] buttonTypes = {
|
||||
InputEvent.BUTTON1_MASK,
|
||||
InputEvent.BUTTON2_MASK,
|
||||
InputEvent.BUTTON3_MASK
|
||||
};
|
||||
|
||||
String[] buttonNames = {
|
||||
"BUTTON1",
|
||||
"BUTTON2",
|
||||
"BUTTON3"
|
||||
};
|
||||
|
||||
int[] buttonMasks = {
|
||||
InputEvent.BUTTON1_DOWN_MASK,
|
||||
InputEvent.BUTTON2_DOWN_MASK,
|
||||
InputEvent.BUTTON3_DOWN_MASK
|
||||
};
|
||||
|
||||
static int[] keyTypes = {
|
||||
KeyEvent.VK_SHIFT,
|
||||
KeyEvent.VK_CONTROL
|
||||
};
|
||||
|
||||
static String[] keyNames = {
|
||||
"SHIFT",
|
||||
"CONTROL"
|
||||
};
|
||||
|
||||
static int[] keyMasks = {
|
||||
KeyEvent.SHIFT_DOWN_MASK,
|
||||
KeyEvent.CTRL_DOWN_MASK
|
||||
};
|
||||
|
||||
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.");
|
||||
|
||||
System.out.println(System.getProperty("os.arch"));
|
||||
if (System.getProperty("os.name").indexOf("Sun") != -1 &&
|
||||
System.getProperty("os.arch").indexOf("sparc") != -1) {
|
||||
keyTypes = new int[]{
|
||||
KeyEvent.VK_SHIFT,
|
||||
KeyEvent.VK_CONTROL,
|
||||
KeyEvent.VK_META
|
||||
};
|
||||
|
||||
keyNames = new String[]{
|
||||
"SHIFT",
|
||||
"CONTROL",
|
||||
"META"
|
||||
};
|
||||
keyMasks = new int[]{
|
||||
KeyEvent.SHIFT_DOWN_MASK,
|
||||
KeyEvent.CTRL_DOWN_MASK,
|
||||
KeyEvent.META_DOWN_MASK
|
||||
};
|
||||
}
|
||||
|
||||
new TrayIconEventModifiersTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
public TrayIconEventModifiersTest() throws Exception {
|
||||
robot = new ExtendedRobot();
|
||||
EventQueue.invokeAndWait(this::initializeGUI);
|
||||
}
|
||||
|
||||
private void initializeGUI() {
|
||||
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
|
||||
icon.addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent event) {
|
||||
if (!icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
mousePressed = true;
|
||||
modifiers = event.getModifiersEx();
|
||||
System.out.println("Icon mousePressed " + modifiers);
|
||||
synchronized (mouseLock) {
|
||||
try {
|
||||
mouseLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseReleased(MouseEvent event) {
|
||||
if (!icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
mouseReleased = true;
|
||||
releaseModifiers = event.getModifiersEx();
|
||||
System.out.println("Icon mouseReleased " + releaseModifiers);
|
||||
synchronized (mouseLock) {
|
||||
try {
|
||||
mouseLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseClicked(MouseEvent event) {
|
||||
if (!icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
mouseClicked = true;
|
||||
clickModifiers = event.getModifiersEx();
|
||||
System.out.println("Icon mouseClickedd " + clickModifiers);
|
||||
synchronized (mouseLock) {
|
||||
try {
|
||||
mouseLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
for (int i = 0; i < buttonTypes.length; i++) {
|
||||
for (int j = 0; j < keyTypes.length; j++) {
|
||||
mousePressed = false;
|
||||
|
||||
robot.keyPress(keyTypes[j]);
|
||||
robot.mousePress(buttonTypes[i]);
|
||||
|
||||
if (! mousePressed) {
|
||||
synchronized (mouseLock) {
|
||||
try {
|
||||
mouseLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mousePressed) {
|
||||
if (! SystemTrayIconHelper.skip(buttonTypes[i]))
|
||||
throw new RuntimeException("FAIL: mousePressed not triggered when " +
|
||||
keyNames[j] + " + " + buttonNames[i] + " pressed");
|
||||
} else {
|
||||
int onMask = buttonMasks[i] | keyMasks[j];
|
||||
if ((modifiers & onMask) != onMask) {
|
||||
throw new RuntimeException("FAIL: getModifiersEx did not return " +
|
||||
"the correct value when " + keyNames[j] + " + " +
|
||||
buttonNames[i] + " pressed");
|
||||
}
|
||||
}
|
||||
|
||||
mouseReleased = false;
|
||||
mouseClicked = false;
|
||||
robot.mouseRelease(buttonTypes[i]);
|
||||
|
||||
if (! mouseReleased) {
|
||||
synchronized (mouseLock) {
|
||||
try {
|
||||
mouseLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
robot.waitForIdle(1000);
|
||||
robot.keyRelease(keyTypes[j]);
|
||||
robot.waitForIdle(1000);
|
||||
|
||||
if (! mouseReleased) {
|
||||
if (! SystemTrayIconHelper.skip(buttonTypes[i]))
|
||||
throw new RuntimeException("FAIL: mouseReleased not triggered when " +
|
||||
keyNames[j] + " + " + buttonNames[i] + " released");
|
||||
} else {
|
||||
int onMask = keyMasks[j];
|
||||
if ((releaseModifiers & onMask) != onMask)
|
||||
throw new RuntimeException("FAIL: getModifiersEx did not return " +
|
||||
"the correct value when " + keyNames[j] + " + " +
|
||||
buttonNames[i] + " released");
|
||||
}
|
||||
if (! mouseClicked) {
|
||||
throw new RuntimeException("FAIL: mouseClicked not triggered when " +
|
||||
keyNames[j] + " + " + buttonNames[i] +
|
||||
" pressed & released");
|
||||
} else {
|
||||
int onMask = keyMasks[j];
|
||||
if ((clickModifiers & onMask) != onMask)
|
||||
throw new RuntimeException("FAIL: getModifiersEx did not return " +
|
||||
"the correct value when " + keyNames[j] + " + " +
|
||||
buttonNames[i] + " pressed & released");
|
||||
}
|
||||
robot.type(KeyEvent.VK_ESCAPE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,269 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check for MouseEvents with all mouse buttons
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../lib/testlibrary ../
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
* @run main TrayIconEventsTest
|
||||
*/
|
||||
|
||||
public class TrayIconEventsTest {
|
||||
|
||||
TrayIcon icon;
|
||||
ExtendedRobot robot;
|
||||
|
||||
boolean actionPerformed = false;
|
||||
Object actionLock = new Object();
|
||||
Object pressLock = new Object();
|
||||
Object releaseLock = new Object();
|
||||
Object clickLock = new Object();
|
||||
Object moveLock = new Object();
|
||||
|
||||
String caption = "Sample Icon";
|
||||
boolean mousePressed = false;
|
||||
boolean mouseReleased = false;
|
||||
boolean mouseClicked = false;
|
||||
boolean mouseMoved = false;
|
||||
|
||||
int[] buttonTypes = {
|
||||
InputEvent.BUTTON1_MASK,
|
||||
InputEvent.BUTTON2_MASK,
|
||||
InputEvent.BUTTON3_MASK
|
||||
};
|
||||
|
||||
String[] buttonNames = {
|
||||
"BUTTON1",
|
||||
"BUTTON2",
|
||||
"BUTTON3"
|
||||
};
|
||||
|
||||
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.");
|
||||
new TrayIconEventsTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
public TrayIconEventsTest() throws Exception {
|
||||
robot = new ExtendedRobot();
|
||||
EventQueue.invokeAndWait(this::initializeGUI);
|
||||
}
|
||||
|
||||
private void initializeGUI(){
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
|
||||
icon.addActionListener(event -> {
|
||||
actionPerformed = true;
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
|
||||
actionLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
icon.addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent event) {
|
||||
mousePressed = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mousePressed");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (pressLock) {
|
||||
try {
|
||||
pressLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseReleased(MouseEvent event) {
|
||||
mouseReleased = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mouseReleased");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (releaseLock) {
|
||||
try {
|
||||
releaseLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseClicked(MouseEvent event) {
|
||||
mouseClicked = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mouseClicked");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (clickLock) {
|
||||
try {
|
||||
clickLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
icon.addMouseMotionListener(new MouseMotionAdapter() {
|
||||
public void mouseMoved(MouseEvent event) {
|
||||
mouseMoved = true;
|
||||
Point p = event.getPoint();
|
||||
if (p.x != event.getX() || p.y != event.getY())
|
||||
throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
|
||||
"not return the same value as getX/getY " +
|
||||
"for mouseMoved");
|
||||
|
||||
if (! icon.equals(event.getSource()))
|
||||
throw new RuntimeException("FAIL: mouseMoved: MouseEvent.getSource " +
|
||||
"did not return TrayIcon object");
|
||||
|
||||
synchronized (moveLock) {
|
||||
try {
|
||||
moveLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (AWTException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
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(2000);
|
||||
|
||||
SystemTrayIconHelper.doubleClick(robot);
|
||||
|
||||
if (! actionPerformed) {
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.wait(10000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! actionPerformed)
|
||||
throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");
|
||||
|
||||
for (int i = 0; i < buttonTypes.length; i++) {
|
||||
mousePressed = false;
|
||||
robot.mousePress(buttonTypes[i]);
|
||||
|
||||
if (! mousePressed) {
|
||||
synchronized (pressLock) {
|
||||
try {
|
||||
pressLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mousePressed)
|
||||
if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
|
||||
throw new RuntimeException("FAIL: mousePressed not triggered when " +
|
||||
buttonNames[i] + " pressed");
|
||||
|
||||
mouseReleased = false;
|
||||
mouseClicked = false;
|
||||
robot.mouseRelease(buttonTypes[i]);
|
||||
|
||||
if (! mouseReleased) {
|
||||
synchronized (releaseLock) {
|
||||
try {
|
||||
releaseLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mouseReleased)
|
||||
throw new RuntimeException("FAIL: mouseReleased not triggered when " +
|
||||
buttonNames[i] + " released");
|
||||
|
||||
if (! mouseClicked) {
|
||||
synchronized (clickLock) {
|
||||
try {
|
||||
clickLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! mouseClicked)
|
||||
throw new RuntimeException("FAIL: mouseClicked not triggered when " +
|
||||
buttonNames[i] + " pressed & released");
|
||||
}
|
||||
|
||||
mouseMoved = false;
|
||||
robot.mouseMove(iconPosition.x + 100, iconPosition.y);
|
||||
robot.glide(iconPosition.x, iconPosition.y);
|
||||
|
||||
if (! mouseMoved)
|
||||
if (! SystemTrayIconHelper.skip(0) )
|
||||
throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check various methods of the TrayIcon - whether the methods
|
||||
* return the proper values, throws the proper exceptions etc
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @run main TrayIconMethodsTest
|
||||
*/
|
||||
|
||||
public class TrayIconMethodsTest {
|
||||
|
||||
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 {
|
||||
new TrayIconMethodsTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
void doTest() throws Exception {
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
|
||||
String toolTip = "Sample Icon";
|
||||
PopupMenu pm = new PopupMenu();
|
||||
pm.add(new MenuItem("Sample"));
|
||||
|
||||
Image image = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
|
||||
TrayIcon icon = new TrayIcon(image, toolTip, pm);
|
||||
|
||||
ActionListener al1 = event -> {};
|
||||
ActionListener al2 = event -> {};
|
||||
MouseMotionListener mml1 = new MouseMotionAdapter() {};
|
||||
MouseMotionListener mml2 = new MouseMotionAdapter() {};
|
||||
MouseListener ml1 = new MouseAdapter() {};
|
||||
MouseListener ml2 = new MouseAdapter() {};
|
||||
|
||||
icon.addActionListener(al1);
|
||||
icon.addActionListener(al2);
|
||||
icon.addMouseMotionListener(mml1);
|
||||
icon.addMouseMotionListener(mml2);
|
||||
icon.addMouseListener(ml1);
|
||||
icon.addMouseListener(ml2);
|
||||
tray.add(icon);
|
||||
|
||||
ActionListener[] actionListeners = icon.getActionListeners();
|
||||
if (actionListeners == null || actionListeners.length != 2)
|
||||
throw new RuntimeException("FAIL: getActionListeners did not return the correct value " +
|
||||
"when there were two listeners present " + actionListeners);
|
||||
|
||||
if (! isPresent(actionListeners, al1) || ! isPresent(actionListeners, al2))
|
||||
throw new RuntimeException("FAIL: All the action listeners added are not returned " +
|
||||
"by the method");
|
||||
|
||||
MouseListener[] mouseListeners = icon.getMouseListeners();
|
||||
if (mouseListeners == null || mouseListeners.length != 2)
|
||||
throw new RuntimeException("FAIL: getMouseListeners did not return the correct value " +
|
||||
"when there were two listeners present " + mouseListeners);
|
||||
|
||||
if (! isPresent(mouseListeners, ml1) || ! isPresent(mouseListeners, ml2))
|
||||
throw new RuntimeException("FAIL: All the mouse listeners added are not returned " +
|
||||
"by the method");
|
||||
|
||||
MouseMotionListener[] mouseMotionListeners = icon.getMouseMotionListeners();
|
||||
if (mouseMotionListeners == null || mouseMotionListeners.length != 2)
|
||||
throw new RuntimeException("FAIL: getMouseMotionListeners did not return the correct value " +
|
||||
"when there were two listeners present " + mouseMotionListeners);
|
||||
|
||||
if (! isPresent(mouseMotionListeners, mml1) || ! isPresent(mouseMotionListeners, mml2))
|
||||
throw new RuntimeException("FAIL: All the mouse motion listeners added are not returned " +
|
||||
"by the method");
|
||||
|
||||
Image im = icon.getImage();
|
||||
if (! image.equals(im))
|
||||
throw new RuntimeException("FAIL: Images are not the same getImage()=" + im +
|
||||
" Image=" + image);
|
||||
|
||||
if (! pm.equals(icon.getPopupMenu()))
|
||||
throw new RuntimeException("FAIL: PopupMenus are not the same getPopupMenu()=" +
|
||||
icon.getPopupMenu() + " PopupMenu=" + pm);
|
||||
|
||||
if (! toolTip.equals(icon.getToolTip()))
|
||||
throw new RuntimeException("FAIL: ToolTips are not the same getToolTip()=" +
|
||||
icon.getToolTip() + " ToolTip=" + toolTip);
|
||||
|
||||
if (icon.isImageAutoSize())
|
||||
throw new RuntimeException("FAIL: Auto size property is true by default");
|
||||
|
||||
icon.setImageAutoSize(true);
|
||||
if (! icon.isImageAutoSize())
|
||||
throw new RuntimeException("FAIL: Auto size property is not set to " +
|
||||
"true by call to setImageAutoSize(true)");
|
||||
|
||||
icon.removeActionListener(al1);
|
||||
icon.removeActionListener(al2);
|
||||
actionListeners = icon.getActionListeners();
|
||||
if (actionListeners == null || actionListeners.length != 0)
|
||||
throw new RuntimeException("FAIL: removeActionListener did not " +
|
||||
"remove the ActionListeners added " + actionListeners);
|
||||
|
||||
icon.removeMouseListener(ml1);
|
||||
icon.removeMouseListener(ml2);
|
||||
mouseListeners = icon.getMouseListeners();
|
||||
if (mouseListeners == null || mouseListeners.length != 0)
|
||||
throw new RuntimeException("FAIL: removeMouseListener did not " +
|
||||
"remove the MouseListeners added " + mouseListeners);
|
||||
|
||||
icon.removeMouseMotionListener(mml1);
|
||||
icon.removeMouseMotionListener(mml2);
|
||||
mouseMotionListeners = icon.getMouseMotionListeners();
|
||||
if (mouseMotionListeners == null || mouseMotionListeners.length != 0)
|
||||
throw new RuntimeException("FAIL: removeMouseMotionListener did not " +
|
||||
"remove the MouseMotionListeners added " + mouseMotionListeners);
|
||||
|
||||
try {
|
||||
icon.setImage(null);
|
||||
throw new RuntimeException("FAIL: setImage(null) did not throw NullPointerException");
|
||||
} catch (NullPointerException npe) {
|
||||
}
|
||||
}
|
||||
|
||||
boolean isPresent(Object[] array, Object obj) {
|
||||
if (array == null || array.length == 0 || obj == null) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (obj.equals(array[i])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,176 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check if ActionEvent is triggered by a TrayIcon only when
|
||||
* it is double clicked using mouse button 1 (or single clicked
|
||||
* with button 3 (on Mac OS X))
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../lib/testlibrary ../
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
* @run main TrayIconMouseTest
|
||||
*/
|
||||
|
||||
public class TrayIconMouseTest {
|
||||
|
||||
TrayIcon icon;
|
||||
ExtendedRobot robot;
|
||||
|
||||
boolean actionPerformed = false;
|
||||
Object actionLock = new Object();
|
||||
static boolean isMacOS = false;
|
||||
|
||||
String caption = "Sample Icon";
|
||||
|
||||
int[] buttonTypes = {
|
||||
InputEvent.BUTTON1_MASK,
|
||||
InputEvent.BUTTON2_MASK,
|
||||
InputEvent.BUTTON3_MASK
|
||||
};
|
||||
|
||||
String[] buttonNames = {
|
||||
"BUTTON1",
|
||||
"BUTTON2",
|
||||
"BUTTON3"
|
||||
};
|
||||
|
||||
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("mac")) {
|
||||
isMacOS = true;
|
||||
}
|
||||
new TrayIconMouseTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
TrayIconMouseTest() throws Exception{
|
||||
robot = new ExtendedRobot();
|
||||
EventQueue.invokeAndWait(this::initializeGUI);
|
||||
}
|
||||
|
||||
void initializeGUI() {
|
||||
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
|
||||
icon.addActionListener(event -> {
|
||||
actionPerformed = true;
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private 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();
|
||||
|
||||
for (int i = 0; i < buttonTypes.length; i++) {
|
||||
actionPerformed = false;
|
||||
robot.click(buttonTypes[i]);
|
||||
robot.waitForIdle(2000);
|
||||
|
||||
if (isMacOS && actionPerformed && i == 2) {
|
||||
|
||||
}else if (isMacOS && i == 2) {
|
||||
throw new RuntimeException("FAIL: ActionEvent NOT triggered when " +
|
||||
buttonNames[i] + " is single clicked on Mac OS");
|
||||
}else if (actionPerformed) {
|
||||
throw new RuntimeException("FAIL: ActionEvent triggered when " +
|
||||
buttonNames[i] + " is single clicked");
|
||||
}
|
||||
}
|
||||
|
||||
if(!isMacOS) {
|
||||
for (int i = 0; i < buttonTypes.length; i++) {
|
||||
for (int j = 0; j < buttonTypes.length; j++) {
|
||||
if (j != i) {
|
||||
actionPerformed = false;
|
||||
robot.mousePress(buttonTypes[i]);
|
||||
robot.mousePress(buttonTypes[j]);
|
||||
robot.mouseRelease(buttonTypes[j]);
|
||||
robot.mouseRelease(buttonTypes[i]);
|
||||
|
||||
robot.waitForIdle();
|
||||
|
||||
if (actionPerformed)
|
||||
throw new RuntimeException("FAIL: ActionEvent triggered when " +
|
||||
buttonNames[i] + " and " + buttonNames[j] +
|
||||
" is clicked and released");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < buttonTypes.length; i++) {
|
||||
actionPerformed = false;
|
||||
robot.mousePress(buttonTypes[i]);
|
||||
robot.delay(50);
|
||||
robot.mouseRelease(buttonTypes[i]);
|
||||
robot.delay(50);
|
||||
robot.mousePress(buttonTypes[i]);
|
||||
robot.delay(50);
|
||||
robot.mouseRelease(buttonTypes[i]);
|
||||
|
||||
if (i == 0) {
|
||||
if (! actionPerformed) {
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! actionPerformed)
|
||||
throw new RuntimeException("FAIL: ActionEvent not triggered when " +
|
||||
buttonNames[i] + " is double clicked");
|
||||
} else {
|
||||
robot.waitForIdle();
|
||||
|
||||
if (actionPerformed)
|
||||
throw new RuntimeException("FAIL: ActionEvent triggered when " +
|
||||
buttonNames[i] + " is double clicked");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
151
jdk/test/java/awt/TrayIcon/TrayIconPopup/TrayIconPopupTest.java
Normal file
151
jdk/test/java/awt/TrayIcon/TrayIconPopup/TrayIconPopupTest.java
Normal file
@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Check if a JPopupMenu can be displayed when TrayIcon is
|
||||
* right clicked. It uses a JWindow as the parent of the JPopupMenu
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @library ../../../../lib/testlibrary ../
|
||||
* @build ExtendedRobot SystemTrayIconHelper
|
||||
* @run main TrayIconPopupTest
|
||||
*/
|
||||
|
||||
public class TrayIconPopupTest {
|
||||
|
||||
TrayIcon icon;
|
||||
ExtendedRobot robot;
|
||||
|
||||
boolean actionPerformed = false;
|
||||
Object actionLock = new Object();
|
||||
static final int ATTEMPTS = 50;
|
||||
|
||||
PopupMenu popup;
|
||||
Dialog window;
|
||||
|
||||
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.");
|
||||
new TrayIconPopupTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
TrayIconPopupTest() throws Exception {
|
||||
robot = new ExtendedRobot();
|
||||
EventQueue.invokeAndWait(this::initializeGUI);
|
||||
robot.waitForIdle(1000);
|
||||
EventQueue.invokeAndWait( () -> window.setLocation(100, 100));
|
||||
robot.waitForIdle(1000);
|
||||
}
|
||||
|
||||
private void initializeGUI() {
|
||||
window = new Dialog((Frame) null);
|
||||
window.setSize(5, 5);
|
||||
window.setVisible(true);
|
||||
|
||||
popup = new PopupMenu("");
|
||||
|
||||
MenuItem item = new MenuItem("Sample");
|
||||
item.addActionListener(event -> {
|
||||
actionPerformed = true;
|
||||
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.notifyAll();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
popup.add(item);
|
||||
popup.add(new MenuItem("Item2"));
|
||||
popup.add(new MenuItem("Item3"));
|
||||
|
||||
window.add(popup);
|
||||
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
|
||||
icon.addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent event) {
|
||||
if (event.isPopupTrigger()) {
|
||||
popup.show(window, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseReleased(MouseEvent event) {
|
||||
if (event.isPopupTrigger()) {
|
||||
popup.show(window, 0, 0);
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
tray.add(icon);
|
||||
} catch (AWTException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
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.BUTTON3_MASK);
|
||||
robot.delay(50);
|
||||
robot.mouseRelease(InputEvent.BUTTON3_MASK);
|
||||
robot.delay(1000);
|
||||
|
||||
robot.mouseMove(window.getLocation().x + 10, window.getLocation().y + 10);
|
||||
robot.mousePress(InputEvent.BUTTON3_MASK);
|
||||
robot.delay(50);
|
||||
robot.mouseRelease(InputEvent.BUTTON3_MASK);
|
||||
|
||||
int attempts = 0;
|
||||
while (!actionPerformed && attempts++ < ATTEMPTS) {
|
||||
synchronized (actionLock) {
|
||||
try {
|
||||
actionLock.wait(3000);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!actionPerformed)
|
||||
throw new RuntimeException("FAIL: ActionEvent not triggered when " +
|
||||
"JPopupMenu shown and menu item selected using keyboard");
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Test the remove method of the TrayIcon
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @run main TrayIconRemoveTest
|
||||
*/
|
||||
|
||||
public class TrayIconRemoveTest {
|
||||
|
||||
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 {
|
||||
new TrayIconRemoveTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
Image image = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
tray.remove(null);
|
||||
|
||||
TrayIcon icon1 = new TrayIcon(image);
|
||||
tray.add(icon1);
|
||||
|
||||
tray.remove(icon1);
|
||||
|
||||
TrayIcon[] icons = tray.getTrayIcons();
|
||||
if (icons.length != 0)
|
||||
throw new RuntimeException("FAIL: There are icons still present even after " +
|
||||
"removing the added icon" + "\n"+
|
||||
"No. of icons present: " + icons.length);
|
||||
|
||||
TrayIcon icon2 = new TrayIcon(image);
|
||||
tray.remove(icon2);
|
||||
|
||||
TrayIcon icon3 = new TrayIcon(image);
|
||||
tray.add(icon3);
|
||||
|
||||
TrayIcon newIcon = new TrayIcon(image);
|
||||
tray.remove(newIcon);
|
||||
|
||||
tray.remove(null);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2014, 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.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Test the methods TrayIcon.getSize and SystemTray.getTrayIconSize.
|
||||
* There is no way to check whether the values returned are correct,
|
||||
* so its checked whether the value is greater than a minimum
|
||||
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
|
||||
* @run main TrayIconSizeTest
|
||||
*/
|
||||
|
||||
public class TrayIconSizeTest {
|
||||
|
||||
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 {
|
||||
new TrayIconSizeTest().doTest();
|
||||
}
|
||||
}
|
||||
|
||||
void doTest() throws Exception {
|
||||
|
||||
SystemTray tray = SystemTray.getSystemTray();
|
||||
Dimension dim = tray.getTrayIconSize();
|
||||
|
||||
if (dim.width <= 5 || dim.height <= 5)
|
||||
throw new RuntimeException("FAIL: value returned by getTrayIconSize is not correct: " + dim);
|
||||
|
||||
TrayIcon icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB));
|
||||
|
||||
if (! icon.getSize().equals(dim))
|
||||
throw new RuntimeException("FAIL: TrayIcon.getSize did not return the same value as " +
|
||||
"getTrayIconSize when TrayIcon not added" + "\n" +
|
||||
"SystemTray.getTrayIconSize(): " + dim + "\n" +
|
||||
"TrayIcon.getSize(): " + icon.getSize());
|
||||
|
||||
tray.add(icon);
|
||||
|
||||
if (icon.getSize().width <= 5 || icon.getSize().height <= 5)
|
||||
throw new RuntimeException("FAIL: value returned by TrayIcon.getSize is not correct: " + icon.getSize());
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user