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/4432247> Modal dialogs are application-modal, but not top-most
  27  @summary com.apple.junit.java.awt.Dialog;
  28  @library ../../regtesthelpers
  29  @build RobotUtilities
  30  @build VisibilityValidator
  31  @build Waypoint
  32  @run junit ModalZOrder
  33  */
  34 
  35 import junit.framework.*;
  36 import java.awt.*;
  37 import java.awt.event.ActionEvent;
  38 import java.awt.event.ActionListener;
  39 
  40 import test.java.awt.regtesthelpers.RobotUtilities;
  41 import test.java.awt.regtesthelpers.VisibilityValidator;
  42 import test.java.awt.regtesthelpers.Waypoint;
  43 
  44 public class ModalZOrder extends TestCase {
  45     public static Test suite() {
  46         return new TestSuite(ModalZOrder.class);
  47     }
  48 
  49     public static void main (String[] args) throws RuntimeException {
  50         TestResult tr = junit.textui.TestRunner.run(suite());
  51         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  52             throw new RuntimeException("### Unexpected JUnit errors or failures.");
  53         }
  54     }
  55 
  56     volatile Frame frameA = null;
  57     volatile Frame frameB = null;
  58     volatile Dialog dialogA = null;
  59     volatile Button buttonA = null;
  60     volatile VisibilityValidator checkpoint = null;
  61     volatile Waypoint didClick = new Waypoint();
  62 
  63     public void testModalZOrder() throws Exception {
  64         try {
  65             frameA = new Frame( "Frame A" );
  66             frameA.setBounds( 0, 0, 100, 100 );
  67             VisibilityValidator.setVisibleAndConfirm(frameA);
  68 
  69             frameB = new Frame( "Frame B" );
  70             frameB.setBounds( 0, 0, 400, 400 );
  71             VisibilityValidator.setVisibleAndConfirm(frameB);
  72 
  73             dialogA = new Dialog( frameA, "Modal Dialog", true );
  74             buttonA = new Button("Click");
  75             buttonA.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
  76                 didClick.clear();
  77             }});
  78             dialogA.setLayout(new FlowLayout());
  79             dialogA.add(buttonA);
  80             dialogA.pack();
  81             dialogA.setBounds(50,50,100,50);
  82 
  83             checkpoint = new VisibilityValidator(dialogA);
  84 
  85             // A bit awkward here, but since dialogA.setVisible(true) won't return...
  86             new Thread(new Runnable() { public void run() {
  87                 // Wait for dialogA to become visible
  88                 checkpoint.requireVisible();
  89 
  90                 // Try to bring frameB above dialogA, then try to click the button
  91                 // Can't use Waypoint here because this isn't supposed to work!
  92                 RobotUtilities.click(frameB);
  93 
  94                 pause(1000);
  95 
  96                 didClick.reset();
  97                 RobotUtilities.click(buttonA);
  98 
  99                 // Go ahead and swallow the exception here, as we assert
 100                 // in just a little bit (and on the test thread) that we
 101                 // got through the waypoint
 102                 try { didClick.requireClear(); } catch (RuntimeException e) {}
 103 
 104                 // hide the dialog so we can test the condition
 105                 dialogA.setVisible(false);
 106 
 107             }}).start();
 108 
 109             dialogA.setVisible( true );
 110 
 111             assertTrue("Modal Dialog is covered!", didClick.isClear());
 112 
 113         } finally {
 114             if (dialogA != null) {
 115                 dialogA.setVisible(false);
 116                 dialogA.dispose();
 117             }
 118             if (frameB != null) {
 119                 frameB.setVisible(false);
 120                 frameB.dispose();
 121             }
 122             if (frameA != null) {
 123                 frameA.setVisible(false);
 124                 frameA.dispose();
 125             }
 126         }
 127     }
 128 
 129     public static void pause( int duration ) {
 130         try { Thread.sleep(duration); } catch(Throwable t) {}
 131     }
 132 }