8238741: java.awt.Robot(GraphicsDevice) constructor does not follow the spec

Reviewed-by: aivanov
This commit is contained in:
Sergey Bylokhov 2020-02-13 13:21:21 -08:00
parent 8d2aa62bd9
commit 784e575134
2 changed files with 91 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, 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
@ -90,9 +90,7 @@ public class Robot {
* @see AWTPermission
*/
public Robot() throws AWTException {
if (GraphicsEnvironment.isHeadless()) {
throw new AWTException("headless environment");
}
checkHeadless();
init(GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice());
}
@ -125,6 +123,7 @@ public class Robot {
* @see AWTPermission
*/
public Robot(GraphicsDevice screen) throws AWTException {
checkHeadless();
checkIsScreenDevice(screen);
init(screen);
}
@ -161,15 +160,24 @@ public class Robot {
}
/* determine if the security policy allows Robot's to be created */
private void checkRobotAllowed() {
private static void checkRobotAllowed() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(AWTPermissions.CREATE_ROBOT_PERMISSION);
}
}
/**
* Check for headless state and throw {@code AWTException} if headless.
*/
private static void checkHeadless() throws AWTException {
if (GraphicsEnvironment.isHeadless()) {
throw new AWTException("headless environment");
}
}
/* check if the given device is a screen device */
private void checkIsScreenDevice(GraphicsDevice device) {
private static void checkIsScreenDevice(GraphicsDevice device) {
if (device == null || device.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) {
throw new IllegalArgumentException("not a valid screen device");
}
@ -300,7 +308,7 @@ public class Robot {
afterEvent();
}
private void checkButtonsArgument(int buttons) {
private static void checkButtonsArgument(int buttons) {
if ( (buttons|LEGAL_BUTTON_MASK) != LEGAL_BUTTON_MASK ) {
throw new IllegalArgumentException("Invalid combination of button flags");
}
@ -359,7 +367,7 @@ public class Robot {
afterEvent();
}
private void checkKeycodeArgument(int keycode) {
private static void checkKeycodeArgument(int keycode) {
// rather than build a big table or switch statement here, we'll
// just check that the key isn't VK_UNDEFINED and assume that the
// peer implementations will throw an exception for other bogus
@ -664,7 +672,7 @@ public class Robot {
}
}
private void checkDelayArgument(int ms) {
private static void checkDelayArgument(int ms) {
if (ms < 0 || ms > MAX_DELAY) {
throw new IllegalArgumentException("Delay must be to 0 to 60,000ms");
}
@ -680,7 +688,7 @@ public class Robot {
((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
}
private void checkNotDispatchThread() {
private static void checkNotDispatchThread() {
if (EventQueue.isDispatchThread()) {
throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread");
}

View File

@ -0,0 +1,73 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.Robot;
/**
* @test
* @bug 8238741
* @summary Check that Robot constructors throw AWTException in a headless
* environment
* @run main/othervm -Djava.awt.headless=true HeadlessRobot
*/
public final class HeadlessRobot {
public static void main(String[] args) {
try {
new Robot();
throw new RuntimeException("Expected AWTException did not occur");
} catch (AWTException ignored) {
// expected AWTException
}
try {
new Robot(new GraphicsDevice() {
@Override
public int getType() {
return TYPE_RASTER_SCREEN;
}
@Override
public String getIDstring() {
return "Stub device";
}
@Override
public GraphicsConfiguration[] getConfigurations() {
return null;
}
@Override
public GraphicsConfiguration getDefaultConfiguration() {
return null;
}
});
throw new RuntimeException("Expected AWTException did not occur");
} catch (AWTException ignored) {
// expected AWTException
}
}
}