Path: blob/master/test/jdk/sanity/client/SwingSet/src/ToggleButtonDemoTest.java
41153 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.jtregext.GuiTestListener;24import com.sun.swingset3.DemoProperties;25import com.sun.swingset3.demos.togglebutton.DirectionPanel;26import com.sun.swingset3.demos.togglebutton.LayoutControlPanel;27import com.sun.swingset3.demos.togglebutton.ToggleButtonDemo;28import static com.sun.swingset3.demos.togglebutton.ToggleButtonDemo.*;29import java.util.function.BiFunction;30import javax.swing.UIManager;31import org.jemmy2ext.JemmyExt.ByClassChooser;32import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;33import static org.jemmy2ext.JemmyExt.getBorderTitledJPanelOperator;34import static org.jemmy2ext.JemmyExt.getLabeledContainerOperator;35import static org.testng.AssertJUnit.*;36import org.testng.annotations.Test;37import org.netbeans.jemmy.ClassReference;38import org.netbeans.jemmy.ComponentChooser;39import org.netbeans.jemmy.operators.ContainerOperator;40import org.netbeans.jemmy.operators.JCheckBoxOperator;41import org.netbeans.jemmy.operators.JFrameOperator;42import org.netbeans.jemmy.operators.JRadioButtonOperator;43import org.netbeans.jemmy.operators.JTabbedPaneOperator;44import org.testng.annotations.Listeners;4546/*47* @test48* @key headful49* @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 checks52* that those change upon clicking. When toggling radio buttons it53* verifies that other radio buttons in the same group are not longer54* selected.55*56* @library /sanity/client/lib/jemmy/src57* @library /sanity/client/lib/Extensions/src58* @library /sanity/client/lib/SwingSet3/src59* @modules java.desktop60* java.logging61* @build org.jemmy2ext.JemmyExt62* @build com.sun.swingset3.demos.togglebutton.ToggleButtonDemo63* @run testng/timeout=600 ToggleButtonDemoTest64*/65@Listeners(GuiTestListener.class)66public class ToggleButtonDemoTest {6768@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)69public void test(String lookAndFeel) throws Exception {70UIManager.setLookAndFeel(lookAndFeel);71new ClassReference(ToggleButtonDemo.class.getCanonicalName()).startApplication();7273JFrameOperator mainFrame = new JFrameOperator(ToggleButtonDemo.class.getAnnotation(DemoProperties.class).value());74JTabbedPaneOperator tabPane = new JTabbedPaneOperator(mainFrame);7576// Radio Button Toggles77testRadioButtons(getBorderTitledJPanelOperator(mainFrame, TEXT_RADIO_BUTTONS), 3, null);78testRadioButtons(getBorderTitledJPanelOperator(mainFrame, IMAGE_RADIO_BUTTONS), 3, null);79testRadioButtons(getLabeledContainerOperator(mainFrame, PAD_AMOUNT), 3, (t, i) -> DEFAULT.equals(t));8081// switch to the Check Boxes Tab82tabPane.selectPage(CHECK_BOXES);8384// Check Box Toggles85ContainerOperator<?> textCheckBoxesJPanel = getBorderTitledJPanelOperator(mainFrame, TEXT_CHECKBOXES);86testCheckBox(textCheckBoxesJPanel, CHECK1, false);87testCheckBox(textCheckBoxesJPanel, CHECK2, false);88testCheckBox(textCheckBoxesJPanel, CHECK3, false);8990ContainerOperator<?> imageCheckBoxesJPanel = getBorderTitledJPanelOperator(mainFrame, IMAGE_CHECKBOXES);91testCheckBox(imageCheckBoxesJPanel, CHECK1, false);92testCheckBox(imageCheckBoxesJPanel, CHECK2, false);93testCheckBox(imageCheckBoxesJPanel, CHECK3, false);9495ContainerOperator<?> displayOptionsContainer = getLabeledContainerOperator(mainFrame, DISPLAY_OPTIONS);96testCheckBox(displayOptionsContainer, PAINT_BORDER, false);97testCheckBox(displayOptionsContainer, PAINT_FOCUS, true);98testCheckBox(displayOptionsContainer, ENABLED, true);99testCheckBox(displayOptionsContainer, CONTENT_FILLED, true);100101// Direction Button Toggles102testToggleButtons(mainFrame);103}104105/**106* The interface is invoked for each radio button providing its name and107* index and it should return whether the radio button has to be selected.108*/109private static interface SelectedRadioButton extends BiFunction<String, Integer, Boolean> {110}111112/**113* Tests a group of radio buttons114*115* @param parent container containing the buttons116* @param radioButtonCount number of radio buttons117* @param selectedRadioButton initial selected radio button118*/119private void testRadioButtons(ContainerOperator<?> parent, int radioButtonCount, SelectedRadioButton selectedRadioButton) {120JRadioButtonOperator[] jrbo = new JRadioButtonOperator[radioButtonCount];121for (int i = 0; i < radioButtonCount; i++) {122jrbo[i] = new JRadioButtonOperator(parent, i);123if (selectedRadioButton != null && selectedRadioButton.apply(jrbo[i].getText(), i)) {124assertTrue("Radio Button " + i + " is initially selected", jrbo[i].isSelected());125} else {126assertFalse("Radio Button " + i + " is initially not selected", jrbo[i].isSelected());127}128}129130for (int i = 0; i < radioButtonCount; i++) {131jrbo[i].push();132jrbo[i].waitSelected(true);133134for (int j = 0; j < radioButtonCount; j++) {135if (i != j) {136jrbo[j].waitSelected(false);137}138}139}140}141142/**143* Will change the state of the CheckBox then change back to initial state.144*145* @param parent146* @param text147* @param expectedValue148* @throws Exception149*/150private void testCheckBox(ContainerOperator<?> parent, String text, boolean expectedValue) {151152System.out.println("Testing " + text);153parent.setComparator(EXACT_STRING_COMPARATOR);154JCheckBoxOperator jcbo = new JCheckBoxOperator(parent, text);155jcbo.waitSelected(expectedValue);156157// click check box (toggle the state)158jcbo.push();159jcbo.waitSelected(!expectedValue);160161jcbo.push();162jcbo.waitSelected(expectedValue);163}164165166/*167* testDirectionRadioButtons(JFrameOperator) will toggle each position of168* the direction radio button panels169*/170private void testToggleButtons(JFrameOperator jfo) {171ComponentChooser directionPanelChooser = new ByClassChooser(DirectionPanel.class);172173String text_Position = LayoutControlPanel.TEXT_POSITION;174ContainerOperator<?> textPositionContainer = getLabeledContainerOperator(jfo, text_Position);175ContainerOperator<?> directionPanelOperator = new ContainerOperator<>(textPositionContainer, directionPanelChooser);176testRadioButtons(directionPanelOperator, 9, (t, i) -> i == 5);177178// Unfortunately, both directionPanels are in the same parent container179// so we have to use indexes here.180// There is no guarantee that if the UI changes, the code would be still181// valid in terms of which directionPanel is checked first. However, it182// does guarantee that two different directionPanels are checked.183String content_Alignment = LayoutControlPanel.CONTENT_ALIGNMENT;184ContainerOperator<?> contentAlignmentContainer = getLabeledContainerOperator(jfo, content_Alignment);185ContainerOperator<?> directionPanelOperator2 = new ContainerOperator<>(contentAlignmentContainer, directionPanelChooser,186contentAlignmentContainer.getSource() == textPositionContainer.getSource() ? 1 : 0);187testRadioButtons(directionPanelOperator2, 9, (t, i) -> i == 4);188189}190191}192193194