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