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

TestDisplayPreempt

public class TestDisplayPreempt extends TestCase
This test is designed to verify values returned by Displayable.isShown. According to the spec isShown should be true only Displayable is visible in the foreground and no system screen/menu is up.

Fields Summary
com.sun.midp.midlet.MIDletStateHandler
msHandler
DisplayEventHandlerImpl
deHandler
MIDlet[]
midlet
Display[]
display
Display
preempt
SystemAlert[]
A
Form[]
F
Command[]
C
Constructors Summary
Methods Summary
protected voidendMIDlet0()
Terminates DummyMIDlet

        midlet[0].notifyDestroyed();
	// Since we are calling MIDlet.notifyDestroyed(), MIDlet.destroyApp()
	// will not be called by System. So make it up here.
	try {
	    ((DummyMIDlet)midlet[0]).destroyApp(true);
	} catch (MIDletStateChangeException ignore) {}
    
protected voidendMIDlet1()
Terminates DummyMIDlet2

        midlet[1].notifyDestroyed();
	// Since we are calling MIDlet.notifyDestroyed(), MIDlet.destroyApp()
	// will not be called by System. So make it up here.
	try {
	    ((DummyMIDlet2)midlet[1]).destroyApp(true);
	} catch (MIDletStateChangeException ignore) {}
    
booleanisShown(Display d, Displayable s)

        
         
        return d.isShown(s.displayableLF);
    
public voidrunTests()

        int i,j;

        for (i=0; i<2; i++ ) {
            for (j=0; j<2; j++ ) {
                String suffix = "_" + i + "_" + j;
                //A[i][j] = new Alert("A" + suffix);
                F[i][j] = new Form("F" + suffix);
                
                C[i][j][0] = new Command("SCREEN" + suffix, Command.SCREEN, 1);
                C[i][j][1] = new Command("BACK" + suffix, Command.BACK, 1);
                C[i][j][2] = new Command("ITEM" + suffix, Command.ITEM, 1);
                
                F[i][j].addCommand(C[i][j][0]);
                F[i][j].addCommand(C[i][j][1]);
                F[i][j].addCommand(C[i][j][2]);
            }
        }

        msHandler = MIDletStateHandler.getMidletStateHandler();
        deHandler = Display.displayEventHandlerImpl;

        // -------------------------------------------------------
        
        test3();
        test1();
        test2();

        // -------------------------------------------------------
        
    
public static voidsleep(int time)

        try {
            Thread.sleep(time);
        } catch (Exception e) {}
    
