11/*
2- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2012, 2016, 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
2222 */
2323
2424/* @test
25- @bug 7123767
26- @summary Wrong tooltip location in Multi-Monitor configurations
27- @author Vladislav Karnaukhov
28- @run main bug7123767
25+ * @bug 7123767
26+ *
27+ * @summary Check if a tooltip location in Multi-Monitor
28+ * configurations is correct.
29+ * If the configurations number per device exceeds 5,
30+ * then some 5 random configurations will be checked.
31+ * Please Use -Dseed=X to set the random generator seed
32+ * (if necessary).
33+ *
34+ * @author Vladislav Karnaukhov
35+ *
36+ * @key headful
37+ * @key randomness
38+ *
39+ * @modules java.desktop/sun.awt
40+ * @library /lib/testlibrary/
41+ * @build jdk.testlibrary.*
42+ *
43+ * @run main/timeout=300 bug7123767
2944*/
3045
3146import javax .swing .*;
3449import java .awt .event .MouseEvent ;
3550import java .lang .reflect .InvocationTargetException ;
3651
52+ import java .util .List ;
53+ import java .util .ArrayList ;
54+ import java .util .Collections ;
55+ import java .util .Random ;
56+
57+ import jdk .testlibrary .RandomFactory ;
58+
59+
3760public class bug7123767 extends JFrame {
3861
62+ // maximum number of GraphicsConfigurations checked per GraphicsDevice
63+ private static final int MAX_N_CONFIGS = 5 ;
64+ private static final List <GraphicsConfiguration > CONFIGS = getConfigs ();
65+
66+ private static List <GraphicsConfiguration > getConfigs () {
67+
68+ Random rnd = RandomFactory .getRandom ();
69+
70+ List <GraphicsConfiguration > configs = new ArrayList <>();
71+
72+ GraphicsEnvironment ge =
73+ GraphicsEnvironment .getLocalGraphicsEnvironment ();
74+ GraphicsDevice [] devices = ge .getScreenDevices ();
75+
76+ for (GraphicsDevice device : devices ) {
77+ GraphicsConfiguration [] allConfigs = device .getConfigurations ();
78+ int nConfigs = allConfigs .length ;
79+ if (nConfigs <= MAX_N_CONFIGS ) {
80+ Collections .addAll (configs , allConfigs );
81+ } else { // see JDK-8159454
82+ System .out .println ("check only " + MAX_N_CONFIGS +
83+ " configurations for device " + device );
84+ configs .add (device .getDefaultConfiguration ()); // check default
85+ for (int j = 0 ; j < MAX_N_CONFIGS - 1 ; j ++) {
86+ int k = rnd .nextInt (nConfigs );
87+ configs .add (allConfigs [k ]);
88+ }
89+ }
90+ }
91+
92+ return configs ;
93+ }
94+
95+
3996 private static class TestFactory extends PopupFactory {
4097
4198 private static TestFactory newFactory = new TestFactory ();
@@ -59,15 +116,21 @@ public static void uninstall() {
59116 }
60117
61118 // Actual test happens here
119+ @ Override
62120 public Popup getPopup (Component owner , Component contents , int x , int y ) {
63- GraphicsConfiguration mouseGC = testGC (MouseInfo .getPointerInfo ().getLocation ());
121+
122+ GraphicsConfiguration mouseGC =
123+ testGC (MouseInfo .getPointerInfo ().getLocation ());
124+
64125 if (mouseGC == null ) {
65- throw new RuntimeException ("Can't find GraphicsConfiguration that mouse pointer belongs to" );
126+ throw new RuntimeException ("Can't find GraphicsConfiguration "
127+ + "that mouse pointer belongs to" );
66128 }
67129
68130 GraphicsConfiguration tipGC = testGC (new Point (x , y ));
69131 if (tipGC == null ) {
70- throw new RuntimeException ("Can't find GraphicsConfiguration that tip belongs to" );
132+ throw new RuntimeException (
133+ "Can't find GraphicsConfiguration that tip belongs to" );
71134 }
72135
73136 if (!mouseGC .equals (tipGC )) {
@@ -78,17 +141,14 @@ public Popup getPopup(Component owner, Component contents, int x, int y) {
78141 }
79142
80143 private static GraphicsConfiguration testGC (Point pt ) {
81- GraphicsEnvironment environment = GraphicsEnvironment .getLocalGraphicsEnvironment ();
82- GraphicsDevice [] devices = environment .getScreenDevices ();
83- for (GraphicsDevice device : devices ) {
84- GraphicsConfiguration [] configs = device .getConfigurations ();
85- for (GraphicsConfiguration config : configs ) {
86- Rectangle rect = config .getBounds ();
87- Insets insets = Toolkit .getDefaultToolkit ().getScreenInsets (config );
88- adjustInsets (rect , insets );
89- if (rect .contains (pt ))
90- return config ;
91- }
144+
145+ for (GraphicsConfiguration config : CONFIGS ) {
146+
147+ Rectangle rect = config .getBounds ();
148+ Insets insets =
149+ Toolkit .getDefaultToolkit ().getScreenInsets (config );
150+ adjustInsets (rect , insets );
151+ if (rect .contains (pt )) { return config ; }
92152 }
93153
94154 return null ;
@@ -100,15 +160,18 @@ private static GraphicsConfiguration testGC(Point pt) {
100160 private static Robot robot ;
101161
102162 public static void main (String [] args ) throws Exception {
163+
103164 UIManager .setLookAndFeel (new MetalLookAndFeel ());
104165 setUp ();
105166 testToolTip ();
106167 TestFactory .uninstall ();
168+ if (frame != null ) { frame .dispose (); }
107169 }
108170
109171 // Creates a window that is stretched across all available monitors
110172 // and adds itself as ContainerListener to track tooltips drawing
111173 private bug7123767 () {
174+
112175 super ();
113176
114177 ToolTipManager .sharedInstance ().setInitialDelay (0 );
@@ -132,17 +195,16 @@ public Point getToolTipLocation(MouseEvent event) {
132195 pack ();
133196
134197 Rectangle rect = new Rectangle ();
135- GraphicsEnvironment environment = GraphicsEnvironment .getLocalGraphicsEnvironment ();
136- GraphicsDevice [] devices = environment .getScreenDevices ();
137- for (GraphicsDevice device : devices ) {
138- GraphicsConfiguration [] configs = device .getConfigurations ();
139- for (GraphicsConfiguration config : configs ) {
140- Insets localInsets = Toolkit .getDefaultToolkit ().getScreenInsets (config );
141- Rectangle localRect = config .getBounds ();
142- adjustInsets (localRect , localInsets );
143- rect .add (localRect );
144- }
198+
199+ for (GraphicsConfiguration config : CONFIGS ) {
200+
201+ Insets localInsets =
202+ Toolkit .getDefaultToolkit ().getScreenInsets (config );
203+ Rectangle localRect = config .getBounds ();
204+ adjustInsets (localRect , localInsets );
205+ rect .add (localRect );
145206 }
207+
146208 setBounds (rect );
147209 }
148210
@@ -163,35 +225,32 @@ private static void testToolTip() throws AWTException {
163225 robot .setAutoDelay (20 );
164226 robot .waitForIdle ();
165227
166- GraphicsEnvironment environment = GraphicsEnvironment .getLocalGraphicsEnvironment ();
167- GraphicsDevice [] devices = environment .getScreenDevices ();
168- for (GraphicsDevice device : devices ) {
169- GraphicsConfiguration [] configs = device .getConfigurations ();
170- for (GraphicsConfiguration config : configs ) {
171- Rectangle rect = config .getBounds ();
172- Insets insets = Toolkit .getDefaultToolkit ().getScreenInsets (config );
173- adjustInsets (rect , insets );
228+ for (GraphicsConfiguration config : CONFIGS ) {
174229
175- // Upper left
176- glide (rect .x + rect .width / 2 , rect .y + rect .height / 2 ,
177- rect .x + MARGIN , rect .y + MARGIN );
178- robot .waitForIdle ();
179-
180- // Lower left
181- glide (rect .x + rect .width / 2 , rect .y + rect .height / 2 ,
182- rect .x + MARGIN , rect .y + rect .height - MARGIN );
183- robot .waitForIdle ();
184-
185- // Upper right
186- glide (rect .x + rect .width / 2 , rect .y + rect .height / 2 ,
187- rect .x + rect .width - MARGIN , rect .y + MARGIN );
188- robot .waitForIdle ();
189-
190- // Lower right
191- glide (rect .x + rect .width / 2 , rect .y + rect .height / 2 ,
192- rect .x + rect .width - MARGIN , rect .y + rect .height - MARGIN );
193- robot .waitForIdle ();
194- }
230+ Rectangle rect = config .getBounds ();
231+ Insets insets = Toolkit .getDefaultToolkit ().getScreenInsets (config );
232+ adjustInsets (rect , insets );
233+
234+ // Upper left
235+ glide (rect .x + rect .width / 2 , rect .y + rect .height / 2 ,
236+ rect .x + MARGIN , rect .y + MARGIN );
237+ robot .waitForIdle ();
238+
239+ // Lower left
240+ glide (rect .x + rect .width / 2 , rect .y + rect .height / 2 ,
241+ rect .x + MARGIN , rect .y + rect .height - MARGIN );
242+ robot .waitForIdle ();
243+
244+ // Upper right
245+ glide (rect .x + rect .width / 2 , rect .y + rect .height / 2 ,
246+ rect .x + rect .width - MARGIN , rect .y + MARGIN );
247+ robot .waitForIdle ();
248+
249+ // Lower right
250+ glide (rect .x + rect .width / 2 , rect .y + rect .height / 2 ,
251+ rect .x + rect .width - MARGIN , rect .y + rect .height - MARGIN );
252+
253+ robot .waitForIdle ();
195254 }
196255 }
197256
0 commit comments