Skip to content

Commit 05e2618

Browse files
committed
Adds unit tests for didUpdateFocusInContextWithAnimationCoordinator.
1 parent 2b9e484 commit 05e2618

File tree

2 files changed

+129
-37
lines changed

2 files changed

+129
-37
lines changed

Tests/RxCocoaTests/UICollectionView+RxTests.swift

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ import XCTest
1212

1313
// UICollectionView
1414
final class UICollectionViewTests : RxTest {
15-
func testCollectionView_DelegateEventCompletesOnDealloc() {
15+
func test_DelegateEventCompletesOnDealloc() {
1616
let layout = UICollectionViewFlowLayout()
1717
let createView: () -> UICollectionView = { UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout) }
1818

1919
ensureEventDeallocated(createView) { (view: UICollectionView) in view.rx.itemSelected }
2020
ensureEventDeallocated(createView) { (view: UICollectionView) in view.rx.itemDeselected }
2121
ensureEventDeallocated(createView) { (view: UICollectionView) in view.rx.modelSelected(Int.self) }
2222
ensureEventDeallocated(createView) { (view: UICollectionView) in view.rx.modelDeselected(Int.self) }
23+
24+
#if os(tvOS)
25+
ensureEventDeallocated(createView) { (view: UICollectionView) in view.rx.didUpdateFocusInContextWithAnimationCoordinator }
26+
#endif
2327
}
2428

25-
func testCollectionView_itemSelected() {
29+
func test_itemSelected() {
2630
let layout = UICollectionViewFlowLayout()
2731
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
2832

@@ -40,7 +44,7 @@ final class UICollectionViewTests : RxTest {
4044
subscription.dispose()
4145
}
4246

43-
func testCollectionView_itemDeselected() {
47+
func test_itemDeselected() {
4448
let layout = UICollectionViewFlowLayout()
4549
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
4650

@@ -58,7 +62,7 @@ final class UICollectionViewTests : RxTest {
5862
subscription.dispose()
5963
}
6064

61-
func testCollectionView_itemHighlighted() {
65+
func test_itemHighlighted() {
6266
let layout = UICollectionViewFlowLayout()
6367
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
6468

@@ -76,7 +80,7 @@ final class UICollectionViewTests : RxTest {
7680
subscription.dispose()
7781
}
7882

79-
func testCollectionView_itemUnhighlighted() {
83+
func test_itemUnhighlighted() {
8084
let layout = UICollectionViewFlowLayout()
8185
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
8286

@@ -94,7 +98,7 @@ final class UICollectionViewTests : RxTest {
9498
subscription.dispose()
9599
}
96100

97-
func testCollectionView_willDisplayCell() {
101+
func test_willDisplayCell() {
98102
let layout = UICollectionViewFlowLayout()
99103
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
100104

@@ -117,7 +121,7 @@ final class UICollectionViewTests : RxTest {
117121
subscription.dispose()
118122
}
119123

120-
func testCollectionView_willDisplaySupplementaryView() {
124+
func test_willDisplaySupplementaryView() {
121125
let layout = UICollectionViewFlowLayout()
122126
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
123127

@@ -145,7 +149,7 @@ final class UICollectionViewTests : RxTest {
145149
subscription.dispose()
146150
}
147151

148-
func testCollectionView_didEndDisplayingCell() {
152+
func test_didEndDisplayingCell() {
149153
let layout = UICollectionViewFlowLayout()
150154
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
151155

@@ -168,7 +172,7 @@ final class UICollectionViewTests : RxTest {
168172
subscription.dispose()
169173
}
170174

171-
func testCollectionView_didEndDisplayingSupplementaryView() {
175+
func test_didEndDisplayingSupplementaryView() {
172176
let layout = UICollectionViewFlowLayout()
173177
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
174178

@@ -195,7 +199,7 @@ final class UICollectionViewTests : RxTest {
195199
subscription.dispose()
196200
}
197201

198-
func testCollectionView_DelegateEventCompletesOnDealloc1() {
202+
func test_DelegateEventCompletesOnDealloc1() {
199203
let items: Observable<[Int]> = Observable.just([1, 2, 3])
200204

201205
let layout = UICollectionViewFlowLayout()
@@ -210,7 +214,7 @@ final class UICollectionViewTests : RxTest {
210214
ensureEventDeallocated(createView) { (view: UICollectionView) in view.rx.modelSelected(Int.self) }
211215
}
212216

213-
func testCollectionView_DelegateEventCompletesOnDealloc2() {
217+
func test_DelegateEventCompletesOnDealloc2() {
214218
let items: Observable<[Int]> = Observable.just([1, 2, 3])
215219

216220
let layout = UICollectionViewFlowLayout()
@@ -227,7 +231,7 @@ final class UICollectionViewTests : RxTest {
227231
ensureEventDeallocated(createView) { (view: UICollectionView) in view.rx.modelSelected(Int.self) }
228232
}
229233

230-
func testCollectionView_DelegateEventCompletesOnDealloc2_cellType() {
234+
func test_DelegateEventCompletesOnDealloc2_cellType() {
231235
let items: Observable<[Int]> = Observable.just([1, 2, 3])
232236

233237
let layout = UICollectionViewFlowLayout()
@@ -244,7 +248,7 @@ final class UICollectionViewTests : RxTest {
244248
ensureEventDeallocated(createView) { (view: UICollectionView) in view.rx.modelSelected(Int.self) }
245249
}
246250

247-
func testCollectionView_ModelSelected_itemsWithCellFactory() {
251+
func test_ModelSelected_itemsWithCellFactory() {
248252
let items: Observable<[Int]> = Observable.just([1, 2, 3])
249253

250254
let layout = UICollectionViewFlowLayout()
@@ -277,7 +281,7 @@ final class UICollectionViewTests : RxTest {
277281
s.dispose()
278282
}
279283

280-
func testCollectionView_ModelSelected_itemsWithCellIdentifier() {
284+
func test_ModelSelected_itemsWithCellIdentifier() {
281285
let items: Observable<[Int]> = Observable.just([1, 2, 3])
282286

283287
let layout = UICollectionViewFlowLayout()
@@ -308,7 +312,7 @@ final class UICollectionViewTests : RxTest {
308312
dataSourceSubscription.dispose()
309313
}
310314

311-
func testCollectionView_ModelDeselected_itemsWithCellFactory() {
315+
func test_ModelDeselected_itemsWithCellFactory() {
312316
let items: Observable<[Int]> = Observable.just([1, 2, 3])
313317

314318
let layout = UICollectionViewFlowLayout()
@@ -342,7 +346,7 @@ final class UICollectionViewTests : RxTest {
342346
s.dispose()
343347
}
344348

345-
func testCollectionView_ModelDeselected_itemsWithCellIdentifier() {
349+
func test_ModelDeselected_itemsWithCellIdentifier() {
346350
let items: Observable<[Int]> = Observable.just([1, 2, 3])
347351

348352
let layout = UICollectionViewFlowLayout()
@@ -373,7 +377,7 @@ final class UICollectionViewTests : RxTest {
373377
dataSourceSubscription.dispose()
374378
}
375379

376-
func testCollectionView_modelAtIndexPath_normal() {
380+
func test_modelAtIndexPath_normal() {
377381
let items: Observable<[Int]> = Observable.just([1, 2, 3])
378382

379383
let layout = UICollectionViewFlowLayout()
@@ -394,6 +398,50 @@ final class UICollectionViewTests : RxTest {
394398

395399
dataSourceSubscription.dispose()
396400
}
401+
402+
#if os(tvOS)
403+
404+
func test_didUpdateFocusInContextWithAnimationCoordinator() {
405+
let items: Observable<[Int]> = Observable.just([1, 2, 3])
406+
407+
let layout = UICollectionViewFlowLayout()
408+
let createView: () -> (UICollectionView, Disposable) = {
409+
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 1, height: 1), collectionViewLayout: layout)
410+
collectionView.register(NSClassFromString("UICollectionViewCell"), forCellWithReuseIdentifier: "a")
411+
let dataSource = SectionedViewDataSourceMock()
412+
let dataSourceSubscription = items.bind(to: collectionView.rx.items(dataSource: dataSource))
413+
414+
return (collectionView, dataSourceSubscription)
415+
416+
}
417+
418+
let (collectionView, dataSourceSubscription) = createView()
419+
420+
var resultContext: UICollectionViewFocusUpdateContext? = nil
421+
var resultAnimationCoordinator: UIFocusAnimationCoordinator? = nil
422+
423+
let subscription = collectionView.rx.didUpdateFocusInContextWithAnimationCoordinator
424+
.subscribe(onNext: { args in
425+
let (context, animationCoordinator) = args
426+
resultContext = context
427+
resultAnimationCoordinator = animationCoordinator
428+
})
429+
430+
let context = UICollectionViewFocusUpdateContext()
431+
let animationCoordinator = UIFocusAnimationCoordinator()
432+
433+
XCTAssertEqual(resultContext, nil)
434+
XCTAssertEqual(resultAnimationCoordinator, nil)
435+
436+
collectionView.delegate!.collectionView!(collectionView, didUpdateFocusIn: context, with: animationCoordinator)
437+
438+
XCTAssertEqual(resultContext, context)
439+
XCTAssertEqual(resultAnimationCoordinator, animationCoordinator)
440+
441+
subscription.dispose()
442+
dataSourceSubscription.dispose()
443+
}
444+
#endif
397445
}
398446

399447
extension UICollectionViewTests {
@@ -465,7 +513,7 @@ extension UICollectionViewTests {
465513
XCTAssert(dataSourceDeallocated == true)
466514
}
467515

468-
func testCollectionViewDataSourceIsResetOnDispose() {
516+
func testDataSourceIsResetOnDispose() {
469517
var disposeEvents: [String] = []
470518

471519
let items: Observable<[Int]> = Observable.just([1, 2, 3]).concat(Observable.never())

0 commit comments

Comments
 (0)