jdk/test/sanity/client/SwingSet/src/ToggleButtonDemoTest.java
changeset 36744 a00905527ec2
child 37677 9774eca96b01
equal deleted inserted replaced
36743:bdc3f1b79fb7 36744:a00905527ec2
       
     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 com.sun.swingset3.DemoProperties;
       
    25 import com.sun.swingset3.demos.togglebutton.DirectionPanel;
       
    26 import com.sun.swingset3.demos.togglebutton.LayoutControlPanel;
       
    27 import com.sun.swingset3.demos.togglebutton.ToggleButtonDemo;
       
    28 import static com.sun.swingset3.demos.togglebutton.ToggleButtonDemo.*;
       
    29 import java.util.function.BiFunction;
       
    30 import org.jemmy2ext.JemmyExt.ByClassChooser;
       
    31 import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;
       
    32 import static org.jemmy2ext.JemmyExt.getBorderTitledJPanelOperator;
       
    33 import static org.jemmy2ext.JemmyExt.getLabeledContainerOperator;
       
    34 import static org.testng.AssertJUnit.*;
       
    35 import org.testng.annotations.Test;
       
    36 import org.netbeans.jemmy.ClassReference;
       
    37 import org.netbeans.jemmy.ComponentChooser;
       
    38 import org.netbeans.jemmy.operators.ContainerOperator;
       
    39 import org.netbeans.jemmy.operators.JCheckBoxOperator;
       
    40 import org.netbeans.jemmy.operators.JFrameOperator;
       
    41 import org.netbeans.jemmy.operators.JRadioButtonOperator;
       
    42 import org.netbeans.jemmy.operators.JTabbedPaneOperator;
       
    43 import static org.jemmy2ext.JemmyExt.captureDebugInfoOnFail;
       
    44 
       
    45 /*
       
    46  * @test
       
    47  * @key headful
       
    48  * @summary Verifies SwingSet3 ToggleButtonDemo by toggling each radio button,
       
    49  *          each checkbox and each location of the direction dial toggle.
       
    50  *          It verifies initial selected values for all the elements and checks
       
    51  *          that those change upon clicking. When toggling radio buttons it
       
    52  *          verifies that other radio buttons in the same group are not longer
       
    53  *          selected.
       
    54  *
       
    55  * @library /sanity/client/lib/jemmy/src
       
    56  * @library /sanity/client/lib/Jemmy2Ext/src
       
    57  * @library /sanity/client/lib/SwingSet3/src
       
    58  * @build org.jemmy2ext.JemmyExt
       
    59  * @build com.sun.swingset3.demos.togglebutton.ToggleButtonDemo
       
    60  * @run testng ToggleButtonDemoTest
       
    61  */
       
    62 public class ToggleButtonDemoTest {
       
    63 
       
    64     @Test
       
    65     public void test() throws Exception {
       
    66         captureDebugInfoOnFail(() -> {
       
    67             new ClassReference(ToggleButtonDemo.class.getCanonicalName()).startApplication();
       
    68 
       
    69             JFrameOperator mainFrame = new JFrameOperator(ToggleButtonDemo.class.getAnnotation(DemoProperties.class).value());
       
    70             JTabbedPaneOperator tabPane = new JTabbedPaneOperator(mainFrame);
       
    71 
       
    72             // Radio Button Toggles
       
    73             testRadioButtons(getBorderTitledJPanelOperator(mainFrame, TEXT_RADIO_BUTTONS), 3, null);
       
    74             testRadioButtons(getBorderTitledJPanelOperator(mainFrame, IMAGE_RADIO_BUTTONS), 3, null);
       
    75             testRadioButtons(getLabeledContainerOperator(mainFrame, PAD_AMOUNT), 3, (t, i) -> DEFAULT.equals(t));
       
    76 
       
    77             // switch to the Check Boxes Tab
       
    78             tabPane.selectPage(CHECK_BOXES);
       
    79 
       
    80             // Check Box Toggles
       
    81             ContainerOperator<?> textCheckBoxesJPanel = getBorderTitledJPanelOperator(mainFrame, TEXT_CHECKBOXES);
       
    82             testCheckBox(textCheckBoxesJPanel, CHECK1, false);
       
    83             testCheckBox(textCheckBoxesJPanel, CHECK2, false);
       
    84             testCheckBox(textCheckBoxesJPanel, CHECK3, false);
       
    85 
       
    86             ContainerOperator<?> imageCheckBoxesJPanel = getBorderTitledJPanelOperator(mainFrame, IMAGE_CHECKBOXES);
       
    87             testCheckBox(imageCheckBoxesJPanel, CHECK1, false);
       
    88             testCheckBox(imageCheckBoxesJPanel, CHECK2, false);
       
    89             testCheckBox(imageCheckBoxesJPanel, CHECK3, false);
       
    90 
       
    91             ContainerOperator<?> displayOptionsContainer = getLabeledContainerOperator(mainFrame, DISPLAY_OPTIONS);
       
    92             testCheckBox(displayOptionsContainer, PAINT_BORDER, false);
       
    93             testCheckBox(displayOptionsContainer, PAINT_FOCUS, true);
       
    94             testCheckBox(displayOptionsContainer, ENABLED, true);
       
    95             testCheckBox(displayOptionsContainer, CONTENT_FILLED, true);
       
    96 
       
    97             // Direction Button Toggles
       
    98             testToggleButtons(mainFrame);
       
    99         });
       
   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 }