Skip to content

Commit

Permalink
[ASDisplayNode] Add unit tests for layout z-order changes (with an op…
Browse files Browse the repository at this point in the history
…en issue to fix). (TextureGroup#816)
  • Loading branch information
appleguy authored and bernieperez committed Apr 25, 2018
1 parent 6a4ac4f commit d8a445f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## master
* Add your own contributions to the next release on the line below this with your name.
- [ASDisplayNode] Add unit tests for layout z-order changes (with an open issue to fix).
- [ASDisplayNode] Consolidate main thread initialization and allow apps to invoke it manually instead of +load.
- [ASRunloopQueue] Introduce new runloop queue(ASCATransactionQueue) to coalesce Interface state update calls for view controller transitions.
- [ASRangeController] Fix stability of "minimum" rangeMode if the app has more than one layout before scrolling.
Expand Down
41 changes: 41 additions & 0 deletions Tests/ASDisplayNodeTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#import <AsyncDisplayKit/ASImageNode.h>
#import <AsyncDisplayKit/ASOverlayLayoutSpec.h>
#import <AsyncDisplayKit/ASInsetLayoutSpec.h>
#import <AsyncDisplayKit/ASStackLayoutSpec.h>
#import <AsyncDisplayKit/ASCenterLayoutSpec.h>
#import <AsyncDisplayKit/ASBackgroundLayoutSpec.h>
#import <AsyncDisplayKit/ASInternalHelpers.h>
Expand Down Expand Up @@ -2374,6 +2375,46 @@ - (void)testThatHavingTheSameNodeTwiceInALayoutSpecCausesExceptionOnLayoutCalcul
XCTAssertThrowsSpecificNamed([node calculateLayoutThatFits:ASSizeRangeMake(CGSizeMake(100, 100))], NSException, NSInternalInconsistencyException);
}

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

DeclareNodeNamed(a);
DeclareNodeNamed(b);
DeclareNodeNamed(c);
DeclareNodeNamed(d);

NSArray *nodesForwardOrder = @[a, b, c, d];
NSArray *nodesReverseOrder = @[d, c, b, a];
__block BOOL flipItemOrder = NO;

node.layoutSpecBlock = ^(ASDisplayNode *node, ASSizeRange size) {
ASStackLayoutSpec *stack = [ASStackLayoutSpec verticalStackLayoutSpec];
stack.children = flipItemOrder ? nodesReverseOrder : nodesForwardOrder;
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.
XCTAssert([node.subnodes isEqualToArray:nodesForwardOrder], @"subnodes: %@, array: %@", node.subnodes, nodesForwardOrder);
XCTAssertNodeSubnodeSubviewSublayerOrder(node, YES /* isLoaded */, NO /* isLayerBacked */,
@"a,b,c,d", @"Forward order");

flipItemOrder = YES;
[node invalidateCalculatedLayout];
[node.view layoutIfNeeded];

// In this case, it's critical that the items are in the new order so that event handling and apparent z-position are correct.
// FIXME: The reversal case is not currently passing.
// XCTAssert([node.subnodes isEqualToArray:nodesReverseOrder], @"subnodes: %@, array: %@", node.subnodes, nodesReverseOrder);
// XCTAssertNodeSubnodeSubviewSublayerOrder(node, YES /* isLoaded */, NO /* isLayerBacked */,
// @"d,c,b,a", @"Reverse order");
}

- (void)testThatOverlaySpecOrdersSubnodesCorrectly
{
ASDisplayNode *node = [[ASDisplayNode alloc] init];
Expand Down
22 changes: 20 additions & 2 deletions Tests/ArrayDiffingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ - (void)testDiffingCommonIndexes
NSIndexSet *indexSet = [test[0] _asdk_commonIndexesWithArray:test[1] compareBlock:^BOOL(id lhs, id rhs) {
return [lhs isEqual:rhs];
}];
NSMutableIndexSet *mutableIndexSet = [indexSet mutableCopy];

for (NSNumber *index in (NSArray *)test[2]) {
XCTAssert([indexSet containsIndex:[index integerValue]]);
[mutableIndexSet removeIndex:[index integerValue]];
}

XCTAssert([mutableIndexSet count] == 0, @"Unaccounted deletions: %@", mutableIndexSet);
}
}

Expand All @@ -80,6 +84,12 @@ - (void)testDiffingInsertionsAndDeletions {
@[@3],
@[],
],
@[
@[@"a", @"b", @"c", @"d"],
@[@"d", @"c", @"b", @"a"],
@[@1, @2, @3],
@[@0, @1, @2],
],
@[
@[@"bob", @"alice", @"dave"],
@[@"bob", @"gary", @"alice", @"dave"],
Expand Down Expand Up @@ -121,12 +131,20 @@ - (void)testDiffingInsertionsAndDeletions {
for (NSArray *test in tests) {
NSIndexSet *insertions, *deletions;
[test[0] asdk_diffWithArray:test[1] insertions:&insertions deletions:&deletions];
NSMutableIndexSet *mutableInsertions = [insertions mutableCopy];
NSMutableIndexSet *mutableDeletions = [deletions mutableCopy];

for (NSNumber *index in (NSArray *)test[2]) {
XCTAssert([insertions containsIndex:[index integerValue]]);
XCTAssert([mutableInsertions containsIndex:[index integerValue]]);
[mutableInsertions removeIndex:[index integerValue]];
}
for (NSNumber *index in (NSArray *)test[3]) {
XCTAssert([deletions containsIndex:[index integerValue]]);
XCTAssert([mutableDeletions containsIndex:[index integerValue]]);
[mutableDeletions removeIndex:[index integerValue]];
}

XCTAssert([mutableInsertions count] == 0, @"Unaccounted insertions: %@", mutableInsertions);
XCTAssert([mutableDeletions count] == 0, @"Unaccounted deletions: %@", mutableDeletions);
}
}

Expand Down

0 comments on commit d8a445f

Please sign in to comment.