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   @bug 6518753
  27   @summary Tests the functionality of modal Swing internal frames
  28   @author artem.ananiev: area=awt.modal
  29   @run main/timeout=30 ModalInternalFrameTest
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 
  35 import javax.swing.*;
  36 
  37 import sun.awt.*;
  38 
  39 public class ModalInternalFrameTest
  40 {
  41     private boolean passed = true;
  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         ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
 143     }
 144 
 145     private void start()
 146         throws Exception
 147     {
 148         Robot r = new Robot();
 149         r.setAutoDelay(200);
 150 
 151         unblocked1 = false;
 152         mouseClick(r, bn1Loc);
 153         if (!unblocked1)
 154         {
 155             throw new RuntimeException("Test FAILED: frame1 must be unblocked, if no modal internal frames are shown");
 156         }
 157 
 158         unblocked2 = false;
 159         mouseClick(r, bn2Loc);
 160         if (!unblocked2)
 161         {
 162             throw new RuntimeException("Test FAILED: frame2 must be unblocked, if no modal internal frame is shown in it");
 163         }
 164 
 165         mouseClick(r, bs1Loc);
 166 
 167         unblocked1 = false;
 168         mouseClick(r, bn1Loc);
 169         if (unblocked1)
 170         {
 171             throw new RuntimeException("Test FAILED: frame1 must be blocked, if a modal internal frame is shown in it");
 172         }
 173 
 174         unblocked2 = false;
 175         mouseClick(r, bn2Loc);
 176         if (!unblocked2)
 177         {
 178             throw new RuntimeException("Test FAILED: frame2 must be unblocked, if no modal internal frame is shown in it");
 179         }
 180 
 181         mouseClick(r, bs2Loc);
 182 
 183         unblocked2 = false;
 184         mouseClick(r, bn2Loc);
 185         if (unblocked2)
 186         {
 187             throw new RuntimeException("Test FAILED: frame2 must be blocked, if a modal internal frame is shown in it");
 188         }
 189     }
 190 
 191     private static ModalInternalFrameTest test;
 192 
 193     public static void main(String[] args)
 194         throws Exception
 195     {
 196         test = new ModalInternalFrameTest();
 197         SwingUtilities.invokeAndWait(new Runnable()
 198         {
 199             public void run()
 200             {
 201                 test.init();
 202             }
 203         });
 204         ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
 205         SwingUtilities.invokeAndWait(new Runnable()
 206         {
 207             public void run()
 208             {
 209                 test.getLocations();
 210             }
 211         });
 212         test.start();
 213     }
 214 }