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 4846413
  28  * @summary Checks if No tooltip modification when no KeyStroke modifier
  29  * @library ../../regtesthelpers
  30  * @build Util
  31  * @author Konstantin Eremin
  32  * @run main bug4846413
  33  */
  34 import java.awt.Dimension;
  35 import java.awt.Point;
  36 import java.awt.Robot;
  37 import java.awt.Toolkit;
  38 import javax.swing.*;
  39 import java.awt.event.*;
  40 import javax.swing.plaf.metal.MetalToolTipUI;
  41 import sun.awt.SunToolkit;
  42 
  43 public class bug4846413 {
  44 
  45     private static volatile boolean isTooltipAdded;
  46     private static JButton button;
  47 
  48     public static void main(String[] args) throws Exception {
  49 
  50         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  51         Robot robot = new Robot();
  52         robot.setAutoDelay(50);
  53 
  54         UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  55 
  56         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
  57 
  58             public void run() {
  59                 createAndShowGUI();
  60             }
  61         });
  62 
  63         toolkit.realSync();
  64 
  65         Point movePoint = getButtonPoint();
  66         robot.mouseMove(movePoint.x, movePoint.y);
  67         toolkit.realSync();
  68 
  69         long timeout = System.currentTimeMillis() + 9000;
  70         while (!isTooltipAdded && (System.currentTimeMillis() < timeout)) {
  71             try {Thread.sleep(500);} catch (Exception e) {}
  72         }
  73 
  74         checkToolTip();
  75     }
  76 
  77     private static void checkToolTip() throws Exception {
  78         SwingUtilities.invokeAndWait(new Runnable() {
  79 
  80             @Override
  81             public void run() {
  82                 JToolTip tooltip = (JToolTip) Util.findSubComponent(
  83                         JFrame.getFrames()[0], "JToolTip");
  84 
  85                 if (tooltip == null) {
  86                     throw new RuntimeException("Tooltip has not been found!");
  87                 }
  88 
  89                 MetalToolTipUI tooltipUI = (MetalToolTipUI) MetalToolTipUI.createUI(tooltip);
  90                 tooltipUI.installUI(tooltip);
  91 
  92                 if (!"-Insert".equals(tooltipUI.getAcceleratorString())) {
  93                     throw new RuntimeException("Tooltip acceleration is not properly set!");
  94                 }
  95 
  96             }
  97         });
  98     }
  99 
 100     private static Point getButtonPoint() throws Exception {
 101         final Point[] result = new Point[1];
 102 
 103         SwingUtilities.invokeAndWait(new Runnable() {
 104 
 105             @Override
 106             public void run() {
 107                 Point p = button.getLocationOnScreen();
 108                 Dimension size = button.getSize();
 109                 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
 110             }
 111         });
 112         return result[0];
 113     }
 114 
 115     private static void createAndShowGUI() {
 116         JFrame frame = new JFrame("Test");
 117         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 118         frame.setSize(200, 200);
 119 
 120         button = new JButton("Press me");
 121         button.setToolTipText("test");
 122         button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
 123                 KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0, true), "someCommand");
 124         button.getActionMap().put("someCommand", null);
 125         frame.getContentPane().add(button);
 126 
 127         JLayeredPane layeredPane = (JLayeredPane) Util.findSubComponent(
 128                 frame, "JLayeredPane");
 129         layeredPane.addContainerListener(new ContainerAdapter() {
 130 
 131             @Override
 132             public void componentAdded(ContainerEvent e) {
 133                 isTooltipAdded = true;
 134             }
 135         });
 136 
 137         frame.setVisible(true);
 138     }
 139 }