changeset 60587:23dc143cca50

8226806: [macOS 10.14] Methods of Java Robot should be called from appropriate thread Reviewed-by: psadhukhan, prr
author serb
date Mon, 16 Mar 2020 17:33:52 -0700
parents 889b4191879c
children a9ffedffd435
files src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m test/jdk/java/awt/Robot/NonEmptyErrorStream.java
diffstat 2 files changed, 145 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m	Mon Mar 16 12:49:08 2020 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m	Mon Mar 16 17:33:52 2020 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 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
@@ -23,7 +23,6 @@
  * questions.
  */
 
-
 #import "jni_util.h"
 
 #import <JavaNativeFoundation/JavaNativeFoundation.h>
@@ -35,6 +34,7 @@
 #import "java_awt_event_InputEvent.h"
 #import "java_awt_event_KeyEvent.h"
 #import "sizecalc.h"
+#import "ThreadUtilities.h"
 
 // Starting number for event numbers generated by Robot.
 // Apple docs don't mention at all what are the requirements
@@ -97,38 +97,40 @@
     // Always set all states, in case Apple ever changes default behaviors.
     static int setupDone = 0;
     if (!setupDone) {
-        int i;
-        jint* tmp;
-        jboolean copy = JNI_FALSE;
+        [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
+            int i;
+            jint* tmp;
+            jboolean copy = JNI_FALSE;
 
-        setupDone = 1;
-        // Don't block local events after posting ours
-        CGSetLocalEventsSuppressionInterval(0.0);
+            setupDone = 1;
+            // Don't block local events after posting ours
+            CGSetLocalEventsSuppressionInterval(0.0);
 
-        // Let our event's modifier key state blend with local hardware events
-        CGEnableEventStateCombining(TRUE);
+            // Let our event's modifier key state blend with local hardware events
+            CGEnableEventStateCombining(TRUE);
 
-        // Don't let our events block local hardware events
-        CGSetLocalEventsFilterDuringSupressionState(
-                                    kCGEventFilterMaskPermitAllEvents,
-                                    kCGEventSupressionStateSupressionInterval);
-        CGSetLocalEventsFilterDuringSupressionState(
-                                    kCGEventFilterMaskPermitAllEvents,
-                                    kCGEventSupressionStateRemoteMouseDrag);
+            // Don't let our events block local hardware events
+            CGSetLocalEventsFilterDuringSupressionState(
+                                        kCGEventFilterMaskPermitAllEvents,
+                                        kCGEventSupressionStateSupressionInterval);
+            CGSetLocalEventsFilterDuringSupressionState(
+                                        kCGEventFilterMaskPermitAllEvents,
+                                        kCGEventSupressionStateRemoteMouseDrag);
 
-        gsClickCount = 0;
-        gsLastClickTime = 0;
-        gsEventNumber = ROBOT_EVENT_NUMBER_START;
+            gsClickCount = 0;
+            gsLastClickTime = 0;
+            gsEventNumber = ROBOT_EVENT_NUMBER_START;
 
-        gsButtonEventNumber = (int*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(int), gNumberOfButtons);
-        if (gsButtonEventNumber == NULL) {
-            JNU_ThrowOutOfMemoryError(env, NULL);
-            return;
-        }
+            gsButtonEventNumber = (int*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(int), gNumberOfButtons);
+            if (gsButtonEventNumber == NULL) {
+                JNU_ThrowOutOfMemoryError(env, NULL);
+                return;
+            }
 
-        for (i = 0; i < gNumberOfButtons; ++i) {
-            gsButtonEventNumber[i] = ROBOT_EVENT_NUMBER_START;
-        }
+            for (i = 0; i < gNumberOfButtons; ++i) {
+                gsButtonEventNumber[i] = ROBOT_EVENT_NUMBER_START;
+            }
+        }];
     }
 }
 
