FileDocCategorySizeDatePackage
RecordedEvent.javaAPI DocJ2ME MIDP 2.08810Thu Nov 07 12:02:22 GMT 2002com.sun.midp.lcdui

RecordedEvent

public class RecordedEvent extends Object
RecordedEvent is a class that encapsulates the semantics of a single MIDP event. This class is designed to automate UI testing. It is the representation of a single event which will comprise a larger event sequence.

Fields Summary
static final byte
KEY_EVENT
static final byte
INPUT_METHOD_EVENT
static final byte
MENU_EVENT
static final byte
PEN_EVENT
static final byte
COMMAND_EVENT
static final byte
DELAY_DUMMY_EVENT
byte
type
The byte representation of an event is : type 1 byte timeDelay 4 bytes val1/inputString 4 bytes [val2] 4 bytes [val3] 4 bytes
int
timeDelay
String
inputString
int
val1
int
val2
int
val3
private int
lengthInBytes
Constructors Summary
RecordedEvent(int timeDelay)
Creates a RecordedEvent for a DELAY_DUMMY_EVENT with the given time This is a dummy event used to create a delay.

param
timeDelay The delay (in ms)



    // --- constructors ---

                                   
      
        this.type = DELAY_DUMMY_EVENT;
        this.timeDelay = timeDelay;

	lengthInBytes = 5; // this.type(1) + timeDelay(4) 
    
RecordedEvent(int timeDelay, String input)
Creates a RecordedEvent for an INPUT_METHOD_EVENT with the given type and input string.

param
timeDelay The delay (in ms) since the previous event occurred
param
input The String entered by the input method

        this.type = INPUT_METHOD_EVENT;
        this.timeDelay = timeDelay;
        inputString = input;

	lengthInBytes = 5 + input.length(); // this.type(1) + timeDelay(4) 
                                            // + String.length()
    
RecordedEvent(int timeDelay, int type, int code)
Creates a RecordedEvent for a KEY_EVENT with the given type and key code.

param
timeDelay The delay (in ms) since the previous event occurred
param
type The type of key event (either PRESSED, RELEASED, REPEATED, or TYPED)
param
code The key code of the key involved

	
        this.type = KEY_EVENT;
        this.timeDelay = timeDelay;
        val1 = type;
        val2 = code;

	lengthInBytes = 13; // this.type(1) + timeDelay(4) + val1(4) + val2(4)
    
RecordedEvent(int timeDelay, int type, int x, int y)
Creates a RecordedEvent for a PEN_EVENT with the given type and coordinate location

param
timeDelay The delay (in ms) since the previous event occurred
param
type The type of the POINTER_EVENT (either PRESSED, RELEASED, or DRAGGED)
param
x The x coordinate location of the event
param
y The y coordinate location of the event

        this.type = PEN_EVENT;
        this.timeDelay = timeDelay;
        val1 = type;
        val2 = x;
        val3 = y;

	lengthInBytes = 17; // this.type(1) + timeDelay(4)
                            // + val1(4) + val2(4) + val3(4)
    
RecordedEvent(int timeDelay, int type)
Creates a RecordedEvent for a COMMAND event with the given Command type (or index)

param
timeDelay The delay (in ms) since the previous event occurred
param
type The type of the COMMAND event (either MENU_REQUESTED, MENU_DISMISSED) or the index of the COMMAND selected


        this.type = COMMAND_EVENT;
        this.timeDelay = timeDelay;
        val1 = type;

	lengthInBytes = 9; // this.type(1) + timeDelay(4) + val1(4)
    
Methods Summary
private static intbytesToInt(byte[] byteArray, int offsetInByteArray)
Converts 4 bytes to an int

	
	int retVal = 0;

	retVal =
	    (((int)(byteArray[offsetInByteArray])   & (0xFF)) << 24) |
	    (((int)(byteArray[offsetInByteArray+1]) & (0xFF)) << 16) |
	    (((int)(byteArray[offsetInByteArray+2]) & (0xFF)) <<  8) |
	    (((int)(byteArray[offsetInByteArray+3]) & (0xFF)) <<  0);

	return retVal;
	
    
