1 /*
   2  * Copyright (c) 2011, 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  * @summary <rdar://problem/4475972> [JavaJDK16] Hidden windows still receive mouse events
  27  * @summary com.apple.junit.java.awt.Window
  28  * @library ../../regtesthelpers
  29  * @build RobotUtilities VisibilityValidator Waypoint
  30  * @run junit HiddenWindowSentEvents
  31  */
  32 
  33 import test.java.awt.regtesthelpers.RobotUtilities;
  34 import test.java.awt.regtesthelpers.VisibilityValidator;
  35 import test.java.awt.regtesthelpers.Waypoint;
  36 import junit.framework.*;
  37 import java.awt.*;
  38 import java.awt.event.*;
  39 
  40 public class HiddenWindowSentEvents extends TestCase {
  41     private volatile boolean hiddenMouseEntered = false;
  42     private Waypoint waypoint = new Waypoint();
  43     private static final int TEH_SIZE = 200;
  44     private static final int TEH_LOC = 50;
  45     private static final int TEH_LOC_F2 = TEH_LOC + TEH_SIZE + 45; // location of f1 + size of f1 + random space
  46 
  47     public void testHiddenEvents() throws Exception {
  48         Frame f1 = new Frame("Hidden");
  49         Frame f2 = new Frame("Not Hidden");
  50 
  51         try {
  52             f1.setLocation(TEH_LOC, TEH_LOC);
  53             f1.setSize(TEH_SIZE, TEH_SIZE);
  54             f1.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) {
  55                 hiddenMouseEntered = true;
  56             }});
  57 
  58             VisibilityValidator.setVisibleAndConfirm(f1);
  59             f1.setVisible(false);
  60 
  61             f2.setLocation(TEH_LOC_F2, TEH_LOC);
  62             f2.setSize(TEH_SIZE, TEH_SIZE);
  63             f2.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) {
  64                 waypoint.clear();
  65             }});
  66 
  67             VisibilityValidator.setVisibleAndConfirm(f2);
  68 
  69             RobotUtilities.moveMouseTo(TEH_LOC_F2 + TEH_SIZE/2, TEH_LOC + TEH_SIZE/2); // f2
  70             hiddenMouseEntered = false;
  71             waypoint.reset();
  72             RobotUtilities.moveMouseTo(TEH_LOC + TEH_SIZE/2, TEH_LOC + TEH_SIZE/2); // f1
  73             RobotUtilities.moveMouseTo(TEH_LOC_F2 + TEH_SIZE/2, TEH_LOC + TEH_SIZE/2); // f2
  74 
  75             waypoint.requireClear("Frame 2 didn't get a mouseEntered as expected");
  76             assertFalse("Frame 1 got unexpected mouseEntered ", hiddenMouseEntered);
  77 
  78         } finally {
  79             f1.setVisible(false);
  80             f1.dispose();
  81             f1 = null;
  82             f2.setVisible(false);
  83             f2.dispose();
  84             f2 = null;
  85         }
  86     }
  87 
  88 
  89     public static Test suite() {
  90         return new TestSuite(HiddenWindowSentEvents.class);
  91     }
  92 
  93     public static void main (String[] args) throws RuntimeException {
  94         TestResult tr = junit.textui.TestRunner.run(suite());
  95         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  96             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  97         }
  98     }
  99 
 100 }