How to create an android espresso test for a parallax toolbar gesture

Originally at:

A parallax toolbar is an android material design concept that will collapse the toolbar when there is scroll content like this

Writing UI Automation tests for any gestures is ridiculously complex and often fragile if you go by the co-ordinates based swipe or gestures.

After a lot of figuring out turns its not as difficult in espresso. Though we need some setup on the app side. Also, espresso has some built in methods that would help us do gestures with ViewActions.

To set this up, first you need setup test hooks in your app to let automation know that swiping has finished or in progress etc. In this case we need to implement listeners on AppbarLayout and set the content descriptions for various states of the image.Then we can create espresso test that uses these contentDescriptions to verify the stat.Take a note the inline comments and how to use espressos matching mechanisms for viewAssertions.

Following a similar procedure we should be able to automate other material design gestures.