changeset 58978:d5d7e0df08d2

8242174: [macos] The NestedModelessDialogTest test make the macOS unstable Reviewed-by: prr
author serb
date Mon, 20 Apr 2020 09:35:04 -0700
parents 4a0330be9a84
children ead0b50a6a79
files src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m test/jdk/ProblemList.txt test/jdk/java/awt/Toolkit/RealSync/Test.java
diffstat 3 files changed, 55 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m	Fri Apr 17 11:29:01 2020 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m	Mon Apr 20 09:35:04 2020 -0700
@@ -67,6 +67,7 @@
 // happen with z-order.
 static int gsEventNumber;
 static int* gsButtonEventNumber;
+static NSTimeInterval gNextKeyEventTime;
 
 static inline CGKeyCode GetCGKeyCode(jint javaKeyCode);
 
@@ -84,6 +85,28 @@
                      [s UTF8String]);
 }
 
+/**
+ * Saves the "safe moment" when the NEXT event can be posted by the robot safely
+ * and sleeps for some time if the "safe moment" for the CURRENT event is not
+ * reached.
+ *
+ * We need to sleep to give time for the macOS to update the state.
+ *
+ * The "mouse move" events are skipped, because it is not a big issue if we lost
+ * some of them, the latest coordinates are saved in the peer and will be used
+ * for clicks.
+ */
+static inline void autoDelay(BOOL isMove) {
+    if (!isMove){
+        NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate];
+        NSTimeInterval delay = gNextKeyEventTime - now;
+        if (delay > 0) {
+            [NSThread sleepForTimeInterval:delay];
+        }
+    }
+    gNextKeyEventTime = [[NSDate date] timeIntervalSinceReferenceDate] + 0.050;
+}
+
 /*
  * Class:     sun_lwawt_macosx_CRobot
  * Method:    initRobot
@@ -119,6 +142,7 @@
 
             gsClickCount = 0;
             gsLastClickTime = 0;
+            gNextKeyEventTime = 0;
             gsEventNumber = ROBOT_EVENT_NUMBER_START;
 
             gsButtonEventNumber = (int*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(int), gNumberOfButtons);
@@ -145,6 +169,7 @@
  jboolean isButtonsDownState, jboolean isMouseMove)
 {
     JNF_COCOA_ENTER(env);
+    autoDelay(isMouseMove);
 
     // This is the native method called when Robot mouse events occur.
     // The CRobot tracks the mouse position, and which button was
@@ -241,14 +266,19 @@
 Java_sun_lwawt_macosx_CRobot_mouseWheel
 (JNIEnv *env, jobject peer, jint wheelAmt)
 {
+    autoDelay(NO);
     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
-        CGEventRef event = CGEventCreateScrollWheelEvent(NULL,
+        CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
+        CGEventRef event = CGEventCreateScrollWheelEvent(source,
                                                 kCGScrollEventUnitLine,
                                                 k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
         if (event != NULL) {
-            CGEventPost(kCGSessionEventTap, event);
+            CGEventPost(kCGHIDEventTap, event);
             CFRelease(event);
         }
+        if (source != NULL) {
+            CFRelease(source);
+        }
     }];
 }
 
@@ -261,13 +291,18 @@
 Java_sun_lwawt_macosx_CRobot_keyEvent
 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
 {
+    autoDelay(NO);
     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
+        CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
         CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
-        CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
+        CGEventRef event = CGEventCreateKeyboardEvent(source, keyCode, keyPressed);
         if (event != NULL) {
-            CGEventPost(kCGSessionEventTap, event);
+            CGEventPost(kCGHIDEventTap, event);
             CFRelease(event);
         }
+        if (source != NULL) {
+            CFRelease(source);
+        }
     }];
 }
 
@@ -338,13 +373,17 @@
                            CGEventType type, int clickCount, int eventNumber)
 {
     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
-        CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, type, point, button);
+        CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
+        CGEventRef mouseEvent = CGEventCreateMouseEvent(source, type, point, button);
         if (mouseEvent != NULL) {
             CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, clickCount);
             CGEventSetIntegerValueField(mouseEvent, kCGMouseEventNumber, eventNumber);
-            CGEventPost(kCGSessionEventTap, mouseEvent);
+            CGEventPost(kCGHIDEventTap, mouseEvent);
             CFRelease(mouseEvent);
         }
+        if (source != NULL) {
+            CFRelease(source);
+        }
     }];
 }
 
--- a/test/jdk/ProblemList.txt	Fri Apr 17 11:29:01 2020 +0530
+++ b/test/jdk/ProblemList.txt	Mon Apr 20 09:35:04 2020 -0700
@@ -196,7 +196,7 @@
 java/awt/datatransfer/DragImage/MultiResolutionDragImageTest.java 8080982 generic-all
 java/awt/datatransfer/SystemFlavorMap/AddFlavorTest.java 8079268 linux-all
 java/awt/Toolkit/ScreenInsetsTest/ScreenInsetsTest.java 6829250 windows-all
-java/awt/Toolkit/RealSync/Test.java 6849383 macosx-all,linux-all
+java/awt/Toolkit/RealSync/Test.java 6849383 linux-all
 java/awt/LightweightComponent/LightweightEventTest/LightweightEventTest.java 8159252 windows-all
 java/awt/EventDispatchThread/HandleExceptionOnEDT/HandleExceptionOnEDT.java 8203047 macosx-all
 java/awt/EventDispatchThread/LoopRobustness/LoopRobustness.java 8073636 macosx-all
@@ -223,7 +223,7 @@
 java/awt/Scrollbar/ScrollbarMouseWheelTest/ScrollbarMouseWheelTest.java 8196018 windows-all,linux-all
 java/awt/TrayIcon/ActionCommand/ActionCommand.java 8150540 windows-all
 java/awt/TrayIcon/ActionEventMask/ActionEventMask.java 8150540 windows-all
-java/awt/TrayIcon/ActionEventTest/ActionEventTest.java 8150540 windows-all
+java/awt/TrayIcon/ActionEventTest/ActionEventTest.java 8150540,8242801 windows-all,macosx-all
 java/awt/TrayIcon/ModalityTest/ModalityTest.java 8150540 windows-all,macosx-all
 java/awt/TrayIcon/MouseEventMask/MouseEventMaskTest.java 8150540 windows-all
 java/awt/TrayIcon/MouseMovedTest/MouseMovedTest.java 8150540 windows-all
@@ -541,6 +541,7 @@
 java/awt/Window/WindowOwnedByEmbeddedFrameTest/WindowOwnedByEmbeddedFrameTest.java 8233558 macosx-all
 java/awt/Mouse/MouseComboBoxTest/MouseComboBoxTest.java 8233564 macosx-all
 java/awt/Modal/NullModalityDialogTest/NullModalityDialogTest.java 8233565 macosx-all
+java/awt/keyboard/AllKeyCode/AllKeyCode.java 8242930 macosx-all
 java/awt/KeyboardFocusmanager/TypeAhead/SubMenuShowTest/SubMenuShowTest.java 8233566 macosx-all
 java/awt/KeyboardFocusmanager/TypeAhead/EnqueueWithDialogTest/EnqueueWithDialogTest.java 8233566,6447537 macosx-all,windows-all
 java/awt/FullScreen/8013581/bug8013581.java 8169471 macosx-all
--- a/test/jdk/java/awt/Toolkit/RealSync/Test.java	Fri Apr 17 11:29:01 2020 +0530
+++ b/test/jdk/java/awt/Toolkit/RealSync/Test.java	Mon Apr 20 09:35:04 2020 -0700
@@ -23,7 +23,7 @@
 
 /*
   @test
-  @bug 6252005
+  @bug 6252005 8242174
   @key headful
   @summary Tests that realSync feature works
   @author denis.mikhalkin: area=awt.toolkit
@@ -31,16 +31,17 @@
   @run main/timeout=6000 Test
 */
 
+import java.awt.Frame;
+import java.awt.Point;
+import java.awt.Robot;
 import java.awt.*;
 import java.awt.event.*;
-import java.util.LinkedList;
-import java.util.Collections;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.util.Collections;
+import java.util.LinkedList;
+
 import javax.swing.*;
-import java.awt.image.*;
-import javax.imageio.*;
-import java.io.*;
 
 /**
  * Tests various problematic areas and how they are fixed using real-sync API: