File: GridBagLayoutDemoTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (368 lines) | stat: -rw-r--r-- 15,477 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
import org.jtregext.GuiTestListener;
import com.sun.swingset3.demos.gridbaglayout.GridBagLayoutDemo;
import static com.sun.swingset3.demos.gridbaglayout.GridBagLayoutDemo.*;
import static com.sun.swingset3.demos.gridbaglayout.Calculator.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.UIManager;
import org.testng.annotations.Test;
import org.netbeans.jemmy.ClassReference;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JTextFieldOperator;
import org.testng.annotations.Listeners;
import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;
import static org.jemmy2ext.JemmyExt.getUIValue;
/*
 * @test
 * @key headful
 * @summary Verifies SwingSet3 GridBagLayoutDemo by checking the relative
 *  location of all the components before and after resizing the frame,
 *  interacting with all the controls and checking this interaction on the
 *  text field display.
 *
 * @library /sanity/client/lib/jemmy/src
 * @library /sanity/client/lib/Extensions/src
 * @library /sanity/client/lib/SwingSet3/src
 * @modules java.desktop
 *          java.logging
 * @build com.sun.swingset3.demos.gridbaglayout.GridBagLayoutDemo
 * @run testng GridBagLayoutDemoTest
 */

@Listeners(GuiTestListener.class)
public class GridBagLayoutDemoTest {

    private JTextFieldOperator tfScreen;
    private JButtonOperator buttonZero;
    private JButtonOperator buttonOne;
    private JButtonOperator buttonTwo;
    private JButtonOperator buttonThree;
    private JButtonOperator buttonFour;
    private JButtonOperator buttonFive;
    private JButtonOperator buttonSix;
    private JButtonOperator buttonSeven;
    private JButtonOperator buttonEight;
    private JButtonOperator buttonNine;
    private JButtonOperator buttonPlus;
    private JButtonOperator buttonMinus;
    private JButtonOperator buttonMultiply;
    private JButtonOperator buttonDivide;
    private JButtonOperator buttonComma;
    private JButtonOperator buttonSqrt;
    private JButtonOperator buttonReciprocal;
    private JButtonOperator buttonToggleSign;
    private JButtonOperator buttonEquals;
    private JButtonOperator backspaceButton;
    private JButtonOperator resetButton;
    private JFrameOperator mainFrame;

    @Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
    public void test(String lookAndFeel) throws Exception {
        initializeUIComponents(lookAndFeel);
        // Check that the relative location of buttons is as expected.
        checkRelativeLocations();
        // Interact with buttons and check the result on the display.
        checkInteractionOnDisplay();
        // Change the location and check that the relative location of buttons is same as before.
        checkChangeLocation();
        // Change the size and check that the relative location of buttons is same as before.
        checkChangeSize();
    }

    private double x(Component component) {
        return component.getLocation().getX();
    }

    private double y(Component component) {
        return component.getLocation().getY();
    }

    private void checkRight(JButtonOperator currentButton, JButtonOperator rightButton) {
        // Check that x coordinate of right button is greater than that of right
        // end of current button
        currentButton.waitStateOnQueue(button -> x(button) + button.getWidth() < x(rightButton.getSource()));
        // Check that the y coordinate of the button is same as the button to
        // the right
        currentButton.waitStateOnQueue(button -> y(button) == y(rightButton.getSource()));
        // Check that the height of the button is same as the button to the
        // right
        currentButton.waitStateOnQueue(button -> button.getHeight() == rightButton.getHeight());
    }

    private void checkBelow(JButtonOperator currentButton, JButtonOperator buttonBelow) {
        // Check that y coordinate of button below is greater than that of
        // bottom end of current button
        currentButton.waitStateOnQueue(button -> y(button) + button.getHeight() < y(buttonBelow.getSource()));
        // Check that the x coordinate of the button is same as the button below
        currentButton.waitStateOnQueue(button -> x(button) == x(buttonBelow.getSource()));
        // Check that the width of the button is same as the button below
        currentButton.waitStateOnQueue(button -> button.getWidth() == buttonBelow.getWidth());
    }

