How to set image in imageview while testing with espresso?

我只是一个虾纸丫 提交于 2020-01-16 05:36:09

问题


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 ?


回答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.



来源:https://stackoverflow.com/questions/38651068/how-to-set-image-in-imageview-while-testing-with-espresso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!