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.demos.textfield.JHistoryTextField;
  25 import com.sun.swingset3.demos.textfield.TextFieldDemo;
  26 import static com.sun.swingset3.demos.textfield.TextFieldDemo.*;
  27 
  28 import java.awt.Color;
  29 import java.awt.Component;
  30 import java.awt.event.KeyEvent;
  31 import java.util.Calendar;
  32 import java.util.Date;
  33 import java.util.Locale;
  34 import javax.swing.JFormattedTextField;
  35 
  36 import static org.jemmy2ext.JemmyExt.*;
  37 
  38 import org.netbeans.jemmy.ClassReference;
  39 import org.netbeans.jemmy.ComponentChooser;
  40 import org.netbeans.jemmy.QueueTool;
  41 import org.netbeans.jemmy.operators.ContainerOperator;
  42 import org.netbeans.jemmy.operators.JButtonOperator;
  43 import org.netbeans.jemmy.operators.JFrameOperator;
  44 import org.netbeans.jemmy.operators.JLabelOperator;
  45 import org.netbeans.jemmy.operators.JPasswordFieldOperator;
  46 import org.netbeans.jemmy.operators.JTextFieldOperator;
  47 
  48 import org.jtregext.GuiTestListener;
  49 
  50 import org.testng.annotations.Listeners;
  51 import org.testng.annotations.Test;
  52 import static org.testng.AssertJUnit.*;
  53 
  54 /*
  55  * @test
  56  * @key headful
  57  * @summary Verifies SwingSet3 TextFieldDemo by entering text in each field and
  58  *          checking that app reacts accordingly.
  59  *
  60  * @library /sanity/client/lib/jemmy/src
  61  * @library /sanity/client/lib/Extensions/src
  62  * @library /sanity/client/lib/SwingSet3/src
  63  * @modules java.desktop
  64  *          java.logging
  65  * @build org.jemmy2ext.JemmyExt
  66  * @build com.sun.swingset3.demos.textfield.TextFieldDemo
  67  * @run testng TextFieldDemoTest
  68  */
  69 @Listeners(GuiTestListener.class)
  70 public class TextFieldDemoTest {
  71 
  72     @Test
  73     public void test() throws Exception {
  74 
  75         new ClassReference(TextFieldDemo.class.getCanonicalName()).startApplication();
  76 
  77         JFrameOperator frame = new JFrameOperator(DEMO_TITLE);
  78 
  79         historyTextField(frame);
  80         dateTextField(frame);
  81         passwordField(frame);
  82     }
  83 
  84     private void historyTextField(JFrameOperator jfo) throws Exception {
  85         JTextFieldOperator jtfo = new JTextFieldOperator(jfo, new ByClassChooser(JHistoryTextField.class));
  86         jtfo.typeText("cat");
  87 
  88         jtfo.pressKey(KeyEvent.VK_DOWN);
  89         jtfo.pressKey(KeyEvent.VK_DOWN);
  90         jtfo.pressKey(KeyEvent.VK_ENTER);
  91 
  92         final String expectedValue = "category";
  93         jtfo.waitText(expectedValue);
  94         assertEquals("Select History Item", expectedValue, jtfo.getText());
  95     }
  96 
  97     public void dateTextField(JFrameOperator jfo) throws Exception {
  98         JTextFieldOperator jtfo = new JTextFieldOperator(jfo,
  99                 new ByClassChooser(JFormattedTextField.class));
 100         ContainerOperator<?> containerOperator = new ContainerOperator<>(jtfo.getParent());
 101         JButtonOperator jbo = new JButtonOperator(containerOperator, GO);
 102         JLabelOperator dowLabel = new JLabelOperator(containerOperator);
 103         Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
 104 
 105         // Check default date Day of the Week
 106         jbo.push();
 107         dowLabel.waitText(calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH));
 108 
 109         // Check Custom Day of the Week
 110         calendar.set(2012, 9, 11); // Represents "Oct 11, 2012"
 111         Date date = calendar.getTime();
 112         String dateString = jtfo.getQueueTool().invokeAndWait(
 113                 new QueueTool.QueueAction<String>("Formatting the value using JFormattedTextField formatter") {
 114 
 115             @Override
 116             public String launch() throws Exception {
 117                 return ((JFormattedTextField) jtfo.getSource()).getFormatter().valueToString(date);
 118             }
 119         });
 120         System.out.println("dateString = " + dateString);
 121         jtfo.enterText(dateString);
 122 
 123         jbo.push();
 124         dowLabel.waitText("Thursday");
 125     }
 126 
 127     public void passwordField(JFrameOperator jfo) throws Exception {
 128         JPasswordFieldOperator password1 = new JPasswordFieldOperator(jfo, 0);
 129         JPasswordFieldOperator password2 = new JPasswordFieldOperator(jfo, 1);
 130 
 131         password1.typeText("password");
 132         password2.typeText("password");
 133 
 134         // Check Matching Passwords
 135         password1.waitState(new ComponentChooser() {
 136             public boolean checkComponent(Component comp) {
 137                 return password1.getBackground().equals(Color.green) &&
 138                        password2.getBackground().equals(Color.green);
 139             }
 140             public String getDescription() {
 141                 return "Passwords to match";
 142             }
 143         });
 144 
 145         // Check non-matching passwords
 146         password2.typeText("passwereertegrs");
 147         password1.waitState(new ComponentChooser() {
 148             public boolean checkComponent(Component comp) {
 149                 return password1.getBackground().equals(Color.white) &&
 150                        password2.getBackground().equals(Color.white);
 151             }
 152             public String getDescription() {
 153                 return "Passwords not to match";
 154             }
 155         });
 156     }
 157 
 158 }