mercredi 27 mars 2019

how to set image (from drawable for example) to imageView while espresso test?

I am using espresso test recording for capture image from camera and set the image into the imageView, I couldn't do that because I when press on the camera capture button its not recording for obvious reason, so the way to do that is to mock the capture image process I found some code to do that but no luck.

I tried this code from here How to set image in imageview while testing with espresso?

public static void simulatePictureFromCameraRoll(Uri pictureUri) throws  Exception {
    Exception returnException = null;
    Intent resultData = new Intent();

    resultData.setData(pictureUri);

    Intents.init();
    try {
        Matcher<Intent> expectedIntent = hasAction(Intent.ACTION_GET_CONTENT);
        intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData));
        onView(withId(R.id.newUpdatedIDCardImg)).perform(click());
        intended(expectedIntent);
    }
    catch (Exception e) {
        returnException = e;
    }
    finally {
        Intents.release();
    }

    if (returnException != null) {
        throw returnException;
    }
}

and here how I use that method, I used some drawable image :

intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(
            new Instrumentation.ActivityResult(Activity.RESULT_OK, null));


    //Uri uri = Uri.parse("R.drawable.bg_shape");
    Uri myURI = Uri.parse("android.resource://com.example.project/" + R.drawable.bg_shape);

    try {
        simulatePictureFromCameraRoll(myURI);
    } catch (Exception e) {
        e.printStackTrace();
    }

what I want is to simply set an image in the imageview with espresso, please help me and thanks in advance.

Aucun commentaire:

Enregistrer un commentaire