1/*
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.awt.FlowLayout;
25import java.awt.Frame;
26import java.awt.GraphicsConfiguration;
27import java.awt.GraphicsDevice;
28import java.awt.GraphicsEnvironment;
29import java.awt.Insets;
30import java.awt.Point;
31import java.awt.PopupMenu;
32import java.awt.Rectangle;
33import java.awt.Robot;
34import java.awt.Toolkit;
35import java.awt.event.InputEvent;
36import java.awt.event.MouseEvent;
37import java.awt.event.*;
38
39/**
40 * @test
41 * @key headful
42 * @bug 8160270
43 * @run main/timeout=300 PopupMenuLocation
44 */
45public final class PopupMenuLocation {
46
47    private static final int SIZE = 350;
48    public static final String TEXT =
49            "Long-long-long-long-long-long-long text in the item-";
50    private static volatile boolean action = false;
51
52    public static void main(final String[] args) throws Exception {
53        GraphicsEnvironment ge =
54                GraphicsEnvironment.getLocalGraphicsEnvironment();
55        GraphicsDevice[] sds = ge.getScreenDevices();
56        for (GraphicsDevice sd : sds) {
57            GraphicsConfiguration gc = sd.getDefaultConfiguration();
58            Rectangle bounds = gc.getBounds();
59            Point point = new Point(bounds.x, bounds.y);
60            Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
61            while (point.y < bounds.y + bounds.height - insets.bottom - SIZE) {
62                while (point.x
63                        < bounds.x + bounds.width - insets.right - SIZE) {
64                    test(point);
65                    point.translate(bounds.width / 5, 0);
66                }
67                point.setLocation(bounds.x, point.y + bounds.height / 5);
68            }
69        }
70    }
71
72    private static void test(final Point tmp) throws Exception {
73        PopupMenu pm = new PopupMenu();
74        for (int i = 1; i < 7; i++) {
75            pm.add(TEXT + i);
76        }
77        pm.addActionListener(e -> action = true);
78        Frame frame = new Frame();
79        try {
80            frame.setAlwaysOnTop(true);
81            frame.setLayout(new FlowLayout());
82            frame.add(pm);
83            frame.pack();
84            frame.setSize(SIZE, SIZE);
85            frame.setVisible(true);
86            frame.setLocation(tmp.x, tmp.y);
87            frame.addMouseListener(new MouseAdapter() {
88                public void mousePressed(MouseEvent e) {
89                    show(e);
90                }
91
92                public void mouseReleased(MouseEvent e) {
93                    show(e);
94                }
95
96                private void show(MouseEvent e) {
97                    if (e.isPopupTrigger()) {
98                        pm.show(frame, 0, 50);
99                    }
100                }
101            });
102            openPopup(frame);
103        } finally {
104            frame.dispose();
105        }
106    }
107
108    private static void openPopup(final Frame frame) throws Exception {
109        Robot robot = new Robot();
110        robot.setAutoDelay(200);
111        robot.waitForIdle();
112        Point pt = frame.getLocationOnScreen();
113        robot.mouseMove(pt.x + frame.getWidth() / 2, pt.y + 50);
114        robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
115        robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
116        int x = pt.x + frame.getWidth() / 2;
117        int y = pt.y + 130;
118        robot.mouseMove(x, y);
119        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
120        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
121        robot.waitForIdle();
122        if (!action) {
123            throw new RuntimeException();
124        }
125        action = false;
126    }
127}
128