Home > Android > androidの単体テスト(AndroidTestCase)

androidの単体テスト(AndroidTestCase)

12月 29th, 2009

 引き続き単体テストのお話。今回は「AndroidTestCase」に焦点をあてます。ApiDemosでAndroidTestCaseを使用しているのはFocus2AndroidTestです。

・AndroidTestCase
 Focus2AndroidTestのjavadocには以下のように書いてあります。

/**
* 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.
*/

 要約すると、ActivityInstrumentationTestCase2よりライトなテストを行うのがAndroidTestCaseであり、Activityをlaunchすることなく、テストを行えるのが特徴で、FocusFinderクラスを使用することにより、UIに対して直接フォーカス移動等のテストが行える、ということのようです。

 まず、setUpメソッドを見てみます。
・com.example.android.apis.view.Focus2AndroidTest

public class Focus2AndroidTest extends AndroidTestCase {

    private FocusFinder mFocusFinder;

    private ViewGroup mRoot;

    private Button mLeftButton;
    private Button mCenterButton;
    private Button mRightButton;

    @Override
    protected void setUp() throws Exception {
        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);
    }

 ここで行っているのは、FocusFinderのインスタンスを作成し、コンテキストからテスト対象のActivityのViewGroupのレイアウトを設定することです。Activityのライフサイクルを通さないので全部自前でやる必要がある、ということですね。

 テストは以下のようになります。

    @SmallTest
    public void testGoingRightFromLeftButtonJumpsOverCenterToRight() {
        assertEquals("right should be next focus from left",
                mRightButton,
                mFocusFinder.findNextFocus(mRoot, mLeftButton, View.FOCUS_RIGHT));
    }

 FocusFinderによって次のフォーカスに移動させ、その移動先がLeftButtonであることを確認しています。

・ということで。
 フォーカスの簡単な確認であればAndroidTestCaseでテストできますが、Activityのライフサイクルもテストしたい場合はActivityInstrumentationTestCase2でテストする、という形ですね。

taga Android , , ,

  1. No comments yet.
  1. No trackbacks yet.