1 /*
   2  * Copyright (c) 2013, 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 4173714
  27   @summary java.awt.button behaves differently under Win32/Solaris
  28   @author tdv@sparc.spb.su
  29   @library ../../../regtesthelpers
  30   @build Util
  31   @run main DisabledComponentsTest
  32 */
  33 
  34 /**
  35  * DisabledComponentsTest.java
  36  *
  37  * summary: java.awt.button behaves differently under Win32/Solaris
  38  * Disabled component should not receive events. This is what this
  39  * test checks out.
  40  */
  41 
  42 import java.awt.*;
  43 import java.awt.event.*;
  44 import java.util.concurrent.atomic.AtomicBoolean;
  45 
  46 import test.java.awt.regtesthelpers.Util;
  47 
  48 import javax.swing.*;
  49 
  50 public class DisabledComponentsTest {
  51 
  52     private static Frame frame;
  53     private static Button b = new Button("Button");
  54     private static final AtomicBoolean pressed = new AtomicBoolean(false);
  55     private static final AtomicBoolean entered = new AtomicBoolean(false);
  56 
  57     private static void init() {
  58         frame = new Frame("Test");
  59         frame.setBounds(100, 100, 100, 100);
  60         b = new Button("Test");
  61         b.setEnabled(false);
  62         b.addMouseListener(new MouseAdapter() {
  63             public void mousePressed(MouseEvent e) {
  64                 System.err.println("Mouse pressed. target=" + e.getSource());
  65                 if (!b.isEnabled()) {
  66                     System.err.println("TEST FAILED: BUTTON RECEIVED AN EVENT WHEN DISABLED!");
  67                     pressed.set(true);
  68                 }
  69             }
  70             public void mouseEntered(MouseEvent e) {
  71                 System.out.println("Mouse entered. target=" + e.getSource());
  72                 if (!b.isEnabled())
  73                     System.err.println("TEST FAILED: BUTTON RECEIVED AN EVENT WHEN DISABLED!");
  74                 entered.set(true);
  75             }
  76         });
  77         frame.add(b);
  78         frame.setVisible(true);
  79     }
  80 
  81     public static void main(String[] args) throws Exception {
  82         try {
  83             Robot r = Util.createRobot();
  84             r.setAutoDelay(200);
  85             r.setAutoWaitForIdle(true);
  86             r.mouseMove(0, 0);
  87             SwingUtilities.invokeAndWait(DisabledComponentsTest::init);
  88             Util.waitForIdle(r);
  89             Util.pointOnComp(b, r);
  90             if (entered.get()) {
  91                 throw new RuntimeException("TEST FAILED: disabled button received MouseEntered event");
  92             }
  93             Util.clickOnComp(b, r);
  94             if (pressed.get()) {
  95                 throw new RuntimeException("TEST FAILED: disabled button received MousePressed event");
  96             }
  97         } finally {
  98             if (frame != null) {
  99                 frame.dispose();
 100             }
 101         }
 102     }
 103 }