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 264
Expand file tree
/
Copy pathViewRecyclerTests.swift
More file actions
178 lines (145 loc) · 5.89 KB
/
ViewRecyclerTests.swift
File metadata and controls
178 lines (145 loc) · 5.89 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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 ViewRecyclerTests: XCTestCase {
func testNilIdNotRecycledAndNotRemoved() {
let root = View()
let zero = View()
zero.layoutKitRootView = nil // default
root.addSubview(zero)
let recycler = ViewRecycler(rootView: root)
let expectedView = View()
let v: View? = recycler.makeOrRecycleView(havingViewReuseId: nil, viewProvider: {
return expectedView
})
XCTAssertEqual(v, expectedView)
recycler.purgeViews()
XCTAssertNotNil(zero.superview, "`zero` should not be removed because `layoutKitRootView` is nil")
}
func testNilIdNotRecycledAndRemoved() {
let root = View()
let zero = View()
zero.layoutKitRootView = root
root.addSubview(zero)
let recycler = ViewRecycler(rootView: root)
let expectedView = View()
let v: View? = recycler.makeOrRecycleView(havingViewReuseId: nil, viewProvider: {
return expectedView
})
XCTAssertEqual(v, expectedView)
recycler.purgeViews()
XCTAssertNil(zero.superview, "`zero` should be removed because `layoutKitRootView` is set")
}
func testNonNilIdRecycled() {
let root = View()
let one = View(viewReuseId: "1")
root.addSubview(one)
one.layoutKitRootView = root
let recycler = ViewRecycler(rootView: root)
let v: View? = recycler.makeOrRecycleView(havingViewReuseId: "1", viewProvider: {
XCTFail("view should have been recycled")
return View()
})
XCTAssertEqual(v, one)
recycler.purgeViews()
XCTAssertNotNil(one.superview)
}
func testRecycledViewWithViewReuseId() {
let root = View()
let one = View(viewReuseId: "1")
root.addSubview(one)
let recycler = ViewRecycler(rootView: root)
let v: View? = recycler.makeOrRecycleView(havingViewReuseId: nil, viewProvider: {
return one
})
XCTAssertEqual(v, one)
recycler.purgeViews()
XCTAssertNotNil(one.superview)
}
func testRecycledViewWithoutViewReuseId() {
let root = View()
let one = View()
root.addSubview(one)
let recycler = ViewRecycler(rootView: root)
let v: View? = recycler.makeOrRecycleView(havingViewReuseId: nil, viewProvider: {
return one
})
XCTAssertEqual(v, one)
recycler.purgeViews()
XCTAssertNotNil(one.superview)
}
/// Create a hierarchy (rootOne > middleView > rootTwo > someSubview) where rootOne and rootTwo
/// are LayoutKit root nodes. Then ensure that purging rootOne doesn't remove someSubview.
func testDoesntPurgeOtherNodesSubviews() {
let rootOne = View(viewReuseId: "1")
let middleView = View()
let rootTwo = View(viewReuseId: "2")
let someSubview = View()
rootOne.addSubview(middleView)
middleView.addSubview(rootTwo)
rootTwo.addSubview(someSubview)
let recycler = ViewRecycler(rootView: rootOne)
_ = recycler.makeOrRecycleView(havingViewReuseId: "3") {
return middleView
}
let recycler2 = ViewRecycler(rootView: rootTwo)
_ = recycler2.makeOrRecycleView(havingViewReuseId: "4") {
return someSubview
}
recycler.purgeViews()
XCTAssertEqual(someSubview.superview, rootTwo)
}
/// Create a hierarchy (rootOne > middleView > rootTwo > someSubview) where rootOne and rootTwo
/// are LayoutKit root nodes. Then ensure that rootOne can't recycle someSubview.
func testDoesntRecycleOtherNodesViews() {
let rootOne = View(viewReuseId: "1")
let middleView = View(viewReuseId: "2")
let rootTwo = View(viewReuseId: "3")
let someSubview = View(viewReuseId: "4")
rootOne.addSubview(middleView)
middleView.layoutKitRootView = rootOne
middleView.addSubview(rootTwo)
rootTwo.addSubview(someSubview)
someSubview.layoutKitRootView = rootTwo
let recycler = ViewRecycler(rootView: rootOne)
let aDifferentView = View()
let v: View? = recycler.makeOrRecycleView(havingViewReuseId: "4") {
return aDifferentView
}
XCTAssertEqual(v, aDifferentView)
}
/// Test for safe subview-purge in composite view e.g. UIButton.
/// - SeeAlso: https://github.com/linkedin/LayoutKit/pull/85
#if os(iOS) || os(tvOS)
func testRecycledCompositeView() {
let root = View()
let button = UIButton(viewReuseId: "1")
root.addSubview(button)
button.layoutKitRootView = root
button.setTitle("dummy", for: .normal)
button.layoutIfNeeded()
XCTAssertEqual(button.subviews.count, 1, "UIButton should have 1 subview because `title` is set")
let recycler = ViewRecycler(rootView: root)
let v: View? = recycler.makeOrRecycleView(havingViewReuseId: "1", viewProvider: {
XCTFail("button should have been recycled")
return View()
})
XCTAssertEqual(v, button)
recycler.purgeViews()
XCTAssertNotNil(button.superview)
XCTAssertEqual(button.subviews.count, 1, "UIButton's subviews should not be removed by `recycler`")
}
#endif
}
extension View {
convenience init(viewReuseId: String) {
self.init(frame: .zero)
self.viewReuseId = viewReuseId
}
}