@@ -239,14 +241,15 @@
 Java_sun_lwawt_macosx_CRobot_mouseWheel
 (JNIEnv *env, jobject peer, jint wheelAmt)
 {
-    CGEventRef event = CGEventCreateScrollWheelEvent(NULL,
-                                            kCGScrollEventUnitLine,
-                                            k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
-
-    if (event != NULL) {
-        CGEventPost(kCGSessionEventTap, event);
-        CFRelease(event);
-    }
+    [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
+        CGEventRef event = CGEventCreateScrollWheelEvent(NULL,
+                                                kCGScrollEventUnitLine,
+                                                k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
+        if (event != NULL) {
+            CGEventPost(kCGSessionEventTap, event);
+            CFRelease(event);
+        }
+    }];
 }
 
 /*
@@ -258,13 +261,14 @@
 Java_sun_lwawt_macosx_CRobot_keyEvent
 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
 {
-    CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
-
-    CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
-    if (event != NULL) {
-        CGEventPost(kCGSessionEventTap, event);
-        CFRelease(event);
-    }
+    [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
+        CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
+        CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
+        if (event != NULL) {
+            CGEventPost(kCGSessionEventTap, event);
+            CFRelease(event);
+        }
+    }];
 }
 
 /*
@@ -333,13 +337,15 @@
 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
                            CGEventType type, int clickCount, int eventNumber)
 {
-    CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, type, point, button);
-    if (mouseEvent != NULL) {
-        CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, clickCount);
-        CGEventSetIntegerValueField(mouseEvent, kCGMouseEventNumber, eventNumber);
-        CGEventPost(kCGSessionEventTap, mouseEvent);
-        CFRelease(mouseEvent);
-    }
+    [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
+        CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, type, point, button);
+        if (mouseEvent != NULL) {
+            CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, clickCount);
+            CGEventSetIntegerValueField(mouseEvent, kCGMouseEventNumber, eventNumber);
+            CGEventPost(kCGSessionEventTap, mouseEvent);
+            CFRelease(mouseEvent);
+        }
+    }];
 }
 
 static inline CGKeyCode GetCGKeyCode(jint javaKeyCode)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/awt/Robot/NonEmptyErrorStream.java	Mon Mar 16 17:33:52 2020 -0700
@@ -0,0 +1,89 @@
+/*
+ * 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.Frame;
+import java.awt.Robot;
+import java.awt.*;
+import java.awt.event.InputEvent;
+import java.awt.event.KeyEvent;
+import java.util.concurrent.TimeUnit;
+
+import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.process.ProcessTools;
+
+/**
+ * @test
+ * @bug 8226806
+ * @key headful
+ * @library /test/lib
+ * @summary checks for unexpected output in stderr and stdout
+ */
+public final class NonEmptyErrorStream {
+
+    public static void main(String[] args) throws Exception {
+        if (args.length == 0) {
+            ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
+                    NonEmptyErrorStream.class.getSimpleName(),"run");
+            Process p = pb.start();
+            OutputAnalyzer output = new OutputAnalyzer(p);
+            p.waitFor(20, TimeUnit.SECONDS);
+            p.destroy();
+            output.shouldHaveExitValue(0);
+            output.stdoutShouldBeEmpty();
+            output.stderrShouldBeEmpty();
+            return;
+        }
+        Frame frame = new Frame();
+        frame.setSize(400, 300);
+        frame.setLocationRelativeTo(null);
+        frame.setVisible(true);
+
+        Robot robot = new Robot();
+        robot.setAutoDelay(50);
+        robot.waitForIdle();
+        frame.toFront();
+
+        Rectangle rect = frame.getBounds();
+        int x = (int) rect.getCenterX();
+        int y = (int) rect.getCenterY();
+
+        for (int i = 0; i < 20; i++) {
+            robot.getPixelColor(x, y);
+            robot.createScreenCapture(rect);
+        }
+        for (int i = 0; i < 20; i++) {
+            robot.mouseMove(x + 50, y + 50);
+            robot.mouseMove(x - 50, y - 50);
+        }
+        for (int i = 0; i < 20; i++) {
+            robot.keyPress(KeyEvent.VK_ESCAPE);
+            robot.keyRelease(KeyEvent.VK_ESCAPE);
+        }
+        for (int i = 0; i < 20; i++) {
+            robot.mousePress(InputEvent.BUTTON1_MASK);
+            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+        }
+        robot.waitForIdle();
+        frame.dispose();
+    }
+}