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