Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit ac0caf3

Browse files
committed
Merge pull request #770 from nguyenhuy/FixTableViewTests
Fix table view tests
2 parents a88ad0a + 7b71462 commit ac0caf3

File tree

2 files changed

+64
-79
lines changed

2 files changed

+64
-79
lines changed

AsyncDisplayKit/ASTableView.mm

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ @interface ASTableView () <ASRangeControllerDelegate, ASDataControllerSource, _A
169169
NSIndexPath *_contentOffsetAdjustmentTopVisibleRow;
170170
CGFloat _contentOffsetAdjustment;
171171

172-
CGFloat _maxWidthForNodesConstrainedSize;
173-
BOOL _ignoreMaxWidthChange;
172+
CGFloat _nodesConstrainedWidth;
173+
BOOL _ignoreNodesConstrainedWidthChange;
174174
}
175175

176176
@property (atomic, assign) BOOL asyncDataSourceLocked;
@@ -224,10 +224,10 @@ - (void)configureWithAsyncDataFetching:(BOOL)asyncDataFetchingEnabled
224224

225225
_automaticallyAdjustsContentOffset = NO;
226226

227-
_maxWidthForNodesConstrainedSize = self.bounds.size.width;
227+
_nodesConstrainedWidth = self.bounds.size.width;
228228
// If the initial size is 0, expect a size change very soon which is part of the initial configuration
229229
// and should not trigger a relayout.
230-
_ignoreMaxWidthChange = (_maxWidthForNodesConstrainedSize == 0);
230+
_ignoreNodesConstrainedWidthChange = (_nodesConstrainedWidth == 0);
231231
}
232232

233233
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
@@ -396,13 +396,13 @@ - (void)endUpdatesAnimated:(BOOL)animated completion:(void (^)(BOOL completed))c
396396

