我有一个包含按钮的简单活动。当我按下按钮时,第二个活动运行。现在我是 Android Instrumentation Testing 的新手。到目前为止,这就是我写的
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 应用程序的上下文中理解了这个异常,但现在在仪器测试方面。如何在按钮上执行点击事件,以及如何检查我的第二个活动是否已加载。