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.demos.optionpane.OptionPaneDemo;
  26 import static com.sun.swingset3.demos.optionpane.OptionPaneDemo.*;
  27 import javax.swing.UIManager;
  28 import static org.testng.AssertJUnit.*;
  29 import org.testng.annotations.Test;
  30 import org.netbeans.jemmy.ClassReference;
  31 import org.netbeans.jemmy.operators.JButtonOperator;
  32 import org.netbeans.jemmy.operators.JComboBoxOperator;
  33 import org.netbeans.jemmy.operators.JDialogOperator;
  34 import org.netbeans.jemmy.operators.JFrameOperator;
  35 import org.netbeans.jemmy.operators.JLabelOperator;
  36 import org.netbeans.jemmy.operators.JTextFieldOperator;
  37 import org.testng.annotations.Listeners;
  38 
  39 
  40 /*
  41  * @test
  42  * @key headful
  43  * @summary Verifies SwingSet3 OptionPaneDemo page by opening all the dialogs
  44  *          and choosing different options in them.
  45  *
  46  * @library /sanity/client/lib/jemmy/src
  47  * @library /sanity/client/lib/Extensions/src
  48  * @library /sanity/client/lib/SwingSet3/src
  49  * @build org.jemmy2ext.JemmyExt
  50  * @build com.sun.swingset3.demos.optionpane.OptionPaneDemo
  51  * @run testng OptionPaneDemoTest
  52  */
  53 @Listeners(GuiTestListener.class)
  54 public class OptionPaneDemoTest {
  55 
  56     public static final String SOME_TEXT_TO_TYPE = "I am some text";
  57     public static final String MESSAGE = UIManager.getString("OptionPane.messageDialogTitle");
  58     public static final String OK = "OK";
  59     public static final String CANCEL = "Cancel";
  60     public static final String INPUT = UIManager.getString("OptionPane.inputDialogTitle");
  61     public static final String TEXT_TO_TYPE = "Hooray! I'm a textField";
  62     public static final String NO = "No";
  63     public static final String YES = "Yes";
  64     public static final String SELECT_AN_OPTION = UIManager.getString("OptionPane.titleText");
  65 
  66     @Test
  67     public void test() throws Exception {
  68 
  69         new ClassReference(OptionPaneDemo.class.getCanonicalName()).startApplication();
  70 
  71         JFrameOperator frame = new JFrameOperator(DEMO_TITLE);
  72 
  73         showInputDialog(frame);
  74         showWarningDialog(frame);
  75         showMessageDialog(frame);
  76         showComponentDialog(frame);
  77         showConfirmationDialog(frame);
  78     }
  79 
  80     public void showInputDialog(JFrameOperator jfo) throws Exception {
  81         // Cancel with text case
  82         {
  83             new JButtonOperator(jfo, INPUT_BUTTON).pushNoBlock();
  84 
  85             JDialogOperator jdo = new JDialogOperator(INPUT);
  86             JTextFieldOperator jto = new JTextFieldOperator(jdo);
  87             jto.setText(SOME_TEXT_TO_TYPE);
  88 
  89             assertTrue("Show Input Dialog cancel w/ Text", jdo.isShowing());
  90 
  91             new JButtonOperator(jdo, CANCEL).push();
  92 
  93             assertFalse("Show Input Dialog cancel w/ Text", jdo.isShowing());
  94         }
  95 
  96         // Cancel with *NO* text case
  97         {
  98             new JButtonOperator(jfo, INPUT_BUTTON).pushNoBlock();
  99 
 100             JDialogOperator jdo = new JDialogOperator(INPUT);
 101 
 102             assertTrue("Show Input Dialog cancel w/o Text", jdo.isShowing());
 103 
 104             new JButtonOperator(jdo, CANCEL).push();
 105 
 106             assertFalse("Show Input Dialog cancel w/o Text", jdo.isShowing());
 107         }
 108 
 109         // Text field has *NO* input
 110         {
 111             new JButtonOperator(jfo, INPUT_BUTTON).pushNoBlock();
 112 
 113             JDialogOperator jdo = new JDialogOperator(INPUT);
 114 
 115             assertTrue("Show Input Dialog w/o Input", jdo.isShowing());
 116 
 117             new JButtonOperator(jdo, OK).push();
 118 
 119             assertFalse("Show Input Dialog w/o Input", jdo.isShowing());
 120         }
 121 
 122         // Text field has input
 123         {
 124             final String enteredText = "Rambo";
 125 
 126             new JButtonOperator(jfo, INPUT_BUTTON).pushNoBlock();
 127 
 128             JDialogOperator jdo = new JDialogOperator(INPUT);
 129             JTextFieldOperator jto = new JTextFieldOperator(jdo);
 130             jto.setText(enteredText);
 131             new JButtonOperator(jdo, OK).pushNoBlock();
 132 
 133             JDialogOperator jdo1 = new JDialogOperator(MESSAGE);
 134 
 135             assertTrue("Show Input Dialog w/ Input", jdo1.isShowing());
 136 
 137             final String labelText = enteredText + INPUT_RESPONSE;
 138             JLabelOperator jLabelOperator = new JLabelOperator(jdo1, labelText);
 139             assertEquals("Text from the field made it into the dialog", labelText, jLabelOperator.getText());
 140 
 141             new JButtonOperator(jdo1, OK).push();
 142 
 143             assertFalse("Show Input Dialog w/ Input", jdo1.isShowing());
 144         }
 145     }
 146 
 147     public void showWarningDialog(JFrameOperator jfo) throws Exception {
 148         new JButtonOperator(jfo, WARNING_BUTTON).pushNoBlock();
 149 
 150         JDialogOperator jdo = new JDialogOperator(WARNING_TITLE);
 151 
 152         assertTrue("Show Warning Dialog", jdo.isShowing());
 153 
 154         new JButtonOperator(jdo, OK).push();
 155 
 156         assertFalse("Show Warning Dialog", jdo.isShowing());
 157     }
 158 
 159     public void showMessageDialog(JFrameOperator jfo) throws Exception {
 160         new JButtonOperator(jfo, MESSAGE_BUTTON).pushNoBlock();
 161 
 162         JDialogOperator jdo = new JDialogOperator(MESSAGE);
 163 
 164         assertTrue("Show Message Dialog", jdo.isShowing());
 165 
 166         new JButtonOperator(jdo, OK).push();
 167 
 168         assertFalse("Show Message Dialog", jdo.isShowing());
 169     }
 170 
 171     public void showComponentDialog(JFrameOperator jfo) throws Exception {
 172         // Case: Cancel
 173         {
 174             new JButtonOperator(jfo, COMPONENT_BUTTON).pushNoBlock();
 175 
 176             JDialogOperator jdo = new JDialogOperator(COMPONENT_TITLE);
 177 
 178             assertTrue("Show Component Dialog Cancel Option", jdo.isShowing());
 179 
 180             new JButtonOperator(jdo, COMPONENT_OP5).push();
 181 
 182             assertFalse("Show Component Dialog Cancel Option", jdo.isShowing());
 183         }
 184 
 185         // Case: Yes option selected
 186         {
 187             new JButtonOperator(jfo, COMPONENT_BUTTON).pushNoBlock();
 188 
 189             JDialogOperator jdo = new JDialogOperator(COMPONENT_TITLE);
 190             new JButtonOperator(jdo, COMPONENT_OP1).pushNoBlock();
 191 
 192             JDialogOperator jdo1 = new JDialogOperator(MESSAGE);
 193 
 194             assertTrue("Component Dialog Example Yes Option", jdo1.isShowing());
 195 
 196             final String labelText = COMPONENT_R1;
 197             JLabelOperator jLabelOperator = new JLabelOperator(jdo1, labelText);
 198             assertEquals("Dialog contains appropriate text", labelText, jLabelOperator.getText());
 199 
 200             new JButtonOperator(jdo1, OK).push();
 201 
 202             assertFalse("Component Dialog Example Yes Option", jdo1.isShowing());
 203         }
 204 
 205         // Case: No option selected
 206         {
 207             new JButtonOperator(jfo, COMPONENT_BUTTON).pushNoBlock();
 208 
 209             JDialogOperator jdo = new JDialogOperator(COMPONENT_TITLE);
 210             new JButtonOperator(jdo, COMPONENT_OP2).pushNoBlock();
 211 
 212             JDialogOperator jdo1 = new JDialogOperator(MESSAGE);
 213 
 214             assertTrue("Component Dialog Example No Option", jdo1.isShowing());
 215 
 216             final String labelText = COMPONENT_R2;
 217             JLabelOperator jLabelOperator = new JLabelOperator(jdo1, labelText);
 218             assertEquals("Dialog contains appropriate text", labelText, jLabelOperator.getText());
 219 
 220             new JButtonOperator(jdo1, OK).push();
 221 
 222             assertFalse("Component Dialog Example No Option", jdo1.isShowing());
 223         }
 224 
 225         // Case: Maybe option selected
 226         {
 227             new JButtonOperator(jfo, COMPONENT_BUTTON).pushNoBlock();
 228 
 229             JDialogOperator jdo = new JDialogOperator(COMPONENT_TITLE);
 230             new JButtonOperator(jdo, COMPONENT_OP3).pushNoBlock();
 231 
 232             JDialogOperator jdo1 = new JDialogOperator(MESSAGE);
 233 
 234             assertTrue("Component Dialog Maybe Yes Option", jdo1.isShowing());
 235 
 236             final String labelText = COMPONENT_R3;
 237             JLabelOperator jLabelOperator = new JLabelOperator(jdo1, labelText);
 238             assertEquals("Dialog contains appropriate text", labelText, jLabelOperator.getText());
 239 
 240             new JButtonOperator(jdo1, OK).push();
 241 
 242             assertFalse("Component Dialog Maybe Yes Option", jdo1.isShowing());
 243         }
 244 
 245         // Case: Probably option selected
 246         {
 247             new JButtonOperator(jfo, COMPONENT_BUTTON).pushNoBlock();
 248 
 249             JDialogOperator jdo = new JDialogOperator(COMPONENT_TITLE);
 250             new JButtonOperator(jdo, COMPONENT_OP4).pushNoBlock();
 251 
 252             JDialogOperator jdo1 = new JDialogOperator(MESSAGE);
 253 
 254             assertTrue("Component Dialog Example Probably Option", jdo1.isShowing());
 255 
 256             final String labelText = COMPONENT_R4;
 257             JLabelOperator jLabelOperator = new JLabelOperator(jdo1, labelText);
 258             assertEquals("Dialog contains appropriate text", labelText, jLabelOperator.getText());
 259 
 260             new JButtonOperator(jdo1, OK).push();
 261 
 262             assertFalse("Component Dialog Example Probably Option", jdo1.isShowing());
 263         }
 264 
 265         // Case TextField and ComboBox functional
 266         {
 267             new JButtonOperator(jfo, COMPONENT_BUTTON).push();
 268 
 269             JDialogOperator jdo = new JDialogOperator(COMPONENT_TITLE);
 270 
 271             JTextFieldOperator jto = new JTextFieldOperator(jdo);
 272             jto.clearText();
 273             jto.typeText(TEXT_TO_TYPE);
 274 
 275             JComboBoxOperator jcbo = new JComboBoxOperator(jdo);
 276             jcbo.selectItem(2);
 277 
 278             assertEquals("Show Component Dialog TextField", TEXT_TO_TYPE, jto.getText());
 279             assertEquals("Show Component Dialog ComboBox", 2, jcbo.getSelectedIndex());
 280 
 281             new JButtonOperator(jdo, "cancel").push();
 282         }
 283     }
 284 
 285     public void showConfirmationDialog(JFrameOperator jfo) throws Exception {
 286         // Case: Yes option selected
 287         {
 288             new JButtonOperator(jfo, CONFIRM_BUTTON).pushNoBlock();
 289 
 290             JDialogOperator jdo = new JDialogOperator(SELECT_AN_OPTION);
 291             new JButtonOperator(jdo, YES).pushNoBlock();
 292 
 293             JDialogOperator jdo1 = new JDialogOperator(MESSAGE);
 294 
 295             assertTrue("Show Confirmation Dialog Yes Option", jdo1.isShowing());
 296 
 297             final String labelText = CONFIRM_YES;
 298             JLabelOperator jLabelOperator = new JLabelOperator(jdo1, labelText);
 299             assertEquals("Dialog contains appropriate text", labelText, jLabelOperator.getText());
 300 
 301             new JButtonOperator(jdo1, OK).push();
 302 
 303             assertFalse("Show Confirmation Dialog Yes Option", jdo1.isShowing());
 304         }
 305 
 306         // Case: No option selected
 307         {
 308             new JButtonOperator(jfo, CONFIRM_BUTTON).pushNoBlock();
 309 
 310             JDialogOperator jdo = new JDialogOperator(SELECT_AN_OPTION);
 311             new JButtonOperator(jdo, NO).pushNoBlock();
 312 
 313             JDialogOperator jdo1 = new JDialogOperator(MESSAGE);
 314 
 315             assertTrue("Show Confirmation Dialog No Option", jdo1.isShowing());
 316 
 317             final String labelText = CONFIRM_NO;
 318             JLabelOperator jLabelOperator = new JLabelOperator(jdo1, labelText);
 319             assertEquals("Dialog contains appropriate text", labelText, jLabelOperator.getText());
 320 
 321             new JButtonOperator(jdo1, OK).push();
 322 
 323             assertFalse("Show Confirmation Dialog No Option", jdo1.isShowing());
 324         }
 325 
 326         // Case: Cancel option selected
 327         {
 328             new JButtonOperator(jfo, CONFIRM_BUTTON).pushNoBlock();
 329 
 330             JDialogOperator jdo = new JDialogOperator(SELECT_AN_OPTION);
 331 
 332             assertTrue("Show Confirmation Dialog Cancel Option", jdo.isShowing());
 333 
 334             new JButtonOperator(jdo, CANCEL).push();
 335 
 336             assertFalse("Show Confirmation Dialog Cancel Option", jdo.isShowing());
 337         }
 338     }
 339 
 340 }