MainActivity
点击按钮Button
,跳转至另一界面OtherActivity
Instrumentation
,执行点击Button的动作,判断跳转后的界面的Activity是否为预期的ActivityActivityMonitor
,该类用来监控应用中的单个活动,可监控一些指定的意图。ActivityMonitor
实例后,通过调用Instrumentation.addMonitor
方法添加这个实例,当目标活动启动后,系统会匹配Instrumentation中的ActivityMonitor实例列表,如果匹配,就会累加计数器package com.example.demo.test;
import com.example.demo.MainActivity;
import android.app.Instrumentation.ActivityMonitor;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
public class MainActivityTest extends
ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mActivity;
private Button button;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
//启动待测应用并打开待测活动
mActivity = getActivity();
button = (Button) mActivity
.findViewById(com.example.demo.R.id.button);
}
//测试Button
public void testButton() {
//跳转后的界面的Activity名为com.example.demo.OtherActivity
ActivityMonitor am = getInstrumentation().addMonitor(
"com.example.demo.OtherActivity", null, false);
//点击操作运行在待测应用的线程中
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
button.performClick();
}
});
//设定等待满足要求的活动创建成功,最多等待5s
am.waitForActivityWithTimeout(5000);
//活动创建成功,am.getHits()值为1,否则为0
assertEquals(1, am.getHits());
}
}
Android Instrumentation 判断跳转后的Activity
原文地址:http://blog.csdn.net/gb112211/article/details/45295029