001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.jellytools;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import javax.swing.JLabel;
047: import junit.framework.Test;
048: import junit.textui.TestRunner;
049: import org.netbeans.jemmy.JemmyProperties;
050: import org.netbeans.junit.NbTestSuite;
051: import org.openide.DialogDescriptor;
052: import org.openide.DialogDisplayer;
053: import org.openide.util.HelpCtx;
054:
055: /**
056: * Test of org.netbeans.jellytools.NbDialogOperator.
057: */
058: public class NbDialogOperatorTest extends JellyTestCase {
059:
060: private static final String TEST_DIALOG_TITLE = "Test Dialog";
061: protected static final String TEST_DIALOG_LABEL = " This is a test dialog.";
062:
063: /** constructor required by JUnit
064: * @param testName method name to be used as testcase
065: */
066: public NbDialogOperatorTest(String testName) {
067: super (testName);
068: }
069:
070: /** method used for explicit testsuite definition
071: */
072: public static Test suite() {
073: NbTestSuite suite = new NbTestSuite(NbDialogOperatorTest.class);
074: return suite;
075: }
076:
077: /** Shows dialog to test. */
078: protected void setUp() {
079: System.out.println("### " + getName() + " ###");
080: showTestDialog(TEST_DIALOG_TITLE);
081: }
082:
083: /** Use for internal test execution inside IDE
084: * @param args command line arguments
085: */
086: public static void main(java.lang.String[] args) {
087: TestRunner.run(suite());
088: }
089:
090: /** Test Close button getter. */
091: public void testBtClose() {
092: new NbDialogOperator(TEST_DIALOG_TITLE).btClose().push();
093: assertTrue("Close not detected correctly.", getResult().equals(
094: DialogDescriptor.CLOSED_OPTION));
095: }
096:
097: /** Test close button pushing. */
098: public void testClose() {
099: new NbDialogOperator(TEST_DIALOG_TITLE).closeByButton();
100: assertTrue("Close not pushed.", getResult().equals(
101: DialogDescriptor.CLOSED_OPTION));
102: }
103:
104: /** Test Help button getter. */
105: public void testBtHelp() {
106: NbDialogOperator dialog = new NbDialogOperator(
107: TEST_DIALOG_TITLE);
108: dialog.btHelp().push();
109: JemmyProperties.setCurrentTimeout(
110: "WindowWaiter.WaitWindowTimeout", 60000);
111: new HelpOperator().close();
112: dialog.close();
113: }
114:
115: /** Test Help button. */
116: public void testHelp() {
117: NbDialogOperator dialog = new NbDialogOperator(
118: TEST_DIALOG_TITLE);
119: dialog.help();
120: JemmyProperties.setCurrentTimeout(
121: "WindowWaiter.WaitWindowTimeout", 60000);
122: new HelpOperator().close();
123: dialog.close();
124: }
125:
126: /** Test OK button getter. */
127: public void testBtOK() {
128: new NbDialogOperator(TEST_DIALOG_TITLE).btOK().push();
129: assertTrue("OK not detected correctly.", getResult().equals(
130: DialogDescriptor.OK_OPTION));
131: }
132:
133: /** Test OK button pushing. */
134: public void testOK() {
135: new NbDialogOperator(TEST_DIALOG_TITLE).ok();
136: assertTrue("OK not pushed.", getResult().equals(
137: DialogDescriptor.OK_OPTION));
138: }
139:
140: /** Test Cancel button getter. */
141: public void testBtCancel() {
142: new NbDialogOperator(TEST_DIALOG_TITLE).btCancel().push();
143: assertTrue("Cancel not detected correctly.", getResult()
144: .equals(DialogDescriptor.CANCEL_OPTION));
145: }
146:
147: /** Test Cancel button pushing. */
148: public void testCancel() {
149: new NbDialogOperator(TEST_DIALOG_TITLE).cancel();
150: assertTrue("Cancel not pushed.", getResult().equals(
151: DialogDescriptor.CANCEL_OPTION));
152: }
153:
154: /** Test Yes button getter. */
155: public void testBtYes() {
156: new NbDialogOperator(TEST_DIALOG_TITLE).btYes().push();
157: assertTrue("Yes not detected correctly.", getResult().equals(
158: DialogDescriptor.YES_OPTION));
159: }
160:
161: /** Test Yes button pushing. */
162: public void testYes() {
163: new NbDialogOperator(TEST_DIALOG_TITLE).yes();
164: assertTrue("Yes not pushed.", getResult().equals(
165: DialogDescriptor.YES_OPTION));
166: }
167:
168: /** Test No button getter. */
169: public void testBtNo() {
170: new NbDialogOperator(TEST_DIALOG_TITLE).btNo().push();
171: assertTrue("No not detected correctly.", getResult().equals(
172: DialogDescriptor.NO_OPTION));
173: }
174:
175: /** Test No button pushing. */
176: public void testNo() {
177: new NbDialogOperator(TEST_DIALOG_TITLE).no();
178: assertTrue("No not pushed.", getResult().equals(
179: DialogDescriptor.NO_OPTION));
180: }
181:
182: private TestLabel label;
183:
184: /** Opens modal dialog with OK, Cancel, Yes, No, Close and Help buttons.
185: * @param testDialogTitle title of test dialog
186: */
187: protected void showTestDialog(String testDialogTitle) {
188: Object[] options = new Object[] { DialogDescriptor.OK_OPTION,
189: DialogDescriptor.CANCEL_OPTION,
190: DialogDescriptor.YES_OPTION,
191: DialogDescriptor.NO_OPTION,
192: DialogDescriptor.CLOSED_OPTION };
193: label = new TestLabel(TEST_DIALOG_LABEL);
194: DialogDescriptor dd = new DialogDescriptor(label,
195: testDialogTitle, false, options, null,
196: DialogDescriptor.BOTTOM_ALIGN, null, label);
197: dd.setHelpCtx(new HelpCtx(""));
198: DialogDisplayer.getDefault().createDialog(dd).setVisible(true);
199: }
200:
201: /** Gets pushed button from Test Dialog. */
202: private Object getResult() {
203: return label.getEvent().getSource();
204: }
205:
206: /** Label intended to use in tested dialog. It enables to find out which
207: * button was pushed by getEvent() method.
208: */
209: private class TestLabel extends JLabel implements ActionListener {
210:
211: /** Create a new label. */
212: public TestLabel(String text) {
213: super (text);
214: }
215:
216: private ActionEvent lastEvent = null;
217:
218: /** Called when a button is pushed. Stores event to be able to get it
219: * later. */
220: public void actionPerformed(ActionEvent actionEvent) {
221: lastEvent = actionEvent;
222: }
223:
224: /** Gets last performed event. Need to detect what button was pushed. */
225: public ActionEvent getEvent() {
226: return lastEvent;
227: }
228: }
229:
230: }
|