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 pathReloadableViewLayoutAdapterTableViewTests.swift
More file actions
142 lines (115 loc) · 5.39 KB
/
ReloadableViewLayoutAdapterTableViewTests.swift
File metadata and controls
142 lines (115 loc) · 5.39 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
// 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 ReloadableViewLayoutAdapterTableViewTests: ReloadableViewLayoutAdapterTestCase {
func testTableViewReloadSync() {
verifyReloadSync(TestTableView())
}
func testTableViewReloadBatchUpdatesSync() {
verifyReloadBatchUpdatesSync(TestTableView())
}
func testTableViewReloadSyncCancelsPreviousLayout() {
verifyReloadSyncCancelsPreviousLayout(TestTableView())
}
func testTableViewReloadAsync() {
verifyReloadAsync(TestTableView())
}
func testTableViewReloadBatchUpdatesAsync() {
verifyReloadBatchUpdatesAsync(TestTableView())
}
func testTableViewReloadAsyncCancelsPreviousLayout() {
verifyReloadAsyncCancelsPreviousLayout(TestTableView())
}
func testTableViewReloadAsyncCancelledOnViewDeinit() {
verifyReloadAsyncCancelledOnViewDeinit(TestTableView())
}
}
private class TestTableView: LayoutAdapterTableView, TestableReloadableView {
var reloadDataCount = 0
var batchUpdates = BatchUpdates()
#if os(tvOS)
// tvOS adds padding around UITableViewCells
// http://stackoverflow.com/questions/33364236/why-is-there-a-space-between-the-cells-in-my-uitableview
let itemSpacing: CGFloat = 14
let itemWidthInset: CGFloat = 16
#else
let itemSpacing: CGFloat = 0
let itemWidthInset: CGFloat = 0
#endif
init() {
let frame = CGRect(x: 0, y: 0, width: 320, height: 1000)
super.init(frame: frame, style: .plain)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func reloadData() {
super.reloadData()
reloadDataCount += 1
}
fileprivate override func insertRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) {
super.insertRows(at: indexPaths, with: animation)
batchUpdates.insertItems.append(contentsOf: indexPaths)
}
fileprivate override func deleteRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) {
super.deleteRows(at: indexPaths, with: animation)
batchUpdates.deleteItems.append(contentsOf: indexPaths)
}
fileprivate override func reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) {
super.reloadRows(at: indexPaths, with: animation)
batchUpdates.reloadItems.append(contentsOf: indexPaths)
}
fileprivate override func moveRow(at indexPath: IndexPath, to newIndexPath: IndexPath) {
super.moveRow(at: indexPath, to: newIndexPath)
batchUpdates.moveItems.append(ItemMove(from: indexPath, to: newIndexPath))
}
fileprivate override func insertSections(_ sections: IndexSet, with animation: UITableView.RowAnimation) {
super.insertSections(sections, with: animation)
batchUpdates.insertSections.formUnion(sections)
}
fileprivate override func reloadSections(_ sections: IndexSet, with animation: UITableView.RowAnimation) {
super.reloadSections(sections, with: animation)
batchUpdates.reloadSections.formUnion(sections)
}
fileprivate override func deleteSections(_ sections: IndexSet, with animation: UITableView.RowAnimation) {
super.deleteSections(sections, with: animation)
batchUpdates.deleteSections.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) {
verify(rectForHeader(inSection:), section: section, text: text, frame: frame, file: file, line: line)
}
fileprivate func verifyFooter(_ section: Int, text: String?, frame: CGRect?, file: StaticString, line: UInt) {
verify(rectForFooter(inSection:), section: section, text: text, frame: frame, file: file, line: line)
}
private func verify(_ getter: (Int) -> CGRect, section: Int, text: String?, frame: CGRect?, file: StaticString, line: UInt) {
let headerFrame = getter(section)
if let frame = frame {
XCTAssertEqual(headerFrame, frame, file: file, line: line)
} else {
XCTAssertEqual(headerFrame.size, CGSize(width: bounds.width, height: 0), 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)
}
}