Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sanity/client/SwingSet/src/GridBagLayoutDemoTest.java
41153 views
1
/*
2
* Copyright (c) 2018, 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
import org.jtregext.GuiTestListener;
24
import com.sun.swingset3.demos.gridbaglayout.GridBagLayoutDemo;
25
import static com.sun.swingset3.demos.gridbaglayout.GridBagLayoutDemo.*;
26
import static com.sun.swingset3.demos.gridbaglayout.Calculator.*;
27
import java.awt.Component;
28
import java.awt.Dimension;
29
import java.awt.Point;
30
import javax.swing.JButton;
31
import javax.swing.UIManager;
32
import org.testng.annotations.Test;
33
import org.netbeans.jemmy.ClassReference;
34
import org.netbeans.jemmy.operators.JFrameOperator;
35
import org.netbeans.jemmy.operators.JButtonOperator;
36
import org.netbeans.jemmy.operators.JTextFieldOperator;
37
import org.testng.annotations.Listeners;
38
import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;
39
import static org.jemmy2ext.JemmyExt.getUIValue;
40
/*
41
* @test
42
* @key headful
43
* @summary Verifies SwingSet3 GridBagLayoutDemo by checking the relative
44
* location of all the components before and after resizing the frame,
45
* interacting with all the controls and checking this interaction on the
46
* text field display.
47
*
48
* @library /sanity/client/lib/jemmy/src
49
* @library /sanity/client/lib/Extensions/src
50
* @library /sanity/client/lib/SwingSet3/src
51
* @modules java.desktop
52
* java.logging
53
* @build org.jemmy2ext.JemmyExt
54
* @build com.sun.swingset3.demos.gridbaglayout.GridBagLayoutDemo
55
* @run testng/timeout=600 GridBagLayoutDemoTest
56
*/
57
58
@Listeners(GuiTestListener.class)
59
public class GridBagLayoutDemoTest {
60
61
private JTextFieldOperator tfScreen;
62
private JButtonOperator buttonZero;
63
private JButtonOperator buttonOne;
64
private JButtonOperator buttonTwo;
65
private JButtonOperator buttonThree;
66
private JButtonOperator buttonFour;
67
private JButtonOperator buttonFive;
68
private JButtonOperator buttonSix;
69
private JButtonOperator buttonSeven;
70
private JButtonOperator buttonEight;
71
private JButtonOperator buttonNine;
72
private JButtonOperator buttonPlus;
73
private JButtonOperator buttonMinus;
74
private JButtonOperator buttonMultiply;
75
private JButtonOperator buttonDivide;
76
private JButtonOperator buttonComma;
77
private JButtonOperator buttonSqrt;
78
private JButtonOperator buttonReciprocal;
79
private JButtonOperator buttonToggleSign;
80
private JButtonOperator buttonEquals;
81
private JButtonOperator backspaceButton;
82
private JButtonOperator resetButton;
83
private JFrameOperator mainFrame;
84
85
@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
86
public void test(String lookAndFeel) throws Exception {
87
initializeUIComponents(lookAndFeel);
88
// Check that the relative location of buttons is as expected.
89
checkRelativeLocations();
90
// Interact with buttons and check the result on the display.
91
checkInteractionOnDisplay();
92
// Change the location and check that the relative location of buttons is same as before.
93
checkChangeLocation();
94
// Change the size and check that the relative location of buttons is same as before.
95
checkChangeSize();
96
}
97
98
private double x(Component component) {
99
return component.getLocation().getX();
100
}
101
102
private double y(Component component) {
103
return component.getLocation().getY();
104
}
105
106
private void checkRight(JButtonOperator currentButton, JButtonOperator rightButton) {
107
// Check that x coordinate of right button is greater than that of right
108
// end of current button
109
currentButton.waitStateOnQueue(button -> x(button) + button.getWidth() < x(rightButton.getSource()));
110
// Check that the y coordinate of the button is same as the button to
111
// the right
112
currentButton.waitStateOnQueue(button -> y(button) == y(rightButton.getSource()));
113
// Check that the height of the button is same as the button to the
114
// right
115
currentButton.waitStateOnQueue(button -> button.getHeight() == rightButton.getHeight());
116
}
117
118
private void checkBelow(JButtonOperator currentButton, JButtonOperator buttonBelow) {
119
// Check that y coordinate of button below is greater than that of
120
// bottom end of current button
121
currentButton.waitStateOnQueue(button -> y(button) + button.getHeight() < y(buttonBelow.getSource()));
122
// Check that the x coordinate of the button is same as the button below
123
currentButton.waitStateOnQueue(button -> x(button) == x(buttonBelow.getSource()));
124
// Check that the width of the button is same as the button below
125
currentButton.waitStateOnQueue(button -> button.getWidth() == buttonBelow.getWidth());
126
}
127
128
private void checkRelativeLocations() {
129
// Check relative location of button 7
130
checkRight(buttonSeven, buttonEight);
131
checkBelow(buttonSeven, buttonFour);
132
133
// Check relative location of button 8
134
checkRight(buttonEight, buttonNine);
135
checkBelow(buttonEight, buttonFive);
136
137
// Check relative location of button 9
138
checkRight(buttonNine, buttonDivide);
139
checkBelow(buttonNine, buttonSix);
140
141
// Check relative location of Division button
142
checkRight(buttonDivide, buttonReciprocal);
143
checkBelow(buttonDivide, buttonMultiply);
144
145
// Check relative location of Reciprocal and Sqrt buttons
146
checkBelow(buttonReciprocal, buttonSqrt);
147
148
// Check relative location of button 4
149
checkRight(buttonFour, buttonFive);
150
checkBelow(buttonFour, buttonOne);
151
152
// Check relative location of button 5
153
checkRight(buttonFive, buttonSix);
154
checkBelow(buttonFive, buttonTwo);
155
156
// Check relative location of button 6
157
checkRight(buttonSix, buttonMultiply);
158
checkBelow(buttonSix, buttonThree);
159
160
// Check relative location of Multiply button
161
checkRight(buttonMultiply, buttonSqrt);
162
checkBelow(buttonMultiply, buttonMinus);
163
164
// Check relative location of button 1
165
checkRight(buttonOne, buttonTwo);
166
checkBelow(buttonOne, buttonZero);
167
168
// Check relative location of button 2
169
checkRight(buttonTwo, buttonThree);
170
checkBelow(buttonTwo, buttonToggleSign);
171
172
// Check relative location of button 3
173
checkRight(buttonThree, buttonMinus);
174
checkBelow(buttonThree, buttonComma);
175
176
// Check relative location of Minus button
177
checkBelow(buttonMinus, buttonPlus);
178
179
// Check relative location of button 0
180
checkRight(buttonZero, buttonToggleSign);
181
182
// Check relative location of Sign Toggle Button
183
checkRight(buttonToggleSign, buttonComma);
184
185
// Check relative location of Comma button
186
checkRight(buttonComma, buttonPlus);
187
188
// Check relative location of plus and Equals buttons
189
checkRight(buttonPlus, buttonEquals);
190
191
// Check relative location of JPanel containing Backspace and Reset
192
// buttons
193
Point parentLocation = getUIValue(backspaceButton, (JButton button) -> button.getParent().getLocation());
194
// Check that the y coordinate of bottom of the screen is
195
// less than that of parent of backspace button
196
tfScreen.waitStateOnQueue(screen -> y(screen) + screen.getHeight() < parentLocation.getY());
197
// Check that the y coordinate of the button 7 is greater
198
// than that of parent of backspace
199
buttonSeven.waitStateOnQueue(button -> parentLocation.getY() < y(button));
200
// Check that the y coordinate of reciprocal button is greater
201
// than that of parent of backspace
202
buttonReciprocal.waitStateOnQueue(button -> parentLocation.getY() < y(button));
203
// Check that x coordinate of screen is same as that of parent of
204
// backspace
205
tfScreen.waitStateOnQueue(screen -> x(screen) == parentLocation.getX());
206
// Check that x coordinate of button 7 is same as that of parent of
207
// backspace
208
buttonSeven.waitStateOnQueue(button -> x(button) == parentLocation.getX());
209
210
// Check relative location of Backspace button
211
// Check that the x coordinate of right of backspace button
212
// is less than that of reset button
213
backspaceButton.waitStateOnQueue(button -> x(button) + button.getWidth() < x(resetButton.getSource()));
214
// Check that the height of backspace button is same as that of reset
215
// button
216
backspaceButton.waitStateOnQueue(button -> button.getHeight() == resetButton.getHeight());
217
// Check that the y coordinate bottom of backspace button is less that
218
// that of button 7
219
backspaceButton.waitStateOnQueue(
220
button -> parentLocation.getY() + button.getParent().getHeight() < y(buttonSeven.getSource()));
221
222
// Check that the x coordinate of reset button is greater than that
223
// of right of backspace button
224
resetButton.waitStateOnQueue(button -> x(backspaceButton.getSource()) + backspaceButton.getWidth() < x(button));
225
226
// Check that the height of reset button is same as that of backspace
227
// button
228
resetButton.waitStateOnQueue(button -> backspaceButton.getHeight() == button.getHeight());
229
230
// Check that the y coordinate of bottom of reset button is less
231
// than that of divide button.
232
resetButton.waitStateOnQueue(
233
button -> parentLocation.getY() + button.getParent().getHeight() < y(buttonDivide.getSource()));
234
235
// Check that the y coordinate of top of screen is lower
236
// than that of parent of backspace button
237
tfScreen.waitStateOnQueue(screen -> y(screen) + screen.getHeight() < parentLocation.getY());
238
}
239
240
private void checkInteractionOnDisplay() {
241
// Check buttons: 1,2,+,=,C
242
buttonOne.push();
243
tfScreen.waitText("1");
244
buttonPlus.push();
245
tfScreen.waitText("1");
246
buttonTwo.push();
247
tfScreen.waitText("2");
248
buttonEquals.push();
249
tfScreen.waitText("3");
250
resetButton.push();
251
tfScreen.waitText("0");
252
253
// Check buttons: 3,4,-
254
buttonFour.push();
255
tfScreen.waitText("4");
256
buttonMinus.push();
257
tfScreen.waitText("4");
258
buttonThree.push();
259
tfScreen.waitText("3");
260
buttonEquals.push();
261
tfScreen.waitText("1");
262
reset();
263
264
// Check buttons: 5,6,*
265
buttonFive.push();
266
tfScreen.waitText("5");
267
buttonMultiply.push();
268
tfScreen.waitText("5");
269
buttonSix.push();
270
tfScreen.waitText("6");
271
buttonEquals.push();
272
tfScreen.waitText("30");
273
reset();
274
275
// Check buttons: 8,9,/
276
buttonNine.push();
277
buttonNine.push();
278
tfScreen.waitText("99");
279
buttonDivide.push();
280
tfScreen.waitText("99");
281
buttonEight.push();
282
tfScreen.waitText("8");
283
buttonEquals.push();
284
tfScreen.waitText("12.375");
285
reset();
286
287
// Check buttons: 7,0,[+/-],Backspace
288
buttonSeven.push();
289
tfScreen.waitText("7");
290
buttonZero.push();
291
tfScreen.waitText("70");
292
buttonToggleSign.push();
293
tfScreen.waitText("-70");
294
buttonToggleSign.push();
295
tfScreen.waitText("70");
296
backspaceButton.push();
297
tfScreen.waitText("7");
298
reset();
299
300
// Check Sqrt Button
301
buttonFour.push();
302
buttonNine.push();
303
tfScreen.waitText("49");
304
buttonSqrt.push();
305
tfScreen.waitText("7");
306
reset();
307
308
// Check Reciprocal Button
309
buttonFour.push();
310
tfScreen.waitText("4");
311
buttonReciprocal.push();
312
tfScreen.waitText("0.25");
313
reset();
314
315
// Check Comma button
316
buttonFour.push();
317
buttonComma.push();
318
tfScreen.waitText("4,");
319
}
320
321
private void reset() {
322
resetButton.push();
323
tfScreen.waitText("0");
324
}
325
326
private void initializeUIComponents(String lookAndFeel) throws Exception {
327
UIManager.setLookAndFeel(lookAndFeel);
328
new ClassReference(GridBagLayoutDemo.class.getCanonicalName()).startApplication();
329
mainFrame = new JFrameOperator(GRID_BAG_LAYOUT_DEMO_TITLE);
330
mainFrame.setComparator(EXACT_STRING_COMPARATOR);
331
buttonZero = new JButtonOperator(mainFrame, ZERO_BUTTON_TITLE);
332
buttonOne = new JButtonOperator(mainFrame, ONE_BUTTON_TITLE);
333
buttonTwo = new JButtonOperator(mainFrame, TWO_BUTTON_TITLE);
334
buttonThree = new JButtonOperator(mainFrame, THREE_BUTTON_TITLE);
335
buttonFour = new JButtonOperator(mainFrame, FOUR_BUTTON_TITLE);
336
buttonFive = new JButtonOperator(mainFrame, FIVE_BUTTON_TITLE);
337
buttonSix = new JButtonOperator(mainFrame, SIX_BUTTON_TITLE);
338
buttonSeven = new JButtonOperator(mainFrame, SEVEN_BUTTON_TITLE);
339
buttonEight = new JButtonOperator(mainFrame, EIGHT_BUTTON_TITLE);
340
buttonNine = new JButtonOperator(mainFrame, NINE_BUTTON_TITLE);
341
buttonPlus = new JButtonOperator(mainFrame, PLUS_BUTTON_TITLE);
342
buttonMinus = new JButtonOperator(mainFrame, MINUS_BUTTON_TITLE);
343
buttonMultiply = new JButtonOperator(mainFrame, MULTIPLY_BUTTON_TITLE);
344
buttonDivide = new JButtonOperator(mainFrame, DIVIDE_BUTTON_TITLE);
345
buttonComma = new JButtonOperator(mainFrame, ",");
346
buttonSqrt = new JButtonOperator(mainFrame, SQRT_BUTTON_TITLE);
347
buttonReciprocal = new JButtonOperator(mainFrame, INVERSE_BUTTON_TITLE);
348
buttonToggleSign = new JButtonOperator(mainFrame, SWAPSIGN_BUTTON_TITLE);
349
buttonEquals = new JButtonOperator(mainFrame, EQUALS_BUTTON_TITLE);
350
resetButton = new JButtonOperator(mainFrame, C_BUTTON_TITLE);
351
backspaceButton = new JButtonOperator(mainFrame, BACKSPACE_BUTTON_TITLE);
352
tfScreen = new JTextFieldOperator(mainFrame, 0);
353
}
354
355
private void checkChangeLocation() {
356
Point startingPoint = new Point(100, 100);
357
mainFrame.setLocation(startingPoint);
358
mainFrame.waitComponentLocation(startingPoint);
359
checkRelativeLocations();
360
}
361
362
private void checkChangeSize() {
363
Dimension newSize = new Dimension((int) mainFrame.getToolkit().getScreenSize().getWidth() / 2,
364
(int) mainFrame.getToolkit().getScreenSize().getHeight() / 2);
365
mainFrame.setSize(newSize);
366
mainFrame.waitComponentSize(newSize);
367
checkRelativeLocations();
368
}
369
}
370
371