public byte[]getAsByte()
Return an array of bytes containing the byte representation of this RecordedEvent

	byte[] rawBytes = new byte[lengthInBytes];
	int indexInByteArray;
	
	// byte 0 is the type of event
	rawBytes[0] = this.type;
	
	indexInByteArray = 1;

	// byte 1-4 is the timeDelay
	indexInByteArray += intIntoBytes(rawBytes, timeDelay, 
					 indexInByteArray);

	// bytes specific to the type of event

	switch (this.type) {
	case KEY_EVENT:

	indexInByteArray += intIntoBytes(rawBytes, val1, 
					 indexInByteArray);
	indexInByteArray += intIntoBytes(rawBytes, val2, 
					 indexInByteArray);

	    break;	    

	case INPUT_METHOD_EVENT:

	    indexInByteArray += stringIntoBytes(rawBytes, inputString, 
						indexInByteArray);

	    break;

	case MENU_EVENT:

	    // NYI

	    for (int i = 0; i < (lengthInBytes-5); i++) {
		rawBytes[indexInByteArray++] = 0x00;
	    }
	    break;


	case PEN_EVENT:

	indexInByteArray += intIntoBytes(rawBytes, val1, 
					 indexInByteArray);
	indexInByteArray += intIntoBytes(rawBytes, val2, 
					 indexInByteArray);
	indexInByteArray += intIntoBytes(rawBytes, val3, 
					 indexInByteArray);

	    break;	    

	case COMMAND_EVENT:

	indexInByteArray += intIntoBytes(rawBytes, val1, 
					 indexInByteArray);

	    break;	    

	case DELAY_DUMMY_EVENT:

	    // no extra bytes needed.

	    break;

	default:
	    
	    throw new IllegalArgumentException();
	}

	return rawBytes;
    
intgetTimeDelay()

	return this.timeDelay;
    
bytegetType()

	return this.type;
    
private static intintIntoBytes(byte[] byteArray, int intToConvert, int indexInByteArray)
Copies the 4 individual bytes of an into the array starting from the specified offset


	byteArray[indexInByteArray]   = (byte)((intToConvert >> 24) & 0xFF);
	byteArray[indexInByteArray+1] = (byte)((intToConvert >> 16) & 0xFF);
	byteArray[indexInByteArray+2] = (byte)((intToConvert >>  8) & 0xFF);
	byteArray[indexInByteArray+3] = (byte)((intToConvert >>  0) & 0xFF);

	return 4;
    
intlengthOfByteRep()
Return the length of the byte representation of this RecordedEvent

	return this.lengthInBytes;
    
static com.sun.midp.lcdui.RecordedEventrecreateRecordedEvent(byte[] byteArray, int offset, int length)
Creates a RecordedEvent from an array of bytes. This is called from EventSequence to recreate a RecordedEvent from storage.

param
byteArray The array of bytes to create the event from.


	RecordedEvent returnEvent = null;

	int timeDelay = bytesToInt(byteArray, offset+1);

	switch (byteArray[offset+0]) {
	case KEY_EVENT:
	    
	    returnEvent = new RecordedEvent(timeDelay,
					     bytesToInt(byteArray, offset+5),
					     bytesToInt(byteArray, offset+9));

	    break;	    

	case INPUT_METHOD_EVENT:
	    
	    returnEvent = new RecordedEvent(timeDelay,
					    new String(byteArray,
						       offset+5,
						       length));

	    break;

	case MENU_EVENT:



	    break;

	case PEN_EVENT:

	    returnEvent = new RecordedEvent(timeDelay,
					    bytesToInt(byteArray, offset+5),
					    bytesToInt(byteArray, offset+9),
					    bytesToInt(byteArray, offset+13));

	    break;

	case COMMAND_EVENT:

	    returnEvent = new RecordedEvent(timeDelay,
					    bytesToInt(byteArray, offset+5));
	    break;
	    
	case DELAY_DUMMY_EVENT:

	    returnEvent = new RecordedEvent(timeDelay);

	    break;

	default:
	    throw new IllegalArgumentException();
	}

	return returnEvent;

    
private static intstringIntoBytes(byte[] byteArray, java.lang.String string, int offsetInByteArray)
Copies the byte representation of the String into the array starting from the specified array offset


	System.arraycopy((Object)string.getBytes(), // src
			 0,                         // src_position
			 (Object)byteArray,         // dest
			 offsetInByteArray,         // dest_position
			 string.length());

	return string.length();