397397
- (void)layoutSubviews
398398
{
399-
if (_maxWidthForNodesConstrainedSize != self.bounds.size.width) {
400-
_maxWidthForNodesConstrainedSize = self.bounds.size.width;
399+
if (_nodesConstrainedWidth != self.bounds.size.width) {
400+
_nodesConstrainedWidth = self.bounds.size.width;
401401

402402
// First width change occurs during initial configuration. An expensive relayout pass is unnecessary at that time
403403
// and should be avoided, assuming that the initial data loading automatically runs shortly afterward.
404-
if (_ignoreMaxWidthChange) {
405-
_ignoreMaxWidthChange = NO;
404+
if (_ignoreNodesConstrainedWidthChange) {
405+
_ignoreNodesConstrainedWidthChange = NO;
406406
} else {
407407
[self beginUpdates];
408408
[_dataController relayoutAllNodes];
@@ -828,8 +828,8 @@ - (ASCellNode *)dataController:(ASDataController *)dataController nodeAtIndexPat
828828

829829
- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
830830
{
831-
return ASSizeRangeMake(CGSizeMake(_maxWidthForNodesConstrainedSize, 0),
832-
CGSizeMake(_maxWidthForNodesConstrainedSize, FLT_MAX));
831+
return ASSizeRangeMake(CGSizeMake(_nodesConstrainedWidth, 0),
832+
CGSizeMake(_nodesConstrainedWidth, FLT_MAX));
833833
}
834834

835835
- (void)dataControllerLockDataSource
@@ -880,7 +880,7 @@ - (void)willLayoutSubviewsOfTableViewCell:(_ASTableViewCell *)tableViewCell
880880
// Normally the content view width equals to the constrained size width (which equals to the table view width).
881881
// If there is a mismatch between these values, for example after the table view entered or left editing mode,
882882
// content view width is preferred and used to re-measure the cell node.
883-
if (contentViewWidth != constrainedSize.max.width) {
883+
if (!_ignoreNodesConstrainedWidthChange && contentViewWidth != constrainedSize.max.width) {
884884
constrainedSize.min.width = contentViewWidth;
885885
constrainedSize.max.width = contentViewWidth;
886886

AsyncDisplayKitTests/ASTableViewTests.m

Lines changed: 53 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,7 @@ - (void)testRelayoutAllRowsWithNonZeroSizeInitially
234234
tableView.asyncDelegate = dataSource;
235235
tableView.asyncDataSource = dataSource;
236236

237-
// Trigger layout measurement on all nodes
238-
[tableView reloadData];
239-
237+
[self triggerFirstLayoutMeasurementForTableView:tableView];
240238
[self triggerSizeChangeAndAssertRelayoutAllRowsForTableView:tableView newSize:tableViewFinalSize];
241239
}
242240

@@ -264,36 +262,6 @@ - (void)testRelayoutAllRowsWithZeroSizeInitially
264262
[self triggerSizeChangeAndAssertRelayoutAllRowsForTableView:tableView newSize:tableViewFinalSize];
265263
}
266264

267-
- (void)triggerSizeChangeAndAssertRelayoutAllRowsForTableView:(ASTableView *)tableView newSize:(CGSize)newSize
268-
{
269-
XCTestExpectation *nodesMeasuredUsingNewConstrainedSizeExpectation = [self expectationWithDescription:@"nodesMeasuredUsingNewConstrainedSize"];
270-
271-
[tableView beginUpdates];
272-
273-
CGRect frame = tableView.frame;
274-
frame.size = newSize;
275-
tableView.frame = frame;
276-
[tableView layoutIfNeeded];
277-
278-
[tableView endUpdatesAnimated:NO completion:^(BOOL completed) {
279-
for (int section = 0; section < NumberOfSections; section++) {
280-
for (int row = 0; row < NumberOfRowsPerSection; row++) {
281-
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
282-
ASTestTextCellNode *node = (ASTestTextCellNode *)[tableView nodeForRowAtIndexPath:indexPath];
283-
XCTAssertEqual(node.numberOfLayoutsOnMainThread, 1);
284-
XCTAssertEqual(node.constrainedSizeForCalculatedLayout.max.width, newSize.width);
285-
}
286-
}
287-
[nodesMeasuredUsingNewConstrainedSizeExpectation fulfill];
288-
}];
289-
290-
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
291-
if (error) {
292-
XCTFail(@"Expectation failed: %@", error);
293-
}
294-
}];
295-
}
296-
297265
- (void)testRelayoutVisibleRowsWhenEditingModeIsChanged
298266
{
299267
CGSize tableViewSize = CGSizeMake(100, 500);
@@ -304,24 +272,8 @@ - (void)testRelayoutVisibleRowsWhenEditingModeIsChanged
304272

305273
tableView.asyncDelegate = dataSource;
306274
tableView.asyncDataSource = dataSource;
307-
308-
XCTestExpectation *reloadDataExpectation = [self expectationWithDescription:@"reloadData"];
309-
[tableView reloadDataWithCompletion:^{
310-
for (int section = 0; section < NumberOfSections; section++) {
311-
for (int row = 0; row < NumberOfRowsPerSection; row++) {
312-
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
313-
ASTestTextCellNode *node = (ASTestTextCellNode *)[tableView nodeForRowAtIndexPath:indexPath];
314-
XCTAssertEqual(node.numberOfLayoutsOnMainThread, 0);
315-
XCTAssertEqual(node.constrainedSizeForCalculatedLayout.max.width, tableViewSize.width);
316-
}
317-
}
318-
[reloadDataExpectation fulfill];
319-
}];
320-
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
321-
if (error) {
322-
XCTFail(@"Expectation failed: %@", error);
323-
}
324-
}];
275+
276+
[self triggerFirstLayoutMeasurementForTableView:tableView];
325277

326278
NSArray *visibleNodes = [tableView visibleNodes];
327279
XCTAssertGreaterThan(visibleNodes.count, 0);
@@ -390,23 +342,7 @@ - (void)DISABLED_testRelayoutRowsAfterEditingModeIsChangedAndTheyBecomeVisible
390342
tableView.asyncDelegate = dataSource;
391343
tableView.asyncDataSource = dataSource;
392344

393-
XCTestExpectation *reloadDataExpectation = [self expectationWithDescription:@"reloadData"];
394-
[tableView reloadDataWithCompletion:^{
395-
for (int section = 0; section < NumberOfSections; section++) {
396-
for (int row = 0; row < NumberOfRowsPerSection; row++) {
397-
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
398-
ASTestTextCellNode *node = (ASTestTextCellNode *)[tableView nodeForRowAtIndexPath:indexPath];
399-
XCTAssertEqual(node.numberOfLayoutsOnMainThread, 0);
400-
XCTAssertEqual(node.constrainedSizeForCalculatedLayout.max.width, tableViewSize.width);
401-
}
402-
}
403-
[reloadDataExpectation fulfill];
404-
}];
405-
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
406-
if (error) {
407-
XCTFail(@"Expectation failed: %@", error);
408-
}
409-
}];
345+
[self triggerFirstLayoutMeasurementForTableView:tableView];
410346

