我使用UI测试测试我的应用,并想检查相机应用是否打开。
我这样做了:
@Test
public void profileImageClickOpensCamera() {
mIntentsRule.getActivity().startActivity(new Intent(mIntentsRule.getActivity(), ProfileActivity.class));
onView(withId(R.id.circleProfileImage)).perform(click());
intended(toPackage("com.android.camera"));
}
它在大多数设备上工作正常,但如果我在SAMSUNG Galaxy S8上下雨,它有" com.sec.android.app.camera"它的相机应用包,测试失败。
我的问题是,我怎么能用浓缩咖啡检查包装中是否含有"相机" ?
这不是最好的解决方案,因为设备的相机应用程序包的名称可能是任何东西,但甚至比我所了解的更好。
所以我想做类似的事情:
intended(StringContains(toPackage("com.android.camera")));
有什么建议吗?
提前致谢。
答案 0 :(得分:1)
我遇到了同样的情况,我设法解决了这个问题:
PackageManager packageManager = InstrumentationRegistry.getTargetContext().getPackageManager();
String pack = resultData.resolveActivity(packageManager).getPackageName();
intended(toPackage(pack));
在我的情况下,我有一个带有按钮的活动,可以打开相机,让您拍照并在活动中返回。这个测试的完整代码是:
@Test
public void testCameraIntent() {
Bitmap icon = BitmapFactory.decodeResource(
InstrumentationRegistry.getTargetContext().getResources(),
R.drawable.husky);
// Build a result to return from the Camera app
Intent resultData = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
resultData.putExtra("data", icon);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
PackageManager packageManager = InstrumentationRegistry.getTargetContext().getPackageManager();
String pack = resultData.resolveActivity(packageManager).getPackageName();
// Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond
// with the ActivityResult we just created
intending(toPackage(pack)).respondWith(result);
// Now that we have the stub in place, click on the button in our app that launches into the Camera
onView(withId(R.id.btn_takePicture)).perform(click());
intended(toPackage(pack));
}
答案 1 :(得分:1)
您可以测试意图操作而不是包。
intended(hasAction(MediaStore.ACTION_IMAGE_CAPTURE))
或intended(hasAction(equalTo(MediaStore.ACTION_IMAGE_CAPTURE)))
之类的内容应该有用。