forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabManagerTests.swift
501 lines (403 loc) · 19.1 KB
/
TabManagerTests.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
/* 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 Shared
import Storage
import UIKit
import WebKit
import XCTest
open class TabManagerMockProfile: MockProfile {
var numberOfTabsStored = 0
override public func storeTabs(_ tabs: [RemoteTab]) -> Deferred<Maybe<Int>> {
numberOfTabsStored = tabs.count
return deferMaybe(tabs.count)
}
}
struct MethodSpy {
let functionName: String
let method: ((_ tabs: [Tab?]) -> Void)?
init(functionName: String) {
self.functionName = functionName
self.method = nil
}
init(functionName: String, method: ((_ tabs: [Tab?]) -> Void)?) {
self.functionName = functionName
self.method = method
}
}
fileprivate let spyDidSelectedTabChange = "tabManager(_:didSelectedTabChange:previous:isRestoring:)"
open class MockTabManagerDelegate: TabManagerDelegate {
//this array represents the order in which delegate methods should be called.
//each delegate method will pop the first struct from the array. If the method name doesn't match the struct then the order is incorrect
//Then it evaluates the method closure which will return true/false depending on if the tabs are correct
var methodCatchers: [MethodSpy] = []
func expect(_ methods: [MethodSpy]) {
self.methodCatchers = methods
}
func verify(_ message: String) {
XCTAssertTrue(methodCatchers.isEmpty, message)
}
func testDelegateMethodWithName(_ name: String, tabs: [Tab?]) {
guard let spy = self.methodCatchers.first else {
XCTAssert(false, "No method was availible in the queue. For the delegate method \(name) to use")
return
}
XCTAssertEqual(spy.functionName, name)
if let methodCheck = spy.method {
methodCheck(tabs)
}
methodCatchers.removeFirst()
}
public func tabManager(_ tabManager: TabManager, didSelectedTabChange selected: Tab?, previous: Tab?, isRestoring: Bool) {
testDelegateMethodWithName(#function, tabs: [selected, previous])
}
public func tabManager(_ tabManager: TabManager, didAddTab tab: Tab, isRestoring: Bool) {
testDelegateMethodWithName(#function, tabs: [tab])
}
public func tabManager(_ tabManager: TabManager, didRemoveTab tab: Tab, isRestoring: Bool) {
testDelegateMethodWithName(#function, tabs: [tab])
}
public func tabManagerDidRestoreTabs(_ tabManager: TabManager) {
testDelegateMethodWithName(#function, tabs: [])
}
public func tabManagerDidAddTabs(_ tabManager: TabManager) {
testDelegateMethodWithName(#function, tabs: [])
}
public func tabManagerDidRemoveAllTabs(_ tabManager: TabManager, toast: ButtonToast?) {
testDelegateMethodWithName(#function, tabs: [])
}
}
class TabManagerTests: XCTestCase {
let didRemove = MethodSpy(functionName: "tabManager(_:didRemoveTab:isRestoring:)")
let didAdd = MethodSpy(functionName: "tabManager(_:didAddTab:isRestoring:)")
var profile: TabManagerMockProfile!
var manager: TabManager!
var delegate: MockTabManagerDelegate!
override func setUp() {
super.setUp()
profile = TabManagerMockProfile()
manager = TabManager(profile: profile, imageStore: nil)
delegate = MockTabManagerDelegate()
}
override func tearDown() {
profile._shutdown()
manager.removeDelegate(delegate)
manager.removeAll()
super.tearDown()
}
func testAddTabShouldAddOneNormalTab() {
manager.addDelegate(delegate)
delegate.expect([didAdd])
manager.addTab()
delegate.verify("Not all delegate methods were called")
XCTAssertEqual(manager.normalTabs.count, 1, "There should be one normal tab")
}
func testAddTabShouldAddOnePrivateTab() {
manager.addDelegate(delegate)
delegate.expect([didAdd])
manager.addTab(isPrivate: true)
delegate.verify("Not all delegate methods were called")
XCTAssertEqual(manager.privateTabs.count, 1, "There should be one private tab")
}
func testAddTabAndSelect() {
manager.selectTab(manager.addTab())
XCTAssertEqual(manager.selectedIndex, 0, "There should be selected first tab")
}
func testMoveTabFromLastToFirstPosition() {
// add two tabs, last one will be selected
manager.selectTab(manager.addTab())
manager.moveTab(isPrivate: false, fromIndex: 1, toIndex: 0)
XCTAssertEqual(manager.selectedIndex, 0, "There should be selected second tab")
}
func testDidDeleteLastTab() {
let didSelect = MethodSpy(functionName: spyDidSelectedTabChange) { tabs in
XCTAssertNotNil(tabs[0])
XCTAssertNotNil(tabs[1])
}
// create the tab before adding the mock delegate. So we don't have to check delegate calls we dont care about
let tab = manager.addTab()
manager.selectTab(tab)
manager.addDelegate(delegate)
// it wont call didSelect because addTabAndSelect did not pass last removed tab
delegate.expect([didRemove, didAdd, didSelect])
manager.removeTabAndUpdateSelectedIndex(tab)
delegate.verify("Not all delegate methods were called")
}
func testDidDeleteLastPrivateTab() {
//create the tab before adding the mock delegate. So we don't have to check delegate calls we dont care about
let tab = manager.addTab()
manager.selectTab(tab)
let privateTab = manager.addTab(isPrivate: true)
manager.selectTab(privateTab)
manager.addDelegate(delegate)
let didSelect = MethodSpy(functionName: spyDidSelectedTabChange) { tabs in
let next = tabs[0]!
let previous = tabs[1]!
XCTAssertTrue(previous != next)
XCTAssertTrue(previous == privateTab)
XCTAssertTrue(next == tab)
XCTAssertTrue(previous.isPrivate)
XCTAssertTrue(self.manager.selectedTab == next)
}
delegate.expect([didRemove, didSelect])
manager.removeTabAndUpdateSelectedIndex(privateTab)
delegate.verify("Not all delegate methods were called")
}
func testDidCreateNormalTabWhenDeletingAll() {
let removeAllTabs = MethodSpy(functionName: "tabManagerDidRemoveAllTabs(_:toast:)")
//create the tab before adding the mock delegate. So we don't have to check delegate calls we dont care about
let tab = manager.addTab()
manager.selectTab(tab)
let privateTab = manager.addTab(isPrivate: true)
manager.selectTab(privateTab)
manager.addDelegate(delegate)
let didSelect = MethodSpy(functionName: spyDidSelectedTabChange) { _ in
// test fails if this not called
}
// This test makes sure that a normal tab is always added even when a normal tab is not selected when calling removeAll
delegate.expect([didRemove, didAdd, didSelect, removeAllTabs])
manager.removeTabsWithToast(manager.normalTabs)
delegate.verify("Not all delegate methods were called")
}
func testDeletePrivateTabsOnExit() {
profile.prefs.setBool(true, forKey: "settings.closePrivateTabs")
// create one private and one normal tab
let tab = manager.addTab()
manager.selectTab(tab)
manager.selectTab(manager.addTab(isPrivate: true))
XCTAssertEqual(manager.selectedTab?.isPrivate, true, "The selected tab should be the private tab")
XCTAssertEqual(manager.privateTabs.count, 1, "There should only be one private tab")
manager.selectTab(tab)
XCTAssertEqual(manager.privateTabs.count, 0, "If the normal tab is selected the private tab should have been deleted")
XCTAssertEqual(manager.normalTabs.count, 1, "The regular tab should stil be around")
manager.selectTab(manager.addTab(isPrivate: true))
XCTAssertEqual(manager.privateTabs.count, 1, "There should be one new private tab")
manager.willSwitchTabMode(leavingPBM: true)
XCTAssertEqual(manager.privateTabs.count, 0, "After willSwitchTabMode there should be no more private tabs")
manager.selectTab(manager.addTab(isPrivate: true))
manager.selectTab(manager.addTab(isPrivate: true))
XCTAssertEqual(manager.privateTabs.count, 2, "Private tabs should not be deleted when another one is added")
manager.selectTab(manager.addTab())
XCTAssertEqual(manager.privateTabs.count, 0, "But once we add a normal tab we've switched out of private mode. Private tabs should be deleted")
XCTAssertEqual(manager.normalTabs.count, 2, "The original normal tab and the new one should both still exist")
profile.prefs.setBool(false, forKey: "settings.closePrivateTabs")
manager.selectTab(manager.addTab(isPrivate: true))
manager.selectTab(tab)
XCTAssertEqual(manager.selectedTab?.isPrivate, false, "The selected tab should not be private")
XCTAssertEqual(manager.privateTabs.count, 1, "If the flag is false then private tabs should still exist")
}
func testTogglePBMDelete() {
profile.prefs.setBool(true, forKey: "settings.closePrivateTabs")
let tab = manager.addTab()
manager.selectTab(tab)
manager.selectTab(manager.addTab())
manager.selectTab(manager.addTab(isPrivate: true))
manager.willSwitchTabMode(leavingPBM: false)
XCTAssertEqual(manager.privateTabs.count, 1, "There should be 1 private tab")
manager.willSwitchTabMode(leavingPBM: true)
XCTAssertEqual(manager.privateTabs.count, 0, "There should be 0 private tab")
manager.removeTabAndUpdateSelectedIndex(tab)
XCTAssertEqual(manager.normalTabs.count, 1, "There should be 1 normal tab")
}
func testRemoveNonSelectedTab() {
let tab = manager.addTab()
manager.selectTab(tab)
manager.addTab()
let deleteTab = manager.addTab()
manager.removeTabAndUpdateSelectedIndex(deleteTab)
XCTAssertEqual(tab, manager.selectedTab)
XCTAssertFalse(manager.tabs.contains(deleteTab))
}
func testDeleteSelectedTab() {
func addTab(_ visit: Bool) -> Tab {
let tab = manager.addTab()
if visit {
tab.lastExecutedTime = Date.now()
}
return tab
}
let tab0 = addTab(false) // not visited
let tab1 = addTab(true)
let tab2 = addTab(true)
let tab3 = addTab(true)
let tab4 = addTab(false) // not visited
// starting at tab1, we should be selecting
// [ tab3, tab4, tab2, tab0 ]
manager.selectTab(tab1)
tab1.parent = tab3
manager.removeTabAndUpdateSelectedIndex(manager.selectedTab!)
// Rule: parent tab if it was the most recently visited
XCTAssertEqual(manager.selectedTab, tab3)
manager.removeTabAndUpdateSelectedIndex(manager.selectedTab!)
// Rule: next to the right.
XCTAssertEqual(manager.selectedTab, tab4)
manager.removeTabAndUpdateSelectedIndex(manager.selectedTab!)
// Rule: next to the left, when none to the right
XCTAssertEqual(manager.selectedTab, tab2)
manager.removeTabAndUpdateSelectedIndex(manager.selectedTab!)
// Rule: last one left.
XCTAssertEqual(manager.selectedTab, tab0)
}
func testDeleteLastTab() {
//create the tab before adding the mock delegate. So we don't have to check delegate calls we dont care about
(0..<10).forEach {_ in manager.addTab() }
manager.selectTab(manager.tabs.last)
let deleteTab = manager.tabs.last
let newSelectedTab = manager.tabs[8]
manager.addDelegate(delegate)
let didSelect = MethodSpy(functionName: spyDidSelectedTabChange) { tabs in
let next = tabs[0]!
let previous = tabs[1]!
XCTAssertEqual(deleteTab, previous)
XCTAssertEqual(next, newSelectedTab)
}
delegate.expect([didRemove, didSelect])
manager.removeTabAndUpdateSelectedIndex(manager.tabs.last!)
delegate.verify("Not all delegate methods were called")
}
func testDelegatesCalledWhenRemovingPrivateTabs() {
//setup
profile.prefs.setBool(true, forKey: "settings.closePrivateTabs")
// create one private and one normal tab
let tab = manager.addTab()
let newTab = manager.addTab()
manager.selectTab(tab)
manager.selectTab(manager.addTab(isPrivate: true))
manager.addDelegate(delegate)
// Double check a few things
XCTAssertEqual(manager.selectedTab?.isPrivate, true, "The selected tab should be the private tab")
XCTAssertEqual(manager.privateTabs.count, 1, "There should only be one private tab")
// switch to normal mode. Which should delete the private tabs
manager.willSwitchTabMode(leavingPBM: true)
//make sure tabs are cleared properly and indexes are reset
XCTAssertEqual(manager.privateTabs.count, 0, "Private tab should have been deleted")
XCTAssertEqual(manager.selectedIndex, -1, "The selected index should have been reset")
// didSelect should still be called when switching between a nil tab
let didSelect = MethodSpy(functionName: spyDidSelectedTabChange) { tabs in
XCTAssertNil(tabs[1], "there should be no previous tab")
let next = tabs[0]!
XCTAssertFalse(next.isPrivate)
}
// make sure delegate method is actually called
delegate.expect([didSelect])
// select the new tab to trigger the delegate methods
manager.selectTab(newTab)
// check
delegate.verify("Not all delegate methods were called")
}
func testDeleteFirstTab() {
//create the tab before adding the mock delegate. So we don't have to check delegate calls we dont care about
(0..<10).forEach {_ in manager.addTab() }
manager.selectTab(manager.tabs.first)
let deleteTab = manager.tabs.first
let newSelectedTab = manager.tabs[1]
manager.addDelegate(delegate)
let didSelect = MethodSpy(functionName: spyDidSelectedTabChange) { tabs in
let next = tabs[0]!
let previous = tabs[1]!
XCTAssertEqual(deleteTab, previous)
XCTAssertEqual(next, newSelectedTab)
}
delegate.expect([didRemove, didSelect])
manager.removeTabAndUpdateSelectedIndex(manager.tabs.first!)
delegate.verify("Not all delegate methods were called")
}
func testRemoveTabSelectedTabShouldChangeIndex() {
let tab1 = manager.addTab()
manager.addTab()
let tab3 = manager.addTab()
manager.selectTab(tab3)
let beforeRemoveTabIndex = manager.selectedIndex
manager.removeTabAndUpdateSelectedIndex(tab1)
XCTAssertNotEqual(manager.selectedIndex, beforeRemoveTabIndex)
XCTAssertEqual(manager.selectedTab, tab3)
XCTAssertEqual(manager.tabs[manager.selectedIndex], tab3)
}
func testRemoveTabRemovingLastNormalTabShouldNotSwitchToPrivateTab() {
let tab0 = manager.addTab()
let tab1 = manager.addTab(isPrivate: true)
manager.selectTab(tab0)
// select private tab, so we are in privateMode
manager.selectTab(tab1, previous: tab0)
// if we are able to remove normal tab this means we are no longer in private mode
manager.removeTabAndUpdateSelectedIndex(tab0)
// manager should creat new tab and select it
XCTAssertNotEqual(manager.selectedTab, tab1)
XCTAssertNotEqual(manager.selectedIndex, manager.tabs.firstIndex(of: tab1))
}
func testRemoveAllShouldRemoveAllTabs() {
let tab0 = manager.addTab()
let tab1 = manager.addTab()
manager.removeAll()
XCTAssert(nil == manager.tabs.firstIndex(of: tab0))
XCTAssert(nil == manager.tabs.firstIndex(of: tab1))
}
// Private tabs and regular tabs are in the same tabs array.
// Make sure that when a private tab is added inbetween regular tabs it isnt accidently selected when removing a regular tab
func testTabsIndex() {
// We add 2 tabs. Then a private one before adding another normal tab and selecting it.
// Make sure that when the last one is deleted we dont switch to the private tab
let (_, _, privateOne, last) = (manager.addTab(), manager.addTab(), manager.addTab(isPrivate: true), manager.addTab())
manager.selectTab(last)
manager.addDelegate(delegate)
let didSelect = MethodSpy(functionName: spyDidSelectedTabChange) { tabs in
let next = tabs[0]!
let previous = tabs[1]!
XCTAssertEqual(last, previous)
XCTAssert(next != privateOne && !next.isPrivate)
}
delegate.expect([didRemove, didSelect])
manager.removeTabAndUpdateSelectedIndex(last)
delegate.verify("Not all delegate methods were called")
}
func testRemoveTabAndUpdateSelectedIndexIsSelectedParentTabAfterRemoval() {
func addTab(_ visit: Bool) -> Tab {
let tab = manager.addTab()
if visit {
tab.lastExecutedTime = Date.now()
}
return tab
}
let _ = addTab(false) // not visited
let tab1 = addTab(true)
let _ = addTab(true)
let tab3 = addTab(true)
let _ = addTab(false) // not visited
manager.selectTab(tab1)
tab1.parent = tab3
manager.removeTabAndUpdateSelectedIndex(tab1)
XCTAssertEqual(manager.selectedTab, tab3)
}
func testTabsIndexClosingFirst() {
// We add 2 tabs. Then a private one before adding another normal tab and selecting the first.
// Make sure that when the last one is deleted we dont switch to the private tab
let deleted = manager.addTab()
let newSelected = manager.addTab()
manager.addTab(isPrivate: true)
manager.addTab()
manager.selectTab(manager.tabs.first)
manager.addDelegate(delegate)
let didSelect = MethodSpy(functionName: spyDidSelectedTabChange) { tabs in
let next = tabs[0]!
let previous = tabs[1]!
XCTAssertEqual(deleted, previous)
XCTAssertEqual(next, newSelected)
}
delegate.expect([didRemove, didSelect])
manager.removeTabAndUpdateSelectedIndex(manager.tabs.first!)
delegate.verify("Not all delegate methods were called")
}
func testUndoCloseTabsRemovesAutomaticallyCreatedNonPrivateTab() {
let tab = manager.addTab()
let tabToSave = Tab(bvc: BrowserViewController.foregroundBVC(), configuration: WKWebViewConfiguration())
tabToSave.sessionData = SessionData(currentPage: 0, urls: [URL(string: "url")!], lastUsedTime: Date.now())
guard let savedTab = SavedTab(tab: tabToSave, isSelected: true) else {
XCTFail("Failed to serialize tab")
return
}
manager.recentlyClosedForUndo = [savedTab]
manager.undoCloseTabs()
XCTAssertNotEqual(manager.tabs.first, tab)
}
}