ボタンを含む簡単なアクティビティがあります。ボタンを押すと、2番目のアクティビティが実行されます。今、私はAndroidInstrumentationTestingを初めて使用します。これまでのところ、これは私が書いたものです
public class TestSplashActivity extends
ActivityInstrumentationTestCase2<ActivitySplashScreen> {
private Button mLeftButton;
private ActivitySplashScreen activitySplashScreen;
private ActivityMonitor childMonitor = null;
public TestSplashActivity() {
super(ActivitySplashScreen.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
final ActivitySplashScreen a = getActivity();
assertNotNull(a);
activitySplashScreen=a;
mLeftButton=(Button) a.findViewById(R.id.btn1);
}
@SmallTest
public void testNameOfButton(){
assertEquals("Press Me", mLeftButton.getText().toString());
this.childMonitor = new ActivityMonitor(SecondActivity.class.getName(), null, true);
this.getInstrumentation().addMonitor(childMonitor);
activitySplashScreen.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mLeftButton.performClick();
}});
Activity childActivity=this.getInstrumentation().waitForMonitorWithTimeout(childMonitor, 5000);
assertEquals(childActivity, SecondActivity.class);
}
}
これで、ボタンのテキストを取得する最初のアサーションが機能します。しかし、実行クリックを呼び出すと、例外が発生します
Only the original thread that created a view hierarchy can touch its views.
これで、Androidアプリケーションのコンテキストではこの例外を理解できましたが、インストルメンテーションテストの観点からは理解できました。ボタンのクリックイベントを実行するにはどうすればよいですか。また、2番目のアクティビティが読み込まれたかどうかを確認するにはどうすればよいですか。