Activityinstrumentationtestcase2
最終更新日13 Feb 2015 00:58
ActivityInstrumentationTestCase2でできること
- アクティビティ内のButtonやEditText等のすべてのウィジェットの操作が可能です。
- [メニュー]、[ホーム]、[戻る]を含めたすべての端末のキーボード入力をシミュレートすることができます。
- 一つのテストメソッドで、対象アクティビティのonCreate()からonDestroy()までが実行されます。
- 別のアクティビティを起動してその状態を確認するような、アクティビティをまたがった試験はできません。[3]
- タッチイベントを生成する
ソースコードの例
ボタンを自動で押させる
package sample.button; import android.test.ActivityInstrumentationTestCase2; import android.widget.Button; import android.widget.TextView; public class SampleButtonTest extends ActivityInstrumentationTestCase2<SampleButton> { private TextView textView1; private Button button1; public SampleButtonTest() { super("sample.button", SampleButton.class); } @Override protected void setUp() throws Exception { super.setUp(); button1 = (Button) getActivity().findViewById(R.id.button1); textView1 = (TextView) getActivity().findViewById(R.id.textView1); } public void testOnClick() { getActivity().runOnUiThread( new Runnable() { @Override public void run() { // ボタンを押す button1.performClick(); } }); // 待機する getInstrumentation().waitForIdleSync(); // TextViewに表示された文字列を確認する assertEquals("Hello World!", textView1.getText()); }
自動で画面タッチさせる
long eventtime = SystemClock.uptimeMillis(); MotionEvent event = MotionEvent.obtain(downtime, eventtime, MotionEvent.ACTION_DOWN, 100, 100, 0); mInstrumentation.sendPointerSync(event); eventtime = SystemClock.uptimeMillis() + 1000; event = MotionEvent.obtain(downtime, eventtime, MotionEvent.ACTION_UP, 100, 100, 0); mInstrumentation.sendPointerSync(event);
Bibliography