1/* 2 * Copyright (c) 2012, 2016, 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 /lib/testlibrary/ 42 * @build jdk.testlibrary.* 43 * 44 * @run main/timeout=300 bug7123767 45 */ 46 47import javax.swing.*; 48import javax.swing.plaf.metal.MetalLookAndFeel; 49import java.awt.*; 50import java.awt.event.MouseEvent; 51import java.lang.reflect.InvocationTargetException; 52 53import java.util.List; 54import java.util.ArrayList; 55import java.util.Collections; 56import java.util.Random; 57 58import jdk.testlibrary.RandomFactory; 59 60 61public class bug7123767 extends JFrame { 62 63 // maximum number of GraphicsConfigurations checked per GraphicsDevice 64 private static final int MAX_N_CONFIGS = 5; 65 private static final List<GraphicsConfiguration> CONFIGS = getConfigs(); 66 67 private static List<GraphicsConfiguration> getConfigs() { 68 69 Random rnd = RandomFactory.getRandom(); 70 71 List<GraphicsConfiguration> configs = new ArrayList<>(); 72 73 GraphicsEnvironment ge = 74 GraphicsEnvironment.getLocalGraphicsEnvironment(); 75 GraphicsDevice[] devices = ge.getScreenDevices(); 76 77 for (GraphicsDevice device : devices) { 78 GraphicsConfiguration[] allConfigs = device.getConfigurations(); 79 int nConfigs = allConfigs.length; 80 if (nConfigs <= MAX_N_CONFIGS) { 81 Collections.addAll(configs, allConfigs); 82 } else { // see JDK-8159454 83 System.out.println("check only " + MAX_N_CONFIGS + 84 " configurations for device " + device); 85 configs.add(device.getDefaultConfiguration()); // check default 86 for (int j = 0; j < MAX_N_CONFIGS - 1; j++) { 87 int k = rnd.nextInt(nConfigs); 88 configs.add(allConfigs[k]); 89 } 90 } 91 } 92 93 return configs; 94 } 95 96 97 private static class TestFactory extends PopupFactory { 98 99 private static TestFactory newFactory = new TestFactory(); 100 private static PopupFactory oldFactory; 101 102 private TestFactory() { 103 super(); 104 } 105 106 public static void install() { 107 if (oldFactory == null) { 108 oldFactory = getSharedInstance(); 109 setSharedInstance(newFactory); 110 } 111 } 112 113 public static void uninstall() { 114 if (oldFactory != null) { 115 setSharedInstance(oldFactory); 116 } 117 } 118 119 // Actual test happens here 120 @Override 121 public Popup getPopup(Component owner, Component contents, int x, int y) { 122 123 GraphicsConfiguration mouseGC = 124 testGC(MouseInfo.getPointerInfo().getLocation()); 125 126 if (mouseGC == null) { 127 throw new RuntimeException("Can't find GraphicsConfiguration " 128 + "that mouse pointer belongs to"); 129 } 130 131 GraphicsConfiguration tipGC = testGC(new Point(x, y)); 132 if (tipGC == null) { 133 throw new RuntimeException( 134 "Can't find GraphicsConfiguration that tip belongs to"); 135 } 136 137 if (!mouseGC.equals(tipGC)) { 138 throw new RuntimeException("Mouse and tip GCs are not equal"); 139 } 140 141 return super.getPopup(owner, contents, x, y); 142 } 143 144 private static GraphicsConfiguration testGC(Point pt) { 145 146 for (GraphicsConfiguration config: CONFIGS) { 147 148 Rectangle rect = config.getBounds(); 149 Insets insets = 150 Toolkit.getDefaultToolkit().getScreenInsets(config); 151 adjustInsets(rect, insets); 152 if (rect.contains(pt)) { return config; } 153 } 154 155 return null; 156 } 157 } 158 159 private static final int MARGIN = 10; 160 private static bug7123767 frame; 161 private static Robot robot; 162 163 public static void main(String[] args) throws Exception { 164 165 UIManager.setLookAndFeel(new MetalLookAndFeel()); 166 setUp(); 167 testToolTip(); 168 TestFactory.uninstall(); 169 if (frame != null) { frame.dispose(); } 170 } 171 172 // Creates a window that is stretched across all available monitors 173 // and adds itself as ContainerListener to track tooltips drawing 174 private bug7123767() { 175 176 super(); 177 178 ToolTipManager.sharedInstance().setInitialDelay(0); 179 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 180 TestFactory.install(); 181 182 JLabel label1 = new JLabel("no preferred location"); 183 label1.setToolTipText("tip"); 184 add(label1, BorderLayout.WEST); 185 186 JLabel label2 = new JLabel("preferred location (20000, 20000)") { 187 public Point getToolTipLocation(MouseEvent event) { 188 return new Point(20000, 20000); 189 } 190 }; 191 192 label2.setToolTipText("tip"); 193 add(label2, BorderLayout.EAST); 194 195 setUndecorated(true); 196 pack(); 197 198 Rectangle rect = new Rectangle(); 199 200 for (GraphicsConfiguration config: CONFIGS) { 201 202 Insets localInsets = 203 Toolkit.getDefaultToolkit().getScreenInsets(config); 204 Rectangle localRect = config.getBounds(); 205 adjustInsets(localRect, localInsets); 206 rect.add(localRect); 207 } 208 209 setBounds(rect); 210 } 211 212 private static void setUp() throws InterruptedException, InvocationTargetException { 213 SwingUtilities.invokeAndWait(new Runnable() { 214 @Override 215 public void run() { 216 frame = new bug7123767(); 217 frame.setVisible(true); 218 } 219 }); 220 } 221 222 // Moves mouse pointer to the corners of every GraphicsConfiguration 223 private static void testToolTip() throws AWTException { 224 225 robot = new Robot(); 226 robot.setAutoDelay(20); 227 robot.waitForIdle(); 228 229 for (GraphicsConfiguration config: CONFIGS) { 230 231 Rectangle rect = config.getBounds(); 232 Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config); 233 adjustInsets(rect, insets); 234 235 // Upper left 236 glide(rect.x + rect.width / 2, rect.y + rect.height / 2, 237 rect.x + MARGIN, rect.y + MARGIN); 238 robot.waitForIdle(); 239 240 // Lower left 241 glide(rect.x + rect.width / 2, rect.y + rect.height / 2, 242 rect.x + MARGIN, rect.y + rect.height - MARGIN); 243 robot.waitForIdle(); 244 245 // Upper right 246 glide(rect.x + rect.width / 2, rect.y + rect.height / 2, 247 rect.x + rect.width - MARGIN, rect.y + MARGIN); 248 robot.waitForIdle(); 249 250 // Lower right 251 glide(rect.x + rect.width / 2, rect.y + rect.height / 2, 252 rect.x + rect.width - MARGIN, rect.y + rect.height - MARGIN); 253 254 robot.waitForIdle(); 255 } 256 } 257 258 private static void glide(int x0, int y0, int x1, int y1) throws AWTException { 259 if (robot == null) { 260 robot = new Robot(); 261 robot.setAutoDelay(20); 262 } 263 264 float dmax = (float) Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0)); 265 float dx = (x1 - x0) / dmax; 266 float dy = (y1 - y0) / dmax; 267 268 robot.mouseMove(x0, y0); 269 for (int i = 1; i <= dmax; i += 10) { 270 robot.mouseMove((int) (x0 + dx * i), (int) (y0 + dy * i)); 271 } 272 } 273 274 private static void adjustInsets(Rectangle rect, final Insets insets) { 275 rect.x += insets.left; 276 rect.y += insets.top; 277 rect.width -= (insets.left + insets.right); 278 rect.height -= (insets.top + insets.bottom); 279 } 280} 281