Skip to content

Commit 527a9a5

Browse files
author
duke
committed
Backport 85f0442
1 parent 83aab07 commit 527a9a5

File tree

2 files changed

+319
-4
lines changed

2 files changed

+319
-4
lines changed

test/jdk/java/awt/regtesthelpers/PassFailJFrame.java

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public final class PassFailJFrame {
170170
private static final int ROWS = 10;
171171
private static final int COLUMNS = 40;
172172

173+
/**
174+
* A gap between windows.
175+
*/
176+
public static final int WINDOW_GAP = 8;
177+
173178
/**
174179
* Prefix for the user-provided failure reason.
175180
*/
@@ -927,13 +932,13 @@ private static void positionInstructionFrame(final Position position) {
927932

928933
switch (position) {
929934
case HORIZONTAL:
930-
int newX = ((screenSize.width / 2) - frame.getWidth());
935+
int newX = (((screenSize.width + WINDOW_GAP) / 2) - frame.getWidth());
931936
frame.setLocation((newX + screenInsets.left),
932937
(frame.getY() + screenInsets.top));
933938
break;
934939

935940
case VERTICAL:
936-
int newY = ((screenSize.height / 2) - frame.getHeight());
941+
int newY = (((screenSize.height + WINDOW_GAP) / 2) - frame.getHeight());
937942
frame.setLocation((frame.getX() + screenInsets.left),
938943
(newY + screenInsets.top));
939944
break;
@@ -981,13 +986,13 @@ public static void positionTestWindow(Window testWindow, Position position) {
981986
switch (position) {
982987
case HORIZONTAL:
983988
case TOP_LEFT_CORNER:
984-
testWindow.setLocation((frame.getX() + frame.getWidth() + 5),
989+
testWindow.setLocation((frame.getX() + frame.getWidth() + WINDOW_GAP),
985990
frame.getY());
986991
break;
987992

988993
case VERTICAL:
989994
testWindow.setLocation(frame.getX(),
990-
(frame.getY() + frame.getHeight() + 5));
995+
(frame.getY() + frame.getHeight() + WINDOW_GAP));
991996
break;
992997
}
993998
}
@@ -1290,6 +1295,7 @@ public Builder testUI(WindowCreator windowCreator) {
12901295
return this;
12911296
}
12921297

1298+
12931299
/**
12941300
* Adds an implementation of {@link PositionWindows PositionWindows}
12951301
* which the framework will use to position multiple test UI windows.
@@ -1313,6 +1319,77 @@ public Builder positionTestUI(PositionWindows positionWindows) {
13131319
return this;
13141320
}
13151321

1322+
/**
1323+
* Positions the test UI windows in a row to the right of
1324+
* the instruction frame. The top of the windows is aligned to
1325+
* that of the instruction frame.
1326+
*
1327+
* @return this builder
1328+
*/
1329+
public Builder positionTestUIRightRow() {
1330+
return position(Position.HORIZONTAL)
1331+
.positionTestUI(WindowLayouts::rightOneRow);
1332+
}
1333+
1334+
/**
1335+
* Positions the test UI windows in a column to the right of
1336+
* the instruction frame. The top of the first window is aligned to
1337+
* that of the instruction frame.
1338+
*
1339+
* @return this builder
1340+
*/
1341+
public Builder positionTestUIRightColumn() {
1342+
return position(Position.HORIZONTAL)
1343+
.positionTestUI(WindowLayouts::rightOneColumn);
1344+
}
1345+
1346+
/**
1347+
* Positions the test UI windows in a column to the right of
1348+
* the instruction frame centering the stack of the windows.
1349+
*
1350+
* @return this builder
1351+
*/
1352+
public Builder positionTestUIRightColumnCentered() {
1353+
return position(Position.HORIZONTAL)
1354+
.positionTestUI(WindowLayouts::rightOneColumnCentered);
1355+
}
1356+
1357+
/**
1358+
* Positions the test UI windows in a row to the bottom of
1359+
* the instruction frame. The left of the first window is aligned to
1360+
* that of the instruction frame.
1361+
*
1362+
* @return this builder
1363+
*/
1364+
public Builder positionTestUIBottomRow() {
1365+
return position(Position.VERTICAL)
1366+
.positionTestUI(WindowLayouts::bottomOneRow);
1367+
}
1368+
1369+
/**
1370+
* Positions the test UI windows in a row to the bottom of
1371+
* the instruction frame centering the row of the windows.
1372+
*
1373+
* @return this builder
1374+
*/
1375+
public Builder positionTestUIBottomRowCentered() {
1376+
return position(Position.VERTICAL)
1377+
.positionTestUI(WindowLayouts::bottomOneRowCentered);
1378+
}
1379+
1380+
/**
1381+
* Positions the test UI windows in a column to the bottom of
1382+
* the instruction frame. The left of the first window is aligned to
1383+
* that of the instruction frame.
1384+
*
1385+
* @return this builder
1386+
*/
1387+
public Builder positionTestUIBottomColumn() {
1388+
return position(Position.VERTICAL)
1389+
.positionTestUI(WindowLayouts::bottomOneColumn);
1390+
}
1391+
1392+
13161393
/**
13171394
* Adds a {@code WindowListCreator} which the framework will use
13181395
* to create a list of test UI windows.
@@ -1415,6 +1492,7 @@ public Builder testUI(PanelCreator panelCreator) {
14151492
return this;
14161493
}
14171494

1495+
14181496
/**
14191497
* Adds a {@code PanelCreator} which the framework will use
14201498
* to create a component with test UI and display it in a split pane.
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/*
2+
* Copyright (c) 2023, 2024, 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+
import java.awt.Component;
25+
import java.awt.Dimension;
26+
import java.awt.GraphicsConfiguration;
27+
import java.awt.Insets;
28+
import java.awt.Point;
29+
import java.awt.Window;
30+
import java.util.List;
31+
32+
import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment;
33+
import static java.awt.Toolkit.getDefaultToolkit;
34+
35+
/**
36+
* A utility class which provides standard window layouts for multi-window
37+
* manual tests using the {@link PassFailJFrame} framework.
38+
* The layout methods {@code right-} and {@code bottom-} implement the
39+
* {@link PassFailJFrame.PositionWindows PositionWindows} interface and
40+
* can be used directly or via builder methods.
41+
* <p>
42+
* There are several helper methods, such as
43+
* {@link #getScreenCenter() getScreenCenter}, which could help you
44+
* implement customized windows layouts.
45+
*/
46+
public final class WindowLayouts {
47+
48+
/** Private constructor to prevent instantiating the utility class. */
49+
private WindowLayouts() {
50+
}
51+
52+
/** A gap between windows. (Local copy makes expressions shorter.) */
53+
private static final int WINDOW_GAP = PassFailJFrame.WINDOW_GAP;
54+
55+
/**
56+
* Lays out the window list in one row to the right of
57+
* the instruction frame. The top of the windows is aligned to
58+
* that of the instruction frame.
59+
*
60+
* @param windows the list of windows to lay out
61+
* @param instructionUI information about the instruction frame
62+
*/
63+
public static void rightOneRow(final List<Window> windows,
64+
final PassFailJFrame.InstructionUI instructionUI) {
65+
layoutRow(instructionUI.getLocation().x
66+
+ instructionUI.getSize().width
67+
+ WINDOW_GAP,
68+
instructionUI.getLocation().y,
69+
windows);
70+
}
71+
72+
/**
73+
* Lays out the window list in one column to the right of
74+
* the instruction frame. The top of the first window is aligned to
75+
* that of the instruction frame.
76+
*
77+
* @param windows the list of windows to lay out
78+
* @param instructionUI information about the instruction frame
79+
*/
80+
public static void rightOneColumn(final List<Window> windows,
81+
final PassFailJFrame.InstructionUI instructionUI) {
82+
layoutColumn(instructionUI.getLocation().x
83+
+ instructionUI.getSize().width
84+
+ WINDOW_GAP,
85+
instructionUI.getLocation().y,
86+
windows);
87+
}
88+
89+
/**
90+
* Lays out the window list in one column to the right of
91+
* the instruction frame centering the stack of the windows.
92+
*
93+
* @param windows the list of windows to lay out
94+
* @param instructionUI information about the instruction frame
95+
*/
96+
public static void rightOneColumnCentered(final List<Window> windows,
97+
final PassFailJFrame.InstructionUI instructionUI) {
98+
layoutColumn(instructionUI.getLocation().x
99+
+ instructionUI.getSize().width
100+
+ WINDOW_GAP,
101+
getScreenCenter().y
102+
- getWindowListHeight(windows) / 2,
103+
windows);
104+
}
105+
106+
107+
/**
108+
* Lays out the window list in one row to the bottom of
109+
* the instruction frame. The left of the first window is aligned to
110+
* that of the instruction frame.
111+
*
112+
* @param windows the list of windows to lay out
113+
* @param instructionUI information about the instruction frame
114+
*/
115+
public static void bottomOneRow(final List<Window> windows,
116+
final PassFailJFrame.InstructionUI instructionUI) {
117+
layoutRow(instructionUI.getLocation().x,
118+
instructionUI.getLocation().y
119+
+ instructionUI.getSize().height
120+
+ WINDOW_GAP,
121+
windows);
122+
}
123+
124+
/**
125+
* Lays out the window list in one row to the bottom of
126+
* the instruction frame centering the row of the windows.
127+
*
128+
* @param windows the list of windows to lay out
129+
* @param instructionUI information about the instruction frame
130+
*/
131+
public static void bottomOneRowCentered(final List<Window> windows,
132+
final PassFailJFrame.InstructionUI instructionUI) {
133+
layoutRow(getScreenCenter().x
134+
- getWindowListWidth(windows) / 2,
135+
instructionUI.getLocation().y
136+
+ instructionUI.getSize().height
137+
+ WINDOW_GAP,
138+
windows);
139+
}
140+
141+
/**
142+
* Lays out the window list in one column to the bottom of
143+
* the instruction frame. The left of the first window is aligned to
144+
* that of the instruction frame.
145+
*
146+
* @param windows the list of windows to lay out
147+
* @param instructionUI information about the instruction frame
148+
*/
149+
public static void bottomOneColumn(final List<Window> windows,
150+
final PassFailJFrame.InstructionUI instructionUI) {
151+
layoutColumn(instructionUI.getLocation().x,
152+
instructionUI.getLocation().y
153+
+ instructionUI.getSize().height
154+
+ WINDOW_GAP,
155+
windows);
156+
}
157+
158+
159+
/**
160+
* Lays out the window list in one row starting at
161+
* ({@code x0}, {@code y}).
162+
*
163+
* @param x0 the starting <var>x</var> coordinate of the windows
164+
* @param y the <var>y</var> coordinate of the windows
165+
* @param windows the list of windows to lay out
166+
*/
167+
public static void layoutRow(final int x0,
168+
final int y,
169+
final List<Window> windows) {
170+
int x = x0;
171+
for (Window w : windows) {
172+
w.setLocation(x, y);
173+
x += w.getWidth() + WINDOW_GAP;
174+
}
175+
}
176+
177+
/**
178+
* Lays out the window list in one column starting at
179+
* ({@code x}, {@code y0}).
180+
*
181+
* @param x the <var>x</var> coordinate of the windows
182+
* @param y0 the starting <var>y</var> coordinate of the windows
183+
* @param windows the list of windows to lay out
184+
*/
185+
public static void layoutColumn(final int x,
186+
final int y0,
187+
final List<Window> windows) {
188+
int y = y0;
189+
for (Window w : windows) {
190+
w.setLocation(x, y);
191+
y += w.getHeight() + WINDOW_GAP;
192+
}
193+
}
194+
195+
196+
/**
197+
* {@return the center point of the main screen}
198+
*/
199+
public static Point getScreenCenter() {
200+
GraphicsConfiguration gc = getLocalGraphicsEnvironment()
201+
.getDefaultScreenDevice()
202+
.getDefaultConfiguration();
203+
Dimension size = gc.getBounds()
204+
.getSize();
205+
Insets insets = getDefaultToolkit()
206+
.getScreenInsets(gc);
207+
208+
return new Point((size.width - insets.left - insets.right) / 2,
209+
(size.height - insets.top - insets.bottom) / 2);
210+
}
211+
212+
/**
213+
* {@return width of the windows in the list, taking into account
214+
* the gap between windows}
215+
*
216+
* @param windows the list of windows to get the width of
217+
*/
218+
public static int getWindowListWidth(final List<Window> windows) {
219+
return windows.stream()
220+
.mapToInt(Component::getWidth)
221+
.sum()
222+
+ WINDOW_GAP * (windows.size() - 1);
223+
}
224+
225+
/**
226+
* {@return height of the windows in the list, taking into account
227+
* the gap between windows}
228+
*
229+
* @param windows the list of windows to get the height of
230+
*/
231+
public static int getWindowListHeight(final List<Window> windows) {
232+
return windows.stream()
233+
.mapToInt(Component::getHeight)
234+
.sum()
235+
+ WINDOW_GAP * (windows.size() - 1);
236+
}
237+
}

0 commit comments

Comments
 (0)