Wednesday, 12 August 2015

android espresso verify image





http://hitherejoe.com/wordpress/testing-imageview-changes-android-espresso-automated-tests/



    public static Matcher<View> isImageTheSame(final Drawable drawable) {
        return new BoundedMatcher<View, ImageView>(ImageView.class) {
 
            @Override
            public void describeTo(Description description) {
                description.appendText("is image the same as: ");
                description.appendValue(drawable);
            }
 
            @Override
            public boolean matchesSafely(ImageView view) {
                Bitmap bitmapCompare = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
                Drawable drawable = view.getDrawable();
                Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
                return bitmapCompare.sameAs(bitmap);
            }
        };
    }
From our tests we can then use this matcher like below:
    public void testProfilePictureSelectPhoto() throws Exception {
        Drawable currentDrawable = ((ImageView)getActivity().findViewById(R.id.image_profile_picture)).getDrawable();
        onView(withId(R.id.image_profile_picture)).perform(click());   
        onView(withId(R.id.text_choose_photo)).perform(click());
        Thread.sleep(getBestSleepTimeForOneRequest());
 
        onView(withId(R.id.image_profile_picture))
                .check(matches(not(isImageTheSame(currentDrawable))));
    }

No comments:

Post a comment