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 See <rdar://problem/4187162> Make sure all (recursive) children of a modal window are functional.
  27  @summary com.apple.junit.java.awt.Window
  28  @library ../../regtesthelpers
  29  @build RobotUtilities
  30  @run main ChildOfModal
  31  */
  32 
  33 import junit.framework.*;
  34 
  35 import java.awt.*;
  36 import java.awt.event.ActionEvent;
  37 import java.awt.event.ActionListener;
  38 import test.java.awt.regtesthelpers.RobotUtilities;
  39 
  40 public class ChildOfModal extends TestCase {
  41     public static Test suite() {
  42         return new TestSuite(ChildOfModal.class);
  43     }
  44 
  45     public static void main (String[] args) throws RuntimeException {
  46         TestResult tr = junit.textui.TestRunner.run(suite());
  47         if ((tr.failureCount() != 0) || (tr.errorCount() != 0)) {
  48             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures");
  49         }
  50     }
  51 
  52     volatile boolean result = false;
  53     Frame baseFrame = null;
  54     volatile Dialog baseDialog = null;
  55     Window childWindow = null;
  56     Window childOfChildWindow = null;
  57     Button childOfChildButton = null;
  58 
  59     public void testChildOfModal() {
  60         try {
  61             // Create the base Frame
  62             baseFrame = new Frame("ChildOfModal");
  63             baseFrame.setLayout(new FlowLayout());
  64             baseFrame.setSize(100,100);
  65             baseFrame.setVisible(true);
  66 
  67             Thread t = new Thread(new Runnable() { public void run() {
  68                 // Make a new modal dialog based on that Frame
  69                 baseDialog = new Dialog(baseFrame, true);
  70                 baseDialog.setSize(200,200);
  71                 baseDialog.setVisible(true);
  72             }});
  73             t.start();
  74             pause(1000); // Wait for dialog to show up - should use events
  75 
  76 
  77             // Make a new Window based on the modal Dialog
  78             childWindow = new Window(baseDialog);
  79             childWindow.setSize(75, 75);
  80             childWindow.setVisible(true);
  81 
  82             // Make a new Window based on the previous Window
  83             // This is the key - this window should still be functional
  84             childOfChildWindow = new Window(childWindow);
  85             childOfChildWindow.setSize(100, 100);
  86             childOfChildButton = new Button("test");
  87             childOfChildButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { result = true; }} );
  88             childOfChildWindow.add(childOfChildButton);
  89             childOfChildWindow.setVisible(true);
  90 
  91             // Click the button!
  92             RobotUtilities.click(childOfChildButton);
  93             pause(1000);
  94             assertTrue("Failed to click childOfChildOfModal button!", result);
  95 
  96         } finally {
  97             if (childOfChildWindow != null) {
  98                 childOfChildWindow.setVisible(false);
  99                 childOfChildWindow.dispose();
 100                 childOfChildWindow = null;
 101             }
 102             if (childWindow != null) {
 103                 childWindow.setVisible(false);
 104                 childWindow.dispose();
 105                 childWindow = null;
 106             }
 107             if (baseDialog != null) {
 108                 baseDialog.setVisible(false);
 109                 baseDialog.dispose();
 110                 baseDialog = null;
 111             }
 112             if (baseFrame != null) {
 113                 baseFrame.setVisible(false);
 114                 baseFrame.dispose();
 115                 baseFrame = null;
 116             }
 117         }
 118     }
 119 
 120     public static void pause( int duration ) {
 121         try {
 122             Thread.sleep(duration);
 123         } catch(Throwable t) {}
 124     }
 125 }