|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
21 | 21 | * questions.
|
22 | 22 | */
|
23 | 23 |
|
| 24 | +import java.awt.EventQueue; |
| 25 | +import java.awt.Frame; |
| 26 | +import java.awt.GraphicsConfiguration; |
| 27 | +import java.awt.Insets; |
| 28 | +import java.awt.Rectangle; |
| 29 | +import java.awt.Robot; |
| 30 | +import java.awt.Toolkit; |
| 31 | +import java.awt.event.WindowAdapter; |
| 32 | +import java.awt.event.WindowEvent; |
| 33 | + |
24 | 34 | /*
|
25 |
| - test |
26 |
| - @bug 4464714 6365898 |
27 |
| - @summary Frames cannot be shown initially maximized |
28 |
| - @author Valeriy Ushakov: area=toplevel |
29 |
| - @run applet/manual=yesno InitialMaximizedTest.html |
| 35 | + * @test |
| 36 | + * @bug 4464714 6365898 |
| 37 | + * @key headful |
| 38 | + * @summary Frames cannot be shown initially maximized |
30 | 39 | */
|
31 | 40 |
|
32 |
| - |
33 |
| -/** |
34 |
| - * InitialMaximizedTest.java |
35 |
| - * |
36 |
| - * summary: |
37 |
| - */ |
38 |
| - |
39 |
| -import java.applet.Applet; |
40 |
| -import java.awt.*; |
41 |
| - |
42 |
| - |
43 |
| -public class InitialMaximizedTest extends Applet |
44 |
| -{ |
45 |
| - Frame f; |
46 |
| - |
47 |
| - public void init() |
48 |
| - { |
49 |
| - this.setLayout (new BorderLayout ()); |
50 |
| - |
51 |
| - String[] instructions = |
52 |
| - { |
53 |
| - "This test creates a frame that is initially maximized.", |
54 |
| - "Press PASS if frame is shown initially maximized, else press FAIL" |
55 |
| - }; |
56 |
| - Sysout.createDialogWithInstructions( instructions ); |
57 |
| - |
58 |
| - }//End init() |
59 |
| - |
60 |
| - public void start () |
61 |
| - { |
62 |
| - //Get things going. Request focus, set size, et cetera |
63 |
| - setSize (200,200); |
64 |
| - setVisible(true); |
65 |
| - validate(); |
66 |
| - |
67 |
| - f = new Frame("The frame SHOULD be shown MAXIMIZED"); |
68 |
| - f.setSize(300, 300); |
69 |
| - f.setLocation(50, 50); |
70 |
| - f.setExtendedState(Frame.MAXIMIZED_BOTH); |
71 |
| - f.setVisible(true); |
72 |
| - }// start() |
73 |
| - |
74 |
| -}// class InitialMaximizedTest |
75 |
| - |
76 |
| -/* Place other classes related to the test after this line */ |
77 |
| - |
78 |
| - |
79 |
| - |
80 |
| - |
81 |
| - |
82 |
| -/**************************************************** |
83 |
| - Standard Test Machinery |
84 |
| - DO NOT modify anything below -- it's a standard |
85 |
| - chunk of code whose purpose is to make user |
86 |
| - interaction uniform, and thereby make it simpler |
87 |
| - to read and understand someone else's test. |
88 |
| - ****************************************************/ |
89 |
| - |
90 |
| -/** |
91 |
| - This is part of the standard test machinery. |
92 |
| - It creates a dialog (with the instructions), and is the interface |
93 |
| - for sending text messages to the user. |
94 |
| - To print the instructions, send an array of strings to Sysout.createDialog |
95 |
| - WithInstructions method. Put one line of instructions per array entry. |
96 |
| - To display a message for the tester to see, simply call Sysout.println |
97 |
| - with the string to be displayed. |
98 |
| - This mimics System.out.println but works within the test harness as well |
99 |
| - as standalone. |
100 |
| - */ |
101 |
| - |
102 |
| -class Sysout |
103 |
| -{ |
104 |
| - private static TestDialog dialog; |
105 |
| - |
106 |
| - public static void createDialogWithInstructions( String[] instructions ) |
107 |
| - { |
108 |
| - dialog = new TestDialog( new Frame(), "Instructions" ); |
109 |
| - dialog.printInstructions( instructions ); |
110 |
| - dialog.setVisible(true); |
111 |
| - println( "Any messages for the tester will display here." ); |
| 41 | +public class InitialMaximizedTest { |
| 42 | + |
| 43 | + static Frame frame; |
| 44 | + |
| 45 | + public static void main(String[] args) throws Exception { |
| 46 | + if (!Toolkit.getDefaultToolkit() |
| 47 | + .isFrameStateSupported(Frame.MAXIMIZED_BOTH)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + Robot robot = new Robot(); |
| 52 | + try { |
| 53 | + EventQueue.invokeAndWait(InitialMaximizedTest::createAndShowFrame); |
| 54 | + robot.waitForIdle(); |
| 55 | + robot.delay(1000); |
| 56 | + EventQueue.invokeAndWait(InitialMaximizedTest::checkMaximized); |
| 57 | + } finally { |
| 58 | + EventQueue.invokeAndWait(() -> { |
| 59 | + if (frame != null) { |
| 60 | + frame.dispose(); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
112 | 64 | }
|
113 | 65 |
|
114 |
| - public static void createDialog( ) |
115 |
| - { |
116 |
| - dialog = new TestDialog( new Frame(), "Instructions" ); |
117 |
| - String[] defInstr = { "Instructions will appear here. ", "" } ; |
118 |
| - dialog.printInstructions( defInstr ); |
119 |
| - dialog.setVisible(true); |
120 |
| - println( "Any messages for the tester will display here." ); |
121 |
| - } |
| 66 | + private static void checkMaximized() { |
| 67 | + Rectangle frameBounds = frame.getBounds(); |
122 | 68 |
|
| 69 | + GraphicsConfiguration gc = frame.getGraphicsConfiguration(); |
| 70 | + Rectangle workArea = gc.getBounds(); |
123 | 71 |
|
124 |
| - public static void printInstructions( String[] instructions ) |
125 |
| - { |
126 |
| - dialog.printInstructions( instructions ); |
127 |
| - } |
| 72 | + Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc); |
| 73 | + workArea.x += screenInsets.left; |
| 74 | + workArea.y += screenInsets.top; |
| 75 | + workArea.width -= screenInsets.left + screenInsets.right; |
| 76 | + workArea.height -= screenInsets.top + screenInsets.bottom; |
128 | 77 |
|
| 78 | + System.out.println("Frame bounds " + frameBounds); |
| 79 | + System.out.println("GraphicsConfiguration bounds " + gc.getBounds()); |
| 80 | + System.out.println("Screen insets: " + screenInsets); |
| 81 | + System.out.println("Work area: " + workArea); |
129 | 82 |
|
130 |
| - public static void println( String messageIn ) |
131 |
| - { |
132 |
| - dialog.displayMessage( messageIn ); |
| 83 | + //frame bounds can exceed screen size on Windows, see 8231043 |
| 84 | + if (!frameBounds.contains(workArea)) { |
| 85 | + throw new RuntimeException("Frame is not maximized"); |
| 86 | + } |
133 | 87 | }
|
134 | 88 |
|
135 |
| -}// Sysout class |
136 |
| - |
137 |
| -/** |
138 |
| - This is part of the standard test machinery. It provides a place for the |
139 |
| - test instructions to be displayed, and a place for interactive messages |
140 |
| - to the user to be displayed. |
141 |
| - To have the test instructions displayed, see Sysout. |
142 |
| - To have a message to the user be displayed, see Sysout. |
143 |
| - Do not call anything in this dialog directly. |
144 |
| - */ |
145 |
| -class TestDialog extends Dialog |
146 |
| -{ |
147 |
| - |
148 |
| - TextArea instructionsText; |
149 |
| - TextArea messageText; |
150 |
| - int maxStringLength = 80; |
151 |
| - |
152 |
| - //DO NOT call this directly, go through Sysout |
153 |
| - public TestDialog( Frame frame, String name ) |
154 |
| - { |
155 |
| - super( frame, name ); |
156 |
| - int scrollBoth = TextArea.SCROLLBARS_BOTH; |
157 |
| - instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); |
158 |
| - add( "North", instructionsText ); |
159 |
| - |
160 |
| - messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); |
161 |
| - add("Center", messageText); |
162 |
| - |
163 |
| - pack(); |
164 |
| - |
165 |
| - setVisible(true); |
166 |
| - }// TestDialog() |
167 |
| - |
168 |
| - //DO NOT call this directly, go through Sysout |
169 |
| - public void printInstructions( String[] instructions ) |
170 |
| - { |
171 |
| - //Clear out any current instructions |
172 |
| - instructionsText.setText( "" ); |
173 |
| - |
174 |
| - //Go down array of instruction strings |
175 |
| - |
176 |
| - String printStr, remainingStr; |
177 |
| - for( int i=0; i < instructions.length; i++ ) |
178 |
| - { |
179 |
| - //chop up each into pieces maxSringLength long |
180 |
| - remainingStr = instructions[ i ]; |
181 |
| - while( remainingStr.length() > 0 ) |
182 |
| - { |
183 |
| - //if longer than max then chop off first max chars to print |
184 |
| - if( remainingStr.length() >= maxStringLength ) |
185 |
| - { |
186 |
| - //Try to chop on a word boundary |
187 |
| - int posOfSpace = remainingStr. |
188 |
| - lastIndexOf( ' ', maxStringLength - 1 ); |
189 |
| - |
190 |
| - if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; |
191 |
| - |
192 |
| - printStr = remainingStr.substring( 0, posOfSpace + 1 ); |
193 |
| - remainingStr = remainingStr.substring( posOfSpace + 1 ); |
194 |
| - } |
195 |
| - //else just print |
196 |
| - else |
197 |
| - { |
198 |
| - printStr = remainingStr; |
199 |
| - remainingStr = ""; |
200 |
| - } |
201 |
| - |
202 |
| - instructionsText.append( printStr + "\n" ); |
203 |
| - |
204 |
| - }// while |
205 |
| - |
206 |
| - }// for |
207 |
| - |
208 |
| - }//printInstructions() |
209 |
| - |
210 |
| - //DO NOT call this directly, go through Sysout |
211 |
| - public void displayMessage( String messageIn ) |
212 |
| - { |
213 |
| - messageText.append( messageIn + "\n" ); |
214 |
| - System.out.println(messageIn); |
| 89 | + private static void createAndShowFrame() { |
| 90 | + frame = new Frame("The frame SHOULD be shown MAXIMIZED"); |
| 91 | + frame.setSize(300, 300); |
| 92 | + frame.setLocation(50, 50); |
| 93 | + frame.setExtendedState(Frame.MAXIMIZED_BOTH); |
| 94 | + frame.addWindowListener(new WindowAdapter() { |
| 95 | + @Override |
| 96 | + public void windowClosing(WindowEvent e) { |
| 97 | + frame.dispose(); |
| 98 | + } |
| 99 | + }); |
| 100 | + frame.setVisible(true); |
215 | 101 | }
|
216 |
| - |
217 |
| -}// TestDialog class |
| 102 | +} |
0 commit comments