411347
// Cause table view to enter editing mode and then scroll to the bottom.
412348
// The last node should be re-measured on main thread with the new (smaller) content view width.
@@ -451,4 +387,53 @@ - (void)testIndexPathForNode
451387
}];
452388
}
453389

390+
- (void)triggerFirstLayoutMeasurementForTableView:(ASTableView *)tableView{
391+
XCTestExpectation *reloadDataExpectation = [self expectationWithDescription:@"reloadData"];
392+
[tableView reloadDataWithCompletion:^{
393+
for (int section = 0; section < NumberOfSections; section++) {
394+
for (int row = 0; row < NumberOfRowsPerSection; row++) {
395+
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
396+
ASTestTextCellNode *node = (ASTestTextCellNode *)[tableView nodeForRowAtIndexPath:indexPath];
397+
XCTAssertEqual(node.numberOfLayoutsOnMainThread, 0);
398+
XCTAssertEqual(node.constrainedSizeForCalculatedLayout.max.width, tableView.frame.size.width);
399+
}
400+
}
401+
[reloadDataExpectation fulfill];
402+
}];
403+
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
404+
if (error) {
405+
XCTFail(@"Expectation failed: %@", error);
406+
}
407+
}];
408+
}
409+
410+
- (void)triggerSizeChangeAndAssertRelayoutAllRowsForTableView:(ASTableView *)tableView newSize:(CGSize)newSize
411+
{
412+
XCTestExpectation *nodesMeasuredUsingNewConstrainedSizeExpectation = [self expectationWithDescription:@"nodesMeasuredUsingNewConstrainedSize"];
413+
414+
[tableView beginUpdates];
415+
416+
CGRect frame = tableView.frame;
417+
frame.size = newSize;
418+
tableView.frame = frame;
419+
[tableView layoutIfNeeded];
420+
421+
[tableView endUpdatesAnimated:NO completion:^(BOOL completed) {
422+
for (int section = 0; section < NumberOfSections; section++) {
423+
for (int row = 0; row < NumberOfRowsPerSection; row++) {
424+
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
425+
ASTestTextCellNode *node = (ASTestTextCellNode *)[tableView nodeForRowAtIndexPath:indexPath];
426+
XCTAssertEqual(node.numberOfLayoutsOnMainThread, 1);
427+
XCTAssertEqual(node.constrainedSizeForCalculatedLayout.max.width, newSize.width);
428+
}
429+
}
430+
[nodesMeasuredUsingNewConstrainedSizeExpectation fulfill];
431+
}];
432+
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
433+
if (error) {
434+
XCTFail(@"Expectation failed: %@", error);
435+
}
436+
}];
437+
}
438+
454439
@end

0 commit comments

Comments
 (0)