Skip to content

Commit

Permalink
Another randomized test with actual ASLayouts
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseoldduck committed Jul 12, 2018
1 parent 067aa85 commit d2ca43e
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Tests/ASDisplayNodeTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,77 @@ - (void)testThatHavingTheSameNodeTwiceInALayoutSpecCausesExceptionOnLayoutCalcul
XCTAssertThrowsSpecificNamed([node calculateLayoutThatFits:ASSizeRangeMake(CGSizeMake(100, 100))], NSException, NSInternalInconsistencyException);
}

- (void)testThatStackSpecOrdersSubnodesCorrectlyRandomness
{
// This test ensures that the z-order of nodes matches the stack spec, including after several random relayouts / transitions.
ASDisplayNode *node = [[ASDisplayNode alloc] init];
node.automaticallyManagesSubnodes = YES;

DeclareNodeNamed(a);
DeclareNodeNamed(b);
DeclareNodeNamed(c);
DeclareNodeNamed(d);
DeclareNodeNamed(e);
DeclareNodeNamed(f);
DeclareNodeNamed(g);
DeclareNodeNamed(h);
DeclareNodeNamed(i);
DeclareNodeNamed(j);

NSMutableArray *allNodes = [@[a, b, c, d, e, f, g, h, i, j] mutableCopy];
NSArray *testPrevious = @[];
NSArray __block *testPending = @[];

int len1 = 1 + arc4random_uniform(9);
for (NSUInteger n = 0; n < len1; n++) { // shuffle and add
[allNodes exchangeObjectAtIndex:n withObjectAtIndex:n + arc4random_uniform(10 - (uint32_t) n)];
testPrevious = [testPrevious arrayByAddingObject:allNodes[n]];
}

__block NSUInteger testCount = 0;
node.layoutSpecBlock = ^(ASDisplayNode *node, ASSizeRange size) {
ASStackLayoutSpec *stack = [ASStackLayoutSpec verticalStackLayoutSpec];

if (testCount++ == 0) {
stack.children = testPrevious;
}
else {
testPending = @[];
int len2 = 1 + arc4random_uniform(9);
for (NSUInteger n = 0; n < len2; n++) { // shuffle and add
[allNodes exchangeObjectAtIndex:n withObjectAtIndex:n + arc4random_uniform(10 - (uint32_t) n)];
testPending = [testPending arrayByAddingObject:allNodes[n]];
}
stack.children = testPending;
}

return stack;
};

ASDisplayNodeSizeToFitSize(node, CGSizeMake(100, 100));
[node.view layoutIfNeeded];

// Because automaticallyManagesSubnodes is used, the subnodes array is constructed from the layout spec's children.
NSString *expected = [[testPrevious valueForKey:@"debugName"] componentsJoinedByString:@","];
XCTAssert([node.subnodes isEqualToArray:testPrevious], @"subnodes: %@, array: %@", node.subnodes, testPrevious);
XCTAssertNodeSubnodeSubviewSublayerOrder(node, YES /* isLoaded */, NO /* isLayerBacked */,
expected, @"Initial order");

for (NSUInteger n = 0; n < 25; n++) {
[node invalidateCalculatedLayout];
[node.view setNeedsLayout];
[node.view layoutIfNeeded];


XCTAssert([node.subnodes isEqualToArray:testPending], @"subnodes: %@, array: %@", node.subnodes, testPending);
expected = [[testPending valueForKey:@"debugName"] componentsJoinedByString:@","];

XCTAssertEqualObjects(orderStringFromSubnodes(node), expected, @"Incorrect node order for Random order #%ld", (unsigned long) n);
XCTAssertEqualObjects(orderStringFromSubviews(node.view), expected, @"Incorrect subviews for Random order #%ld", (unsigned long) n);
XCTAssertEqualObjects(orderStringFromSublayers(node.layer), expected, @"Incorrect sublayers for Random order #%ld", (unsigned long) n);
}
}

- (void)testThatStackSpecOrdersSubnodesCorrectly
{
// This test ensures that the z-order of nodes matches the stack spec, including after relayout / transition.
Expand Down

0 comments on commit d2ca43e

Please sign in to comment.