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         robot.delay(1000);
  71         dispose();
  72     }
  73 
  74     private static void createUI() {
  75 
  76         desktopPane = new JDesktopPane();
  77         frame.getContentPane().add(desktopPane);
  78         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79 
  80         menuBar = new JMenuBar();
  81         frame.setJMenuBar(menuBar);
  82 
  83         menu = new JMenu("File");
  84         menuBar.add(menu);
  85 
  86         menuItem = new JMenuItem("New Child");
  87         menuItem.addActionListener(
  88                 new ActionListener() {
  89                     @Override
  90                     public void actionPerformed(ActionEvent e) {
  91                         JInternalFrame f = new JInternalFrame("Child "
  92                                 + (++count), true, true, true, true);
  93                         f.setDefaultCloseOperation(
  94                                 JInternalFrame.DO_NOTHING_ON_CLOSE);
  95                         f.addInternalFrameListener(new InternalFrameAdapter() {
  96                             @Override
  97                             public void internalFrameClosing(
  98                                     InternalFrameEvent e) {
  99                                         e.getInternalFrame().dispose();
 100                                     }
 101                         });
 102                         f.setSize(200, 300);
 103                         f.setLocation(count * 20, count * 20);
 104                         desktopPane.add(f);
 105                         f.setVisible(true);
 106                     }
 107                 });
 108         menu.add(menuItem);
 109 
 110         frame.setSize(400, 500);
 111         frame.setLocationRelativeTo(null);
 112         frame.setVisible(true);
 113     }
 114 
 115     private static void executeTest() throws Exception {
 116 
 117         Point point = Util.getCenterPoint(menu);
 118         performMouseOperations(point);
 119         point = Util.getCenterPoint(menuItem);
 120         performMouseOperations(point);
 121         point = Util.getCenterPoint(menu);
 122         performMouseOperations(point);
 123         point = Util.getCenterPoint(menuItem);
 124         performMouseOperations(point);
 125         SwingUtilities.invokeAndWait(new Runnable() {
 126 
 127             @Override
 128             public void run() {
 129                 internalFrame = desktopPane.getSelectedFrame();
 130                 internalFrame.doDefaultCloseAction();
 131                 internalFrame = desktopPane.getSelectedFrame();
 132             }
 133         });
 134 
 135         robot.delay(2000);
 136         if (internalFrame == null) {
 137             dispose();
 138             throw new RuntimeException("Test Failed");
 139         }
 140     }
 141 
 142     private static void dispose() throws Exception {
 143         SwingUtilities.invokeAndWait(new Runnable() {
 144 
 145             @Override
 146             public void run() {
 147                 frame.dispose();
 148             }
 149         });
 150     }
 151 
 152     private static void performMouseOperations(Point point) {
 153         robot.mouseMove(point.x, point.y);
 154         robot.mousePress(InputEvent.BUTTON1_MASK);
 155         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 156         robot.delay(1000);
 157         robot.waitForIdle();
 158     }
 159 }