Path: blob/master/test/jdk/javax/swing/JToolBar/4247996/bug4247996.java
41154 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 4247996 426048527* @summary Test that rollover toolbar doesn't corrupt buttons28* @author Peter Zhelezniakov29* @run main bug424799630*/31import java.awt.*;32import javax.swing.*;3334public class bug4247996 {3536private static JButton button;37private static JToggleButton toogleButton;3839public static void main(String[] args) throws Exception {4041Robot robot = new Robot();42robot.setAutoDelay(50);4344UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");4546javax.swing.SwingUtilities.invokeAndWait(new Runnable() {4748public void run() {49createAndShowGUI();50}51});5253robot.waitForIdle();5455Point point = getButtonCenter();56robot.mouseMove(point.x, point.y);57robot.waitForIdle();5859checkButtonsSize();6061}6263private static void checkButtonsSize() throws Exception {64SwingUtilities.invokeAndWait(new Runnable() {6566@Override67public void run() {68if (!button.getSize().equals(toogleButton.getSize())) {69throw new RuntimeException("Button sizes are different!");70}71}72});73}7475private static Point getButtonCenter() throws Exception {76final Point[] result = new Point[1];7778SwingUtilities.invokeAndWait(new Runnable() {7980@Override81public void run() {82Point p = button.getLocationOnScreen();83Dimension size = button.getSize();84result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);85}86});87return result[0];88}8990private static void createAndShowGUI() {91JFrame frame = new JFrame("Test");92frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);93frame.setSize(200, 200);9495JButton rButton = new JButton("Rollover");96rButton.setRolloverEnabled(true);97JToolBar nrToolbar = new JToolBar();98nrToolbar.add(rButton);99nrToolbar.remove(rButton);100101if (!rButton.isRolloverEnabled()) {102throw new Error("Failed (bug 4260485): "103+ "toolbar overrode button's rollover property");104}105106JToolBar rToolbar = new JToolBar();107rToolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);108rToolbar.add(button = new JButton("Test"));109rToolbar.add(toogleButton = new JToggleButton("Test"));110111frame.getContentPane().add(rToolbar, BorderLayout.NORTH);112frame.setVisible(true);113}114}115116117