1 /*
   2  * Copyright (c) 2016, 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  * @key headful
  27  * @bug 8145896
  28  * @summary JInternalFrame setMaximum before adding to desktop throws null pointer exception
  29  * @library ../../regtesthelpers
  30  * @build Util
  31  * @run main TestJInternalFrameMaximize
  32  */
  33 import java.awt.Point;
  34 import java.awt.Robot;
  35 import java.awt.event.ActionEvent;
  36 import java.awt.event.InputEvent;
  37 import java.beans.PropertyVetoException;
  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.UIManager;
  46 import javax.swing.UnsupportedLookAndFeelException;
  47 
  48 public class TestJInternalFrameMaximize {
  49 
  50     private static JDesktopPane desktopPane;
  51     private static JFrame 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 volatile String errorMessage = "";
  58 
  59     public static void main(String[] args) throws Exception {
  60         robot = new Robot();
  61         UIManager.LookAndFeelInfo[] lookAndFeelArray
  62                 = UIManager.getInstalledLookAndFeels();
  63         for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
  64             String lookAndFeelString = lookAndFeelItem.getClassName();
  65             if (tryLookAndFeel(lookAndFeelString)) {
  66                 createUI();
  67                 robot.waitForIdle();
  68                 executeTest();
  69                 robot.delay(1000);
  70             }
  71         }
  72         if (!"".equals(errorMessage)) {
  73             throw new RuntimeException(errorMessage);
  74         }
  75     }
  76 
  77     private static boolean tryLookAndFeel(String lookAndFeelString) {
  78         try {
  79             UIManager.setLookAndFeel(lookAndFeelString);
  80             return true;
  81         } catch (UnsupportedLookAndFeelException | ClassNotFoundException |
  82                 InstantiationException | IllegalAccessException e) {
  83             errorMessage += e.getMessage() + "\n";
  84             System.err.println("Caught Exception: " + e.getMessage());
  85             return false;
  86         }
  87     }
  88 
  89     private static void createUI() throws Exception {
  90 
  91         SwingUtilities.invokeAndWait(() -> {
  92             frame = new JFrame("Test Frame");
  93             desktopPane = new JDesktopPane();
  94             frame.getContentPane().add(desktopPane);
  95             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  96 
  97             menuBar = new JMenuBar();
  98             frame.setJMenuBar(menuBar);
  99 
 100             menu = new JMenu("File");
 101             menuBar.add(menu);
 102 
 103             menuItem = new JMenuItem("New Child");
 104             menuItem.addActionListener((ActionEvent e) -> {
 105                 try {
 106                     JInternalFrame f = new JInternalFrame("Child "
 107                             + (++count), true, true, true, true);
 108                     f.setSize(200, 300);
 109                     f.setLocation(count * 20, count * 20);
 110                     f.setMaximum(true);
 111                     desktopPane.add(f);
 112                     f.setVisible(true);
 113                 } catch (PropertyVetoException ex) {
 114                 } catch (RuntimeException ex) {
 115                     errorMessage = "Test Failed";
 116                 } finally {
 117                     frame.dispose();
 118                 }
 119             });
 120             menu.add(menuItem);
 121             frame.setSize(500, 500);
 122             frame.setLocationRelativeTo(null);
 123             frame.setVisible(true);
 124         });
 125     }
 126 
 127     private static void executeTest() throws Exception {
 128 
 129         Point point = Util.getCenterPoint(menu);
 130         performMouseOperations(point);
 131         point = Util.getCenterPoint(menuItem);
 132         performMouseOperations(point);
 133     }
 134 
 135     private static void performMouseOperations(Point point) {
 136         robot.mouseMove(point.x, point.y);
 137         robot.mousePress(InputEvent.BUTTON1_MASK);
 138         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 139         robot.delay(1000);
 140         robot.waitForIdle();
 141     }
 142 }