JRE-401 added regression test (AppCode freezes during autocomplete and other operations)
diff --git a/test/jb/javax/swing/Popup/Popup401.java b/test/jb/javax/swing/Popup/Popup401.java
new file mode 100644
index 0000000..09eb350
--- /dev/null
+++ b/test/jb/javax/swing/Popup/Popup401.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2000-2017 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.awt.Robot;
+import java.awt.event.ComponentAdapter;
+import java.awt.event.ComponentEvent;
+import javax.swing.JEditorPane;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JTextArea;
+import javax.swing.Popup;
+import javax.swing.PopupFactory;
+import javax.swing.SwingUtilities;
+import javax.swing.WindowConstants;
+
+/* @test
+ * @summary regression test on
+ * <ul>
+ *     <li>JRE-401 AppCode freezes during autocomplete and other operations; and</li>
+ *     <li>JRE-415 Mistake in "Merge with jdk8u152-b00"</li>
+ * </ul>
+ * @run main/othervm/timeout=360 Popup401
+ */
+
+/*
+ * Description: The test invoke <code>Popup.show()</code>/<code>hide()</code> methods <code>ITERATION_NUMBER</code>
+ * times (by default 1000 times) and fails
+ * <ul>
+ *     <li>by <code>java.lang.RuntimeException: Waiting for the invocation event timed out</code> if it hangs because of
+ *     the deadlock <code>at sun.lwawt.macosx.CPlatformComponent.$$YJP$$nativeCreateComponent(Native Method)</code> as
+ *     it was described in JRE-401</li>
+ *
+ *     <li>or by <code>java.lang.RuntimeException: The test is near to be hang</code> if the method
+ *     <code>Popup.show()</code> was executed <code>HANG_TIME_FACTOR</code> times longer than it was executed on the
+ *     first iteration.
+ * </ul>
+ */
+public class Popup401 extends JPanel {
+
+    private JTextArea textArea;
+    private JEditorPane editorPane;
+
+    private static Popup401 test;
+    private static Robot robot;
+
+    private static final int ITERATION_NUMBER = 1000;
+    private static final int ROBOT_DELAY = 200;
+    private static final int HANG_TIME_FACTOR = 10;
+
+    private static Object testCompleted = new Object();
+
+    private Popup401() {
+        textArea = new JTextArea("§1234567890-=\nqwertyuiop[]\nasdfghjkl;'\\\n`zxcvbnm,./\n");
+        editorPane = new JEditorPane("text/html", "§1234567890-=\nqwertyuiop[]\nasdfghjkl;'\\\n`zxcvbnm,./\n");
+        editorPane.setSize(300, 300);
+        add(editorPane);
+    }
+
+    private void run() {
+        JPanel panel = new JPanel();
+
+        int count = 0;
+        long diffTime, initialDiffTime = 0;
+        while (count < ITERATION_NUMBER) {
+            robot.delay(ROBOT_DELAY);
+
+            PopupFactory factory = PopupFactory.getSharedInstance();
+            Popup popup = factory.getPopup(panel, textArea, editorPane.getLocation().x + 20,
+                    editorPane.getLocation().y + 20);
+
+            long startTime = System.currentTimeMillis();
+            popup.show();
+            long endTime = System.currentTimeMillis();
+            diffTime = endTime - startTime;
+
+            if (count > 1) {
+                if (diffTime * HANG_TIME_FACTOR < (endTime - startTime)) {
+                    throw new RuntimeException("The test is near to be hang: iteration count = " + count
+                            + " initial time = " + initialDiffTime
+                            + " current time = " + diffTime);
+                }
+            } else {
+                initialDiffTime = diffTime;
+            }
+            count++;
+            robot.delay(ROBOT_DELAY);
+
+            popup.hide();
+        }
+    }
+
+    private static void createAndShowGUI() {
+        JFrame frame = new JFrame("HangPopupTest");
+        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
+        frame.setSize(1000, 1000);
+
+        test = new Popup401();
+        frame.add(test);
+        frame.addComponentListener(new ComponentAdapter() {
+            @Override
+            public void componentShown(ComponentEvent e) {
+                super.componentShown(e);
+                test.run();
+                synchronized (testCompleted) {
+                    testCompleted.notifyAll();
+                }
+            }
+        });
+        frame.pack();
+        frame.setVisible(true);
+    }
+
+    public static void main(String[] args) throws Exception {
+        robot = new Robot();
+        synchronized (testCompleted) {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+            testCompleted.wait();
+        }
+    }
+}
\ No newline at end of file