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