The code was right. The test was wrong. But it still passed.
Later on we added a DrawerLayout to the app. All the tests still passed - all was well with the world. Luckily one of the team is noticed that the swiping test was pulling out the drawer instead of swiping the ViewPager. They don't call him eagle-eyed Pete for nothing. (Actually they don't call him that as his name isn't Pete.) In any case the test wasn't failing even though it was doing something different to what was being tested for.
The reason why the drawer was being pulled out is down to the Expresso swipeRight(). It started the swipe from the very left of the display, which caused the drawer to pop out.
That was fixed by writing an alternative swiping action that starts from a little inside the view, thusly:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
The 100 in line 11 is the number of pixels in from the left to start the swipe. On line 21 the swipe is stopped 50 pixels from the right of the view. There's no real reason for this. :)
But why was the test passing? The ViewPager creates not just the view you can see but the ones to the left and to the right as well. The test did a swipe and then checked for some text in the view but not that it was visible. Espresso looks through the whole view hierarchy, not just the view you can see, and that includes the pages to either side. The text being checked for existed.
Darren @ Æ
Never trust a test you haven't seen fail. ↩