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