    private void checkRelativeLocations() {
        // Check relative location of button 7
        checkRight(buttonSeven, buttonEight);
        checkBelow(buttonSeven, buttonFour);

        // Check relative location of button 8
        checkRight(buttonEight, buttonNine);
        checkBelow(buttonEight, buttonFive);

        // Check relative location of button 9
        checkRight(buttonNine, buttonDivide);
        checkBelow(buttonNine, buttonSix);

        // Check relative location of Division button
        checkRight(buttonDivide, buttonReciprocal);
        checkBelow(buttonDivide, buttonMultiply);

        // Check relative location of Reciprocal and Sqrt buttons
        checkBelow(buttonReciprocal, buttonSqrt);

        // Check relative location of button 4
        checkRight(buttonFour, buttonFive);
        checkBelow(buttonFour, buttonOne);

        // Check relative location of button 5
        checkRight(buttonFive, buttonSix);
        checkBelow(buttonFive, buttonTwo);

        // Check relative location of button 6
        checkRight(buttonSix, buttonMultiply);
        checkBelow(buttonSix, buttonThree);

        // Check relative location of Multiply button
        checkRight(buttonMultiply, buttonSqrt);
        checkBelow(buttonMultiply, buttonMinus);

        // Check relative location of button 1
        checkRight(buttonOne, buttonTwo);
        checkBelow(buttonOne, buttonZero);

        // Check relative location of button 2
        checkRight(buttonTwo, buttonThree);
        checkBelow(buttonTwo, buttonToggleSign);

        // Check relative location of button 3
        checkRight(buttonThree, buttonMinus);
        checkBelow(buttonThree, buttonComma);

        // Check relative location of Minus button
        checkBelow(buttonMinus, buttonPlus);

        // Check relative location of button 0
        checkRight(buttonZero, buttonToggleSign);

        // Check relative location of Sign Toggle Button
        checkRight(buttonToggleSign, buttonComma);

        // Check relative location of Comma button
        checkRight(buttonComma, buttonPlus);

        // Check relative location of plus and Equals buttons
        checkRight(buttonPlus, buttonEquals);

        // Check relative location of JPanel containing Backspace and Reset
        // buttons
        Point parentLocation = getUIValue(backspaceButton, (JButton button) -> button.getParent().getLocation());
        // Check that the y coordinate of bottom of the screen is
        // less than that of parent of backspace button
        tfScreen.waitStateOnQueue(screen -> y(screen) + screen.getHeight() < parentLocation.getY());
        // Check that the y coordinate of the button 7 is greater
        // than that of parent of backspace
        buttonSeven.waitStateOnQueue(button -> parentLocation.getY() < y(button));
        // Check that the y coordinate of reciprocal button is greater
        // than that of parent of backspace
        buttonReciprocal.waitStateOnQueue(button -> parentLocation.getY() < y(button));
        // Check that x coordinate of screen is same as that of parent of
        // backspace
        tfScreen.waitStateOnQueue(screen -> x(screen) == parentLocation.getX());
        // Check that x coordinate of button 7 is same as that of parent of
        // backspace
        buttonSeven.waitStateOnQueue(button -> x(button) == parentLocation.getX());

        // Check relative location of Backspace button
        // Check that the x coordinate of right of backspace button
        // is less than that of reset button
        backspaceButton.waitStateOnQueue(button -> x(button) + button.getWidth() < x(resetButton.getSource()));
        // Check that the height of backspace button is same as that of reset
        // button
        backspaceButton.waitStateOnQueue(button -> button.getHeight() == resetButton.getHeight());
        // Check that the y coordinate bottom of backspace button is less that
        // that of button 7
        backspaceButton.waitStateOnQueue(
                button -> parentLocation.getY() + button.getParent().getHeight() < y(buttonSeven.getSource()));

        // Check that the x coordinate of reset button is greater than that
        // of right of backspace button
        resetButton.waitStateOnQueue(button -> x(backspaceButton.getSource()) + backspaceButton.getWidth() < x(button));

        // Check that the height of reset button is same as that of backspace
        // button
        resetButton.waitStateOnQueue(button -> backspaceButton.getHeight() == button.getHeight());

        // Check that the y coordinate of bottom of reset button is less
        // than that of divide button.
        resetButton.waitStateOnQueue(
                button -> parentLocation.getY() + button.getParent().getHeight() < y(buttonDivide.getSource()));

        // Check that the y coordinate of top of screen is lower
        // than that of parent of backspace button
        tfScreen.waitStateOnQueue(screen -> y(screen) + screen.getHeight() < parentLocation.getY());
    }

