1 /* 2 * Copyright (c) 2012, 2015, 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 javax.swing.*; 35 import javax.swing.plaf.metal.MetalLookAndFeel; 36 import java.awt.*; 37 import java.awt.event.MouseEvent; 38 import java.lang.reflect.InvocationTargetException; 39 40 public class bug7123767 extends JFrame { 41 42 private static class TestFactory extends PopupFactory { 43 44 private static TestFactory newFactory = new TestFactory(); 45 private static PopupFactory oldFactory; 46 47 private TestFactory() { 48 super(); 49 } 50 51 public static void install() { 52 if (oldFactory == null) { 53 oldFactory = getSharedInstance(); 54 setSharedInstance(newFactory); 55 } 56 } 57 58 public static void uninstall() { 59 if (oldFactory != null) { 60 setSharedInstance(oldFactory); 61 } 62 } 63 64 // Actual test happens here 65 public Popup getPopup(Component owner, Component contents, int x, int y) { 66 GraphicsConfiguration mouseGC = testGC(MouseInfo.getPointerInfo().getLocation()); 67 if (mouseGC == null) { 68 throw new RuntimeException("Can't find GraphicsConfiguration that mouse pointer belongs to"); 69 } 70 71 GraphicsConfiguration tipGC = testGC(new Point(x, y)); 72 if (tipGC == null) { 73 throw new RuntimeException("Can't find GraphicsConfiguration that tip belongs to"); 74 } 75 76 if (!mouseGC.equals(tipGC)) { 77 throw new RuntimeException("Mouse and tip GCs are not equal"); 78 } 79 80 return super.getPopup(owner, contents, x, y); 81 } 82 83 private static GraphicsConfiguration testGC(Point pt) { 84 GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); 85 GraphicsDevice[] devices = environment.getScreenDevices(); 86 for (GraphicsDevice device : devices) { 87 GraphicsConfiguration[] configs = device.getConfigurations(); 88 for (GraphicsConfiguration config : configs) { 89 Rectangle rect = config.getBounds(); 90 Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config); 91 adjustInsets(rect, insets); 92 if (rect.contains(pt)) 93 return config; 94 } 95 } 96 97 return null; 98 } 99 } 100 101 private static final int MARGIN = 10; 102 private static bug7123767 frame; 103 private static Robot robot; 104 105 public static void main(String[] args) throws Exception { 106 UIManager.setLookAndFeel(new MetalLookAndFeel()); 107 setUp(); 108 testToolTip(); 109 TestFactory.uninstall(); 110 } 111 112 // Creates a window that is stretched across all available monitors 113 // and adds itself as ContainerListener to track tooltips drawing 114 private bug7123767() { 115 super(); 116 117 ToolTipManager.sharedInstance().setInitialDelay(0); 118 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 119 TestFactory.install(); 120 121 JLabel label1 = new JLabel("no preferred location"); 122 label1.setToolTipText("tip"); 123 add(label1, BorderLayout.WEST); 124 125 JLabel label2 = new JLabel("preferred location (20000, 20000)") { 126 public Point getToolTipLocation(MouseEvent event) { 127 return new Point(20000, 20000); 128 } 129 }; 130 131 label2.setToolTipText("tip"); 132 add(label2, BorderLayout.EAST); 133 134 setUndecorated(true); 135 pack(); 136 137 Rectangle rect = new Rectangle(); 138 GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); 139 GraphicsDevice[] devices = environment.getScreenDevices(); 140 for (GraphicsDevice device : devices) { 141 GraphicsConfiguration[] configs = device.getConfigurations(); 142 for (GraphicsConfiguration config : configs) { 143 Insets localInsets = Toolkit.getDefaultToolkit().getScreenInsets(config); 144 Rectangle localRect = config.getBounds(); 145 adjustInsets(localRect, localInsets); 146 rect.add(localRect); 147 } 148 } 149 setBounds(rect); 150 } 151 152 private static void setUp() throws InterruptedException, InvocationTargetException { 153 SwingUtilities.invokeAndWait(new Runnable() { 154 @Override 155 public void run() { 156 frame = new bug7123767(); 157 frame.setVisible(true); 158 } 159 }); 160 } 161 162 // Moves mouse pointer to the corners of every GraphicsConfiguration 163 private static void testToolTip() throws AWTException { 164 165 robot = new Robot(); 166 robot.setAutoDelay(20); 167 robot.waitForIdle(); 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.getDefaultToolkit().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 robot.waitForIdle(); 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 robot.waitForIdle(); 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 robot.waitForIdle(); 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 robot.waitForIdle(); 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 }