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