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