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