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