    private void checkInteractionOnDisplay() {
        // Check buttons: 1,2,+,=,C
        buttonOne.push();
        tfScreen.waitText("1");
        buttonPlus.push();
        tfScreen.waitText("1");
        buttonTwo.push();
        tfScreen.waitText("2");
        buttonEquals.push();
        tfScreen.waitText("3");
        resetButton.push();
        tfScreen.waitText("0");

        // Check buttons: 3,4,-
        buttonFour.push();
        tfScreen.waitText("4");
        buttonMinus.push();
        tfScreen.waitText("4");
        buttonThree.push();
        tfScreen.waitText("3");
        buttonEquals.push();
        tfScreen.waitText("1");
        reset();

        // Check buttons: 5,6,*
        buttonFive.push();
        tfScreen.waitText("5");
        buttonMultiply.push();
        tfScreen.waitText("5");
        buttonSix.push();
        tfScreen.waitText("6");
        buttonEquals.push();
        tfScreen.waitText("30");
        reset();

        // Check buttons: 8,9,/
        buttonNine.push();
        buttonNine.push();
        tfScreen.waitText("99");
        buttonDivide.push();
        tfScreen.waitText("99");
        buttonEight.push();
        tfScreen.waitText("8");
        buttonEquals.push();
        tfScreen.waitText("12.375");
        reset();

        // Check buttons: 7,0,[+/-],Backspace
        buttonSeven.push();
        tfScreen.waitText("7");
        buttonZero.push();
        tfScreen.waitText("70");
        buttonToggleSign.push();
        tfScreen.waitText("-70");
        buttonToggleSign.push();
        tfScreen.waitText("70");
        backspaceButton.push();
        tfScreen.waitText("7");
        reset();

        // Check Sqrt Button
        buttonFour.push();
        buttonNine.push();
        tfScreen.waitText("49");
        buttonSqrt.push();
        tfScreen.waitText("7");
        reset();

        // Check Reciprocal Button
        buttonFour.push();
        tfScreen.waitText("4");
        buttonReciprocal.push();
        tfScreen.waitText("0.25");
        reset();

        // Check Comma button
        buttonFour.push();
        buttonComma.push();
        tfScreen.waitText("4,");
    }

    private void reset() {
        resetButton.push();
        tfScreen.waitText("0");
    }

    private void initializeUIComponents(String lookAndFeel) throws Exception {
        UIManager.setLookAndFeel(lookAndFeel);
        new ClassReference(GridBagLayoutDemo.class.getCanonicalName()).startApplication();
        mainFrame = new JFrameOperator(GRID_BAG_LAYOUT_DEMO_TITLE);
        mainFrame.setComparator(EXACT_STRING_COMPARATOR);
        buttonZero = new JButtonOperator(mainFrame, ZERO_BUTTON_TITLE);
        buttonOne = new JButtonOperator(mainFrame, ONE_BUTTON_TITLE);
        buttonTwo = new JButtonOperator(mainFrame, TWO_BUTTON_TITLE);
        buttonThree = new JButtonOperator(mainFrame, THREE_BUTTON_TITLE);
        buttonFour = new JButtonOperator(mainFrame, FOUR_BUTTON_TITLE);
        buttonFive = new JButtonOperator(mainFrame, FIVE_BUTTON_TITLE);
        buttonSix = new JButtonOperator(mainFrame, SIX_BUTTON_TITLE);
        buttonSeven = new JButtonOperator(mainFrame, SEVEN_BUTTON_TITLE);
        buttonEight = new JButtonOperator(mainFrame, EIGHT_BUTTON_TITLE);
        buttonNine = new JButtonOperator(mainFrame, NINE_BUTTON_TITLE);
        buttonPlus = new JButtonOperator(mainFrame, PLUS_BUTTON_TITLE);
        buttonMinus = new JButtonOperator(mainFrame, MINUS_BUTTON_TITLE);
        buttonMultiply = new JButtonOperator(mainFrame, MULTIPLY_BUTTON_TITLE);
        buttonDivide = new JButtonOperator(mainFrame, DIVIDE_BUTTON_TITLE);
        buttonComma = new JButtonOperator(mainFrame, ",");
        buttonSqrt = new JButtonOperator(mainFrame, SQRT_BUTTON_TITLE);
        buttonReciprocal = new JButtonOperator(mainFrame, INVERSE_BUTTON_TITLE);
        buttonToggleSign = new JButtonOperator(mainFrame, SWAPSIGN_BUTTON_TITLE);
        buttonEquals = new JButtonOperator(mainFrame, EQUALS_BUTTON_TITLE);
        resetButton = new JButtonOperator(mainFrame, C_BUTTON_TITLE);
        backspaceButton = new JButtonOperator(mainFrame, BACKSPACE_BUTTON_TITLE);
        tfScreen = new JTextFieldOperator(mainFrame, 0);
    }

    private void checkChangeLocation() {
        Point startingPoint = new Point(100, 100);
        mainFrame.setLocation(startingPoint);
        mainFrame.waitComponentLocation(startingPoint);
        checkRelativeLocations();
    }

    private void checkChangeSize() {
        Dimension newSize = new Dimension((int) mainFrame.getToolkit().getScreenSize().getWidth() / 2,
                (int) mainFrame.getToolkit().getScreenSize().getHeight() / 2);
        mainFrame.setSize(newSize);
        mainFrame.waitComponentSize(newSize);
        checkRelativeLocations();
    }
}