Quantcast
Channel: Android - espresso - clicking on a listview entry based on custom objects - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by tm1701 for Android - espresso - clicking on a listview entry based on custom objects

$
0
0

An addition to the previous answer, I create a complete version with 2 kind of validations. This may help you understand Espresso and Custom Matchers.

The difference with the standard Espresso LongList example is that I use a list of Custom objects to show in a listview. Scrolling to the right list-entry and checking the result is shown below.

Method 1 - the validation against a string

In the test script is:

onData( allOf( instanceOf( MyObjectWithItemAndSize.class), myCustomObjectShouldHaveString( "my_item: 60")))         .perform(click());// testing the result ... as in the longlist exampleonView(withId(R.id.selection_pos2)).check(matches(withText("my_item: 60"))); 

The matcher is:

public static Matcher<Object> myCustomObjectShouldHaveString( String expectedTest) {    return myCustomObjectShouldHaveString( equalTo( expectedTest));}private static Matcher<Object> myCustomObjectShouldHaveString(final Matcher<String> expectedObject) {return new BoundedMatcher<Object, MyObjectWithItemAndSize>( MyObjectWithItemAndSize.class) {    @Override    public boolean matchesSafely(final MyObjectWithItemAndSize actualObject) {        // next line is important ... requiring a String having an "equals" method        if( expectedObject.matches( actualObject.item) ) {             return true;           } else {              return false;           }      }      @Override      public void describeTo(final Description description) {         // could be improved, of course         description.appendText("getnumber should return ");      }   };}

Method 2: validating against the (complete object).

In the test script you find:

MyObjectWithItemAndSize myObject = new MyObjectWithItemAndSize( "my_item: 60", 11);onData( allOf( instanceOf( MyObjectWithItemAndSize.class), myObjectHasContent( myObject))).perform( click());onView(withId( R.id.selection_pos2)).check(matches(withText("my_item: 60"))); 

The matcher is.

The most important line (I have been struggling with) is below // ****

public static Matcher<Object> myObjectHasContent( MyObjectWithItemAndSize expectedObject) {   return myObjectHasContent( equalTo( expectedObject));}//private method that does the work of matchingprivate static Matcher<Object> myObjectHasContent(final Matcher<MyObjectWithItemAndSize> expectedObject) {     return new BoundedMatcher<Object, MyObjectWithItemAndSize>(MyObjectWithItemAndSize.class) {        @Override        public boolean matchesSafely( final MyObjectWithItemAndSize actualObject) {            // ****** ... the 'matches'. See below.             // this requires the MyObjectWithItemAndSize to have an 'equals' method            if( expectedObject.matches( actualObject) ) {                return true;            } else {                 return false;            }        }        @Override        public void describeTo(final Description description) {           description.appendText("getnumber should return ");        }     };  }

What is very important is that the Custom object has this method (and I guess overriden):

@Overridepublic boolean equals( Object mob2) {    return( (this.item.equals( ((MyObjectWithItemAndSize) mob2).item)));    // of course, could have also a check on this.size.} 

And it works!!!! Pfff, took a while, but overcame. Thanks also to Yash F.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>