Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sanity/client/SwingSet/src/ToggleButtonDemoTest.java
41153 views
1
/*
2
* Copyright (c) 2010, 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
24
import org.jtregext.GuiTestListener;
25
import com.sun.swingset3.DemoProperties;
26
import com.sun.swingset3.demos.togglebutton.DirectionPanel;
27
import com.sun.swingset3.demos.togglebutton.LayoutControlPanel;
28
import com.sun.swingset3.demos.togglebutton.ToggleButtonDemo;
29
import static com.sun.swingset3.demos.togglebutton.ToggleButtonDemo.*;
30
import java.util.function.BiFunction;
31
import javax.swing.UIManager;
32
import org.jemmy2ext.JemmyExt.ByClassChooser;
33
import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;
34
import static org.jemmy2ext.JemmyExt.getBorderTitledJPanelOperator;
35
import static org.jemmy2ext.JemmyExt.getLabeledContainerOperator;
36
import static org.testng.AssertJUnit.*;
37
import org.testng.annotations.Test;
38
import org.netbeans.jemmy.ClassReference;
39
import org.netbeans.jemmy.ComponentChooser;
40
import org.netbeans.jemmy.operators.ContainerOperator;
41
import org.netbeans.jemmy.operators.JCheckBoxOperator;
42
import org.netbeans.jemmy.operators.JFrameOperator;
43
import org.netbeans.jemmy.operators.JRadioButtonOperator;
44
import org.netbeans.jemmy.operators.JTabbedPaneOperator;
45
import org.testng.annotations.Listeners;
46
47
/*
48
* @test
49
* @key headful
50
* @summary Verifies SwingSet3 ToggleButtonDemo by toggling each radio button,
51
* each checkbox and each location of the direction dial toggle.
52
* It verifies initial selected values for all the elements and checks
53
* that those change upon clicking. When toggling radio buttons it
54
* verifies that other radio buttons in the same group are not longer
55
* selected.
56
*
57
* @library /sanity/client/lib/jemmy/src
58
* @library /sanity/client/lib/Extensions/src
59
* @library /sanity/client/lib/SwingSet3/src
60
* @modules java.desktop
61
* java.logging
62
* @build org.jemmy2ext.JemmyExt
63
* @build com.sun.swingset3.demos.togglebutton.ToggleButtonDemo
64
* @run testng/timeout=600 ToggleButtonDemoTest
65
*/
66
@Listeners(GuiTestListener.class)
67
public class ToggleButtonDemoTest {
68
69
@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
70
public void test(String lookAndFeel) throws Exception {
71
UIManager.setLookAndFeel(lookAndFeel);
72
new ClassReference(ToggleButtonDemo.class.getCanonicalName()).startApplication();
73
74
JFrameOperator mainFrame = new JFrameOperator(ToggleButtonDemo.class.getAnnotation(DemoProperties.class).value());
75
JTabbedPaneOperator tabPane = new JTabbedPaneOperator(mainFrame);
76
77
// Radio Button Toggles
78
testRadioButtons(getBorderTitledJPanelOperator(mainFrame, TEXT_RADIO_BUTTONS), 3, null);
79
testRadioButtons(getBorderTitledJPanelOperator(mainFrame, IMAGE_RADIO_BUTTONS), 3, null);
80
testRadioButtons(getLabeledContainerOperator(mainFrame, PAD_AMOUNT), 3, (t, i) -> DEFAULT.equals(t));
81
82
// switch to the Check Boxes Tab
83
tabPane.selectPage(CHECK_BOXES);
84
85
// Check Box Toggles
86
ContainerOperator<?> textCheckBoxesJPanel = getBorderTitledJPanelOperator(mainFrame, TEXT_CHECKBOXES);
87
testCheckBox(textCheckBoxesJPanel, CHECK1, false);
88
testCheckBox(textCheckBoxesJPanel, CHECK2, false);
89
testCheckBox(textCheckBoxesJPanel, CHECK3, false);
90
91
ContainerOperator<?> imageCheckBoxesJPanel = getBorderTitledJPanelOperator(mainFrame, IMAGE_CHECKBOXES);
92
testCheckBox(imageCheckBoxesJPanel, CHECK1, false);
93
testCheckBox(imageCheckBoxesJPanel, CHECK2, false);
94
testCheckBox(imageCheckBoxesJPanel, CHECK3, false);
95
96
ContainerOperator<?> displayOptionsContainer = getLabeledContainerOperator(mainFrame, DISPLAY_OPTIONS);
97
testCheckBox(displayOptionsContainer, PAINT_BORDER, false);
98
testCheckBox(displayOptionsContainer, PAINT_FOCUS, true);
99
testCheckBox(displayOptionsContainer, ENABLED, true);
100
testCheckBox(displayOptionsContainer, CONTENT_FILLED, true);
101
102
// Direction Button Toggles
103
testToggleButtons(mainFrame);
104
}
105
106
/**
107
* The interface is invoked for each radio button providing its name and
108
* index and it should return whether the radio button has to be selected.
109
*/
110
private static interface SelectedRadioButton extends BiFunction<String, Integer, Boolean> {
111
}
112
113
/**
114
* Tests a group of radio buttons
115
*
116
* @param parent container containing the buttons
117
* @param radioButtonCount number of radio buttons
118
* @param selectedRadioButton initial selected radio button
119
*/
120
private void testRadioButtons(ContainerOperator<?> parent, int radioButtonCount, SelectedRadioButton selectedRadioButton) {
121
JRadioButtonOperator[] jrbo = new JRadioButtonOperator[radioButtonCount];
122
for (int i = 0; i < radioButtonCount; i++) {
123
jrbo[i] = new JRadioButtonOperator(parent, i);
124
if (selectedRadioButton != null && selectedRadioButton.apply(jrbo[i].getText(), i)) {
125
assertTrue("Radio Button " + i + " is initially selected", jrbo[i].isSelected());
126
} else {
127
assertFalse("Radio Button " + i + " is initially not selected", jrbo[i].isSelected());
128
}
129
}
130
131
for (int i = 0; i < radioButtonCount; i++) {
132
jrbo[i].push();
133
jrbo[i].waitSelected(true);
134
135
for (int j = 0; j < radioButtonCount; j++) {
136
if (i != j) {
137
jrbo[j].waitSelected(false);
138
}
139
}
140
}
141
}
142
143
/**
144
* Will change the state of the CheckBox then change back to initial state.
145
*
146
* @param parent
147
* @param text
148
* @param expectedValue
149
* @throws Exception
150
*/
151
private void testCheckBox(ContainerOperator<?> parent, String text, boolean expectedValue) {
152
153
System.out.println("Testing " + text);
154
parent.setComparator(EXACT_STRING_COMPARATOR);
155
JCheckBoxOperator jcbo = new JCheckBoxOperator(parent, text);
156
jcbo.waitSelected(expectedValue);
157
158
// click check box (toggle the state)
159
jcbo.push();
160
jcbo.waitSelected(!expectedValue);
161
162
jcbo.push();
163
jcbo.waitSelected(expectedValue);
164
}
165
166
167
/*
168
* testDirectionRadioButtons(JFrameOperator) will toggle each position of
169
* the direction radio button panels
170
*/
171
private void testToggleButtons(JFrameOperator jfo) {
172
ComponentChooser directionPanelChooser = new ByClassChooser(DirectionPanel.class);
173
174
String text_Position = LayoutControlPanel.TEXT_POSITION;
175
ContainerOperator<?> textPositionContainer = getLabeledContainerOperator(jfo, text_Position);
176
ContainerOperator<?> directionPanelOperator = new ContainerOperator<>(textPositionContainer, directionPanelChooser);
177
testRadioButtons(directionPanelOperator, 9, (t, i) -> i == 5);
178
179
// Unfortunately, both directionPanels are in the same parent container
180
// so we have to use indexes here.
181
// There is no guarantee that if the UI changes, the code would be still
182
// valid in terms of which directionPanel is checked first. However, it
183
// does guarantee that two different directionPanels are checked.
184
String content_Alignment = LayoutControlPanel.CONTENT_ALIGNMENT;
185
ContainerOperator<?> contentAlignmentContainer = getLabeledContainerOperator(jfo, content_Alignment);
186
ContainerOperator<?> directionPanelOperator2 = new ContainerOperator<>(contentAlignmentContainer, directionPanelChooser,
187
contentAlignmentContainer.getSource() == textPositionContainer.getSource() ? 1 : 0);
188
testRadioButtons(directionPanelOperator2, 9, (t, i) -> i == 4);
189
190
}
191
192
}
193
194