Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASDisplayNode] Add unit tests for layout z-order changes (with an open issue to fix). #816

Merged
merged 1 commit into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

@appleguy appleguy Mar 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without these mutable index sets and checking that all values were consumed, the test actually didn't fail when I tried deleting one of the supposed-to-be-present index values from the test set.

So although everything seems to behave correctly, this was important to ensure the validation checks both necessity and sufficiency of the test set.

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

Expand Down