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