-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TabDisplayManagerTests.swift
613 lines (501 loc) · 25.4 KB
/
TabDisplayManagerTests.swift
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
@testable import Client
import WebKit
import Shared
import XCTest
import Common
class TabDisplayManagerTests: XCTestCase {
var tabCellIdentifier: TabDisplayerDelegate.TabCellIdentifier = TopTabCell.cellIdentifier
var mockDataStore: WeakListMock<Tab>!
var dataStore: WeakList<Tab>!
var collectionView: MockCollectionView!
var profile: TabManagerMockProfile!
var manager: TabManager!
var cfrDelegate: MockInactiveTabsCFRDelegate!
override func setUp() {
super.setUp()
DependencyHelperMock().bootstrapDependencies()
mockDataStore = WeakListMock<Tab>()
dataStore = WeakList<Tab>()
profile = TabManagerMockProfile(databasePrefix: "TabDisplayManagerTests") // not using the default prefix to avoid side effects with other tests
manager = LegacyTabManager(profile: profile, imageStore: nil)
collectionView = MockCollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
cfrDelegate = MockInactiveTabsCFRDelegate()
LegacyFeatureFlagsManager.shared.initializeDeveloperFeatures(with: profile)
UserDefaults().setValue(true, forKey: PrefsKeys.KeyInactiveTabsFirstTimeRun)
}
override func tearDown() {
super.tearDown()
AppContainer.shared.reset()
profile.shutdown()
manager.testRemoveAll()
manager.testClearArchive()
profile = nil
manager = nil
collectionView = nil
cfrDelegate = nil
dataStore.removeAll()
mockDataStore.removeAll()
mockDataStore = nil
dataStore = nil
}
// MARK: Index to place tab
func testIndexToPlaceTab_countAtZero_placeAtZero() {
mockDataStore.countToReturn = 0
let tabDisplayManager = createTabDisplayManager()
let index = tabDisplayManager.getIndexToPlaceTab(placeNextToParentTab: false)
XCTAssertEqual(index, 0)
}
func testIndexToPlaceTab_countAtOne_placeAtOne() {
mockDataStore.countToReturn = 1
let tabDisplayManager = createTabDisplayManager()
let index = tabDisplayManager.getIndexToPlaceTab(placeNextToParentTab: false)
XCTAssertEqual(index, 1)
}
func testIndexToPlaceTab_countAtTwo_placeAtTwo() {
mockDataStore.countToReturn = 2
let tabDisplayManager = createTabDisplayManager()
let index = tabDisplayManager.getIndexToPlaceTab(placeNextToParentTab: false)
XCTAssertEqual(index, 2)
}
func testIndexToPlaceTab_countAtMinusOne_placeAtZero() {
mockDataStore.countToReturn = -1
let tabDisplayManager = createTabDisplayManager()
let index = tabDisplayManager.getIndexToPlaceTab(placeNextToParentTab: false)
XCTAssertEqual(index, 0)
}
func testIndexToPlaceTab_hasOneTab_newTabAtIndex1() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
// Add one tab
let selectedTab = manager.addTab()
manager.selectTab(selectedTab)
XCTAssertEqual(tabDisplayManager.dataStore.count, 1)
// Add new tab
let newTab = manager.addTab()
XCTAssertEqual(tabDisplayManager.dataStore.count, 2)
XCTAssertEqual(manager.tabs[1].tabUUID, newTab.tabUUID, "New tab should be placed at last position, at index 1")
XCTAssertEqual(tabDisplayManager.dataStore.at(1)?.tabUUID, newTab.tabUUID, "New tab should be placed at last position, at index 1")
}
func testIndexToPlaceTab_hasThreeTabsLastSelected_newTabAtIndex3() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
// Add three tab
manager.addTab()
manager.addTab()
let selectedTab = manager.addTab()
manager.selectTab(selectedTab)
XCTAssertEqual(tabDisplayManager.dataStore.count, 3)
// Add new tab
let newTab = manager.addTab()
XCTAssertEqual(tabDisplayManager.dataStore.count, 4)
XCTAssertEqual(manager.tabs[3].tabUUID, newTab.tabUUID, "New tab should be placed at last position, at index 3")
XCTAssertEqual(tabDisplayManager.dataStore.at(3)?.tabUUID, newTab.tabUUID, "New tab should be placed at last position, at index 3")
}
func testIndexToPlaceTab_hasThreeTabsFirstSelected_newTabAtIndex3() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
// Add three tab
manager.addTab()
manager.addTab()
let selectedTab = manager.addTab()
manager.selectTab(selectedTab)
XCTAssertEqual(tabDisplayManager.dataStore.count, 3)
// Add new tab
let newTab = manager.addTab()
XCTAssertEqual(tabDisplayManager.dataStore.count, 4)
XCTAssertEqual(manager.tabs[3].tabUUID, newTab.tabUUID, "New tab should be placed at last position, at index 3")
XCTAssertEqual(tabDisplayManager.dataStore.at(3)?.tabUUID, newTab.tabUUID, "New tab should be placed at last position, at index 3")
}
// MARK: Place tab next to parent tab
func testPlaceNextToParentTab_parentWasLastTab_newTabPlaceIsLast() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
// Add three tabs with parent at last position
manager.addTab()
manager.addTab()
let parentTab = manager.addTab()
manager.selectTab(parentTab)
XCTAssertEqual(tabDisplayManager.dataStore.count, 3)
// Add child tab after parent
let childTab = manager.addTab(afterTab: parentTab)
XCTAssertEqual(tabDisplayManager.dataStore.count, 4)
XCTAssertEqual(manager.tabs[3].tabUUID, childTab.tabUUID, "Child tab should be placed after the parent tab, at index 3")
XCTAssertEqual(tabDisplayManager.dataStore.at(3)?.tabUUID, childTab.tabUUID, "Child tab should be placed after the parent tab, at index 3")
}
func testPlaceNextToParentTab_parentWasNotLastTab_newTabPlaceIsAfterParent() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
// Add three tabs with parent at second position, not last
manager.addTab()
let parentTab = manager.addTab()
manager.addTab()
manager.selectTab(parentTab)
XCTAssertEqual(tabDisplayManager.dataStore.count, 3)
// Add child tab after parent
let childTab = manager.addTab(afterTab: parentTab)
XCTAssertEqual(tabDisplayManager.dataStore.count, 4)
XCTAssertEqual(manager.tabs[2].tabUUID, childTab.tabUUID, "Child tab should be placed after the parent tab, at index 2")
XCTAssertEqual(tabDisplayManager.dataStore.at(2)?.tabUUID, childTab.tabUUID, "Child tab should be placed after the parent tab, at index 2")
}
func testPlaceNextToParentTab_parentWasFirstTab_newTabPlaceIsAfterParent() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
// Add three tabs with parent at first position
let parentTab = manager.addTab()
manager.addTab()
manager.addTab()
manager.selectTab(parentTab)
XCTAssertEqual(tabDisplayManager.dataStore.count, 3)
// Add child tab after parent
let childTab = manager.addTab(afterTab: parentTab)
XCTAssertEqual(tabDisplayManager.dataStore.count, 4)
XCTAssertEqual(manager.tabs[1].tabUUID, childTab.tabUUID, "Child tab should be placed after the parent tab, at index 1")
XCTAssertEqual(tabDisplayManager.dataStore.at(1)?.tabUUID, childTab.tabUUID, "Child tab should be placed after the parent tab, at index 1")
}
// MARK: Selected cell
func testSelectedCell_lastTabSelectedAndRemoved() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
// Add four tabs with selected at last position
manager.addTab()
manager.addTab()
manager.addTab()
let selectedTab = manager.addTab()
manager.selectTab(selectedTab)
removeTabAndAssert(tab: selectedTab) {
// Should be selected tab is index 2, and no other one is selected
self.testSelectedCells(tabDisplayManager: tabDisplayManager, numberOfCells: 3, selectedIndex: 2)
}
}
func testSelectedCell_secondToLastTabSelectedAndRemoved() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
// Add four tabs with selected at second to last position
manager.addTab()
manager.addTab()
let selectedTab = manager.addTab()
manager.addTab()
manager.selectTab(selectedTab)
// Remove selected
removeTabAndAssert(tab: selectedTab) {
// Should be selected tab is index 1, and no other one is selected
self.testSelectedCells(tabDisplayManager: tabDisplayManager, numberOfCells: 3, selectedIndex: 2)
}
}
func testSelectedCell_secondToLastTabSelectedAndTabBeforeRemoved() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
// Add four tabs with selected at second to last position
manager.addTab()
let tabToRemove = manager.addTab()
let selectedTab = manager.addTab()
manager.addTab()
manager.selectTab(selectedTab)
// Remove second tab
removeTabAndAssert(tab: tabToRemove) {
// Should be selected tab is index 1, and no other one is selected
self.testSelectedCells(tabDisplayManager: tabDisplayManager, numberOfCells: 3, selectedIndex: 1)
}
}
// MARK: Inactive Tabs
func testInactiveTabs_iPad_hiddenInTopTabs() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TopTabTray
// Add four tabs (2 inactive, 2 active)
let inactiveTab1 = manager.addTab()
inactiveTab1.lastExecutedTime = Date().older.toTimestamp()
let inactiveTab2 = manager.addTab()
inactiveTab2.lastExecutedTime = Date().older.toTimestamp()
let activeTab1 = manager.addTab()
_ = manager.addTab()
manager.selectTab(activeTab1)
let expectation = self.expectation(description: "TabDisplayManagerTests")
tabDisplayManager.refreshStore {
XCTAssertEqual(tabDisplayManager.filteredTabs.count, 2, "Only 2 active tabs should be displayed")
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}
func testInactiveTabs_grid_singleInactiveTabShowAsActive() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
// Add 1 inactive tab
let inactiveTab1 = manager.addTab()
inactiveTab1.lastExecutedTime = Date().older.toTimestamp()
let expectation = self.expectation(description: "TabDisplayManagerTests")
tabDisplayManager.refreshStore {
XCTAssertEqual(tabDisplayManager.filteredTabs.count, 1, "1 active tabs should be displayed")
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}
func testInactiveTabs_grid_filterInactive() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
// Add four tabs (2 inactive, 2 active)
let inactiveTab1 = manager.addTab()
inactiveTab1.lastExecutedTime = Date().older.toTimestamp()
let inactiveTab2 = manager.addTab()
inactiveTab2.lastExecutedTime = Date().older.toTimestamp()
let activeTab1 = manager.addTab()
_ = manager.addTab()
manager.selectTab(activeTab1)
let expectation = self.expectation(description: "TabDisplayManagerTests")
tabDisplayManager.refreshStore {
XCTAssertEqual(tabDisplayManager.filteredTabs.count, 2, "Only 2 active tabs should be displayed")
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}
func testInactiveTabs_grid_closeTabs() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
tabDisplayManager.inactiveViewModel = InactiveTabViewModel(theme: LightTheme())
// Add four tabs (2 inactive, 2 active)
let inactiveTab1 = manager.addTab()
inactiveTab1.lastExecutedTime = Date().older.toTimestamp()
let inactiveTab2 = manager.addTab()
inactiveTab2.lastExecutedTime = Date().older.toTimestamp()
let activeTab1 = manager.addTab()
_ = manager.addTab()
manager.selectTab(activeTab1)
tabDisplayManager.inactiveViewModel?.inactiveTabs = [inactiveTab1,
inactiveTab2]
// Force collectionView reload section to avoid crash
cfrDelegate.isUndoButtonPressed = false
// Force collectionView reload to avoid crash
collectionView.reloadSections(IndexSet(integer: 0))
tabDisplayManager.didTapCloseInactiveTabs(tabsCount: 2)
// For delete all inactive tabs we don't actually delete the tabs we collapse
// the section and delete after toast delay
XCTAssertTrue(tabDisplayManager.inactiveViewModel?.shouldHideInactiveTabs ?? false, "Inactive tabs should be empty after closing")
}
func testInactiveTabs_grid_undoSingleTab() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
tabDisplayManager.inactiveViewModel = InactiveTabViewModel(theme: LightTheme())
// Add three tabs (2 inactive, 1 active)
let inactiveTab1 = manager.addTab()
inactiveTab1.lastExecutedTime = Date().older.toTimestamp()
let inactiveTab2 = manager.addTab()
inactiveTab2.lastExecutedTime = Date().older.toTimestamp()
let activeTab1 = manager.addTab()
manager.selectTab(activeTab1)
tabDisplayManager.inactiveViewModel?.inactiveTabs = [inactiveTab1,
inactiveTab2]
// Force collectionView reload section to avoid crash
collectionView.reloadSections(IndexSet(integer: 0))
cfrDelegate.isUndoButtonPressed = true
// Force collectionView reload to avoid crash
collectionView.reloadSections(IndexSet(integer: 0))
tabDisplayManager.closeInactiveTab(inactiveTab1, index: 0)
let expectation = self.expectation(description: "TabDisplayManagerTests")
tabDisplayManager.refreshStore {
XCTAssertEqual(tabDisplayManager.inactiveViewModel?.inactiveTabs.count, 2, "Expected 2 inactive tabs after undo")
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}
func testInactiveTabs_grid_closeSingleTab() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
tabDisplayManager.inactiveViewModel = InactiveTabViewModel(theme: LightTheme())
// Add three tabs (2 inactive, 1 active)
let inactiveTab1 = manager.addTab()
inactiveTab1.lastExecutedTime = Date().older.toTimestamp()
let inactiveTab2 = manager.addTab()
inactiveTab2.lastExecutedTime = Date().older.toTimestamp()
let activeTab1 = manager.addTab()
manager.selectTab(activeTab1)
tabDisplayManager.inactiveViewModel?.inactiveTabs = [inactiveTab1,
inactiveTab2]
// Force collectionView reload section to avoid crash
collectionView.reloadSections(IndexSet(integer: 0))
cfrDelegate.isUndoButtonPressed = false
// Force collectionView reload to avoid crash
collectionView.reloadSections(IndexSet(integer: 0))
tabDisplayManager.closeInactiveTab(inactiveTab1, index: 0)
let expectation = self.expectation(description: "TabDisplayManagerTests")
// Add delay so the tab removal finishes and the refreshStore gets the right tabs data
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
tabDisplayManager.refreshStore {
XCTAssertEqual(tabDisplayManager.inactiveViewModel?.inactiveTabs.count, 1, "Expected 1 inactive tab after deletion")
expectation.fulfill()
}
}
waitForExpectations(timeout: 5)
}
func testInactiveTabs_grid_undoCloseTabs() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
tabDisplayManager.inactiveViewModel = InactiveTabViewModel(theme: LightTheme())
// Add four tabs (2 inactive, 2 active)
let inactiveTab1 = manager.addTab()
inactiveTab1.lastExecutedTime = Date().older.toTimestamp()
let inactiveTab2 = manager.addTab()
inactiveTab2.lastExecutedTime = Date().older.toTimestamp()
let activeTab1 = manager.addTab()
_ = manager.addTab()
manager.selectTab(activeTab1)
tabDisplayManager.inactiveViewModel?.inactiveTabs = [inactiveTab1,
inactiveTab2]
// Force collectionView reload section to avoid crash
collectionView.reloadSections(IndexSet(integer: 0))
cfrDelegate.isUndoButtonPressed = true
// Force collectionView reload to avoid crash
collectionView.reloadSections(IndexSet(integer: 0))
tabDisplayManager.didTapCloseInactiveTabs(tabsCount: 2)
let expectation = self.expectation(description: "TabDisplayManagerTests")
tabDisplayManager.refreshStore {
XCTAssertEqual(tabDisplayManager.inactiveViewModel?.inactiveTabs.count, 2, "Expected 2 inactive tabs after undo")
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}
// MARK: - Undo tab close
func testShouldPresentUndoToastOnHomepage_ForLastTab() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
manager.addTab()
XCTAssertTrue(tabDisplayManager.shouldPresentUndoToastOnHomepage,
"Expected to present toast on homepage")
}
func testShouldNotPresentUndoToastOnHomepage_MultlipleTabs() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
// Add 2 tabs
let activeTab1 = manager.addTab()
_ = manager.addTab()
manager.selectTab(activeTab1)
XCTAssertFalse(tabDisplayManager.shouldPresentUndoToastOnHomepage,
"Expected to present toast on TabTray")
}
func testShouldNotPresentUndoToastOnHomepage_ForLastPrivateTab() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
_ = manager.addTab(nil, afterTab: nil, isPrivate: true)
XCTAssertFalse(tabDisplayManager.shouldPresentUndoToastOnHomepage,
"Expected to present toast on TabTray")
}
func testShouldNotPresentUndoToastOnHomepage_ForMultiplePrivateTabs() {
let tabDisplayManager = createTabDisplayManager(useMockDataStore: false)
tabDisplayManager.tabDisplayType = .TabGrid
// Add 2 tabs
let activeTab1 = manager.addTab(nil, afterTab: nil, isPrivate: true)
_ = manager.addTab(nil, afterTab: nil, isPrivate: true)
manager.selectTab(activeTab1)
XCTAssertFalse(tabDisplayManager.shouldPresentUndoToastOnHomepage,
"Expected to present toast on TabTray")
}
func testInitWithExistingRegularTabs() {
let tabDisplayManager = createTabDisplayManagerWithTabs(amountOfTabs: 3, isPrivate: false)
tabDisplayManager.tabDisplayType = .TabGrid
XCTAssertEqual(manager.normalTabs.count, 3, "Expected 3 tabs")
}
func testInitWithExistingPrivateTabs() {
let tabDisplayManager = createTabDisplayManagerWithTabs(amountOfTabs: 3, isPrivate: true)
tabDisplayManager.tabDisplayType = .TabGrid
XCTAssertEqual(manager.privateTabs.count, 3, "Expected 3 tabs")
}
func testTabDisplayManager_doesntLeak() {
let subject = createTabDisplayManager(useMockDataStore: false)
trackForMemoryLeaks(subject)
}
}
// Helper methods
extension TabDisplayManagerTests {
func removeTabAndAssert(tab: Tab, completion: @escaping () -> Void) {
let expectation = self.expectation(description: "Tab is removed")
manager.removeTab(tab) {
completion()
expectation.fulfill()
}
waitForExpectations(timeout: 5, handler: nil)
}
func createTabDisplayManager(file: StaticString = #file,
line: UInt = #line,
useMockDataStore: Bool = true) -> TabDisplayManager {
let tabDisplayManager = TabDisplayManager(collectionView: collectionView,
tabManager: manager,
tabDisplayer: self,
reuseID: TopTabCell.cellIdentifier,
tabDisplayType: .TopTabTray,
profile: profile,
cfrDelegate: cfrDelegate,
theme: LightTheme())
collectionView.dataSource = tabDisplayManager
tabDisplayManager.dataStore = useMockDataStore ? mockDataStore : dataStore
trackForMemoryLeaks(tabDisplayManager, file: file, line: line)
return tabDisplayManager
}
func createTabDisplayManagerWithTabs(amountOfTabs: Int, isPrivate: Bool) -> TabDisplayManager {
for _ in 0..<amountOfTabs {
_ = manager.addTab(nil, afterTab: nil, isPrivate: isPrivate)
}
let tabDisplayManager = TabDisplayManager(collectionView: collectionView,
tabManager: manager,
tabDisplayer: self,
reuseID: TopTabCell.cellIdentifier,
tabDisplayType: .TopTabTray,
profile: profile,
cfrDelegate: cfrDelegate,
theme: LightTheme())
collectionView.dataSource = tabDisplayManager
trackForMemoryLeaks(tabDisplayManager)
return tabDisplayManager
}
func testSelectedCells(tabDisplayManager: TabDisplayManager, numberOfCells: Int, selectedIndex: Int, file: StaticString = #filePath, line: UInt = #line) {
XCTAssertEqual(tabDisplayManager.dataStore.count, numberOfCells, file: file, line: line)
for index in 0..<numberOfCells {
let cell = tabDisplayManager.collectionView(collectionView, cellForItemAt: IndexPath(row: index, section: 0)) as! TabTrayCell
if index == selectedIndex {
XCTAssertTrue(cell.isSelectedTab, file: file, line: line)
} else {
XCTAssertFalse(cell.isSelectedTab, file: file, line: line)
}
}
}
}
extension TabDisplayManagerTests: TabDisplayerDelegate {
func focusSelectedTab() {}
func cellFactory(for cell: UICollectionViewCell, using tab: Tab) -> UICollectionViewCell {
guard let tabCell = cell as? TabTrayCell else { return UICollectionViewCell() }
let isSelected = (tab == manager.selectedTab)
tabCell.configureWith(tab: tab, isSelected: isSelected, theme: LightTheme())
return tabCell
}
}
class WeakListMock<T: AnyObject>: WeakList<T> {
var countToReturn: Int = 0
override var count: Int {
return countToReturn
}
override var isEmpty: Bool {
return countToReturn <= 0
}
}
class MockCollectionView: UICollectionView {
override init(frame: CGRect, collectionViewLayout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout: collectionViewLayout)
register(TopTabCell.self, forCellWithReuseIdentifier: TopTabCell.cellIdentifier)
register(InactiveTabCell.self, forCellWithReuseIdentifier: InactiveTabCell.cellIdentifier)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func performBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil) {
updates?()
completion?(true)
}
/// Due to the usage of MockCollectionView with the overridden performBatchUpdates in prod code for those tests, deleteItems needs an extra check.
/// No check was added for prod code so it would crash in case of concurrency (which would be abnormal and need to be detected)
override func deleteItems(at indexPaths: [IndexPath]) {
guard indexPaths[0].row <= numberOfItems(inSection: 0) - 1 else { return }
super.deleteItems(at: indexPaths)
}
}
class MockInactiveTabsCFRDelegate: InactiveTabsCFRProtocol {
var isUndoButtonPressed = true
func setupCFR(with view: UILabel) { }
func presentCFR() { }
func presentUndoToast(tabsCount: Int, completion: @escaping (Bool) -> Void) {
completion(isUndoButtonPressed)
}
func presentUndoSingleToast(completion: @escaping (Bool) -> Void) {
completion(isUndoButtonPressed)
}
}