1 /* 2 * Copyright (c) 2012, 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 /** 26 * @test 27 * @bug 7123767 28 * 29 * @summary Check if a tooltip location in Multi-Monitor 30 * configurations is correct. 31 * If the configurations number per device exceeds 5, 32 * then some 5 random configurations will be checked. 33 * Please Use -Dseed=X to set the random generator seed 34 * (if necessary). 35 * 36 * @author Vladislav Karnaukhov 37 * 38 * @key headful randomness 39 * 40 * @modules java.desktop/sun.awt 41 * @library /test/lib 42 * 43 * @run main/timeout=300 bug7123767 44 */ 45 46 import sun.awt.SunToolkit; 47 48 import javax.swing.*; 49 import javax.swing.plaf.metal.MetalLookAndFeel; 50 import java.awt.*; 51 import java.awt.event.MouseEvent; 52 import java.lang.reflect.InvocationTargetException; 53 54 public class bug7123767 extends JFrame { 55 56 private static class TestFactory extends PopupFactory { 57 58 private static TestFactory newFactory = new TestFactory(); 59 private static PopupFactory oldFactory; 60 61 private TestFactory() { 62 super(); 63 } 64 65 public static void install() { 66 if (oldFactory == null) { 67 oldFactory = getSharedInstance(); 68 setSharedInstance(newFactory); 69 } 70 } 71 72 public static void uninstall() { 73 if (oldFactory != null) { 74 setSharedInstance(oldFactory); 75 } 76 } 77 78 // Actual test happens here 79 public Popup getPopup(Component owner, Component contents, int x, int y) { 80 GraphicsConfiguration mouseGC = testGC(MouseInfo.getPointerInfo().getLocation()); 81 if (mouseGC == null) { 82 throw new RuntimeException("Can't find GraphicsConfiguration that mouse pointer belongs to"); 83 } 84 85 GraphicsConfiguration tipGC = testGC(new Point(x, y)); 86 if (tipGC == null) { 87 throw new RuntimeException("Can't find GraphicsConfiguration that tip belongs to"); 88 } 89 90 if (!mouseGC.equals(tipGC)) { 91 throw new RuntimeException("Mouse and tip GCs are not equal"); 92 } 93 94 return super.getPopup(owner, contents, x, y); 95 } 96 97 private static GraphicsConfiguration testGC(Point pt) { 98 GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); 99 GraphicsDevice[] devices = environment.getScreenDevices(); 100 for (GraphicsDevice device : devices) { 101 GraphicsConfiguration[] configs = device.getConfigurations(); 102 for (GraphicsConfiguration config : configs) { 103 Rectangle rect = config.getBounds(); 104 Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config); 105 adjustInsets(rect, insets); 106 if (rect.contains(pt)) 107 return config; 108 } 109 } 110 111 return null; 112 } 113 } 114 115 private static final int MARGIN = 10; 116 private static bug7123767 frame; 117 private static Robot robot; 118 119 public static void main(String[] args) throws Exception { 120 UIManager.setLookAndFeel(new MetalLookAndFeel()); 121 setUp(); 122 testToolTip(); 123 TestFactory.uninstall(); 124 } 125 126 // Creates a window that is stretched across all available monitors 127 // and adds itself as ContainerListener to track tooltips drawing 128 private bug7123767() { 129 super(); 130 131 ToolTipManager.sharedInstance().setInitialDelay(0); 132 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 133 TestFactory.install(); 134 135 JLabel label1 = new JLabel("no preferred location"); 136 label1.setToolTipText("tip"); 137 add(label1, BorderLayout.WEST); 138 139 JLabel label2 = new JLabel("preferred location (20000, 20000)") { 140 public Point getToolTipLocation(MouseEvent event) { 141 return new Point(20000, 20000); 142 } 143 }; 144 145 label2.setToolTipText("tip"); 146 add(label2, BorderLayout.EAST); 147 148 setUndecorated(true); 149 pack(); 150 151 Rectangle rect = new Rectangle(); 152 GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); 153 GraphicsDevice[] devices = environment.getScreenDevices(); 154 for (GraphicsDevice device : devices) { 155 GraphicsConfiguration[] configs = device.getConfigurations(); 156 for (GraphicsConfiguration config : configs) { 157 Insets localInsets = Toolkit.getDefaultToolkit().getScreenInsets(config); 158 Rectangle localRect = config.getBounds(); 159 adjustInsets(localRect, localInsets); 160 rect.add(localRect); 161 } 162 } 163 setBounds(rect); 164 } 165 166 private static void setUp() throws InterruptedException, InvocationTargetException { 167 SwingUtilities.invokeAndWait(new Runnable() { 168 @Override 169 public void run() { 170 frame = new bug7123767(); 171 frame.setVisible(true); 172 } 173 }); 174 } 175 176 // Moves mouse pointer to the corners of every GraphicsConfiguration 177 private static void testToolTip() throws AWTException { 178 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 179 toolkit.realSync(); 180 181 GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); 182 GraphicsDevice[] devices = environment.getScreenDevices(); 183 for (GraphicsDevice device : devices) { 184 GraphicsConfiguration[] configs = device.getConfigurations(); 185 for (GraphicsConfiguration config : configs) { 186 Rectangle rect = config.getBounds(); 187 Insets insets = toolkit.getScreenInsets(config); 188 adjustInsets(rect, insets); 189 190 // Upper left 191 glide(rect.x + rect.width / 2, rect.y + rect.height / 2, 192 rect.x + MARGIN, rect.y + MARGIN); 193 toolkit.realSync(); 194 195 // Lower left 196 glide(rect.x + rect.width / 2, rect.y + rect.height / 2, 197 rect.x + MARGIN, rect.y + rect.height - MARGIN); 198 toolkit.realSync(); 199 200 // Upper right 201 glide(rect.x + rect.width / 2, rect.y + rect.height / 2, 202 rect.x + rect.width - MARGIN, rect.y + MARGIN); 203 toolkit.realSync(); 204 205 // Lower right 206 glide(rect.x + rect.width / 2, rect.y + rect.height / 2, 207 rect.x + rect.width - MARGIN, rect.y + rect.height - MARGIN); 208 toolkit.realSync(); 209 } 210 } 211 } 212 213 private static void glide(int x0, int y0, int x1, int y1) throws AWTException { 214 if (robot == null) { 215 robot = new Robot(); 216 robot.setAutoDelay(20); 217 } 218 219 float dmax = (float) Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0)); 220 float dx = (x1 - x0) / dmax; 221 float dy = (y1 - y0) / dmax; 222 223 robot.mouseMove(x0, y0); 224 for (int i = 1; i <= dmax; i += 10) { 225 robot.mouseMove((int) (x0 + dx * i), (int) (y0 + dy * i)); 226 } 227 } 228 229 private static void adjustInsets(Rectangle rect, final Insets insets) { 230 rect.x += insets.left; 231 rect.y += insets.top; 232 rect.width -= (insets.left + insets.right); 233 rect.height -= (insets.top + insets.bottom); 234 } 235 }