Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
@testable import Client
6
import Shared
7
import Storage
8
import UIKit
9
import WebKit
10
11
import XCTest
12
13
class TabManagerStoreTests: XCTestCase {
14
let profile = TabManagerMockProfile()
15
var manager: TabManager!
16
let configuration = WKWebViewConfiguration()
17
18
override func setUp() {
19
super.setUp()
20
21
manager = TabManager(profile: profile, imageStore: nil)
22
configuration.processPool = WKProcessPool()
23
24
if UIDevice.current.userInterfaceIdiom == .pad {
25
// BVC.viewWillAppear() calls restoreTabs() which interferes with these tests. (On iPhone, ClientTests never dismiss the intro screen, on iPad the intro is a popover on the BVC).
26
// Wait for this to happen (UIView.window only gets assigned after viewWillAppear()), then begin testing.
27
let bvc = (UIApplication.shared.delegate as! AppDelegate).browserViewController
28
let predicate = XCTNSPredicateExpectation(predicate: NSPredicate(format: "view.window != nil"), object: bvc)
29
wait(for: [predicate], timeout: 20)
30
}
31
32
manager.testClearArchive()
33
}
34
35
override func tearDown() {
36
super.tearDown()
37
}
38
39
// Without session data, a Tab can't become a SavedTab and get archived
40
func addTabWithSessionData(isPrivate: Bool = false) {
41
let tab = Tab(bvc: BrowserViewController.foregroundBVC(), configuration: configuration, isPrivate: isPrivate)
42
tab.url = URL(string: "http://yahoo.com")!
43
manager.configureTab(tab, request: URLRequest(url: tab.url!), flushToDisk: false, zombie: false)
44
tab.sessionData = SessionData(currentPage: 0, urls: [tab.url!], lastUsedTime: Date.now())
45
}
46
47
func testNoData() {
48
XCTAssertEqual(manager.testTabCountOnDisk(), 0, "Expected 0 tabs on disk")
49
XCTAssertEqual(manager.testCountRestoredTabs(), 0)
50
}
51
52
func testPrivateTabsAreArchived() {
53
for _ in 0..<2 {
54
addTabWithSessionData(isPrivate: true)
55
}
56
let e = expectation(description: "saved")
57
manager.storeChanges().uponQueue(.main) {_ in
58
XCTAssertEqual(self.manager.testTabCountOnDisk(), 2)
59
e.fulfill()
60
}
61
waitForExpectations(timeout: 2, handler: nil)
62
}
63
64
func testAddedTabsAreStored() {
65
// Add 2 tabs
66
for _ in 0..<2 {
67
addTabWithSessionData()
68
}
69
70
var e = expectation(description: "saved")
71
manager.storeChanges().uponQueue(.main) { _ in
72
XCTAssertEqual(self.manager.testTabCountOnDisk(), 2)
73
e.fulfill()
74
}
75
waitForExpectations(timeout: 2, handler: nil)
76
77
// Add 2 more
78
for _ in 0..<2 {
79
addTabWithSessionData()
80
}
81
82
e = expectation(description: "saved")
83
manager.storeChanges().uponQueue(.main) { _ in
84
XCTAssertEqual(self.manager.testTabCountOnDisk(), 4)
85
e.fulfill()
86
}
87
waitForExpectations(timeout: 2, handler: nil)
88
89
// Remove all tabs, and add just 1 tab
90
manager.removeAll()
91
addTabWithSessionData()
92
93
e = expectation(description: "saved")
94
manager.storeChanges().uponQueue(.main) {_ in
95
XCTAssertEqual(self.manager.testTabCountOnDisk(), 1)
96
e.fulfill()
97
}
98
waitForExpectations(timeout: 2, handler: nil)
99
}
100
}
101