protected voidstartMIDlet0()
starts DummyMIDlet

        try {
            // Start a new instance of DummyMIDlet
            msHandler.startMIDlet("com.sun.midp.util.DummyMIDlet",
                                  "DummyMIDlet");
            
            // Wait for async request to be processed 
            while (DummyMIDlet.midlet == null) {
                try { Thread.sleep(100); } catch (InterruptedException ie) {}
            }
            midlet[0] = DummyMIDlet.midlet;
            display[0] = Display.getDisplay(midlet[0]);
            display[0].setCurrent(null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    
protected voidstartMIDlet1()
starts DummyMIDlet2

        try {
            // Start a new instance of DummyMIDlet
            msHandler.startMIDlet("com.sun.midp.util.DummyMIDlet2",
                                  "DummyMIDlet2");
            
            // Wait for async request to be processed 
            while (DummyMIDlet2.midlet == null) {
                try { Thread.sleep(100); } catch (InterruptedException ie) {}
            }
            midlet[1] = DummyMIDlet2.midlet;
            display[1] = Display.getDisplay(midlet[1]);
            display[1].setCurrent(null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    
protected voidtest1()
test checks how SystemAlert preempts headless midlet

        declare("TestDisplayPreempt 1");
        
        // start midlet w/o screens - no modal dialogs started 
        startMIDlet0();
        display[0].setCurrent(null);
        midlet[0].resumeRequest();
        sleep(500);
        // modal dialog not yet exists - empty screen is in background
        assertFalse("MIDlet before Alert : Display 0 has foreground", display[0].hasForeground);
        sleep(500);
        // create modal dialog - midlet w/o screens exists
        A[0][1] = new SystemAlert(deHandler, "A_0_1", "Alert with MIDlet", null, AlertType.INFO);
        A[0][1].run();
        A[0][1].setTimeout(1500);
        // preempt = deHandler.preemptingDisplay.getDisplay();
        sleep(500);
        // modal dialog is active ... - empty screen is hidden 
        // assertTrue("MIDlet with Alert : Preempt Display has foreground", preempt.hasForeground);
        assertFalse("MIDlet with Alert : Display 0 has foreground", display[0].hasForeground);
        sleep(1500);
        // modal dialog has gone ... - empty screen appears in background again 
        // assertFalse("MIDlet after Alert : Preempt Display has foreground", preempt.hasForeground);
        assertFalse("MIDlet after Alert : Display 0 has foreground", display[0].hasForeground);

        // destroy midlet
        display[0].setCurrent(null);
        sleep(500);
        endMIDlet0();
    
protected voidtest2()
test checks how SystemAlert preempts normal midlet (with screens)

        declare("TestDisplayPreempt 2");
        
        // start midlet with screens - no modal dialogs started
        startMIDlet1();
        display[1].setCurrent(F[1][0]);
        midlet[1].resumeRequest();
        sleep(500);
        // modal dialog not yet exists - midlet screen is active 
        assertTrue("MIDlet before Alert : Display 1 has foreground", display[1].hasForeground);
        assertTrue("MIDlet before Alert : F 1 0 is shown", isShown(display[1], F[1][0]));
        sleep(500);
        // create modal dialog - midlet with screens exists
        A[1][1] = new SystemAlert(deHandler, "A_1_1", "Alert with MIDlet", null, AlertType.INFO);
        A[1][1].run();
        A[1][1].setTimeout(1500);
        // preempt = deHandler.preemptingDisplay.getDisplay();
        sleep(500);
        // assertTrue("MIDlet with Alert : Preempt Display has foreground", preempt.hasForeground);
        assertFalse("MIDlet with Alert : Display 0 has foreground", display[1].hasForeground);
        assertFalse("MIDlet with Alert : F 1 0 is shown", isShown(display[1], F[1][0]));
        // while modal dialog is active - change current midlet screen
        display[1].setCurrent(F[1][1]);
        sleep(500);
        // modal dialog is active ... - midlet screen is hidden 
        // assertTrue("Alert with MIDlet : Preempt Display has foreground", preempt.hasForeground);
        assertFalse("Alert with MIDlet : Display 1 has foreground", display[1].hasForeground);
        assertFalse("Alert with MIDlet : F 1 0 is shown", isShown(display[1], F[1][0]));
        assertFalse("Alert with MIDlet : F 1 1 is shown", isShown(display[1], F[1][1]));
        sleep(1500);
        // modal dialog has gone ... - midlet screen appears again 
        // assertFalse("MIDlet after Alert : Preempt Display has foreground", preempt.hasForeground);
        assertTrue("MIDlet after Alert : Display 0 has foreground", display[1].hasForeground);
        assertFalse("MIDlet after Alert : F 1 0 is shown", isShown(display[1], F[1][0]));
        assertTrue("MIDlet after Alert : F 1 1 is shown", isShown(display[1], F[1][1]));

        display[0].setCurrent(null);
        sleep(500);
        endMIDlet1();
    
protected voidtest3()
test checks how systemAlert w/o midlets works

        declare("TestDisplayPreempt 3");
        
        // create modal dialog (no midlets exist)
        A[0][0] = new SystemAlert(deHandler, "A_0_0", "Alert w/o MIDlet", null, AlertType.WARNING);
        A[0][0].run();
        A[0][0].setTimeout(1000);
        // preempt = deHandler.preemptingDisplay.getDisplay();
        sleep(500);
        // dialog is active 
        // assertTrue("Alert w/o MIDlet : Preempt Display has foreground", display[1].hasForeground);
        sleep(1000);
        // dialog has gone !
        // assertFalse("Alert w/o MIDlet : Preempt Display has foreground", display[1].hasForeground);
        
        sleep(500);
        
        assertTrue(true);