forked from ra1028/DifferenceKit
-
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.
ra1028#17: Add stress tests of continous random collection view updat…
…es in a one dimentional collection
- Loading branch information
Showing
11 changed files
with
570 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import XCTest | ||
import UIKit | ||
|
||
private let collectionSize = 100 | ||
private let iterationsCount = 100 | ||
|
||
final class DifferenceKitStressTests: XCTestCase { | ||
func test_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges_1() { | ||
performTest_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges() | ||
} | ||
|
||
func test_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges_2() { | ||
performTest_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges() | ||
} | ||
|
||
func test_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges_3() { | ||
performTest_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges() | ||
} | ||
|
||
func test_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges_4() { | ||
performTest_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges() | ||
} | ||
|
||
func test_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges_5() { | ||
performTest_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges() | ||
} | ||
|
||
// MARK: - Private | ||
private func performTest_differenceKit_doesNotCrashUICollectionView_duringStressCollectionChanges() { | ||
let expectations = (0..<iterationsCount).map { | ||
expectation(description: "async expectation of iteration \($0)") | ||
} | ||
|
||
performTests(expectations: expectations) | ||
waitForExpectations(timeout: 60) | ||
} | ||
|
||
private func performTests(expectations: [XCTestExpectation]) { | ||
performTest( | ||
index: 0, | ||
expectations: expectations, | ||
previousCellDataList: [] | ||
) | ||
} | ||
|
||
private func performTest( | ||
index: Int, | ||
expectations: [XCTestExpectation], | ||
previousCellDataList: [CellData]) | ||
{ | ||
guard index < expectations.count else { return } | ||
|
||
autoreleasepool { | ||
print("testing iteration \(index)") | ||
|
||
let cellDataGenerator = CellDataGenerator() | ||
|
||
// Given | ||
let cellDataGeneratorResult: CellDataGeneratorResult | ||
if index == 0 { | ||
cellDataGeneratorResult = cellDataGenerator | ||
.generateCellData(count: collectionSize) | ||
} else if index == iterationsCount / 2 { | ||
// Special case: applying no changes to original data | ||
cellDataGeneratorResult = cellDataGenerator | ||
.performNoActionsOnCellData( | ||
previousCellDataList | ||
) | ||
} else { | ||
cellDataGeneratorResult = cellDataGenerator | ||
.performRandomActionsOnCellData( | ||
previousCellDataList, | ||
minimumCountAfterActions: collectionSize / 10, | ||
maximumCountAfterActions: collectionSize * 10 | ||
) | ||
} | ||
|
||
print(" from \(cellDataGeneratorResult.from.count), to: \(cellDataGeneratorResult.to.count).") | ||
|
||
print(" deletes \(cellDataGeneratorResult.deletes), " | ||
+ "inserts: \(cellDataGeneratorResult.inserts), " | ||
+ "moves \(cellDataGeneratorResult.moves), " | ||
+ "updates: \(cellDataGeneratorResult.updates)." | ||
) | ||
|
||
// When | ||
let collectionViewUpdater = CollectionViewUpdater() | ||
collectionViewUpdater.updateCollectionView( | ||
from: cellDataGeneratorResult.from, | ||
to: cellDataGeneratorResult.to, | ||
completion: { [weak self] result in | ||
// Then | ||
switch result { | ||
case let .exception(exception): | ||
XCTFail("Failed to update collection view using DifferenceKit. exception: \(exception)") | ||
case .noUpdateRequired: | ||
break | ||
case let .success(visibleCellDataList, expectedCellDataList): | ||
XCTAssert( | ||
visibleCellDataList.count == expectedCellDataList.count, | ||
"Update the layout to fit all cells in the screen" | ||
) | ||
|
||
let zippedCellDataLists = zip(visibleCellDataList, expectedCellDataList) | ||
for (index, (visibleCellData, expectedCellData)) in zippedCellDataLists.enumerated() { | ||
XCTAssert( | ||
visibleCellData.isContentEqual(to: expectedCellData), | ||
"visible cell data and expected cell data are not in sync at index: \(index)" | ||
) | ||
} | ||
} | ||
|
||
print("") | ||
|
||
collectionViewUpdater.cleanUp() | ||
|
||
expectations[index].fulfill() | ||
|
||
DispatchQueue.main.async { | ||
self?.performTest( | ||
index: index + 1, | ||
expectations: expectations, | ||
previousCellDataList: cellDataGeneratorResult.to | ||
) | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} |
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,7 @@ | ||
import UIKit | ||
|
||
final class Cell: UICollectionViewCell { | ||
var cellData: CellData? | ||
|
||
static let reuseIdentifier = "Cell.reuseIdentifier" | ||
} |
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,33 @@ | ||
import DifferenceKit | ||
import XCTest | ||
|
||
final class CellData: Differentiable { | ||
// MARK: - Properties | ||
let title: String | ||
let subtitle: String | ||
|
||
// MARK: - Differentiable | ||
let differenceIdentifier: UUID | ||
|
||
// MARK: - Init | ||
init( | ||
differenceIdentifier: UUID, | ||
title: String, | ||
subtitle: String) | ||
{ | ||
self.differenceIdentifier = differenceIdentifier | ||
self.title = title | ||
self.subtitle = subtitle | ||
} | ||
|
||
// MARK: - Differentiable | ||
func isContentEqual(to source: CellData) -> Bool { | ||
XCTAssert( | ||
self.differenceIdentifier == source.differenceIdentifier, | ||
"We expect the algorythm to compare items only with same `differenceIdentifier`" | ||
) | ||
|
||
return self.title == source.title | ||
&& self.subtitle == source.subtitle | ||
} | ||
} |
Oops, something went wrong.