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 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 Robot robot = new Robot(); 50 robot.setAutoDelay(50); 51 52 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 53 54 javax.swing.SwingUtilities.invokeAndWait(new Runnable() { 55 56 public void run() { 57 createAndShowGUI(); 58 } 59 }); 60 61 robot.waitForIdle(); 62 63 Point movePoint = getButtonPoint(); 64 robot.mouseMove(movePoint.x, movePoint.y); 65 robot.waitForIdle(); 66 67 long timeout = System.currentTimeMillis() + 9000; 68 while (!isTooltipAdded && (System.currentTimeMillis() < timeout)) { 69 try {Thread.sleep(500);} catch (Exception e) {} 70 } 71 72 checkToolTip(); 73 } 74 75 private static void checkToolTip() throws Exception { 76 SwingUtilities.invokeAndWait(new Runnable() { 77 78 @Override 79 public void run() { 80 JToolTip tooltip = (JToolTip) Util.findSubComponent( 81 JFrame.getFrames()[0], "JToolTip"); 82 83 if (tooltip == null) { 84 throw new RuntimeException("Tooltip has not been found!"); 85 } 86 87 MetalToolTipUI tooltipUI = (MetalToolTipUI) MetalToolTipUI.createUI(tooltip); 88 tooltipUI.installUI(tooltip); 89 90 if (!"-Insert".equals(tooltipUI.getAcceleratorString())) { 91 throw new RuntimeException("Tooltip acceleration is not properly set!"); 92 } 93 94 } 95 }); 96 } 97 98 private static Point getButtonPoint() throws Exception { 99 final Point[] result = new Point[1]; 100 101 SwingUtilities.invokeAndWait(new Runnable() { 102 103 @Override 104 public void run() { 105 Point p = button.getLocationOnScreen(); 106 Dimension size = button.getSize(); 107 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2); 108 } 109 }); 110 return result[0]; 111 } 112 113 private static void createAndShowGUI() { 114 JFrame frame = new JFrame("Test"); 115 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 116 frame.setSize(200, 200); 117 118 button = new JButton("Press me"); 119 button.setToolTipText("test"); 120 button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( 121 KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0, true), "someCommand"); 122 button.getActionMap().put("someCommand", null); 123 frame.getContentPane().add(button); 124 125 JLayeredPane layeredPane = (JLayeredPane) Util.findSubComponent( 126 frame, "JLayeredPane"); 127 layeredPane.addContainerListener(new ContainerAdapter() { 128 129 @Override 130 public void componentAdded(ContainerEvent e) { 131 isTooltipAdded = true; 132 } 133 }); 134 135 frame.setVisible(true); 136 } 137 }