Path: blob/master/test/jdk/sanity/client/SwingSet/src/TextFieldDemoTest.java
41153 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.sun.swingset3.demos.textfield.JHistoryTextField;24import com.sun.swingset3.demos.textfield.TextFieldDemo;25import static com.sun.swingset3.demos.textfield.TextFieldDemo.*;2627import java.awt.Color;28import java.awt.Component;29import java.awt.event.KeyEvent;30import java.util.Calendar;31import java.util.Date;32import java.util.Locale;33import javax.swing.JFormattedTextField;34import javax.swing.UIManager;3536import static org.jemmy2ext.JemmyExt.*;3738import org.netbeans.jemmy.ClassReference;39import org.netbeans.jemmy.ComponentChooser;40import org.netbeans.jemmy.QueueTool;41import org.netbeans.jemmy.operators.ContainerOperator;42import org.netbeans.jemmy.operators.JButtonOperator;43import org.netbeans.jemmy.operators.JFrameOperator;44import org.netbeans.jemmy.operators.JLabelOperator;45import org.netbeans.jemmy.operators.JPasswordFieldOperator;46import org.netbeans.jemmy.operators.JTextFieldOperator;4748import org.jtregext.GuiTestListener;4950import org.testng.annotations.Listeners;51import org.testng.annotations.Test;52import static org.testng.AssertJUnit.*;5354/*55* @test56* @key headful57* @summary Verifies SwingSet3 TextFieldDemo by entering text in each field and58* checking that app reacts accordingly.59*60* @library /sanity/client/lib/jemmy/src61* @library /sanity/client/lib/Extensions/src62* @library /sanity/client/lib/SwingSet3/src63* @modules java.desktop64* java.logging65* @build org.jemmy2ext.JemmyExt66* @build com.sun.swingset3.demos.textfield.TextFieldDemo67* @run testng/timeout=600 TextFieldDemoTest68*/69@Listeners(GuiTestListener.class)70public class TextFieldDemoTest {7172@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)73public void test(String lookAndFeel) throws Exception {74UIManager.setLookAndFeel(lookAndFeel);75new ClassReference(TextFieldDemo.class.getCanonicalName()).startApplication();7677JFrameOperator frame = new JFrameOperator(DEMO_TITLE);7879historyTextField(frame);80dateTextField(frame);81passwordField(frame);82}8384private void historyTextField(JFrameOperator jfo) throws Exception {85JTextFieldOperator jtfo = new JTextFieldOperator(jfo, new ByClassChooser(JHistoryTextField.class));86jtfo.typeText("cat");8788jtfo.pressKey(KeyEvent.VK_DOWN);89jtfo.pressKey(KeyEvent.VK_DOWN);90jtfo.pressKey(KeyEvent.VK_ENTER);9192final String expectedValue = "category";93jtfo.waitText(expectedValue);94assertEquals("Select History Item", expectedValue, jtfo.getText());95}9697public void dateTextField(JFrameOperator jfo) throws Exception {98JTextFieldOperator jtfo = new JTextFieldOperator(jfo,99new ByClassChooser(JFormattedTextField.class));100ContainerOperator<?> containerOperator = new ContainerOperator<>(jtfo.getParent());101JButtonOperator jbo = new JButtonOperator(containerOperator, GO);102JLabelOperator dowLabel = new JLabelOperator(containerOperator);103Calendar calendar = Calendar.getInstance(Locale.ENGLISH);104calendar.setTime((Date) getUIValue(jtfo, jtf -> ((JFormattedTextField)jtf).getValue()));105106// Check default date Day of the Week107jbo.push();108dowLabel.waitText(calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH));109110// Check Custom Day of the Week111calendar.set(2012, 9, 11); // Represents "Oct 11, 2012"112Date date = calendar.getTime();113String dateString = jtfo.getQueueTool().invokeAndWait(114new QueueTool.QueueAction<String>("Formatting the value using JFormattedTextField formatter") {115116@Override117public String launch() throws Exception {118return ((JFormattedTextField) jtfo.getSource()).getFormatter().valueToString(date);119}120});121System.out.println("dateString = " + dateString);122jtfo.enterText(dateString);123124jbo.push();125dowLabel.waitText("Thursday");126}127128public void passwordField(JFrameOperator jfo) throws Exception {129JPasswordFieldOperator password1 = new JPasswordFieldOperator(jfo, 0);130JPasswordFieldOperator password2 = new JPasswordFieldOperator(jfo, 1);131132password1.typeText("password");133password2.typeText("password");134135// Check Matching Passwords136password1.waitState(new ComponentChooser() {137public boolean checkComponent(Component comp) {138return password1.getBackground().equals(Color.green) &&139password2.getBackground().equals(Color.green);140}141public String getDescription() {142return "Passwords to match";143}144});145146// Check non-matching passwords147final Color backgroundColor = UIManager.getColor("TextField.background");148password2.typeText("passwereertegrs");149password1.waitState(new ComponentChooser() {150public boolean checkComponent(Component comp) {151return password1.getBackground().equals(backgroundColor) &&152password2.getBackground().equals(backgroundColor);153}154public String getDescription() {155return "Passwords not to match";156}157});158}159160}161162163