FileDocCategorySizeDatePackage
TestEventQueue.javaAPI DocphoneME MR2 API (J2ME)7583Wed May 02 18:00:16 BST 2007com.sun.midp.events

TestEventQueue

public class TestEventQueue extends TestCase
Unit tests for the EventQueue class.

Fields Summary
Constructors Summary
Methods Summary
public voidrunTests()
Runs all tests.

        declare("testCreate");
        testCreate();
        declare("testRegister");
        testRegister();
        declare("testGrowDispatchTable");
        testGrowDispatchTable();
        declare("testPost1");
        testPost1();
        declare("testPost3");
        testPost3();
        declare("testPreprocess");
        testPreprocess();
    
voidtestCreate()
Test simple creation of an event queue.

        EventQueue eq = new EventQueue();

        assertFalse(eq.alive);
        assertNotNull(eq.dispatchTable);
        assertTrue(eq.dispatchTable.length > 0);
        assertNotNull(eq.pool);
        assertEquals(-1, eq.nativeEventQueueHandle);
        assertNotNull(eq.eventQueueThread);
        assertNotNull(eq.eventMonitorThread);
        assertFalse(eq.eventQueueThread.isAlive());
        assertFalse(eq.eventMonitorThread.isAlive());
    
voidtestGrowDispatchTable()
Tests whether the dispatch table is grown properly.

        final int EVENT_TYPE_A = 4;
        final int EVENT_TYPE_B = 97;
            // must be larger than the dispatch table default size

        EventQueue eq = new EventQueue();

        assertTrue(EVENT_TYPE_B > eq.dispatchTable.length);

        InstrumentedEventListener iela = new InstrumentedEventListener();
        InstrumentedEventListener ielb = new InstrumentedEventListener();

        eq.registerEventListener(EVENT_TYPE_A, iela);
        eq.registerEventListener(EVENT_TYPE_B, ielb);

        DispatchData dda = eq.dispatchTable[EVENT_TYPE_A-1];
        assertNotNull(dda);
        assertEquals(iela, dda.listener);

        DispatchData ddb = eq.dispatchTable[EVENT_TYPE_B-1];
        assertNotNull(ddb);
        assertEquals(ielb, ddb.listener);
    
voidtestPost1()
Tests posting of an event.

        final int EVENT_TYPE = 14;
        EventQueue eq = new EventQueue();

        InstrumentedEventListener iel = new InstrumentedEventListener();
        eq.registerEventListener(EVENT_TYPE, iel);

        Event ev = new Event(EVENT_TYPE);
        eq.post(ev);

        // assertions on the event queue

        assertSame("nextEvent should be ev", ev, eq.nextEvent);
        assertSame("lastEvent should be ev", ev, eq.lastEvent);

        // assertions from the event listener

        Event[] arr;

        arr = iel.getProcessedEvents();
        assertEquals("processed should be length 0", 0, arr.length);

        arr = iel.getPreprocessedEvents();
        assertEquals("preprocessed should be length 1", 1, arr.length);
        assertSame("preprocessed[0] should be ev", ev, arr[0]);

        arr = iel.getWaitingEvents();
        assertEquals("waiting should be length 1", 1, arr.length);
        assertNull("waiting[0] should be null", arr[0]);
    
voidtestPost3()
Tests posting of three events.

        final int EVENT_TYPE_A = 5;
        final int EVENT_TYPE_B = 7;

        EventQueue eq = new EventQueue();

        InstrumentedEventListener iel = new InstrumentedEventListener();
        eq.registerEventListener(EVENT_TYPE_A, iel);
        eq.registerEventListener(EVENT_TYPE_B, iel);

        Event ev0 = new Event(EVENT_TYPE_A);
        Event ev1 = new Event(EVENT_TYPE_B);
        Event ev2 = new Event(EVENT_TYPE_A);
        eq.post(ev0);
        eq.post(ev1);
        eq.post(ev2);

        // assertions on the event queue

        assertSame("nextEvent should be ev0", ev0, eq.nextEvent);
        assertSame("lastEvent should be ev2", ev2, eq.lastEvent);
        assertSame("ev0.next should be ev1", ev1, ev0.next);
        assertSame("ev1.next should be ev2", ev2, ev1.next);
        assertNull("ev2.next should be null", ev2.next);

        // assertions from the event listener

        Event[] arr;

        arr = iel.getProcessedEvents();
        assertEquals("processed should be length 0", 0, arr.length);

        arr = iel.getPreprocessedEvents();
        assertEquals("preprocessed should be length 3", 3, arr.length);
        assertSame("preprocessed[0] should be ev0", ev0, arr[0]);
        assertSame("preprocessed[1] should be ev1", ev1, arr[1]);
        assertSame("preprocessed[2] should be ev2", ev2, arr[2]);

        arr = iel.getWaitingEvents();
        assertEquals("waiting should be length 3", 3, arr.length);
        assertNull("waiting[0] should be null", arr[0]);
        assertNull("waiting[1] should be null", arr[1]);
        assertEquals("waiting[2] should be ev0", ev0, arr[2]);
    
voidtestPreprocess()
Tests preprocessing of events.

        EventQueue eq = new EventQueue();
        InstrumentedEventListener iel = new InstrumentedEventListener(true);
        final int EVENT_TYPE = 10;

        eq.registerEventListener(EVENT_TYPE, iel);

        Event ev0 = new Event(EVENT_TYPE);
        Event ev1 = new Event(EVENT_TYPE);
        eq.post(ev0);
        iel.setPreprocess(false);
        eq.post(ev1);

        // assertions on the event queue

        assertSame("nextEvent should be ev0", ev0, eq.nextEvent);
        assertSame("lastEvent should be ev0", ev0, eq.lastEvent);
        assertNull("ev0.next should be null", ev0.next);

        // assertions from the event listener

        Event[] arr;

        arr = iel.getProcessedEvents();
        assertEquals("processed should be length 0", 0, arr.length);

        arr = iel.getPreprocessedEvents();
        assertEquals("preprocessed should be length 2", 2, arr.length);
        assertSame("preprocessed[0] should be ev0", ev0, arr[0]);
        assertSame("preprocessed[1] should be ev1", ev1, arr[1]);

        arr = iel.getWaitingEvents();
        assertEquals("waiting should be length 2", 2, arr.length);
        assertNull("waiting[0] should be null", arr[0]);
        assertEquals("waiting[1] should be ev0", ev0, arr[1]);
    
voidtestRegister()
Tests the ability to register an event.

        final int EVENT_TYPE = 7;
        EventQueue eq = new EventQueue();
        InstrumentedEventListener iel = new InstrumentedEventListener();
        eq.registerEventListener(EVENT_TYPE, iel);

        DispatchData dd = eq.dispatchTable[EVENT_TYPE-1];
        assertNotNull(dd);
        assertEquals(iel, dd.listener);