我有一个简单的活动包含一个按钮。当我按下按钮时,第二个活动运行。现在我对Android检测测试还不熟悉。到目前为止,这是我写的:
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);
}
现在我获取按钮文本的第一个断言是有效的。但是当我调用perform click时,会出现异常。
Only the original thread that created a view hierarchy can touch its views.
我现在理解了Android应用程序中的这个异常,但是现在想在仪器测试的术语中理解它。我如何执行按钮的点击事件,以及如何检查我的第二个活动是否已加载。