Path: blob/master/test/jdk/javax/swing/JInternalFrame/6288609/TestJInternalFrameDispose.java
41153 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 628860927* @summary JInternalFrame.setDefaultCloseOperation() interferes with "close"28behavior29* @library ../../regtesthelpers30* @build Util31* @run main TestJInternalFrameDispose32*/3334import java.awt.Point;35import java.awt.Robot;36import java.awt.event.ActionEvent;37import java.awt.event.ActionListener;38import java.awt.event.InputEvent;39import javax.swing.JFrame;40import javax.swing.JDesktopPane;41import javax.swing.JMenu;42import javax.swing.JMenuBar;43import javax.swing.JMenuItem;44import javax.swing.JInternalFrame;45import javax.swing.SwingUtilities;46import javax.swing.event.InternalFrameAdapter;47import javax.swing.event.InternalFrameEvent;4849public class TestJInternalFrameDispose {5051private static JDesktopPane desktopPane;52private static JFrame frame = new JFrame("Test Frame");53private static int count = 0;54private static JMenu menu;55private static JMenuBar menuBar;56private static JMenuItem menuItem;57private static Robot robot;58private static JInternalFrame internalFrame;5960public static void main(String[] args) throws Exception {61robot = new Robot();62SwingUtilities.invokeAndWait(new Runnable() {63@Override64public void run() {65createUI();66}67});6869robot.waitForIdle();70executeTest();71robot.delay(1000);72dispose();73}7475private static void createUI() {7677desktopPane = new JDesktopPane();78frame.getContentPane().add(desktopPane);79frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);8081menuBar = new JMenuBar();82frame.setJMenuBar(menuBar);8384menu = new JMenu("File");85menuBar.add(menu);8687menuItem = new JMenuItem("New Child");88menuItem.addActionListener(89new ActionListener() {90@Override91public void actionPerformed(ActionEvent e) {92JInternalFrame f = new JInternalFrame("Child "93+ (++count), true, true, true, true);94f.setDefaultCloseOperation(95JInternalFrame.DO_NOTHING_ON_CLOSE);96f.addInternalFrameListener(new InternalFrameAdapter() {97@Override98public void internalFrameClosing(99InternalFrameEvent e) {100e.getInternalFrame().dispose();101}102});103f.setSize(200, 300);104f.setLocation(count * 20, count * 20);105desktopPane.add(f);106f.setVisible(true);107}108});109menu.add(menuItem);110111frame.setSize(400, 500);112frame.setLocationRelativeTo(null);113frame.setVisible(true);114}115116private static void executeTest() throws Exception {117118Point point = Util.getCenterPoint(menu);119performMouseOperations(point);120point = Util.getCenterPoint(menuItem);121performMouseOperations(point);122point = Util.getCenterPoint(menu);123performMouseOperations(point);124point = Util.getCenterPoint(menuItem);125performMouseOperations(point);126SwingUtilities.invokeAndWait(new Runnable() {127128@Override129public void run() {130internalFrame = desktopPane.getSelectedFrame();131internalFrame.doDefaultCloseAction();132internalFrame = desktopPane.getSelectedFrame();133}134});135136robot.delay(2000);137if (internalFrame == null) {138dispose();139throw new RuntimeException("Test Failed");140}141}142143private static void dispose() throws Exception {144SwingUtilities.invokeAndWait(new Runnable() {145146@Override147public void run() {148frame.dispose();149}150});151}152153private static void performMouseOperations(Point point) {154robot.mouseMove(point.x, point.y);155robot.mousePress(InputEvent.BUTTON1_MASK);156robot.mouseRelease(InputEvent.BUTTON1_MASK);157robot.delay(1000);158robot.waitForIdle();159}160}161162163