在我的代码中,我有HomeActivity.java
。在此活动中,我有3个图像按钮,当我点击任何按钮时,它会打开一个带有ItemListActivity
的新活动(intent.putExtra
)。
例如:
Intent intent = new Intent(HomeActivity.this, ItemListActivity.class);
intent.putExtra("for", "files");
finish();
startActivity(intent);)**
这里是完整的代码:
public class HomeActivityTest extends ActivityInstrumentationTestCase2<HomeActivity> {
private HomeActivity homeActivity;
ImageView ivFiles;
ImageView ivForms;
ImageView ivModels;
private static final int TIMEOUT_IN_MS = 5000;
private Intent mLaunchIntent;
public HomeActivityTest() {
super(HomeActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
try {
/**
* Using ActivityInstrumentationTestCase2
*/
setActivityInitialTouchMode(true);
CommonUtilities.createSharedPreferenceEditor(getInstrumentation().getContext());
homeActivity = getActivity();
ivFiles = (ImageView) homeActivity.findViewById(R.id.ivFilesBtn);
ivForms = (ImageView) homeActivity.findViewById(R.id.ivFormsBtn);
ivModels = (ImageView) homeActivity.findViewById(R.id.ivModelBtn);
assertNotNull("HomeActivity is null", homeActivity);
assertNotNull("File Image is null",ivFiles);
assertNotNull("File Image is null",ivForms);
assertNotNull("File Image is null",ivModels);
} catch (Exception e){
e.printStackTrace();
}
}
@MediumTest
public void testOpenItemListActivity(){
Instrumentation.ActivityMonitor receiverActivityMonitor1 = getInstrumentation()
.addMonitor(ItemListActivity.class.getName(), null, false);
/*
When I click on this ivFiles in HomeActivity, it Opens new ItemListActivity.
Intent intent = new Intent(HomeActivity.this, ItemListActivity.class);
intent.putExtra("for", "files");
finish();
startActivity(intent);
*/
TouchUtils.clickView(this, ivFiles);
ItemListActivity itemListActivity1 = (ItemListActivity) receiverActivityMonitor1
.waitForActivityWithTimeout(TIMEOUT_IN_MS);
assertNotNull("ItemListActivity is null", itemListActivity1);
assertEquals("Monitor for ReceiverActivity has not been called", 1,
receiverActivityMonitor1.getHits());
assertEquals("Activity is of wrong type", ItemListActivity.class,
itemListActivity1.getClass());
assertEquals("Wrong data pass on ivFiles Click", itemListActivity1.dataToOpen, "files");
getInstrumentation().removeMonitor(receiverActivityMonitor1);
receiverActivityMonitor1 = getInstrumentation()
.addMonitor(ItemListActivity.class.getName(), null, false);
/*
When I click on this ivFiles in HomeActivity, it Opens new ItemListActivity.
Intent intent = new Intent(HomeActivity.this, ItemListActivity.class);
intent.putExtra("for", "files");
finish();
startActivity(intent);
*/
TouchUtils.clickView(this, ivModels);
ItemListActivity itemListActivity2 = (ItemListActivity) receiverActivityMonitor1
.waitForActivityWithTimeout(TIMEOUT_IN_MS);
assertNotNull("ItemListActivity is null", itemListActivity2);
assertEquals("Monitor for ReceiverActivity has not been called", 1,
receiverActivityMonitor1.getHits());
assertEquals("Activity is of wrong type", ItemListActivity.class,
itemListActivity1.getClass());
assertEquals("Wrong data pass on ivModels Click", itemListActivity2.dataToOpen, "models");
getInstrumentation().removeMonitor(receiverActivityMonitor1);
}
}
我已为此HomeActivity
编写了HomeActivityTest.java
的测试用例。但是我收到了这个错误:
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
那么我应该如何测试这种情况?
答案 0 :(得分:0)
如果构建服务器上的设备被锁定且屏幕无法访问,您也可以获得此信息。
答案 1 :(得分:-2)
您是否在清单文件中添加了实际权限?
这是<uses-permission android:name="android.permission.INJECT_EVENTS"></uses-permission>
。