1 /* 2 * Copyright (c) 2012, 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 /* 25 * @test 26 * @bug 7123767 27 * 28 * @summary Check if a tooltip location in Multi-Monitor 29 * configurations is correct. 30 * If the configurations number per device exceeds 5, 31 * then some 5 random configurations will be checked. 32 * Please Use -Dseed=X to set the random generator seed 33 * (if necessary). 34 * 35 * @author Vladislav Karnaukhov 36 * 37 * @key headful 38 * @key randomness 39 * 40 * @modules java.desktop/sun.awt 41 * @library /test/lib 42 * 43 * @run main/timeout=300 bug7123767 44 */ 45 46 import javax.swing.*; 47 import javax.swing.plaf.metal.MetalLookAndFeel; 48 import java.awt.*; 49 import java.awt.event.MouseEvent; 50 import java.lang.reflect.InvocationTargetException; 51 52 import java.util.List; 53 import java.util.ArrayList; 54 import java.util.Collections; 55 import java.util.Random; 56 57 import jdk.test.lib.RandomFactory; 58 59 60 public class bug7123767 extends JFrame { 61 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 96 private static class TestFactory extends PopupFactory { 97 98 private static TestFactory newFactory = new TestFactory(); 99 private static PopupFactory oldFactory; 100 101 private TestFactory() { 102 super(); 103 } 104 105 public static void install() { 106 if (oldFactory == null) { 107 oldFactory = getSharedInstance(); 108 setSharedInstance(newFactory); 109 } 110 } 111 112 public static void uninstall() { 113 if (oldFactory != null) { 114 setSharedInstance(oldFactory); 115 } 116 } 117 118 // Actual test happens here 119 @Override 120 public Popup getPopup(Component owner, Component contents, int x, int y) { 121 122 GraphicsConfiguration mouseGC = 123 testGC(MouseInfo.getPointerInfo().getLocation()); 124 125 if (mouseGC == null) { 126 throw new RuntimeException("Can't find GraphicsConfiguration " 127 + "that mouse pointer belongs to"); 128 } 129 130 GraphicsConfiguration tipGC = testGC(new Point(x, y)); 131 if (tipGC == null) { 132 throw new RuntimeException( 133 "Can't find GraphicsConfiguration that tip belongs to"); 134 } 135 136 if (!mouseGC.equals(tipGC)) { 137 throw new RuntimeException("Mouse and tip GCs are not equal"); 138 } 139 140 return super.getPopup(owner, contents, x, y); 141 } 142 143 private static GraphicsConfiguration testGC(Point pt) { 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; } 152 } 153 154 return null; 155 } 156 } 157 158 private static final int MARGIN = 10; 159 private static bug7123767 frame; 160 private static Robot robot; 161 162 public static void main(String[] args) throws Exception { 163 164 UIManager.setLookAndFeel(new MetalLookAndFeel()); 165 setUp(); 166 testToolTip(); 167 TestFactory.uninstall(); 168 if (frame != null) { frame.dispose(); } 169 } 170 171 // Creates a window that is stretched across all available monitors 172 // and adds itself as ContainerListener to track tooltips drawing 173 private bug7123767() { 174 175 super(); 176 177 ToolTipManager.sharedInstance().setInitialDelay(0); 178 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 179 TestFactory.install(); 180 181 JLabel label1 = new JLabel("no preferred location"); 182 label1.setToolTipText("tip"); 183 add(label1, BorderLayout.WEST); 184 185 JLabel label2 = new JLabel("preferred location (20000, 20000)") { 186 public Point getToolTipLocation(MouseEvent event) { 187 return new Point(20000, 20000); 188 } 189 }; 190 191 label2.setToolTipText("tip"); 192 add(label2, BorderLayout.EAST); 193 194 setUndecorated(true); 195 pack(); 196 197 Rectangle rect = new Rectangle(); 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); 206 } 207 208 setBounds(rect); 209 } 210 211 private static void setUp() throws InterruptedException, InvocationTargetException { 212 SwingUtilities.invokeAndWait(new Runnable() { 213 @Override 214 public void run() { 215 frame = new bug7123767(); 216 frame.setVisible(true); 217 } 218 }); 219 } 220 221 // Moves mouse pointer to the corners of every GraphicsConfiguration 222 private static void testToolTip() throws AWTException { 223 224 robot = new Robot(); 225 robot.setAutoDelay(20); 226 robot.waitForIdle(); 227 228 for (GraphicsConfiguration config: CONFIGS) { 229 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(); 254 } 255 } 256 257 private static void glide(int x0, int y0, int x1, int y1) throws AWTException { 258 if (robot == null) { 259 robot = new Robot(); 260 robot.setAutoDelay(20); 261 } 262 263 float dmax = (float) Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0)); 264 float dx = (x1 - x0) / dmax; 265 float dy = (y1 - y0) / dmax; 266 267 robot.mouseMove(x0, y0); 268 for (int i = 1; i <= dmax; i += 10) { 269 robot.mouseMove((int) (x0 + dx * i), (int) (y0 + dy * i)); 270 } 271 } 272 273 private static void adjustInsets(Rectangle rect, final Insets insets) { 274 rect.x += insets.left; 275 rect.y += insets.top; 276 rect.width -= (insets.left + insets.right); 277 rect.height -= (insets.top + insets.bottom); 278 } 279 }