/*
 * Copyright (c) 2023, 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.Point;
import java.awt.PopupMenu;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.stream.IntStream;

/*
 * @test
 * @key headful
 * @run main/timeout=300 WaitForIdleIssue
 */
public final class WaitForIdleIssue {

    public static final String TEXT = "Item-";
    public static final int OFFSET = 50;
    private static final int SIZE = 350;
    private static Robot robot;
    private static Frame frame;

    public static void main(final String[] args) throws Exception {
        robot = new Robot();
        test(new Point(200, 200));
    }

    private static void test(final Point loc) {
        frame = new Frame();
        PopupMenu pm = new PopupMenu();
        IntStream.rangeClosed(1, 3).forEach(i -> pm.add(TEXT + i));
        try {
            frame.setUndecorated(true);
            frame.add(pm);
            frame.setSize(SIZE, SIZE);
            frame.setLocation(loc);
            frame.setVisible(true);

            frame.addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    show(e);
                }

                private void show(MouseEvent e) {
                    if (e.isPopupTrigger()) {
                        pm.show(frame, 150, 50);
                    }
                }
            });
            robot.waitForIdle();
            openPopup(loc);
        } finally {
            frame.dispose();
        }
    }

    private static void openPopup(final Point frameLoc) {
        Point pt = new Point(frameLoc);
        int x = pt.x + SIZE / 2;
        int y = pt.y + OFFSET;

        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);

        System.out.println("Entering first waitForIdle()");
        long time = System.currentTimeMillis();
        // Takes around 10 seconds time
        robot.waitForIdle();
        time = (System.currentTimeMillis() - time) / 1000;
        System.out.println(
                "Exits first waitForIdle(), time taken in seconds = " + time);

        y += OFFSET;

        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

        System.out.println("Entering second waitForIdle()");
        time = System.currentTimeMillis();
        // Takes less than 1 seconds time
        robot.waitForIdle();
        time = (System.currentTimeMillis() - time) / 1000;
        System.out.println(
                "Exits second waitForIdle(), time taken in seconds = " + time);
    }

}
