Path: blob/master/test/jdk/javax/swing/JInternalFrame/8145896/TestJInternalFrameMaximize.java
41154 views
/*1* Copyright (c) 2016, 2019, 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 8145896 819494427* @summary JInternalFrame setMaximum before adding to desktop throws null pointer exception28* @library ../../regtesthelpers29* @build Util30* @run main TestJInternalFrameMaximize31*/3233import java.awt.Point;34import java.awt.Robot;35import java.awt.event.ActionEvent;36import java.awt.event.InputEvent;37import java.beans.PropertyVetoException;38import javax.swing.JFrame;39import javax.swing.JDesktopPane;40import javax.swing.JMenu;41import javax.swing.JMenuBar;42import javax.swing.JMenuItem;43import javax.swing.JInternalFrame;44import javax.swing.SwingUtilities;45import javax.swing.UIManager;46import javax.swing.UnsupportedLookAndFeelException;4748public class TestJInternalFrameMaximize {4950private static JDesktopPane desktopPane;51private static JFrame frame;52private static int count = 0;53private static JMenu menu;54private static JMenuBar menuBar;55private static JMenuItem menuItem;56private static Robot robot;57private static volatile String errorMessage = "";58private static volatile boolean isFrameShowing;5960public static void main(String[] args) throws Exception {61robot = new Robot();62robot.setAutoDelay(100);63UIManager.LookAndFeelInfo[] lookAndFeelArray64= UIManager.getInstalledLookAndFeels();65for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {66try {67String lookAndFeelString = lookAndFeelItem.getClassName();68if (tryLookAndFeel(lookAndFeelString)) {69createUI();70robot.waitForIdle();71blockTillDisplayed(frame);72executeTest();73robot.delay(1000);74}75} finally {76frame.dispose();77isFrameShowing = false;78robot.waitForIdle();79}80}81if (!"".equals(errorMessage)) {82throw new RuntimeException(errorMessage);83}84}8586private static boolean tryLookAndFeel(String lookAndFeelString) {87try {88UIManager.setLookAndFeel(lookAndFeelString);89return true;90} catch (UnsupportedLookAndFeelException | ClassNotFoundException |91InstantiationException | IllegalAccessException e) {92errorMessage += e.getMessage() + "\n";93System.err.println("Caught Exception: " + e.getMessage());94return false;95}96}9798private static void createUI() throws Exception {99100SwingUtilities.invokeAndWait(() -> {101frame = new JFrame("Test Frame");102desktopPane = new JDesktopPane();103frame.getContentPane().add(desktopPane);104frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);105106menuBar = new JMenuBar();107frame.setJMenuBar(menuBar);108109menu = new JMenu("File");110menuBar.add(menu);111112menuItem = new JMenuItem("New Child");113menuItem.addActionListener((ActionEvent e) -> {114try {115JInternalFrame f = new JInternalFrame("Child "116+ (++count), true, true, true, true);117f.setSize(200, 300);118f.setLocation(count * 20, count * 20);119f.setMaximum(true);120desktopPane.add(f);121f.setVisible(true);122} catch (PropertyVetoException ex) {123} catch (RuntimeException ex) {124errorMessage = "Test Failed";125}126});127menu.add(menuItem);128frame.setSize(500, 500);129frame.setLocationRelativeTo(null);130frame.setVisible(true);131});132}133134private static void blockTillDisplayed(JFrame frame) throws Exception {135while (!isFrameShowing) {136try {137SwingUtilities.invokeAndWait(()-> isFrameShowing = frame.isShowing());138if (!isFrameShowing) {139Thread.sleep(1000);140}141} catch (InterruptedException ex) {142} catch (Exception ex) {143throw new RuntimeException(ex);144}145}146}147148private static void executeTest() throws Exception {149Point point = Util.getCenterPoint(menu);150performMouseOperations(point);151point = Util.getCenterPoint(menuItem);152performMouseOperations(point);153}154155private static void performMouseOperations(Point point) {156robot.mouseMove(point.x, point.y);157robot.mousePress(InputEvent.BUTTON1_MASK);158robot.mouseRelease(InputEvent.BUTTON1_MASK);159robot.delay(1000);160robot.waitForIdle();161}162}163164165