8202768: [macos] Appkit thread slows when any Window Manager active

Reviewed-by: ant
diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/JavaComponentAccessibility.m b/src/java.desktop/macosx/native/libawt_lwawt/awt/JavaComponentAccessibility.m
index 01b495b..5a5708a 100644
--- a/src/java.desktop/macosx/native/libawt_lwawt/awt/JavaComponentAccessibility.m
+++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/JavaComponentAccessibility.m
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, 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
@@ -325,15 +325,15 @@
         }
 
         JavaComponentAccessibility *child = [self createWithParent:parent accessible:jchild role:childJavaRole index:childIndex withEnv:env withView:parent->fView];
-        
+
         (*env)->DeleteLocalRef(env, jchild);
         (*env)->DeleteLocalRef(env, jchildJavaRole);
-        
+
         [children addObject:child];
         childIndex++;
     }
     (*env)->DeleteLocalRef(env, jchildrenAndRoles);
-    
+
     return children;
 }
 
@@ -646,6 +646,9 @@
         }
         // The above set of attributes is immutable per role, but some objects, if
         // they are the child of a list, need to add the selected and index attributes.
+        if ([self accessibilityIsIgnored]) {
+            return names;
+        }
         id myParent = [self accessibilityParentAttribute];
         if ([myParent isKindOfClass:[JavaComponentAccessibility class]]) {
             NSString *parentRole = [(JavaComponentAccessibility *)myParent javaRole];
@@ -1060,7 +1063,7 @@
                                     sjc_CAccessibility,
                                     "requestSelection",
                                     "(Ljavax/accessibility/Accessible;Ljava/awt/Component;)V" );
-    
+
     if ([(NSNumber*)value boolValue]) {
         JNIEnv* env = [ThreadUtilities getJNIEnv];
         JNFCallStaticVoidMethod(env, jm_requestSelection, fAccessible, fComponent); // AWT_THREADING Safe (AWTRunLoop)
@@ -1167,7 +1170,7 @@
     // Need to handle popupmenus differently.
     //
     // At least for now don't handle combo box menus.
-    // This may change when later fixing issues which currently 
+    // This may change when later fixing issues which currently
     // exist for combo boxes, but for now the following is only
     // for JPopupMenus, not for combobox menus.
     id parent = [self parent];
@@ -1349,7 +1352,7 @@
     NSWindow* hostWindow = [[self->fView window] retain];
     jobject focused = JNFCallStaticObjectMethod(env, jm_getFocusOwner, fComponent); // AWT_THREADING Safe (AWTRunLoop)
     [hostWindow release];
-    
+
     if (focused != NULL) {
         if (JNFIsInstanceOf(env, focused, &sjc_Accessible)) {
             value = [JavaComponentAccessibility createWithAccessible:focused withEnv:env withView:fView];
diff --git a/test/jdk/javax/accessibility/SlowPanelIteration/SlowPanelIteration.java b/test/jdk/javax/accessibility/SlowPanelIteration/SlowPanelIteration.java
new file mode 100644
index 0000000..daf2f1c
--- /dev/null
+++ b/test/jdk/javax/accessibility/SlowPanelIteration/SlowPanelIteration.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2018, 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.BorderLayout;
+import java.awt.Color;
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.EventQueue;
+import java.awt.Point;
+import java.awt.Robot;
+import java.awt.event.InputEvent;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+
+/**
+ * @test
+ * @key headful
+ * @bug 8202768
+ * @summary we should not hang when lots of panels are used
+ */
+public final class SlowPanelIteration {
+
+    private static JFrame frame;
+    private static Point center = new Point();
+    private static volatile CountDownLatch go;
+
+    public static void main(final String[] args) throws Exception {
+        Robot r = new Robot();
+        // accessibility tool will need time to react to our clicks
+        r.setAutoDelay(200);
+        try {
+            EventQueue.invokeAndWait(SlowPanelIteration::showUI);
+            for (int i = 0; i < 10; ++i) {
+                go = new CountDownLatch(1);
+                r.mouseMove(center.x, center.y);
+                r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
+                r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
+                if (!go.await(10, TimeUnit.SECONDS)) {
+                    throw new RuntimeException("Too slow operation");
+                }
+            }
+        } finally {
+            EventQueue.invokeAndWait(SlowPanelIteration::dispose);
+        }
+    }
+
+    private static void showUI() {
+        frame = new JFrame();
+        frame.setSize(new Dimension(400, 400));
+        frame.setLocationRelativeTo(null);
+
+        final Container content = frame.getContentPane();
+        content.setLayout(new BorderLayout(0, 0));
+        Container lastPanel = content;
+        for (int i = 0; i < 500; i++) {
+            final JPanel p = new JPanel();
+            p.setLayout(new BorderLayout(0, 0));
+            lastPanel.add(p);
+            lastPanel.addMouseListener(new MouseAdapter() {
+                @Override
+                public void mouseClicked(MouseEvent e) {
+                    System.out.println("click");
+                    go.countDown();
+                }
+            });
+            lastPanel = p;
+        }
+
+        lastPanel.setBackground(Color.GREEN);
+        frame.setVisible(true);
+
+        Point loc = frame.getLocationOnScreen();
+        center.x = loc.x + frame.getWidth() / 2;
+        center.y = loc.y + frame.getHeight() / 2;
+    }
+
+    private static void dispose() {
+        if (frame != null) {
+            frame.dispose();
+        }
+    }
+}