This repository was archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathReloadableViewLayoutAdapterCollectionViewTests.swift
More file actions
153 lines (124 loc) · 5.71 KB
/
ReloadableViewLayoutAdapterCollectionViewTests.swift
File metadata and controls
153 lines (124 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2016 LinkedIn Corp.
// 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
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import XCTest
@testable import LayoutKit
class ReloadableViewLayoutAdapterCollectionViewTests: ReloadableViewLayoutAdapterTestCase {
func testCollectionViewReloadSync() {
verifyReloadSync(TestCollectionView())
}
func testCollectionViewReloadBatchUpdatesSync() {
verifyReloadBatchUpdatesSync(TestCollectionView())
}
func testCollectionViewReloadSyncCancelsPreviousLayout() {
verifyReloadSyncCancelsPreviousLayout(TestCollectionView())
}
func testCollectionViewReloadAsync() {
verifyReloadAsync(TestCollectionView())
}
func testCollectionViewReloadBatchUpdatesAsync() {
verifyReloadBatchUpdatesAsync(TestCollectionView())
}
func testCollectionViewReloadAsyncCancelsPreviousLayout() {
verifyReloadAsyncCancelsPreviousLayout(TestCollectionView())
}
func testCollectionViewReloadAsyncCancelledOnViewDeinit() {
verifyReloadAsyncCancelledOnViewDeinit(TestCollectionView())
}
}
private class TestCollectionView: LayoutAdapterCollectionView, TestableReloadableView {
var reloadDataCount = 0
var batchUpdates = BatchUpdates()
let itemSpacing: CGFloat = 0
let itemWidthInset: CGFloat = 0
init() {
let frame = CGRect(x: 0, y: 0, width: 320, height: 1000)
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
super.init(frame: frame, collectionViewLayout: layout)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func reloadData() {
super.reloadData()
reloadDataCount += 1
}
fileprivate override func insertItems(at indexPaths: [IndexPath]) {
super.insertItems(at: indexPaths)
batchUpdates.insertItems.append(contentsOf: indexPaths)
}
fileprivate override func deleteItems(at indexPaths: [IndexPath]) {
super.deleteItems(at: indexPaths)
batchUpdates.deleteItems.append(contentsOf: indexPaths)
}
fileprivate override func reloadItems(at indexPaths: [IndexPath]) {
super.reloadItems(at: indexPaths)
batchUpdates.reloadItems.append(contentsOf: indexPaths)
}
fileprivate override func moveItem(at indexPath: IndexPath, to newIndexPath: IndexPath) {
super.moveItem(at: indexPath, to: newIndexPath)
batchUpdates.moveItems.append(ItemMove(from: indexPath, to: newIndexPath))
}
fileprivate override func insertSections(_ sections: IndexSet) {
super.insertSections(sections)
batchUpdates.insertSections.formUnion(sections)
}
fileprivate override func deleteSections(_ sections: IndexSet) {
super.deleteSections(sections)
batchUpdates.deleteSections.formUnion(sections)
}
fileprivate override func reloadSections(_ sections: IndexSet) {
super.reloadSections(sections)
batchUpdates.reloadSections.formUnion(sections)
}
fileprivate override func moveSection(_ section: Int, toSection newSection: Int) {
super.moveSection(section, toSection: newSection)
batchUpdates.moveSections.append(SectionMove(from: section, to: newSection))
}
fileprivate func resetTestCounts() {
reloadDataCount = 0
batchUpdates = BatchUpdates()
}
fileprivate func verifyHeader(_ section: Int, text: String?, frame: CGRect?, file: StaticString, line: UInt) {
verifySupplementaryView(UICollectionView.elementKindSectionHeader, section: section, text: text, frame: frame, file: file, line: line)
}
fileprivate func verifyFooter(_ section: Int, text: String?, frame: CGRect?, file: StaticString, line: UInt) {
verifySupplementaryView(UICollectionView.elementKindSectionFooter, section: section, text: text, frame: frame, file: file, line: line)
}
private func verifySupplementaryView(_ kind: String, section: Int, text: String?, frame: CGRect?, file: StaticString, line: UInt) {
let indexPath = IndexPath(item: 0, section: section)
let attributes = layoutAttributesForSupplementaryElement(ofKind: kind, at: indexPath)
if let frame = frame {
XCTAssertEqual(attributes?.frame, frame, file: file, line: line)
} else {
XCTAssertEqual(attributes?.frame.size, .zero, file: file, line: line)
}
if #available(iOS 9.0, *) {
func labelText(_ reusableView: UICollectionReusableView?) -> String? {
guard let label = reusableView?.subviews.first as? UILabel else {
return nil
}
return label.text
}
let supplementaryViewTexts = visibleSupplementaryViews(ofKind: kind).compactMap(labelText)
if let text = text {
XCTAssertTrue(supplementaryViewTexts.contains(text), file: file, line: line)
}
}
}
fileprivate func verifyVisibleItem(_ text: String, frame: CGRect, file: StaticString, line: UInt) {
let items = visibleCells.filter { cell -> Bool in
guard let label = cell.contentView.subviews.first as? UILabel else {
return false
}
return label.text == text
}
XCTAssertEqual(items.only?.frame, frame, file: file, line: line)
}
}