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