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 
  24 import java.awt.FlowLayout;
  25 import java.awt.Frame;
  26 import java.awt.GraphicsConfiguration;
  27 import java.awt.GraphicsDevice;
  28 import java.awt.GraphicsEnvironment;
  29 import java.awt.Insets;
  30 import java.awt.Point;
  31 import java.awt.PopupMenu;
  32 import java.awt.Rectangle;
  33 import java.awt.Robot;
  34 import java.awt.Toolkit;
  35 import java.awt.event.InputEvent;
  36 import java.awt.event.MouseEvent;
  37 import java.awt.event.*;
  38 
  39 /**
  40  * @test
  41  * @bug 8160270
  42  * @run main/timeout=300 PopupMenuLocation
  43  */
  44 public final class PopupMenuLocation {
  45 
  46     private static final int SIZE = 350;
  47     public static final String TEXT =
  48             "Long-long-long-long-long-long-long text in the item-";
  49     private static volatile boolean action = false;
  50 
  51     public static void main(final String[] args) throws Exception {
  52         GraphicsEnvironment ge =
  53                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  54         GraphicsDevice[] sds = ge.getScreenDevices();
  55         for (GraphicsDevice sd : sds) {
  56             GraphicsConfiguration gc = sd.getDefaultConfiguration();
  57             Rectangle bounds = gc.getBounds();
  58             Point point = new Point(bounds.x, bounds.y);
  59             Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
  60             while (point.y < bounds.y + bounds.height - insets.bottom - SIZE) {
  61                 while (point.x
  62                         < bounds.x + bounds.width - insets.right - SIZE) {
  63                     test(point);
  64                     point.translate(bounds.width / 5, 0);
  65                 }
  66                 point.setLocation(bounds.x, point.y + bounds.height / 5);
  67             }
  68         }
  69     }
  70 
  71     private static void test(final Point tmp) throws Exception {
  72         PopupMenu pm = new PopupMenu();
  73         for (int i = 1; i < 7; i++) {
  74             pm.add(TEXT + i);
  75         }
  76         pm.addActionListener(e -> action = true);
  77         Frame frame = new Frame();
  78         try {
  79             frame.setAlwaysOnTop(true);
  80             frame.setLayout(new FlowLayout());
  81             frame.add(pm);
  82             frame.pack();
  83             frame.setSize(SIZE, SIZE);
  84             frame.setVisible(true);
  85             frame.setLocation(tmp.x, tmp.y);
  86             frame.addMouseListener(new MouseAdapter() {
  87                 public void mousePressed(MouseEvent e) {
  88                     show(e);
  89                 }
  90 
  91                 public void mouseReleased(MouseEvent e) {
  92                     show(e);
  93                 }
  94 
  95                 private void show(MouseEvent e) {
  96                     if (e.isPopupTrigger()) {
  97                         pm.show(frame, 0, 50);
  98                     }
  99                 }
 100             });
 101             openPopup(frame);
 102         } finally {
 103             frame.dispose();
 104         }
 105     }
 106 
 107     private static void openPopup(final Frame frame) throws Exception {
 108         Robot robot = new Robot();
 109         robot.setAutoDelay(200);
 110         robot.waitForIdle();
 111         Point pt = frame.getLocationOnScreen();
 112         robot.mouseMove(pt.x + frame.getWidth() / 2, pt.y + 50);
 113         robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
 114         robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
 115         int x = pt.x + frame.getWidth() / 2;
 116         int y = pt.y + 130;
 117         robot.mouseMove(x, y);
 118         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 119         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 120         robot.waitForIdle();
 121         if (!action) {
 122             throw new RuntimeException();
 123         }
 124         action = false;
 125     }
 126 }