1 /*
   2  * Copyright (c) 2007, 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 6518753
  28   @summary Tests the functionality of modal Swing internal frames
  29   @author artem.ananiev: area=awt.modal
  30   @run main/timeout=30 ModalInternalFrameTest
  31 */
  32 
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 
  36 import javax.swing.*;
  37 
  38 public class ModalInternalFrameTest
  39 {
  40     private boolean passed = true;
  41     private static Robot r;
  42 
  43     private JDesktopPane pane1;
  44     private JDesktopPane pane2;
  45 
  46     private JFrame frame1;
  47     private JFrame frame2;
  48 
  49     private JButton bn1;
  50     private JButton bs1;
  51     private JButton bn2;
  52     private JButton bs2;
  53 
  54     private Point bn1Loc;
  55     private Point bs1Loc;
  56     private Point bn2Loc;
  57     private Point bs2Loc;
  58 
  59     private volatile boolean unblocked1 = true;
  60     private volatile boolean unblocked2 = true;
  61 
  62     public ModalInternalFrameTest()
  63     {
  64     }
  65 
  66     public void init()
  67     {
  68         pane1 = new JDesktopPane();
  69         pane2 = new JDesktopPane();
  70 
  71         frame1 = new JFrame("F1");
  72         frame1.setBounds(100, 100, 320, 240);
  73         frame1.getContentPane().setLayout(new BorderLayout());
  74         frame1.getContentPane().add(pane1);
  75         bn1 = new JButton("Test");
  76         bn1.addActionListener(new ActionListener()
  77         {
  78             public void actionPerformed(ActionEvent ae)
  79             {
  80                 unblocked1 = true;
  81             }
  82         });
  83         frame1.getContentPane().add(bn1, BorderLayout.NORTH);
  84         bs1 = new JButton("Show dialog");
  85         bs1.addActionListener(new ActionListener()
  86         {
  87             public void actionPerformed(ActionEvent ae)
  88             {
  89                 JOptionPane.showInternalMessageDialog(pane1, "Dialog1");
  90             }
  91         });
  92         frame1.getContentPane().add(bs1, BorderLayout.SOUTH);
  93 
  94         frame2 = new JFrame("F2");
  95         frame2.setBounds(100, 400, 320, 240);
  96         frame2.getContentPane().setLayout(new BorderLayout());
  97         frame2.getContentPane().add(pane2);
  98         bn2 = new JButton("Test");
  99         bn2.addActionListener(new ActionListener()
 100         {
 101             public void actionPerformed(ActionEvent ae)
 102             {
 103                 unblocked2 = true;
 104             }
 105         });
 106         frame2.getContentPane().add(bn2, BorderLayout.NORTH);
 107         bs2 = new JButton("Show dialog");
 108         bs2.addActionListener(new ActionListener()
 109         {
 110             public void actionPerformed(ActionEvent ae)
 111             {
 112                 JOptionPane.showInternalMessageDialog(pane2, "Dialog2");
 113             }
 114         });
 115         frame2.getContentPane().add(bs2, BorderLayout.SOUTH);
 116 
 117         frame1.setVisible(true);
 118         frame2.setVisible(true);
 119     }
 120 
 121     private void getLocations()
 122     {
 123         bn1Loc = bn1.getLocationOnScreen();
 124         bn1Loc.translate(bn1.getWidth() / 2, bn1.getHeight() / 2);
 125 
 126         bn2Loc = bn2.getLocationOnScreen();
 127         bn2Loc.translate(bn2.getWidth() / 2, bn2.getHeight() / 2);
 128 
 129         bs1Loc = bs1.getLocationOnScreen();
 130         bs1Loc.translate(bs1.getWidth() / 2, bs1.getHeight() / 2);
 131 
 132         bs2Loc = bs2.getLocationOnScreen();
 133         bs2Loc.translate(bs2.getWidth() / 2, bs2.getHeight() / 2);
 134     }
 135 
 136     private void mouseClick(Robot r, Point p)
 137         throws Exception
 138     {
 139         r.mouseMove(p.x, p.y);
 140         r.mousePress(InputEvent.BUTTON1_MASK);
 141         r.mouseRelease(InputEvent.BUTTON1_MASK);
 142         r.waitForIdle();
 143     }
 144 
 145     private void start()
 146         throws Exception
 147     {
 148         r.setAutoDelay(200);
 149 
 150         unblocked1 = false;
 151         mouseClick(r, bn1Loc);
 152         if (!unblocked1)
 153         {
 154             throw new RuntimeException("Test FAILED: frame1 must be unblocked, if no modal internal frames are shown");
 155         }
 156 
 157         unblocked2 = false;
 158         mouseClick(r, bn2Loc);
 159         if (!unblocked2)
 160         {
 161             throw new RuntimeException("Test FAILED: frame2 must be unblocked, if no modal internal frame is shown in it");
 162         }
 163 
 164         mouseClick(r, bs1Loc);
 165 
 166         unblocked1 = false;
 167         mouseClick(r, bn1Loc);
 168         if (unblocked1)
 169         {
 170             throw new RuntimeException("Test FAILED: frame1 must be blocked, if a modal internal frame is shown in it");
 171         }
 172 
 173         unblocked2 = false;
 174         mouseClick(r, bn2Loc);
 175         if (!unblocked2)
 176         {
 177             throw new RuntimeException("Test FAILED: frame2 must be unblocked, if no modal internal frame is shown in it");
 178         }
 179 
 180         mouseClick(r, bs2Loc);
 181 
 182         unblocked2 = false;
 183         mouseClick(r, bn2Loc);
 184         if (unblocked2)
 185         {
 186             throw new RuntimeException("Test FAILED: frame2 must be blocked, if a modal internal frame is shown in it");
 187         }
 188     }
 189 
 190     private static ModalInternalFrameTest test;
 191 
 192     public static void main(String[] args)
 193         throws Exception
 194     {
 195         r = new Robot();
 196         test = new ModalInternalFrameTest();
 197         SwingUtilities.invokeAndWait(new Runnable()
 198         {
 199             public void run()
 200             {
 201                 test.init();
 202             }
 203         });
 204         r.waitForIdle();
 205         SwingUtilities.invokeAndWait(new Runnable()
 206         {
 207             public void run()
 208             {
 209                 test.getLocations();
 210             }
 211         });
 212         test.start();
 213     }
 214 }