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

EventSequence

public class EventSequence extends Object
EventSequence 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 sequence of events and the corresponding screen capture taken at the end of those events.

Fields Summary
private byte[]
capture
private ByteArrayInputStream
inputStream
An input stream of bytes represents events that have been recorded previously and that are passed to this EventSequence
private byte[]
byteArray
The byte array associated with the event sequence.
private ByteArrayOutputStream
outputStream
The output stream represents events that are to be recorded in this EventSequence
static final int
EVENT_START_MARKER
static final int
CAPTURE_START_MARKER
static final int
END_OF_EVENT_SEQUENCE
Constructors Summary
EventSequence()


	// set up the empty input stream and byte array
	byteArray = new byte[0];
	inputStream = new ByteArrayInputStream(byteArray);

	outputStream = new ByteArrayOutputStream();
    
public EventSequence(byte[] input)
Recreate a stored event sequence and its associated screen capture from the given input stream.

param
input The byte[] which was obtained from a previous EventSequence from its getEventSequence() method.

	byteArray = input;
	inputStream = new ByteArrayInputStream(byteArray);
	
	outputStream = new ByteArrayOutputStream();
    
public EventSequence(EventSequence[] sequences)
Create a new EventSequence Object based on a set of other EventSequences. This will create a composite EventSequence object which will be treated as a single sequence by the AutomatedHandler when testing. That is, a call to replayEventSequence() with a composite EventSequence Object will test each sub sequence and its screen capture. If any sub sequence fails, it will return false. If all of the sub sequences pass, it will return true.

param
sequences An array of EventSequence objects to construct a new composite EventSequence from

	try {
	    for (int i = 0; i < sequences.length; i++) {
		this.outputStream.write(sequences[i].toByteArray());
	    }
	} catch (IOException e) {
	    // System.out.println("Exception in writing to stream");
	}
    
public EventSequence(Vector sequences)
Create a new EventSequence Object based on a set of other EventSequences. This will create a composite EventSequence object which will be treated as a single sequence by the AutomatedHandler when testing. That is, a call to replayEventSequence() with a composite EventSequence Object will test each sub sequence and its screen capture. If any sub sequence fails, it will return false. If all of the sub sequences pass, it will return true.

param
sequences A Vector of EventSequence objects to construct a new composite EventSequence from

	
	int length = sequences.size();
	EventSequence q;
	
	try {
	    for (int i = 0; i < length; i++) {
		q = (EventSequence)sequences.elementAt(i);
		this.outputStream.write(q.toByteArray());
	    }
	} catch (IOException e) {
	    // System.out.println("Exception in writing to stream");
	}
    
Methods Summary
voidappendEvent(RecordedEvent event)


	try {

	    outputStream.write(EVENT_START_MARKER);
	    outputStream.write(event.lengthOfByteRep());
	    outputStream.write(event.getAsByte());

	} catch (java.io.IOException e) {
	    System.out.println("EXCEPTION!");
	    // EXCEPTION!
	}

    
public voidappendSequence(com.sun.midp.lcdui.EventSequence sequence)
Append the given EventSequence to this sequence and create a composite event sequence as a result.

param
sequence The EventSequence to append to the end of this sequence.

	try {
	    this.outputStream.write(sequence.toByteArray());
	} catch (IOException e) {
	    // System.out.println("Exception in writing to stream");
	}
    
byte[]getCapture()


	int length = inputStream.read();
	
	byte[] captureByteArray = new byte[length];

	inputStream.read(captureByteArray, 0, length);

	return captureByteArray;
	 
    
RecordedEventgetNextEvent()

	
	RecordedEvent event = null;
	
	int length = inputStream.read();

	if (length == 0xFF) {
	    // if the length byte's value is 0xFF,
	    // then the length is greater than 127 and
	    // needs to be read as an int.

	    // using this as most events' length will
	    // be small engouh, that a byte is sufficient 
	    // to represent it.
	    
	}

	byte[] eventByteArray = new byte[length];

	inputStream.read(eventByteArray, 0, length);

	event = RecordedEvent.recreateRecordedEvent(eventByteArray, 
						    0, length);

	return event;
    
intgetNextObjectType()

	
	return (inputStream.read());
	// returns -1 if end of input stream is reached.
	
    
voidinitializeReplay()

	
	byte[] tempByteArray = new byte[byteArray.length + 
					outputStream.toByteArray().length];

         // Copy existing bytes in the byteArray to tempByteArray
	 System.arraycopy(byteArray,
			  0,
			  tempByteArray,
			  0,
			  byteArray.length);

	 // Append bytes from the output streams' byte array
	 // to tempByteArray
	 // byteArray.length is the offset to start from in the dst
	 System.arraycopy(outputStream.toByteArray(),
			  0,
			  tempByteArray,
			  byteArray.length,
			  outputStream.toByteArray().length);

	 // now tempByteArray holds all the bytes!
	 // make it the byteArray!
	 byteArray = tempByteArray;

	 // reset the output stream
	 outputStream.reset();
	 
	 inputStream = new ByteArrayInputStream(byteArray);
	 // input stream now points at 0 index in the byteArray
    
voidsetScreenCapture(byte[] capture)


        this.capture = capture;

	    outputStream.write(CAPTURE_START_MARKER);
	    outputStream.write(capture.length);

	for (int byteCounter = 0; byteCounter < capture.length; 
	    byteCounter++) {
		outputStream.write(capture[byteCounter]);

	}

    
public byte[]toByteArray()
Store this EventSequence and its corresponding screen capture to the designated output stream.

return
The contents of this event sequence as an array of bytes. This array of bytes can be passed to the constructor to re-create the event sequence.

	// the entire eventSequence consists of the original input sequence
	// null if there was no original input sequence
	// plus the events that were appended
	
	// therefore when we are returning the byte representation,
	// we actually do three things.
	// 1. append any existing bytes in the output array to the input array.
	// 2. reset the output array
	// 3. return the bytearray
	
	byte[] tempByteArray = new byte[byteArray.length + 
					outputStream.toByteArray().length];
	
	// Copy existing bytes in the byteArray to tempByteArray
	System.arraycopy(byteArray,
			 0,
			 tempByteArray,
			 0,
			 byteArray.length);
	
	// Append bytes from the output streams' byte array
	// to tempByteArray
	// byteArray.length is the offset to start from in the dst
	System.arraycopy(outputStream.toByteArray(),
			 0,
			 tempByteArray,
			 byteArray.length,
			 outputStream.toByteArray().length);
	
	// now tempByteArray holds all the bytes!
	// make it the byteArray!
	byteArray = tempByteArray;
	
	// reset the output stream
	outputStream.reset();
	
	return byteArray;