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