FileDocCategorySizeDatePackage
TestTextFieldInput.javaAPI DocphoneME MR2 API (J2ME)8767Wed May 02 18:00:18 BST 2007javax.microedition.lcdui

TestTextFieldInput

public class TestTextFieldInput extends TestCase
This test case covers Input Session management code in TextField and TextBox. Bugs it tests against is : 6297297 - IllegalStateException in event processing thread

Fields Summary
Display
display
instance of display
com.sun.midp.util.SerialCallback
scb
callback serialization adaptor
Form
form
instance of form
TextField
tf
instance of text field
static final String
longtext
sample of the long text string
Constructors Summary
Methods Summary
booleanisCommandInMenu(Command c)
Checks the command in Menu

param
c Command
return
true if the command is in menu, false otherwise



                            
       
        Command[] soft2 = display.getWindow().getSoftTwo();
        if (soft2 == null) {
            return false;
        }
        for (int i = 0; i < soft2.length; i++) {
            if (soft2[i] == c) {
                return true;
            }
        }
        return false;  
    
public voidrunTests()
Overridden from TestCase parent. This method will kick off each individual test

        setUp();
        
        testShow();
        testSetConstraints();
        testTraverse();

        tearDown();
    
voidsetUp()
Starts the test

        display = new StubDisplay();
        scb = new SerialCallback(display);
        
        form = new Form("Form");
        tf = new TextField("TextField", longtext, 256, TextField.ANY);
        form.append(tf);

        form.append(new DateField("DateField", DateField.TIME));
    
voidtearDown()
destroy test

    
voidtestSetConstraints()
Tests setConstraints method

        declare("testSetUneditable");
        
        tf.setConstraints(TextField.ANY | TextField.UNEDITABLE);
        verifyInputStates();
        
        declare("testSetNumeric");
        
        // Change input constraints from ANY to NUMERIC editable
        tf.setConstraints(TextField.NUMERIC);
        verifyInputStates();
        // Expect AlphaNumeric input mode does not exist
        verifyAvailableInputModes(true);
    
voidtestShow()
Show the form on the display

        declare("testShow");
        
        // Make form shown
        display.setCurrent(form);
        scb.invokeAndWait();
        assertTrue(form.isShown());
        
        // Expect textfield1 in focus
        verifyInputStates();
        // Expect both AlphaNumeric and Numeric input modes are available
        verifyAvailableInputModes(false);
    
voidtestTraverse()
Tests traverse in/ traverse out

        EventQueue eventQueue = EventQueue.getEventQueue();
        NativeEvent event;

        // Restore textfield to ANY editable
        tf.setConstraints(TextField.ANY);
        tf.setString(longtext);

        FormLFImpl formlf = (FormLFImpl)form.formLF;
        TextFieldLFImpl tfLF = (TextFieldLFImpl)tf.textFieldLF;
        
        declare("testTraverseOut");
        event = new NativeEvent(EventTypes.KEY_EVENT);
        event.intParam1 = EventConstants.PRESSED;
        event.intParam2 = Constants.KEYCODE_DOWN;
        event.intParam4 = display.displayId;
        eventQueue.post(event);
        
        // wait till the focus is transferred
        scb.invokeAndWait();

        assertFalse(tfLF.hasFocus);
        verifyInputStates();
        
        declare("testTraverseIn");
        event = new NativeEvent(EventTypes.KEY_EVENT);
        event.intParam1 = EventConstants.PRESSED;
        event.intParam2 = Constants.KEYCODE_UP;
        event.intParam4 = display.displayId;
        eventQueue.post(event);

        // wait till the focus is transferred
        scb.invokeAndWait();

        assertTrue(tfLF.hasFocus);
        verifyInputStates();
    
voidverifyAvailableInputModes(boolean isNumericOnly)
Verifies available input modes

param
isNumericOnly if only numeric only, false otherwise

        TextFieldLFImpl lf = (TextFieldLFImpl)tf.textFieldLF;
        Command[] cmds = lf.inputMenu.getSubCommands();
        boolean hasAlpha = false, hasNumeric = false;
        AlphaNumericInputMode alphaIM = new AlphaNumericInputMode();
        NumericInputMode numericIM = new NumericInputMode();
        
        if (cmds != null) {
            for (int i = 0; i < cmds.length; i++) {
                if (alphaIM.getName().equals(cmds[i].getLabel())) {
                    hasAlpha = true;
                } else if (numericIM.getName().equals(cmds[i].getLabel())) {
                    hasNumeric = true;
                }
            }
        }
        
        assertTrue("Numeric IM", hasNumeric);
        assertTrue("AlphaNumeric IM", isNumericOnly || hasAlpha);
    
voidverifyInputStates()
Checks the input states

        TextFieldLFImpl lf = (TextFieldLFImpl)tf.textFieldLF;
        if (lf.hasFocus) {
            if (lf.editable) {
                // Input mode indicator layer exists
                assertSame("Indicator layer", 
                           display.getWindow().getTopMostPopup(),
                           lf.inputModeIndicator);
                
                // Input session should have been started
                assertTrue("Input session",
                           lf.inputSession.getCurrentInputMode() != null);
                
                // At least one input mode available for constaint ANY
                assertTrue("Available IMs",
                           lf.inputSession.getAvailableModes().length >= 1);
                
                // Input mode selection command exists in menu
                assertTrue("InputSubMenu", isCommandInMenu(lf.inputMenu));
                
                // cursor is visible
                assertTrue("Cursor", lf.cursor.visible);
                
                // No active autoscrolling task
                assertTrue("AutoScrolling", lf.textScrollPainter == null);
                
            } else {
                // cursor is invisible
                assertTrue("Cursor", !lf.cursor.visible);
                
                // if contents too long, autoscrolling should be activated
                assertTrue("AutoScrolling",
                           lf.textWidth <= lf.scrollWidth ||
                           lf.textScrollPainter != null);
                
                // input session is ended
                assertTrue("Input session",
                           lf.inputSession.getCurrentInputMode() == null);
                
                // Input mode selection command doesn't exist in menu
                assertTrue("InputSubMenu", !isCommandInMenu(lf.inputMenu));
            }
        } else {
            // autoscrolling is not activated
            assertTrue("AutoScrolling", lf.textScrollPainter == null);
            
            // input mode selection command doesn't exist in menu
            assertTrue("InputSubMenu", !isCommandInMenu(lf.inputMenu));
        }