From d2ca43e7881b2e8f7951b1c3e9c6ae423072f3d0 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Thu, 12 Jul 2018 15:58:57 -0700 Subject: [PATCH] Another randomized test with actual ASLayouts --- Tests/ASDisplayNodeTests.mm | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/Tests/ASDisplayNodeTests.mm b/Tests/ASDisplayNodeTests.mm index 6a3385c94..3c6d9a71e 100644 --- a/Tests/ASDisplayNodeTests.mm +++ b/Tests/ASDisplayNodeTests.mm @@ -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.