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 4247996 4260485
28 * @summary Test that rollover toolbar doesn't corrupt buttons
29 * @author Peter Zhelezniakov
30 * @run main bug4247996
31 */
32import java.awt.*;
33import javax.swing.*;
34
35public class bug4247996 {
36
37    private static JButton button;
38    private static JToggleButton toogleButton;
39
40    public static void main(String[] args) throws Exception {
41
42        Robot robot = new Robot();
43        robot.setAutoDelay(50);
44
45        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
46
47        javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
48
49            public void run() {
50                createAndShowGUI();
51            }
52        });
53
54        robot.waitForIdle();
55
56        Point point = getButtonCenter();
57        robot.mouseMove(point.x, point.y);
58        robot.waitForIdle();
59
60        checkButtonsSize();
61
62    }
63
64    private static void checkButtonsSize() throws Exception {
65        SwingUtilities.invokeAndWait(new Runnable() {
66
67            @Override
68            public void run() {
69                if (!button.getSize().equals(toogleButton.getSize())) {
70                    throw new RuntimeException("Button sizes are different!");
71                }
72            }
73        });
74    }
75
76    private static Point getButtonCenter() throws Exception {
77        final Point[] result = new Point[1];
78
79        SwingUtilities.invokeAndWait(new Runnable() {
80
81            @Override
82            public void run() {
83                Point p = button.getLocationOnScreen();
84                Dimension size = button.getSize();
85                result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
86            }
87        });
88        return result[0];
89    }
90
91    private static void createAndShowGUI() {
92        JFrame frame = new JFrame("Test");
93        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
94        frame.setSize(200, 200);
95
96        JButton rButton = new JButton("Rollover");
97        rButton.setRolloverEnabled(true);
98        JToolBar nrToolbar = new JToolBar();
99        nrToolbar.add(rButton);
100        nrToolbar.remove(rButton);
101
102        if (!rButton.isRolloverEnabled()) {
103            throw new Error("Failed (bug 4260485): "
104                    + "toolbar overrode button's rollover property");
105        }
106
107        JToolBar rToolbar = new JToolBar();
108        rToolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
109        rToolbar.add(button = new JButton("Test"));
110        rToolbar.add(toogleButton = new JToggleButton("Test"));
111
112        frame.getContentPane().add(rToolbar, BorderLayout.NORTH);
113        frame.setVisible(true);
114    }
115}
116