FileDocCategorySizeDatePackage
RequestFocusTest.javaAPI DocAndroid 1.5 API3536Wed May 06 22:42:02 BST 2009com.android.frameworktest.focus

RequestFocusTest

public class RequestFocusTest extends android.test.ActivityInstrumentationTestCase
{@link RequestFocusTest} is set up to exercise cases where the views that have focus become invisible or GONE.

Fields Summary
private android.widget.Button
mTopLeftButton
private android.widget.Button
mBottomLeftButton
private android.widget.Button
mTopRightButton
private android.widget.Button
mBottomRightButton
private android.os.Handler
mHandler
Constructors Summary
public RequestFocusTest()

        super("com.android.frameworktest", RequestFocus.class);
    
Methods Summary
public voidsetUp()

        super.setUp();

        final RequestFocus a = getActivity();
        mHandler = a.getHandler();
        mTopLeftButton = (Button) a.findViewById(R.id.topLeftButton);
        mBottomLeftButton = (Button) a.findViewById(R.id.bottomLeftButton);
        mTopRightButton = (Button) a.findViewById(R.id.topRightButton);
        mBottomRightButton = (Button) a.findViewById(R.id.bottomRightButton);
    
public voidtestPostedRequestFocus()

        mHandler.post(new Runnable() { public void run() {
            mBottomLeftButton.requestFocus();
        }});
        synchronized(this) {
            try {
                wait(500);
            } catch (InterruptedException e) {
                // Don't care.
            }
        }
        assertTrue("Focus should move to bottom left", mBottomLeftButton.hasFocus());
    
public voidtestSetUpConditions()

        assertNotNull(mHandler);
        assertNotNull(mTopLeftButton);
        assertNotNull(mTopRightButton);
        assertNotNull(mBottomLeftButton);
        assertNotNull(mBottomRightButton);
        assertTrue("requestFocus() should work from onCreate.", mBottomRightButton.hasFocus());
    
public voidtestWrongThreadRequestFocusFails()

        try {
            mTopRightButton.requestFocus();
            fail("requestFocus from wrong thread should raise exception.");
        } catch (AndroidRuntimeException e) {
            // Expected.  The actual exception is not public, so we can't catch it.
            assertEquals("android.view.ViewRoot$CalledFromWrongThreadException",
                         e.getClass().getName());
        }