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