0

我正在调用意图在 Activity 的 onCreate() 中打开画廊

此方法在 onCreate() 中调用

 @SuppressLint("IntentReset")
    private void requestForPickImage() {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        i.setType("image/*");
        startActivityForResult(i, SELECT_PICTURE_REQUEST_CODE);
        Log.e("---", "test_selectImageForOcr: 2" );
    }

这是我的测试课

@RunWith(AndroidJUnit4.class)
@LargeTest
public class F_SelectImageForOcrActivityUnitTest {

    @Rule
    public GrantPermissionRule permissionRule =
            GrantPermissionRule.grant(android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    @Rule
    public IntentsTestRule<SelectImageToOcrActivity> mActivity =
            new IntentsTestRule<SelectImageToOcrActivity>(SelectImageToOcrActivity.class, true, true) {
                @Override
                protected void beforeActivityLaunched() {
                    super.beforeActivityLaunched();
                    Intents.init();
                    Log.e("---", "test_selectImageForOcr: before1");
                    test_selectImageForOcr();
                    Log.e("---", "test_selectImageForOcr: before2");
                }

                @Override
                protected void afterActivityFinished() {
                    Log.e("---", "test_selectImageForOcr: 3");
                    intended(expectedIntent);
                    Log.e("---", "test_selectImageForOcr: 4");
                    Intents.release();
                    super.afterActivityFinished();
                }
            };
    private Matcher<Intent> expectedIntent;

    @Before
    public void init() {

    }

    public void test_selectImageForOcr() {
        expectedIntent = AllOf.allOf(
                hasAction(Intent.ACTION_PICK),
                hasData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
                hasType("image/*")
        );

        ActivityResult picker = createPicker();
        Log.e("---", "test_selectImageForOcr: 1");
        intending(expectedIntent).respondWith(picker);
    }

    @Test
    public void testStart() {
        Espresso.onView(ViewMatchers.withId(R.id.btn_scan)).check(matches(isDisplayed())).perform(ViewActions.click());
    }

    private ActivityResult createPicker() {
        Resources resources = ApplicationProvider.getApplicationContext().getResources();
        Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                resources.getResourcePackageName(R.mipmap.ic_launcher) + '/' +
                resources.getResourceTypeName(R.mipmap.ic_launcher) + '/' +
                resources.getResourceEntryName(R.mipmap.ic_launcher));

        Intent resultData = new Intent();
        resultData.setData(imageUri);
        resultData.setType("image/*");

        return new ActivityResult(
                Activity.RESULT_OK, resultData);

    }
}

这是我在 logCat 中得到的日志

E/---: test_selectImageForOcr: before1
E/---: test_selectImageForOcr: 1
E/---: test_selectImageForOcr: before2
E/---: test_selectImageForOcr: 2
E/---: test_selectImageForOcr: 3

我想启动一个活动,因为我在选择图像后在此活动中进行了另一个 UI 测试。所以保持launchActivity真实

但我得到了这个例外

junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: (has action: is "android.intent.action.PICK" and has data: is <content://media/external/images/media> and has type: is "image/*")

Matched intents:[]

Recorded intents:
-Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.my.app/com.my.app.camera.SelectImageToOcrActivity } handling packages:[[com.my.app]])
-Intent { act=android.intent.action.PICK typ=image/* } handling packages:[[com.google.android.apps.photos]])
    at junit.framework.Assert.fail(Assert.java:50)
    at androidx.test.espresso.intent.VerificationModes$Times.verify(VerificationModes.java:80)
    at androidx.test.espresso.intent.Intents.internalIntended(Intents.java:348)
    at androidx.test.espresso.intent.Intents$3.run(Intents.java:206)
    at androidx.test.espresso.intent.Intents$PropogatingRunnable.run(Intents.java:226)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:2223)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7478)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)

我尝试了什么:

  1. 我试图让它成为一个单独的测试
  2. 我试图调用方法但得到同样的test_selectImageForOcr错误@Before

另外,我需要在测试成功后关闭图像选择器。任何帮助表示赞赏。


编辑

我找到了为什么会这样

当我设定setType("image/*");意图时,我遇到了这个问题。如果我删除此类型,则测试通过并且图像选择器也将关闭。这个问题有什么解决办法吗?

4

1 回答 1

0

哦,那是我的错。由于@SuppressLint()我无法识别这个

我需要根据这个警告设置类型和 URI

在此处输入图像描述

Intent i = new Intent(Intent.ACTION_PICK);
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
于 2021-01-06T12:48:23.460 回答