首页 > 解决方案 > React 本机应用程序未从 Espresso-Intent 接收图像

问题描述

我正在编写 Espresso 测试以在我的 React Native 应用程序上运行。我Espresso-Intents用来模拟用户拍照,代码如下:

public void takePicture() {
    Instrumentation.ActivityResult result = createImageCaptureActivityResultStub();

    intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
    clickText("Camera");
}

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        // Log exception
        System.out.println(e.toString());
        return null;
    }
}

private Instrumentation.ActivityResult createImageCaptureActivityResultStub() {
    // Put the drawable in a bundle.
    Bundle bundle = new Bundle();
    bundle.putParcelable("data", getBitmapFromURL("https://vignette.wikia.nocookie.net/the-feed-your-pets-community/images/6/69/Banana.png/revision/latest?cb=20180527162222"));

    // Create the Intent that will include the bundle.
    Intent resultData = new Intent();
    resultData.putExtras(bundle);

    // Create the ActivityResult with the Intent.
    return new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
}

运行时,相机 Intent 已成功存根,但应显示照片的视图卡在其加载状态。

在调试中,我发现这getBitmapFromURL不是问题,因为bundle包含正确的图像。

谁能建议问题可能出在哪里,或者如何最好地进一步调试?

标签: javaandroidandroid-espresso

解决方案


推荐阅读