jdk/test/javax/swing/JInternalFrame/6288609/TestJInternalFrameDispose.java
changeset 34398 0c7abf4664f5
child 38994 a6b14a17689b
equal deleted inserted replaced
34397:86a74cb6c903 34398:0c7abf4664f5
       
     1 /*
       
     2  * Copyright (c) 2015, 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 6288609
       
    27  * @summary JInternalFrame.setDefaultCloseOperation() interferes with "close"
       
    28               behavior
       
    29  * @library ../../regtesthelpers
       
    30  * @build Util
       
    31  * @run main TestJInternalFrameDispose
       
    32  */
       
    33 import java.awt.Point;
       
    34 import java.awt.Robot;
       
    35 import java.awt.event.ActionEvent;
       
    36 import java.awt.event.ActionListener;
       
    37 import java.awt.event.InputEvent;
       
    38 import javax.swing.JFrame;
       
    39 import javax.swing.JDesktopPane;
       
    40 import javax.swing.JMenu;
       
    41 import javax.swing.JMenuBar;
       
    42 import javax.swing.JMenuItem;
       
    43 import javax.swing.JInternalFrame;
       
    44 import javax.swing.SwingUtilities;
       
    45 import javax.swing.event.InternalFrameAdapter;
       
    46 import javax.swing.event.InternalFrameEvent;
       
    47 
       
    48 public class TestJInternalFrameDispose {
       
    49 
       
    50     private static JDesktopPane desktopPane;
       
    51     private static JFrame frame = new JFrame("Test Frame");
       
    52     private static int count = 0;
       
    53     private static JMenu menu;
       
    54     private static JMenuBar menuBar;
       
    55     private static JMenuItem menuItem;
       
    56     private static Robot robot;
       
    57     private static JInternalFrame internalFrame;
       
    58 
       
    59     public static void main(String[] args) throws Exception {
       
    60         robot = new Robot();
       
    61         SwingUtilities.invokeAndWait(new Runnable() {
       
    62             @Override
       
    63             public void run() {
       
    64                 createUI();
       
    65             }
       
    66         });
       
    67 
       
    68         robot.waitForIdle();
       
    69         executeTest();
       
    70         dispose();
       
    71     }
       
    72 
       
    73     private static void createUI() {
       
    74 
       
    75         desktopPane = new JDesktopPane();
       
    76         frame.getContentPane().add(desktopPane);
       
    77         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    78 
       
    79         menuBar = new JMenuBar();
       
    80         frame.setJMenuBar(menuBar);
       
    81 
       
    82         menu = new JMenu("File");
       
    83         menuBar.add(menu);
       
    84 
       
    85         menuItem = new JMenuItem("New Child");
       
    86         menuItem.addActionListener(
       
    87                 new ActionListener() {
       
    88                     @Override
       
    89                     public void actionPerformed(ActionEvent e) {
       
    90                         JInternalFrame f = new JInternalFrame("Child "
       
    91                                 + (++count), true, true, true, true);
       
    92                         f.setDefaultCloseOperation(
       
    93                                 JInternalFrame.DO_NOTHING_ON_CLOSE);
       
    94                         f.addInternalFrameListener(new InternalFrameAdapter() {
       
    95                             @Override
       
    96                             public void internalFrameClosing(
       
    97                                     InternalFrameEvent e) {
       
    98                                         e.getInternalFrame().dispose();
       
    99                                     }
       
   100                         });
       
   101                         f.setSize(200, 300);
       
   102                         f.setLocation(count * 20, count * 20);
       
   103                         desktopPane.add(f);
       
   104                         f.setVisible(true);
       
   105                     }
       
   106                 });
       
   107         menu.add(menuItem);
       
   108 
       
   109         frame.setSize(400, 500);
       
   110         frame.setLocationRelativeTo(null);
       
   111         frame.setVisible(true);
       
   112     }
       
   113 
       
   114     private static void executeTest() throws Exception {
       
   115 
       
   116         Point point = Util.getCenterPoint(menu);
       
   117         performMouseOperations(point);
       
   118         point = Util.getCenterPoint(menuItem);
       
   119         performMouseOperations(point);
       
   120         point = Util.getCenterPoint(menu);
       
   121         performMouseOperations(point);
       
   122         point = Util.getCenterPoint(menuItem);
       
   123         performMouseOperations(point);
       
   124         SwingUtilities.invokeAndWait(new Runnable() {
       
   125 
       
   126             @Override
       
   127             public void run() {
       
   128                 internalFrame = desktopPane.getSelectedFrame();
       
   129                 internalFrame.doDefaultCloseAction();
       
   130                 internalFrame = desktopPane.getSelectedFrame();
       
   131             }
       
   132         });
       
   133 
       
   134         robot.delay(2000);
       
   135         if (internalFrame == null) {
       
   136             dispose();
       
   137             throw new RuntimeException("Test Failed");
       
   138         }
       
   139     }
       
   140 
       
   141     private static void dispose() throws Exception {
       
   142         SwingUtilities.invokeAndWait(new Runnable() {
       
   143 
       
   144             @Override
       
   145             public void run() {
       
   146                 frame.dispose();
       
   147             }
       
   148         });
       
   149     }
       
   150 
       
   151     private static void performMouseOperations(Point point) {
       
   152         robot.mouseMove(point.x, point.y);
       
   153         robot.mousePress(InputEvent.BUTTON1_MASK);
       
   154         robot.mouseRelease(InputEvent.BUTTON1_MASK);
       
   155         robot.delay(1000);
       
   156         robot.waitForIdle();
       
   157     }
       
   158 }