The issue is that the cell you're trying to find in your test case, the one with label "This is cell 19", does not exist until the collection view has already been scrolled. So we need to make the view scroll first and then look for the cell. The easiest way to make the collection view scroll with Subliminal is through an application hook. In (for example) your view controller's viewDidLoad
method, you could register the view controller to respond to a particular message from any Subliminal test case, like so:
[[SLTestController sharedTestController] registerTarget:self forAction:@selector(scrollToBottom)];
and the view controller could implement that method as:
- (void)scrollToBottom {
[self.collectionView setContentOffset:CGPointMake(0.0, 1774.0)];
}
That 1774
is just the offset that happens to scroll the collection view in your test app all the way to the bottom. In a real application the app hook would probably be more sophisticated. (And in a real application you would want to make sure to call [[SLTestController sharedTestController] deregisterTarget:self]
in your view controller's dealloc
method.)
To trigger the scrollToBottom
method from a Subliminal test case you can use:
[[SLTestController sharedTestController] sendAction:@selector(scrollToBottom)];
or the convenience macro:
SLAskApp(scrollToBottom);
The shared SLTestController
will send the scrollToBottom
message to the object that registered to receive it (your view controller).
When the sendAction
or SLAskApp
macro returns your cell 19 will already be visible, so you don't need to bother with the [lastLabel scrollToVisible]
call anymore. Your complete test case could look like this:
- (void)testScrollingCase {
SLElement *label1 = [SLElement elementWithAccessibilityLabel:@"This is cell 0"];
SLAssertTrue([UIAElement(label1) isVisible], @"Cell 0 should be visible at this point");
SLElement *label5 = [SLElement elementWithAccessibilityLabel:@"This is cell 5"];
SLAssertFalse([UIAElement(label5) isValid], @"Cell 5 should not be visible at this point");
// Cause the collection view to scroll to the bottom.
SLAskApp(scrollToBottom);
SLElement *lastLabel = [SLElement elementWithAccessibilityLabel:@"This is cell 19"];
SLAssertTrue([UIAElement(lastLabel) isVisible], @"Last cell should be visible at this point");
}