11/*
2- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
3333import java .awt .Robot ;
3434import java .awt .Toolkit ;
3535import java .awt .event .InputEvent ;
36+ import java .awt .event .MouseAdapter ;
3637import java .awt .event .MouseEvent ;
37- import java .awt .event .*;
38+ import java .io .File ;
39+ import java .util .stream .IntStream ;
40+ import javax .imageio .ImageIO ;
3841
39- /**
42+ /*
4043 * @test
4144 * @key headful
4245 * @bug 8160270
@@ -47,43 +50,59 @@ public final class PopupMenuLocation {
4750 private static final int SIZE = 350 ;
4851 public static final String TEXT =
4952 "Long-long-long-long-long-long-long text in the item-" ;
53+ public static final int OFFSET = 50 ;
5054 private static volatile boolean action = false ;
55+ private static Robot robot ;
56+ private static Frame frame ;
57+ private static Rectangle screenBounds ;
58+
5159
5260 public static void main (final String [] args ) throws Exception {
61+ robot = new Robot ();
62+ robot .setAutoDelay (200 );
5363 GraphicsEnvironment ge =
5464 GraphicsEnvironment .getLocalGraphicsEnvironment ();
5565 GraphicsDevice [] sds = ge .getScreenDevices ();
5666 for (GraphicsDevice sd : sds ) {
5767 GraphicsConfiguration gc = sd .getDefaultConfiguration ();
58- Rectangle bounds = gc .getBounds ();
59- Point point = new Point (bounds .x , bounds .y );
68+ screenBounds = gc .getBounds ();
6069 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 ) {
70+ Point point = new Point (screenBounds .x + insets .left ,
71+ screenBounds .y + insets .top );
72+ final int yBound = screenBounds .y + screenBounds .height
73+ - insets .bottom - SIZE ;
74+ final int xBound = screenBounds .x + screenBounds .width
75+ - insets .right - SIZE ;
76+ while (point .y < yBound ) {
77+ while (point .x < xBound ) {
6478 test (point );
65- point .translate (bounds .width / 5 , 0 );
79+ point .translate (screenBounds .width / 5 , 0 );
6680 }
67- point .setLocation (bounds .x , point .y + bounds .height / 5 );
81+ point .setLocation (screenBounds .x ,
82+ point .y + screenBounds .height / 5 );
6883 }
6984 }
7085 }
7186
72- private static void test (final Point tmp ) throws Exception {
87+ private static void test (final Point loc ) {
88+ frame = new Frame ();
7389 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 ();
90+ IntStream .rangeClosed (1 , 6 ).forEach (i -> pm .add (TEXT + i ));
91+ pm .addActionListener (e -> {
92+ action = true ;
93+ System .out .println (" Got action event " + e );
94+ });
95+
7996 try {
97+ frame .setUndecorated (true );
8098 frame .setAlwaysOnTop (true );
8199 frame .setLayout (new FlowLayout ());
82100 frame .add (pm );
83101 frame .pack ();
84102 frame .setSize (SIZE , SIZE );
103+ frame .setLocation (loc );
85104 frame .setVisible (true );
86- frame . setLocation ( tmp . x , tmp . y );
105+
87106 frame .addMouseListener (new MouseAdapter () {
88107 public void mousePressed (MouseEvent e ) {
89108 show (e );
@@ -105,23 +124,37 @@ private void show(MouseEvent e) {
105124 }
106125 }
107126
108- private static void openPopup (final Frame frame ) throws Exception {
109- Robot robot = new Robot ();
110- robot .setAutoDelay (200 );
127+ private static void openPopup (final Frame frame ) {
111128 robot .waitForIdle ();
112129 Point pt = frame .getLocationOnScreen ();
113- robot .mouseMove (pt .x + frame .getWidth () / 2 , pt .y + 50 );
130+ int x = pt .x + frame .getWidth () / 2 ;
131+ int y = pt .y + OFFSET ;
132+ robot .mouseMove (x , y );
114133 robot .mousePress (InputEvent .BUTTON3_DOWN_MASK );
115134 robot .mouseRelease (InputEvent .BUTTON3_DOWN_MASK );
116- int x = pt . x + frame . getWidth () / 2 ;
117- int y = pt . y + 130 ;
135+ robot . delay ( 200 ) ;
136+ y += OFFSET ;
118137 robot .mouseMove (x , y );
119138 robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
120139 robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
121140 robot .waitForIdle ();
122141 if (!action ) {
123- throw new RuntimeException ();
142+ captureScreen ();
143+ throw new RuntimeException (
144+ "Failed, didn't receive the PopupMenu ActionEvent on " +
145+ "frame= " + frame + ", isFocused = " + frame .isFocused ());
124146 }
125147 action = false ;
126148 }
149+
150+ private static void captureScreen () {
151+ try {
152+ ImageIO .write (robot .createScreenCapture (screenBounds ),
153+ "png" ,
154+ new File ("screen.png" ));
155+ } catch (Exception e ) {
156+ e .printStackTrace ();
157+ }
158+ }
159+
127160}
0 commit comments