< prev index next >

test/javax/swing/ToolTipManager/7123767/bug7123767.java

Print this page


   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) {
   1 /*
   2  * Copyright (c) 2012, 2016, 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  * @bug      7123767
  27  *
  28  * @summary  Check if a tooltip location in Multi-Monitor
  29  *           configurations is correct.
  30  *           If the configurations number per device exceeds 5,
  31  *           then some 5 random configurations will be checked.
  32  *           Please Use -Dseed=X to set the random generator seed
  33  *           (if necessary).
  34  *
  35  * @author   Vladislav Karnaukhov
  36  *
  37  * @key      headful
  38  * @key      randomness
  39  *
  40  * @modules  java.desktop/sun.awt
  41  * @library  /lib/testlibrary/
  42  * @build    jdk.testlibrary.*
  43  *
  44  * @run      main/timeout=300 bug7123767
  45  */
  46 
  47 import javax.swing.*;
  48 import javax.swing.plaf.metal.MetalLookAndFeel;
  49 import java.awt.*;
  50 import java.awt.event.MouseEvent;
  51 import java.lang.reflect.InvocationTargetException;
  52 
  53 import java.util.List;
  54 import java.util.ArrayList;
  55 import java.util.Collections;
  56 import java.util.Random;
  57 
  58 import jdk.testlibrary.RandomFactory;
  59 
  60 
  61 public class bug7123767 extends JFrame {
  62 
  63     // maximum number of GraphicsConfigurations checked per GraphicsDevice
  64     private static final int MAX_N_CONFIGS = 5;
  65     private static final List<GraphicsConfiguration> CONFIGS = getConfigs();
  66 
  67     private static List<GraphicsConfiguration> getConfigs() {
  68 
  69         Random rnd = RandomFactory.getRandom();
  70 
  71         List<GraphicsConfiguration> configs = new ArrayList<>();
  72 
  73         GraphicsEnvironment ge =
  74                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  75         GraphicsDevice[] devices = ge.getScreenDevices();
  76 
  77         for (GraphicsDevice device : devices) {
  78             GraphicsConfiguration[] allConfigs = device.getConfigurations();
  79             int nConfigs = allConfigs.length;
  80             if (nConfigs <= MAX_N_CONFIGS) {
  81                 Collections.addAll(configs, allConfigs);
  82             } else { // see JDK-8159454
  83                 System.out.println("check only " + MAX_N_CONFIGS +
  84                     " configurations for device " + device);
  85                 for (int j = 0; j < MAX_N_CONFIGS; j++) {
  86                     int k = rnd.nextInt(nConfigs);
  87                     configs.add(allConfigs[k]);
  88                 }
  89             }
  90         }
  91 
  92         return configs;
  93     }
  94 
  95 
  96     private static class TestFactory extends PopupFactory {
  97 
  98         private static TestFactory newFactory = new TestFactory();
  99         private static PopupFactory oldFactory;
 100 
 101         private TestFactory() {
 102             super();
 103         }
 104 
 105         public static void install() {
 106             if (oldFactory == null) {
 107                 oldFactory = getSharedInstance();
 108                 setSharedInstance(newFactory);
 109             }
 110         }
 111 
 112         public static void uninstall() {
 113             if (oldFactory != null) {
 114                 setSharedInstance(oldFactory);
 115             }
 116         }
 117 
 118         // Actual test happens here
 119         @Override
 120         public Popup getPopup(Component owner, Component contents, int x, int y) {
 121 
 122             GraphicsConfiguration mouseGC =
 123                 testGC(MouseInfo.getPointerInfo().getLocation());
 124 
 125             if (mouseGC == null) {
 126                 throw new RuntimeException("Can't find GraphicsConfiguration "
 127                         + "that mouse pointer belongs to");
 128             }
 129 
 130             GraphicsConfiguration tipGC = testGC(new Point(x, y));
 131             if (tipGC == null) {
 132                 throw new RuntimeException(
 133                         "Can't find GraphicsConfiguration that tip belongs to");
 134             }
 135 
 136             if (!mouseGC.equals(tipGC)) {
 137                 throw new RuntimeException("Mouse and tip GCs are not equal");
 138             }
 139 
 140             return super.getPopup(owner, contents, x, y);
 141         }
 142 
 143         private static GraphicsConfiguration testGC(Point pt) {
 144 
 145             for (GraphicsConfiguration config: CONFIGS) {
 146 


 147                 Rectangle rect = config.getBounds();
 148                 Insets insets =
 149                     Toolkit.getDefaultToolkit().getScreenInsets(config);
 150                 adjustInsets(rect, insets);
 151                 if (rect.contains(pt)) { return config; }


 152             }
 153 
 154             return null;
 155         }
 156     }
 157 
 158     private static final int MARGIN = 10;
 159     private static bug7123767 frame;
 160     private static Robot robot;
 161 
 162     public static void main(String[] args) throws Exception {
 163 
 164         UIManager.setLookAndFeel(new MetalLookAndFeel());
 165         setUp();
 166         testToolTip();
 167         TestFactory.uninstall();
 168         if (frame != null) { frame.dispose(); }
 169     }
 170 
 171     // Creates a window that is stretched across all available monitors
 172     // and adds itself as ContainerListener to track tooltips drawing
 173     private bug7123767() {
 174 
 175         super();
 176 
 177         ToolTipManager.sharedInstance().setInitialDelay(0);
 178         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 179         TestFactory.install();
 180 
 181         JLabel label1 = new JLabel("no preferred location");
 182         label1.setToolTipText("tip");
 183         add(label1, BorderLayout.WEST);
 184 
 185         JLabel label2 = new JLabel("preferred location (20000, 20000)") {
 186             public Point getToolTipLocation(MouseEvent event) {
 187                 return new Point(20000, 20000);
 188             }
 189         };
 190 
 191         label2.setToolTipText("tip");
 192         add(label2, BorderLayout.EAST);
 193 
 194         setUndecorated(true);
 195         pack();
 196 
 197         Rectangle rect = new Rectangle();
 198 
 199         for (GraphicsConfiguration config: CONFIGS) {
 200 
 201             Insets localInsets =
 202                 Toolkit.getDefaultToolkit().getScreenInsets(config);

 203             Rectangle localRect = config.getBounds();
 204             adjustInsets(localRect, localInsets);
 205             rect.add(localRect);
 206         }
 207 
 208         setBounds(rect);
 209     }
 210 
 211     private static void setUp() throws InterruptedException, InvocationTargetException {
 212         SwingUtilities.invokeAndWait(new Runnable() {
 213             @Override
 214             public void run() {
 215                 frame = new bug7123767();
 216                 frame.setVisible(true);
 217             }
 218         });
 219     }
 220 
 221     // Moves mouse pointer to the corners of every GraphicsConfiguration
 222     private static void testToolTip() throws AWTException {
 223 
 224         robot = new Robot();
 225         robot.setAutoDelay(20);
 226         robot.waitForIdle();
 227 
 228         for (GraphicsConfiguration config: CONFIGS) {
 229 



 230             Rectangle rect = config.getBounds();
 231             Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);
 232             adjustInsets(rect, insets);
 233 
 234             // Upper left
 235             glide(rect.x + rect.width / 2, rect.y + rect.height / 2,
 236                     rect.x + MARGIN, rect.y + MARGIN);
 237             robot.waitForIdle();
 238 
 239             // Lower left
 240             glide(rect.x + rect.width / 2, rect.y + rect.height / 2,
 241                     rect.x + MARGIN, rect.y + rect.height - MARGIN);
 242             robot.waitForIdle();
 243 
 244             // Upper right
 245             glide(rect.x + rect.width / 2, rect.y + rect.height / 2,
 246                     rect.x + rect.width - MARGIN, rect.y + MARGIN);
 247             robot.waitForIdle();
 248 
 249             // Lower right
 250             glide(rect.x + rect.width / 2, rect.y + rect.height / 2,
 251                     rect.x + rect.width - MARGIN, rect.y + rect.height - MARGIN);
 252 
 253             robot.waitForIdle();

 254         }
 255     }
 256 
 257     private static void glide(int x0, int y0, int x1, int y1) throws AWTException {
 258         if (robot == null) {
 259             robot = new Robot();
 260             robot.setAutoDelay(20);
 261         }
 262 
 263         float dmax = (float) Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0));
 264         float dx = (x1 - x0) / dmax;
 265         float dy = (y1 - y0) / dmax;
 266 
 267         robot.mouseMove(x0, y0);
 268         for (int i = 1; i <= dmax; i += 10) {
 269             robot.mouseMove((int) (x0 + dx * i), (int) (y0 + dy * i));
 270         }
 271     }
 272 
 273     private static void adjustInsets(Rectangle rect, final Insets insets) {
< prev index next >