FileDocCategorySizeDatePackage
Focus2AndroidTest.javaAPI DocAndroid 1.5 API4548Wed May 06 22:41:08 BST 2009com.example.android.apis.view

Focus2AndroidTest

public class Focus2AndroidTest extends android.test.AndroidTestCase
This exercises the same logic as {@link Focus2ActivityTest} but in a lighter weight manner; it doesn't need to launch the activity, and it can test the focus behavior by calling {@link FocusFinder} methods directly. {@link Focus2ActivityTest} is still useful to verify that, at an end to end level, key events actually translate to focus transitioning in the way we expect. A good complementary way to use both types of tests might be to have more exhaustive coverage in the lighter weight test case, and a few end to end scenarios in the functional {@link android.test.ActivityInstrumentationTestCase}. This would provide reasonable assurance that the end to end system is working, while avoiding the overhead of having every corner case exercised in the slower, heavier weight way. Even as a lighter weight test, this test still needs access to a {@link Context} to inflate the file, which is why it extends {@link AndroidTestCase}. If you ever need a context to do your work in tests, you can extend {@link AndroidTestCase}, and when run via an {@link android.test.InstrumentationTestRunner}, the context will be injected for you. See {@link com.example.android.apis.app.ForwardingTest} for an example of an Activity unit test. See {@link com.example.android.apis.AllTests} for documentation on running all tests and individual tests in this application.

Fields Summary
private android.view.FocusFinder
mFocusFinder
private android.view.ViewGroup
mRoot
private android.widget.Button
mLeftButton
private android.widget.Button
mCenterButton
private android.widget.Button
mRightButton
Constructors Summary
Methods Summary
protected voidsetUp()

        super.setUp();

        mFocusFinder = FocusFinder.getInstance();

        // inflate the layout
        final Context context = getContext();
        final LayoutInflater inflater = LayoutInflater.from(context);
        mRoot = (ViewGroup) inflater.inflate(R.layout.focus_2, null);

        // manually measure it, and lay it out
        mRoot.measure(500, 500);
        mRoot.layout(0, 0, 500, 500);

        mLeftButton = (Button) mRoot.findViewById(R.id.leftButton);
        mCenterButton = (Button) mRoot.findViewById(R.id.centerButton);
        mRightButton = (Button) mRoot.findViewById(R.id.rightButton);
    
public voidtestGoingLeftFromRightButtonGoesToCenter()

        assertEquals("center should be next focus from right",
                mCenterButton,
                mFocusFinder.findNextFocus(mRoot, mRightButton, View.FOCUS_LEFT));
    
public voidtestGoingRightFromLeftButtonJumpsOverCenterToRight()

        assertEquals("right should be next focus from left",
                mRightButton,
                mFocusFinder.findNextFocus(mRoot, mLeftButton, View.FOCUS_RIGHT));
    
public voidtestPreconditions()
The name 'test preconditions' is a convention to signal that if this test doesn't pass, the test case was not set up properly and it might explain any and all failures in other tests. This is not guaranteed to run before other tests, as junit uses reflection to find the tests.

        assertNotNull(mLeftButton);
        assertTrue("center button should be right of left button",
                mLeftButton.getRight() < mCenterButton.getLeft());
        assertTrue("right button should be right of center button",
                mCenterButton.getRight() < mRightButton.getLeft());