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.optionpane.OptionPaneDemo;
25import static com.sun.swingset3.demos.optionpane.OptionPaneDemo.*;
26
27import javax.swing.UIManager;
28
29import org.jtregext.GuiTestListener;
30
31import org.netbeans.jemmy.ClassReference;
32import org.netbeans.jemmy.operators.Operator.DefaultStringComparator;
33import org.netbeans.jemmy.operators.JButtonOperator;
34import org.netbeans.jemmy.operators.JComboBoxOperator;
35import org.netbeans.jemmy.operators.JDialogOperator;
36import org.netbeans.jemmy.operators.JFrameOperator;
37import org.netbeans.jemmy.operators.JLabelOperator;
38import org.netbeans.jemmy.operators.JTextFieldOperator;
39
40import org.testng.annotations.Listeners;
41import org.testng.annotations.Test;
42import static org.testng.AssertJUnit.*;
43
44
45/*
46 * @test
47 * @key headful
48 * @summary Verifies SwingSet3 OptionPaneDemo page by opening all the dialogs
49 *          and choosing different options in them.
50 *
51 * @library /sanity/client/lib/jemmy/src
52 * @library /sanity/client/lib/Extensions/src
53 * @library /sanity/client/lib/SwingSet3/src
54 * @modules java.desktop
55 *          java.logging
56 * @build org.jemmy2ext.JemmyExt
57 * @build com.sun.swingset3.demos.optionpane.OptionPaneDemo
58 * @run testng OptionPaneDemoTest
59 */
60@Listeners(GuiTestListener.class)
61public class OptionPaneDemoTest {
62
63    public static final String SOME_TEXT_TO_TYPE = "I am some text";
64    public static final String MESSAGE = UIManager.getString("OptionPane.messageDialogTitle");
65    public static final String OK = "OK";
66    public static final String CANCEL = "Cancel";
67    public static final String INPUT = UIManager.getString("OptionPane.inputDialogTitle");
68    public static final String TEXT_TO_TYPE = "Hooray! I'm a textField";
69    public static final String NO = "No";
70    public static final String YES = "Yes";
71    public static final String SELECT_AN_OPTION = UIManager.getString("OptionPane.titleText");
72
73    @Test
74    public void test() throws Exception {
75
76        new ClassReference(OptionPaneDemo.class.getCanonicalName()).startApplication();
77
78        JFrameOperator frame = new JFrameOperator(DEMO_TITLE);
79
80        showInputDialog(frame);
81        showWarningDialog(frame);
82        showMessageDialog(frame);
83        showComponentDialog(frame);
84        showConfirmationDialog(frame);
85    }
86
87    private void checkMessage(String message) {
88        JDialogOperator jdo = new JDialogOperator(MESSAGE);
89        new JLabelOperator(jdo, message);
90        new JButtonOperator(jdo, OK).push();
91        jdo.waitClosed();
92    }
93
94    private void useInputDialog(JFrameOperator jfo, String textToType, String buttonToPush) {
95        new JButtonOperator(jfo, INPUT_BUTTON).pushNoBlock();
96        JDialogOperator jdo = new JDialogOperator(INPUT);
97        if(textToType != null) {
98            JTextFieldOperator jto = new JTextFieldOperator(jdo);
99            jto.typeText(textToType);
100            jto.waitText(textToType);
101        }
102        new JButtonOperator(jdo, buttonToPush).push();
103        jdo.waitClosed();
104    }
105
106    public void showInputDialog(JFrameOperator jfo) throws Exception {
107        // Cancel with text case
108        useInputDialog(jfo, SOME_TEXT_TO_TYPE, CANCEL);
109        //TODO: wait for no dialog displayed
110
111        // Cancel with *NO* text case
112        useInputDialog(jfo, null, CANCEL);
113        //TODO: wait for no dialog displayed
114
115        // Text field has *NO* input
116        useInputDialog(jfo, null, OK);
117        //TODO: wait for no dialog displayed
118
119        // Text field has input
120        {
121            final String enteredText = "Rambo";
122
123            useInputDialog(jfo, enteredText, OK);
124            checkMessage(enteredText + INPUT_RESPONSE);
125        }
126    }
127
128    public void showWarningDialog(JFrameOperator jfo) throws Exception {
129        new JButtonOperator(jfo, WARNING_BUTTON).pushNoBlock();
130
131        JDialogOperator jdo = new JDialogOperator(WARNING_TITLE);
132
133        new JButtonOperator(jdo, OK).push();
134
135        jdo.waitClosed();
136    }
137
138    public void showMessageDialog(JFrameOperator jfo) throws Exception {
139        new JButtonOperator(jfo, MESSAGE_BUTTON).pushNoBlock();
140
141        JDialogOperator jdo = new JDialogOperator(MESSAGE);
142
143        new JButtonOperator(jdo, OK).push();
144
145        jdo.waitClosed();
146    }
147
148    private void callADialogAndClose(JFrameOperator jfo, String buttonToOpenDialog,
149                                     String dialogTitle, String buttonToPush) {
150        new JButtonOperator(jfo, buttonToOpenDialog).pushNoBlock();
151        JDialogOperator jdo = new JDialogOperator(dialogTitle);
152        new JButtonOperator(jdo, buttonToPush).push();
153        jdo.waitClosed();
154    }
155
156    public void showComponentDialog(JFrameOperator jfo) throws Exception {
157        // Case: Cancel
158        callADialogAndClose(jfo, COMPONENT_BUTTON, COMPONENT_TITLE, COMPONENT_OP5);
159        //TODO: wait for no dialog displayed
160
161        // Case: Yes option selected
162        {
163            callADialogAndClose(jfo, COMPONENT_BUTTON, COMPONENT_TITLE, COMPONENT_OP1);
164            checkMessage(COMPONENT_R1);
165        }
166
167        // Case: No option selected
168        {
169            callADialogAndClose(jfo, COMPONENT_BUTTON, COMPONENT_TITLE, COMPONENT_OP2);
170            checkMessage(COMPONENT_R2);
171        }
172
173        // Case: Maybe option selected
174        {
175            callADialogAndClose(jfo, COMPONENT_BUTTON, COMPONENT_TITLE, COMPONENT_OP3);
176            checkMessage(COMPONENT_R3);
177        }
178
179        // Case: Probably option selected
180        {
181            callADialogAndClose(jfo, COMPONENT_BUTTON, COMPONENT_TITLE, COMPONENT_OP4);
182            checkMessage(COMPONENT_R4);
183        }
184
185        // Case TextField and ComboBox functional
186        {
187            new JButtonOperator(jfo, COMPONENT_BUTTON).push();
188
189            JDialogOperator jdo = new JDialogOperator(COMPONENT_TITLE);
190
191            JTextFieldOperator jto = new JTextFieldOperator(jdo);
192            jto.clearText();
193            jto.typeText(TEXT_TO_TYPE);
194            jto.waitText(TEXT_TO_TYPE);
195
196            JComboBoxOperator jcbo = new JComboBoxOperator(jdo);
197            jcbo.selectItem(2);
198            jcbo.waitItemSelected(2);
199
200            new JButtonOperator(jdo, COMPONENT_OP5).push();
201            jdo.waitClosed();
202            //TODO: wait for no dialog displayed
203        }
204    }
205
206    public void showConfirmationDialog(JFrameOperator jfo) throws Exception {
207        // Case: Yes option selected
208        {
209            callADialogAndClose(jfo, CONFIRM_BUTTON, SELECT_AN_OPTION, YES);
210            checkMessage(CONFIRM_YES);
211        }
212
213        // Case: No option selected
214        {
215            callADialogAndClose(jfo, CONFIRM_BUTTON, SELECT_AN_OPTION, NO);
216            checkMessage(CONFIRM_NO);
217        }
218
219        // Case: Cancel option selected
220        {
221            callADialogAndClose(jfo, CONFIRM_BUTTON, SELECT_AN_OPTION, CANCEL);
222            //TODO: wait for no dialog displayed
223        }
224    }
225
226}
227