2

I have used third party library in my sign up form for selecting image from gallery. My signup form works fine. Now I want to test it using espresso.The biggest problem that I am facing right now is how to set profile photo's imageview while testing ?

Mrugesh
  • 4,381
  • 8
  • 42
  • 84
  • Which library do you use. What is exactly the Problem? What did you try so far? How is the profile image set when not in tests? – thaussma Jul 29 '16 at 08:26

1 Answers1

1

You should use espresso-intents to detect that intent from the camera roll and set a picture.

Here you have the method I use:

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.lytProfImageChooseFromLibrary)).perform(click());
        intended(expectedIntent);
    }
    catch (Exception e) {
        returnException = e;
    }
    finally {
        Intents.release();
    }

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

Hope this helps.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • I capture the exception because it's important to ensure that the Intents.release is done or you can have problems if your tests call the Intents.init multiple times – jeprubio Jul 29 '16 at 11:21