001: /*
002: * $Id$
003: *
004: * ---------------------------------------------------------------------------
005: *
006: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
007: *
008: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
009: *
010: * The contents of this file are subject to the terms of either the GNU
011: * General Public License Version 2 only ("GPL") or the Common
012: * Development and Distribution License("CDDL") (collectively, the
013: * "License"). You may not use this file except in compliance with the
014: * License. You can obtain a copy of the License at
015: * http://www.netbeans.org/cddl-gplv2.html
016: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
017: * specific language governing permissions and limitations under the
018: * License. When distributing the software, include this License Header
019: * Notice in each file and include the License file at
020: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
021: * particular file as subject to the "Classpath" exception as provided
022: * by Sun in the GPL Version 2 section of the License file that
023: * accompanied this code. If applicable, add the following below the
024: * License Header, with the fields enclosed by brackets [] replaced by
025: * your own identifying information:
026: * "Portions Copyrighted [year] [name of copyright owner]"
027: *
028: * Contributor(s): Manfred Riem (mriem@netbeans.org).
029: *
030: * The Original Software is the Jemmy library. The Initial Developer of the
031: * Original Software is Alexandre Iline. All Rights Reserved.
032: *
033: * If you wish your version of this file to be governed by only the CDDL
034: * or only the GPL Version 2, indicate your decision by adding
035: * "[Contributor] elects to include this software in this distribution
036: * under the [CDDL or GPL Version 2] license." If you do not indicate a
037: * single choice of license, a recipient has the option to distribute
038: * your version of this file under either the CDDL, the GPL Version 2 or
039: * to extend the choice of license to its licensees as provided above.
040: * However, if you add GPL Version 2 code and therefore, elected the GPL
041: * Version 2 license, then the option applies only if the new code is
042: * made subject to such option by the copyright holder.
043: */
044: package org.netbeans.jemmy.operators;
045:
046: import java.awt.event.ActionEvent;
047:
048: import java.awt.event.ActionListener;
049:
050: import javax.swing.JFrame;
051:
052: import javax.swing.JTextField;
053:
054: import javax.swing.SwingConstants;
055:
056: import junit.framework.Test;
057:
058: import junit.framework.TestCase;
059:
060: import junit.framework.TestSuite;
061:
062: import org.netbeans.jemmy.util.NameComponentChooser;
063:
064: /**
065:
066: * A JUnit test for JTextFieldOperator.
067:
068: *
069:
070: * @author Manfred Riem (mriem@netbeans.org)
071:
072: * @version $Revision$
073:
074: */
075:
076: public class JTextFieldOperatorTest extends TestCase {
077:
078: /**
079:
080: * Stores the frame we use for testing.
081:
082: */
083:
084: private JFrame frame;
085:
086: /**
087:
088: * Stores the checkBox we use for testing.
089:
090: */
091:
092: private JTextField textField;
093:
094: /**
095:
096: * Constructor.
097:
098: *
099:
100: * @param testName the name of the test.
101:
102: */
103:
104: public JTextFieldOperatorTest(String testName) {
105:
106: super (testName);
107:
108: }
109:
110: /**
111:
112: * Setup before testing.
113:
114: */
115:
116: protected void setUp() throws Exception {
117:
118: frame = new JFrame();
119:
120: textField = new JTextField("JTextFieldOperatorTest");
121:
122: textField.setName("JTextFieldOperatorTest");
123:
124: frame.getContentPane().add(textField);
125:
126: frame.pack();
127:
128: frame.setLocationRelativeTo(null);
129:
130: }
131:
132: /**
133:
134: * Cleanup after testing.
135:
136: */
137:
138: protected void tearDown() throws Exception {
139:
140: frame.setVisible(false);
141:
142: frame.dispose();
143:
144: frame = null;
145:
146: }
147:
148: /**
149:
150: * Suite method.
151:
152: */
153:
154: public static Test suite() {
155:
156: TestSuite suite = new TestSuite(JTextFieldOperatorTest.class);
157:
158: return suite;
159:
160: }
161:
162: /**
163:
164: * Test constructor.
165:
166: */
167:
168: public void testConstructor() {
169:
170: frame.setVisible(true);
171:
172: JFrameOperator operator1 = new JFrameOperator();
173:
174: assertNotNull(operator1);
175:
176: JTextFieldOperator operator2 = new JTextFieldOperator(operator1);
177:
178: assertNotNull(operator2);
179:
180: JTextFieldOperator operator3 = new JTextFieldOperator(
181: operator1, new NameComponentChooser(
182: "JTextFieldOperatorTest"));
183:
184: assertNotNull(operator3);
185:
186: JTextFieldOperator operator4 = new JTextFieldOperator(
187: operator1, "JTextFieldOperatorTest");
188:
189: assertNotNull(operator4);
190:
191: }
192:
193: /**
194:
195: * Test findJTextField method.
196:
197: */
198:
199: public void testFindJTextField() {
200:
201: frame.setVisible(true);
202:
203: JTextField textField1 = JTextFieldOperator.findJTextField(
204: frame, new NameComponentChooser(
205: "JTextFieldOperatorTest"));
206:
207: assertNotNull(textField1);
208:
209: JTextField textField2 = JTextFieldOperator.findJTextField(
210: frame, "JTextFieldOperatorTest", false, false);
211:
212: assertNotNull(textField2);
213:
214: }
215:
216: /**
217:
218: * Test waitJTextField method.
219:
220: */
221:
222: public void testWaitJTextField() {
223:
224: frame.setVisible(true);
225:
226: JTextField textField1 = JTextFieldOperator.waitJTextField(
227: frame, new NameComponentChooser(
228: "JTextFieldOperatorTest"));
229:
230: assertNotNull(textField1);
231:
232: JTextField textField2 = JTextFieldOperator.waitJTextField(
233: frame, "JTextFieldOperatorTest", false, false);
234:
235: assertNotNull(textField2);
236:
237: }
238:
239: /**
240:
241: * Test waitText method.
242:
243: */
244:
245: public void testWaitText() {
246:
247: frame.setVisible(true);
248:
249: JFrameOperator operator1 = new JFrameOperator();
250:
251: assertNotNull(operator1);
252:
253: JTextFieldOperator operator2 = new JTextFieldOperator(
254: operator1, new NameComponentChooser(
255: "JTextFieldOperatorTest"));
256:
257: assertNotNull(operator2);
258:
259: operator2.waitText("JTextFieldOperatorTest");
260:
261: assertEquals("JTextFieldOperatorTest", textField.getText());
262:
263: operator2.waitText("JTextFieldOperatorTest\n");
264:
265: assertEquals("JTextFieldOperatorTest", textField.getText());
266:
267: }
268:
269: /**
270:
271: * Test addActionListener method.
272:
273: */
274:
275: public void testAddActionListener() {
276:
277: frame.setVisible(true);
278:
279: JFrameOperator operator1 = new JFrameOperator();
280:
281: assertNotNull(operator1);
282:
283: JTextFieldOperator operator2 = new JTextFieldOperator(
284: operator1, new NameComponentChooser(
285: "JTextFieldOperatorTest"));
286:
287: assertNotNull(operator2);
288:
289: ActionListener listener = new ActionListener() {
290:
291: public void actionPerformed(ActionEvent event) {
292:
293: }
294:
295: };
296:
297: operator2.addActionListener(listener);
298:
299: assertEquals(listener, textField.getActionListeners()[0]);
300:
301: operator2.removeActionListener(listener);
302:
303: assertEquals(0, textField.getActionListeners().length);
304:
305: }
306:
307: /**
308:
309: * Test getColumns method.
310:
311: */
312:
313: public void testGetColumns() {
314:
315: frame.setVisible(true);
316:
317: JFrameOperator operator1 = new JFrameOperator();
318:
319: assertNotNull(operator1);
320:
321: JTextFieldOperator operator2 = new JTextFieldOperator(
322: operator1, new NameComponentChooser(
323: "JTextFieldOperatorTest"));
324:
325: assertNotNull(operator2);
326:
327: operator2.setColumns(10);
328:
329: assertEquals(10, operator2.getColumns());
330:
331: assertEquals(textField.getColumns(), operator2.getColumns());
332:
333: }
334:
335: /**
336:
337: * Test of getHorizontalAlignment method, of class org.netbeans.jemmy.operators.JTextFieldOperator.
338:
339: */
340:
341: public void testGetHorizontalAlignment() {
342:
343: frame.setVisible(true);
344:
345: JFrameOperator operator1 = new JFrameOperator();
346:
347: assertNotNull(operator1);
348:
349: JTextFieldOperator operator2 = new JTextFieldOperator(
350: operator1, new NameComponentChooser(
351: "JTextFieldOperatorTest"));
352:
353: assertNotNull(operator2);
354:
355: operator2.setHorizontalAlignment(SwingConstants.RIGHT);
356:
357: assertEquals(SwingConstants.RIGHT, operator2
358: .getHorizontalAlignment());
359:
360: assertEquals(textField.getHorizontalAlignment(), operator2
361: .getHorizontalAlignment());
362:
363: }
364:
365: /**
366:
367: * Test getScrollOffset method.
368:
369: *
370:
371: * @todo See why scroll offset is not working properly. What is the real
372:
373: * contract?
374:
375: */
376:
377: public void testGetScrollOffset() {
378:
379: frame.setVisible(true);
380:
381: JFrameOperator operator1 = new JFrameOperator();
382:
383: assertNotNull(operator1);
384:
385: JTextFieldOperator operator2 = new JTextFieldOperator(
386: operator1, new NameComponentChooser(
387: "JTextFieldOperatorTest"));
388:
389: assertNotNull(operator2);
390:
391: operator2.setScrollOffset(operator2.getScrollOffset());
392:
393: }
394:
395: /**
396:
397: * Test postActionEvent method.
398:
399: */
400:
401: public void testPostActionEvent() {
402:
403: frame.setVisible(true);
404:
405: JFrameOperator operator1 = new JFrameOperator();
406:
407: assertNotNull(operator1);
408:
409: JTextFieldOperator operator2 = new JTextFieldOperator(
410: operator1, new NameComponentChooser(
411: "JTextFieldOperatorTest"));
412:
413: assertNotNull(operator2);
414:
415: operator2.setActionCommand("ACTION_COMMAND");
416:
417: ActionListener1 listener = new ActionListener1();
418:
419: operator2.addActionListener(listener);
420:
421: operator2.postActionEvent();
422:
423: assertEquals(listener.actionCommand, "ACTION_COMMAND");
424:
425: }
426:
427: /**
428:
429: * Inner class used for testing.
430:
431: */
432:
433: public class ActionListener1 implements ActionListener {
434:
435: public String actionCommand;
436:
437: public void actionPerformed(ActionEvent event) {
438:
439: actionCommand = event.getActionCommand();
440:
441: }
442:
443: }
444:
445: /**
446:
447: * Test getHorizontalVisiblity method.
448:
449: */
450:
451: public void testGetHorizontalVisibility() {
452:
453: frame.setVisible(true);
454:
455: JFrameOperator operator1 = new JFrameOperator();
456:
457: assertNotNull(operator1);
458:
459: JTextFieldOperator operator2 = new JTextFieldOperator(operator1);
460:
461: assertNotNull(operator2);
462:
463: operator2.getHorizontalVisibility();
464:
465: }
466:
467: }
|