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