forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a unit test for double-initial-load issue (TextureGroup#352)
- Loading branch information
1 parent
2307d72
commit c4dfcd2
Showing
7 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// ASCollectionModernDataSourceTests.m | ||
// Texture | ||
// | ||
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <XCTest/XCTest.h> | ||
#import <OCMock/OCMock.h> | ||
#import <AsyncDisplayKit/AsyncDisplayKit.h> | ||
#import <AsyncDisplayKit/NSIndexSet+ASHelpers.h> | ||
#import "OCMockObject+ASAdditions.h" | ||
#import "ASTestCase.h" | ||
|
||
@interface ASCollectionModernDataSourceTests : ASTestCase | ||
|
||
@end | ||
|
||
@implementation ASCollectionModernDataSourceTests { | ||
@private | ||
id mockDataSource; | ||
UIWindow *window; | ||
UIViewController *viewController; | ||
ASCollectionNode *collectionNode; | ||
} | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; | ||
viewController = [[UIViewController alloc] init]; | ||
|
||
window.rootViewController = viewController; | ||
[window makeKeyAndVisible]; | ||
collectionNode = [[ASCollectionNode alloc] initWithCollectionViewLayout:[UICollectionViewFlowLayout new]]; | ||
collectionNode.frame = viewController.view.bounds; | ||
collectionNode.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | ||
[viewController.view addSubnode:collectionNode]; | ||
|
||
mockDataSource = OCMStrictProtocolMock(@protocol(ASCollectionDataSource)); | ||
[mockDataSource addImplementedOptionalProtocolMethods: | ||
@selector(numberOfSectionsInCollectionNode:), | ||
@selector(collectionNode:numberOfItemsInSection:), | ||
@selector(collectionNode:nodeBlockForItemAtIndexPath:), | ||
nil]; | ||
[mockDataSource setExpectationOrderMatters:YES]; | ||
|
||
// NOTE: Adding optionally-implemented methods after this point won't work due to ASCollectionNode selector caching. | ||
collectionNode.dataSource = mockDataSource; | ||
} | ||
|
||
- (void)tearDown | ||
{ | ||
OCMVerifyAll(mockDataSource); | ||
[super tearDown]; | ||
} | ||
|
||
- (void)testInitialDataLoadingCallPattern | ||
{ | ||
/// BUG: these methods are called twice in a row i.e. this for-loop shouldn't be here. https://github.com/TextureGroup/Texture/issues/351 | ||
for (int i = 0; i < 2; i++) { | ||
NSArray *counts = @[ @2 ]; | ||
[self expectDataSourceMethodsWithCounts:counts]; | ||
} | ||
|
||
[window layoutIfNeeded]; | ||
} | ||
|
||
#pragma mark - Helpers | ||
|
||
/** | ||
* Adds expectations for the sequence: | ||
* | ||
* numberOfSectionsInCollectionNode: | ||
* for section in countsArray | ||
* numberOfItemsInSection: | ||
* for item < itemCount | ||
* nodeBlockForItemAtIndexPath: | ||
*/ | ||
- (void)expectDataSourceMethodsWithCounts:(NSArray<NSNumber *> *)counts | ||
{ | ||
// -numberOfSectionsInCollectionNode | ||
OCMExpect([mockDataSource numberOfSectionsInCollectionNode:collectionNode]) | ||
.andReturn(counts.count); | ||
|
||
// For each section: | ||
// Note: Skip fast enumeration for readability. | ||
for (NSInteger section = 0; section < counts.count; section++) { | ||
NSInteger itemCount = counts[section].integerValue; | ||
OCMExpect([mockDataSource collectionNode:collectionNode numberOfItemsInSection:section]) | ||
.andReturn(itemCount); | ||
|
||
// For each item: | ||
for (NSInteger i = 0; i < itemCount; i++) { | ||
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:section]; | ||
OCMExpect([mockDataSource collectionNode:collectionNode nodeBlockForItemAtIndexPath:indexPath]) | ||
.andReturn((ASCellNodeBlock)^{ return [[ASCellNode alloc] init]; }); | ||
} | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// ASTestCase.h | ||
// Texture | ||
// | ||
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <XCTest/XCTest.h> | ||
|
||
@interface ASTestCase : XCTestCase | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// ASTestCase.m | ||
// Texture | ||
// | ||
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import "ASTestCase.h" | ||
#import <objc/runtime.h> | ||
|
||
@implementation ASTestCase | ||
|
||
- (void)tearDown | ||
{ | ||
// Clear out all application windows. Note: the system will retain these sometimes on its | ||
// own but we'll do our best. | ||
for (UIWindow *window in [UIApplication sharedApplication].windows) { | ||
[window resignKeyWindow]; | ||
window.hidden = YES; | ||
for (UIView *view in window.subviews) { | ||
[view removeFromSuperview]; | ||
} | ||
} | ||
|
||
// Set nil for all our subclasses' ivars. Use setValue:forKey: so memory is managed correctly. | ||
Class c = [self class]; | ||
while (c != [ASTestCase class]) { | ||
unsigned int ivarCount; | ||
Ivar *ivars = class_copyIvarList(c, &ivarCount); | ||
for (unsigned int i = 0; i < ivarCount; i++) { | ||
Ivar ivar = ivars[i]; | ||
NSString *key = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding]; | ||
[self setValue:nil forKey:key]; | ||
} | ||
if (ivars) { | ||
free(ivars); | ||
} | ||
|
||
c = [c superclass]; | ||
} | ||
|
||
[super tearDown]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// OCMockObject+ASAdditions.h | ||
// Texture | ||
// | ||
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <OCMock/OCMockObject.h> | ||
|
||
@interface OCMockObject (ASAdditions) | ||
|
||
/** | ||
* A method to manually specify which optional protocol methods should return YES | ||
* from -respondsToSelector:. | ||
* | ||
* If you don't call this method, the default OCMock behavior is to | ||
* "implement" all optional protocol methods, which makes it impossible to | ||
* test scenarios where only a subset of optional protocol methods are implemented. | ||
* | ||
* You should only call this on protocol mocks. | ||
*/ | ||
- (void)addImplementedOptionalProtocolMethods:(SEL)aSelector, ... NS_REQUIRES_NIL_TERMINATION; | ||
|
||
@end |
Oops, something went wrong.