001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.lcdui;
028:
029: import com.sun.midp.i3test.*;
030:
031: import com.sun.midp.chameleon.*;
032: import com.sun.midp.chameleon.input.*;
033: import java.util.Vector;
034:
035: import com.sun.midp.events.*;
036: import com.sun.midp.lcdui.EventConstants;
037: import com.sun.midp.configurator.Constants;
038: import com.sun.midp.util.SerialCallback;
039:
040: /**
041: * This test case covers Input Session management code in TextField and
042: * TextBox.
043: * Bugs it tests against is :
044: * 6297297 - IllegalStateException in event processing thread
045: */
046: public class TestTextFieldInput extends TestCase {
047: /** instance of display */
048: Display display;
049: /** callback serialization adaptor */
050: SerialCallback scb;
051: /** instance of form */
052: Form form;
053: /** instance of text field */
054: TextField tf;
055: /** sample of the long text string */
056: static final String longtext = "some"
057: + " long long long long long long long long"
058: + " long long long long long long long long"
059: + " long long long long long long long long" + " text";
060:
061: /**
062: * Checks the command in Menu
063: *
064: * @param c Command
065: * @return true if the command is in menu, false otherwise
066: */
067: boolean isCommandInMenu(Command c) {
068: Command[] soft2 = display.getWindow().getSoftTwo();
069: if (soft2 == null) {
070: return false;
071: }
072: for (int i = 0; i < soft2.length; i++) {
073: if (soft2[i] == c) {
074: return true;
075: }
076: }
077: return false;
078: }
079:
080: /**
081: * Checks the input states
082: */
083: void verifyInputStates() {
084: TextFieldLFImpl lf = (TextFieldLFImpl) tf.textFieldLF;
085: if (lf.hasFocus) {
086: if (lf.editable) {
087: // Input mode indicator layer exists
088: assertSame("Indicator layer", display.getWindow()
089: .getTopMostPopup(), lf.inputModeIndicator);
090:
091: // Input session should have been started
092: assertTrue("Input session", lf.inputSession
093: .getCurrentInputMode() != null);
094:
095: // At least one input mode available for constaint ANY
096: assertTrue("Available IMs", lf.inputSession
097: .getAvailableModes().length >= 1);
098:
099: // Input mode selection command exists in menu
100: assertTrue("InputSubMenu",
101: isCommandInMenu(lf.inputMenu));
102:
103: // cursor is visible
104: assertTrue("Cursor", lf.cursor.visible);
105:
106: // No active autoscrolling task
107: assertTrue("AutoScrolling",
108: lf.textScrollPainter == null);
109:
110: } else {
111: // cursor is invisible
112: assertTrue("Cursor", !lf.cursor.visible);
113:
114: // if contents too long, autoscrolling should be activated
115: assertTrue("AutoScrolling",
116: lf.textWidth <= lf.scrollWidth
117: || lf.textScrollPainter != null);
118:
119: // input session is ended
120: assertTrue("Input session", lf.inputSession
121: .getCurrentInputMode() == null);
122:
123: // Input mode selection command doesn't exist in menu
124: assertTrue("InputSubMenu",
125: !isCommandInMenu(lf.inputMenu));
126: }
127: } else {
128: // autoscrolling is not activated
129: assertTrue("AutoScrolling", lf.textScrollPainter == null);
130:
131: // input mode selection command doesn't exist in menu
132: assertTrue("InputSubMenu", !isCommandInMenu(lf.inputMenu));
133: }
134:
135: }
136:
137: /**
138: * Verifies available input modes
139: * @param isNumericOnly if only numeric only, false otherwise
140: */
141: void verifyAvailableInputModes(boolean isNumericOnly) {
142: TextFieldLFImpl lf = (TextFieldLFImpl) tf.textFieldLF;
143: Command[] cmds = lf.inputMenu.getSubCommands();
144: boolean hasAlpha = false, hasNumeric = false;
145: AlphaNumericInputMode alphaIM = new AlphaNumericInputMode();
146: NumericInputMode numericIM = new NumericInputMode();
147:
148: if (cmds != null) {
149: for (int i = 0; i < cmds.length; i++) {
150: if (alphaIM.getName().equals(cmds[i].getLabel())) {
151: hasAlpha = true;
152: } else if (numericIM.getName().equals(
153: cmds[i].getLabel())) {
154: hasNumeric = true;
155: }
156: }
157: }
158:
159: assertTrue("Numeric IM", hasNumeric);
160: assertTrue("AlphaNumeric IM", isNumericOnly || hasAlpha);
161: }
162:
163: /**
164: * Starts the test
165: */
166: void setUp() {
167: display = new StubDisplay();
168: scb = new SerialCallback(display);
169:
170: form = new Form("Form");
171: tf = new TextField("TextField", longtext, 256, TextField.ANY);
172: form.append(tf);
173:
174: form.append(new DateField("DateField", DateField.TIME));
175: }
176:
177: /**
178: * Show the form on the display
179: */
180: void testShow() {
181: declare("testShow");
182:
183: // Make form shown
184: display.setCurrent(form);
185: scb.invokeAndWait();
186: assertTrue(form.isShown());
187:
188: // Expect textfield1 in focus
189: verifyInputStates();
190: // Expect both AlphaNumeric and Numeric input modes are available
191: verifyAvailableInputModes(false);
192: }
193:
194: /**
195: * Tests setConstraints method
196: */
197: void testSetConstraints() {
198: declare("testSetUneditable");
199:
200: tf.setConstraints(TextField.ANY | TextField.UNEDITABLE);
201: verifyInputStates();
202:
203: declare("testSetNumeric");
204:
205: // Change input constraints from ANY to NUMERIC editable
206: tf.setConstraints(TextField.NUMERIC);
207: verifyInputStates();
208: // Expect AlphaNumeric input mode does not exist
209: verifyAvailableInputModes(true);
210: }
211:
212: /**
213: * Tests traverse in/ traverse out
214: */
215: void testTraverse() {
216: EventQueue eventQueue = EventQueue.getEventQueue();
217: NativeEvent event;
218:
219: // Restore textfield to ANY editable
220: tf.setConstraints(TextField.ANY);
221: tf.setString(longtext);
222:
223: FormLFImpl formlf = (FormLFImpl) form.formLF;
224: TextFieldLFImpl tfLF = (TextFieldLFImpl) tf.textFieldLF;
225:
226: declare("testTraverseOut");
227: event = new NativeEvent(EventTypes.KEY_EVENT);
228: event.intParam1 = EventConstants.PRESSED;
229: event.intParam2 = Constants.KEYCODE_DOWN;
230: event.intParam4 = display.displayId;
231: eventQueue.post(event);
232:
233: // wait till the focus is transferred
234: scb.invokeAndWait();
235:
236: assertFalse(tfLF.hasFocus);
237: verifyInputStates();
238:
239: declare("testTraverseIn");
240: event = new NativeEvent(EventTypes.KEY_EVENT);
241: event.intParam1 = EventConstants.PRESSED;
242: event.intParam2 = Constants.KEYCODE_UP;
243: event.intParam4 = display.displayId;
244: eventQueue.post(event);
245:
246: // wait till the focus is transferred
247: scb.invokeAndWait();
248:
249: assertTrue(tfLF.hasFocus);
250: verifyInputStates();
251: }
252:
253: /**
254: * destroy test
255: */
256: void tearDown() {
257: }
258:
259: /**
260: * Overridden from TestCase parent. This method will kick off each
261: * individual test
262: */
263: public void runTests() {
264: setUp();
265:
266: testShow();
267: testSetConstraints();
268: testTraverse();
269:
270: tearDown();
271: }
272: }
|