我正在尝试通过Eclipse中的Android检测工具和Android JUnit将模拟意图发送到Android活动。
我能够成功创建一个将Intent
发送到Activity
的测试,但我希望自动执行此操作并连续发送几个Intents
,以便我可以测试{{1}在Activity
中将许多数据作为“额外”放入。
我的代码(适用于单个Intent)如下:
Intent
如何在public class SearchTest extends ActivityInstrumentationTestCase2<SearchResults> {
private ListActivity mActivity;
private ArrayList<String> testManifest = new ArrayList<String>();
TextView tv;
public SearchTest() {
super("org.fdroid.fdroid", SearchResults.class);
}//SearchTest
@Override
protected void setUp() throws Exception{
setUpTestManifest();
super.setUp();
setActivityInitialTouchMode(false);
Intent i = new Intent(Intent.ACTION_SEARCH);
i.setClassName("org.fdroid.fdroid", "org.fdroid.fdroid.SearchResults");
i.putExtra(SearchManager.QUERY, testManifest.get(0));
setActivityIntent(i);
mActivity = getActivity();
tv = (TextView) mActivity.findViewById(R.id.description);
}//setUp
public void testSearchResult(){
assertTrue(mActivity.getListView().getCount() > 0);
}//testSearchResult
public void setUpTestManifest(){
//populate the test manifest
testManifest.add("Sample Key Word 1");
testManifest.add("Sample Key Word 2");
testManifest.add("Sample Key Word 3");
}//setupManifest
}//SearchTest
中拥有数百个项目并创建一个Intent并测试每个项目?
答案 0 :(得分:5)
您是否尝试过从setUp代码中启动活动并进入测试方法中的循环?例如,
protected void setUp() {
setUpTestManifest();
super.setUp();
}
public void testSearchResult(){
for (String query : testManifest) {
setActivityInitialTouchMode(false);
Intent i = new Intent(Intent.ACTION_SEARCH);
i.setClassName("org.fdroid.fdroid", "org.fdroid.fdroid.SearchResults");
i.putExtra(SearchManager.QUERY, query);
setActivityIntent(i);
mActivity = getActivity();
tv = (TextView) mActivity.findViewById(R.id.description);
assertTrue(mActivity.getListView().getCount() > 0);
mActivity.finish(); // close the activity
setActivity(null); // forces next call of getActivity to re-open the activity
}
}
-Dan