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