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 Tests various owned modal dialogs
  27  * @summary com.apple.junit.java.awt.Window
  28  * @library ../../regtesthelpers
  29  * @build RobotUtilities VisibilityValidator Waypoint
  30  * @run junit OwnedDialogs
  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 
  41 public class OwnedDialogs extends TestCase {
  42     public static Test suite() {
  43         return new TestSuite(OwnedDialogs.class);
  44     }
  45 
  46     public static void main (String[] args) throws RuntimeException {
  47         TestResult tr = junit.textui.TestRunner.run(suite());
  48         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  49             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  50         }
  51     }
  52 
  53     public void testChainedModalDialogs() throws Exception {
  54         _doLotsOfModalDialogs(true, true, "Chained Modal Dialogs Frame");
  55     }
  56 
  57     public void testCommonModalDialogs() throws Exception {
  58         _doLotsOfModalDialogs(false, false, "Common Modal Dialogs Frame");
  59     }
  60 
  61     public void testMix1ModalDialogs() throws Exception {
  62         _doLotsOfModalDialogs(true, false, "Mix-1 Modal Dialogs Frame");
  63     }
  64 
  65     public void testMix2ModalDialogs() throws Exception {
  66         _doLotsOfModalDialogs(false, true, "Mix-2 Modal Dialogs Frame");
  67     }
  68 
  69     volatile Waypoint didClick = new Waypoint();
  70     volatile VisibilityValidator checkpoint = null;
  71 
  72     volatile Frame frame = null;
  73 
  74     volatile Button modal1Button = null;
  75     volatile Button modal2Button = null;
  76     volatile Button modal3Button = null;
  77 
  78     volatile Button closeModal1Button = null;
  79     volatile Button closeModal2Button = null;
  80     volatile Button closeModal3Button = null;
  81 
  82     volatile Dialog modal1 = null;
  83     volatile Dialog modal2 = null;
  84     volatile Dialog modal3 = null;
  85 
  86     public void _doLotsOfModalDialogs(boolean modal2chained, boolean modal3chained, String frameName) throws Exception {
  87 
  88         frame = new Frame(frameName);
  89 
  90         modal1Button = new Button("Open Modal 1");
  91         modal2Button = new Button("Open Modal 2");
  92         modal3Button = new Button("Open Modal 3");
  93 
  94         closeModal1Button = new Button("Close Modal 1");
  95         closeModal2Button = new Button("Close Modal 2");
  96         closeModal3Button = new Button("Close Modal 3");
  97 
  98         // Set up the dialogs!
  99         modal1 = new Dialog(frame,  "Modal 1", true);
 100 
 101         modal2 = null;
 102         if (modal2chained)
 103             modal2 = new Dialog( modal1, "Modal 2", true);
 104         else
 105             modal2 = new Dialog( frame, "Modal 2", true);
 106 
 107         modal3 = null;
 108         if (modal3chained)
 109             modal3 = new Dialog( modal2, "Modal 3", true);
 110         else
 111             modal3 = new Dialog( frame, "Modal 3", true);
 112 
 113 
 114         try {
 115             // Setup buttons to open dialogs
 116             modal1Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
 117                 modal1.setVisible(true);
 118             }});
 119             modal2Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
 120                 modal2.setVisible(true);
 121             }});
 122             modal3Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
 123                 modal3.setVisible(true);
 124             }});
 125 
 126             // Setup buttons to close dialogs
 127             closeModal1Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
 128                 modal1.setVisible(false);
 129                 didClick.clear();
 130             }});
 131             closeModal2Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
 132                 modal2.setVisible(false);
 133                 didClick.clear();
 134             }});
 135             closeModal3Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
 136                 modal3.setVisible(false);
 137                 didClick.clear();
 138             }});
 139 
 140             // Set up the main frame and dialogs
 141             frame.setLayout(new FlowLayout());
 142             frame.setSize(400, 300);
 143             frame.add(modal1Button);
 144             frame.pack();
 145 
 146             modal1.setLayout(new FlowLayout());
 147             modal1.setSize(300, 200);
 148             modal1.add(modal2Button);
 149             modal1.add(closeModal1Button);
 150             modal1.validate();
 151 
 152             modal2.setLayout(new FlowLayout());
 153             modal2.setSize(300, 200);
 154             modal2.add(modal3Button);
 155             modal2.add(closeModal2Button);
 156             modal2.validate();
 157 
 158             modal3.setLayout(new FlowLayout());
 159             modal3.setSize(300, 200);
 160             modal3.add(closeModal3Button);
 161             modal3.validate();
 162 
 163             VisibilityValidator.setVisibleAndConfirm(frame);
 164 
 165             // Show the dialogs in order, close and re-open the last one, then close in reverse order
 166             // Note that the setVisible(false) actions have pauses, since we don't have an InvisibilityValidator
 167             checkpoint = new VisibilityValidator(modal1);
 168             RobotUtilities.click(modal1Button);
 169             checkpoint.requireVisible();
 170             assertTrue("Modal 1 is not showing! Check double-click speed in control panel is not too slow.", checkpoint.isValid());
 171 
 172             checkpoint = new VisibilityValidator(modal2);
 173             RobotUtilities.click(modal2Button);
 174             checkpoint.requireVisible();
 175             assertTrue("Modal 2 is not showing! Check double-click speed in control panel is not too slow.", checkpoint.isValid());
 176 
 177             checkpoint = new VisibilityValidator(modal3);
 178             RobotUtilities.click(modal3Button);
 179             checkpoint.requireVisible();
 180             assertTrue("Modal 3 is not showing! Check double-click speed in control panel is not too slow.", checkpoint.isValid());
 181 
 182             didClick.reset();
 183             RobotUtilities.click(closeModal3Button);
 184             didClick.requireClear();
 185             pause(500);
 186             assertFalse("Modal 3 is still showing! Check double-click speed in control panel is not too slow.", modal3.isShowing());
 187 
 188             // VisibilityValidator only works for new windows (windowOpened), so we fake our own
 189             modal3.addWindowFocusListener(new WindowAdapter() {
 190                 public void windowGainedFocus( WindowEvent e ) {
 191                     didClick.clear();
 192                 }
 193             });
 194 
 195             didClick.reset();
 196             RobotUtilities.click(modal3Button);
 197             didClick.requireClear();
 198             assertTrue("Modal 3 is not showing 2! Check double-click speed in control panel is not too slow.", didClick.isClear());
 199 
 200             didClick.reset();
 201             RobotUtilities.click(closeModal3Button);
 202             didClick.requireClear();
 203             pause(500);
 204             assertFalse("Modal 3 is still showing 2! Check double-click speed in control panel is not too slow.", modal3.isShowing());
 205 
 206             didClick.reset();
 207             RobotUtilities.click(closeModal2Button);
 208             didClick.requireClear();
 209             pause(500);
 210             assertFalse("Modal 2 is still showing! Check double-click speed in control panel is not too slow.", modal2.isShowing());
 211 
 212             didClick.reset();
 213             RobotUtilities.click(closeModal1Button);
 214             didClick.requireClear();
 215             pause(500);
 216             assertFalse("Modal 1 is still showing! Check double-click speed in control panel is not too slow.", modal1.isShowing());
 217 
 218         } finally {
 219             if (modal1 != null) {
 220                 modal1.setVisible(false);
 221                 modal1.dispose();
 222                 modal1 = null;
 223             }
 224             if (modal2 != null) {
 225                 modal2.setVisible(false);
 226                 modal2.dispose();
 227                 modal2 = null;
 228             }
 229             if (modal3 != null) {
 230                 modal3.setVisible(false);
 231                 modal3.dispose();
 232                 modal3 = null;
 233             }
 234             if (frame != null) {
 235                 frame.setVisible(false);
 236                 frame.dispose();
 237                 frame = null;
 238             }
 239         }
 240     }
 241 
 242     public static void pause( int duration ) {
 243         try { Thread.sleep(duration); } catch(Throwable t) {}
 244     }
 245 }