Espresso IntentsTestRule 测试多活动
Espresso IntentsTestRule on Test with Multiple Activities
我正在尝试使用 Espresso-Intents 库去除 Android 相机的结果。
我知道要初始化 Espresso-Inents 库,我需要定义一个 IntentsTestRule。我已经根据我的测试输入的第一个 Activity 定义了规则,即 MainActivity.class,因此规则是这样写的:
@Rule
public IntentsTestRule<MainActivity> mIntentsTestRules = new IntentsTestRule(MainActivity.class);
问题是 MainActivity 永远不会加载,因为启动 MainActivity 的系统 Intent 正在被 Espresso-Intents 捕获..
我收到此异常:
java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.greenpathenergy.facilitysurveyapp/.ui.activities.MainActivity } within 45 seconds.
此外,由于这个 Intent 被 Espresso-Intents 捕获,我需要在同一个 @Test 块中从 MainActivity 移动到 EditorActivity,我如何允许一些内部在编辑器Activity?
中触发的外部意图(例如当编辑器Activity 调用相机API 时)的意图通过
谢谢!
IntentsTestRule 的目的是在@Test 块中的任何测试 运行 之前单独初始化 Esspresso-Intents。 IntentsTestRule 只需在@Test 块之前调用 Intents.init() 并在@Test 块完成后调用 Intents.release()。
话虽这么说,如果您只想在 @Test 块中存根特定的 Intents,则应在 @Test 块中触发外部(对您的应用程序的实例)Intent 的操作之前初始化 Espresso-Intents (例如单击按钮加载相机),并在 return 我们的存根后立即释放 Espresso-Intents。
这是在存根外部 Intent 的同时允许内部 Intent 的最简单的方法。
示例代码:
@Test
public void MainActivityTest {
// Tap the button that loads the EditorActivity from MainActivity
onView(withId(R.id.btn_load_editor_activity)).perform(click());
// Initialize Espresso-Intents library to capture the external Intent to the camera API
Intents.init();
// ActivityResult will be provided as a stub result for the camera API's natural result
// Note: I've ignored the bitmap creation and instead used null for simplicity, you will
// want to mock the bitmap here by creating a fake picture as the resultData
ActivityResult result = new ActivityResult(Activity.RESULT_OK, null);
// Notify Espresso the stub result above should be provided when it sees an Intent to load the camera API
intending(toPackage("com.android.camera2")).respondWith(result);
// Simulate a button tap of the button that loads the camera API, the stub will be automatically returned as the result immediately
// instead of the camera API opening and sending back its result
onView(withId(R.id.btn_take_placard_photo)).perform(click());
// Release the Espresso-Intents library to allow other internal Intents to work as intended without being intercepted by Espresso-Intents
Intents.release();
}
希望对您有所帮助!
我正在尝试使用 Espresso-Intents 库去除 Android 相机的结果。
我知道要初始化 Espresso-Inents 库,我需要定义一个 IntentsTestRule。我已经根据我的测试输入的第一个 Activity 定义了规则,即 MainActivity.class,因此规则是这样写的:
@Rule
public IntentsTestRule<MainActivity> mIntentsTestRules = new IntentsTestRule(MainActivity.class);
问题是 MainActivity 永远不会加载,因为启动 MainActivity 的系统 Intent 正在被 Espresso-Intents 捕获..
我收到此异常:
java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.greenpathenergy.facilitysurveyapp/.ui.activities.MainActivity } within 45 seconds.
此外,由于这个 Intent 被 Espresso-Intents 捕获,我需要在同一个 @Test 块中从 MainActivity 移动到 EditorActivity,我如何允许一些内部在编辑器Activity?
中触发的外部意图(例如当编辑器Activity 调用相机API 时)的意图通过谢谢!
IntentsTestRule 的目的是在@Test 块中的任何测试 运行 之前单独初始化 Esspresso-Intents。 IntentsTestRule 只需在@Test 块之前调用 Intents.init() 并在@Test 块完成后调用 Intents.release()。
话虽这么说,如果您只想在 @Test 块中存根特定的 Intents,则应在 @Test 块中触发外部(对您的应用程序的实例)Intent 的操作之前初始化 Espresso-Intents (例如单击按钮加载相机),并在 return 我们的存根后立即释放 Espresso-Intents。
这是在存根外部 Intent 的同时允许内部 Intent 的最简单的方法。
示例代码:
@Test
public void MainActivityTest {
// Tap the button that loads the EditorActivity from MainActivity
onView(withId(R.id.btn_load_editor_activity)).perform(click());
// Initialize Espresso-Intents library to capture the external Intent to the camera API
Intents.init();
// ActivityResult will be provided as a stub result for the camera API's natural result
// Note: I've ignored the bitmap creation and instead used null for simplicity, you will
// want to mock the bitmap here by creating a fake picture as the resultData
ActivityResult result = new ActivityResult(Activity.RESULT_OK, null);
// Notify Espresso the stub result above should be provided when it sees an Intent to load the camera API
intending(toPackage("com.android.camera2")).respondWith(result);
// Simulate a button tap of the button that loads the camera API, the stub will be automatically returned as the result immediately
// instead of the camera API opening and sending back its result
onView(withId(R.id.btn_take_placard_photo)).perform(click());
// Release the Espresso-Intents library to allow other internal Intents to work as intended without being intercepted by Espresso-Intents
Intents.release();
}
希望对您有所帮助!