-
Notifications
You must be signed in to change notification settings - Fork 3k
/
TestAppDelegate.swift
181 lines (146 loc) · 8 KB
/
TestAppDelegate.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
/* 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/. */
import Foundation
import Shared
import SDWebImage
import XCGLogger
private let log = Logger.browserLogger
class TestAppDelegate: AppDelegate {
lazy var dirForTestProfile = { return "\(self.appRootDir())/profile.testProfile" }()
override func getProfile(_ application: UIApplication) -> Profile {
if let profile = self.profile {
return profile
}
var profile: BrowserProfile
let launchArguments = ProcessInfo.processInfo.arguments
launchArguments.forEach { arg in
if arg.starts(with: LaunchArguments.ServerPort) {
let portString = arg.replacingOccurrences(of: LaunchArguments.ServerPort, with: "")
if let port = Int(portString) {
AppInfo.webserverPort = port
} else {
fatalError("Failed to set web server port override.")
}
}
if arg.starts(with: LaunchArguments.LoadDatabasePrefix) {
if launchArguments.contains(LaunchArguments.ClearProfile) {
fatalError("Clearing profile and loading a test database is not a supported combination.")
}
// Grab the name of file in the bundle's test-fixtures dir, and copy it to the runtime app dir.
let filename = arg.replacingOccurrences(of: LaunchArguments.LoadDatabasePrefix, with: "")
let input = URL(fileURLWithPath: Bundle(for: TestAppDelegate.self).path(forResource: filename, ofType: nil, inDirectory: "test-fixtures")!)
try? FileManager.default.createDirectory(atPath: dirForTestProfile, withIntermediateDirectories: false, attributes: nil)
let output = URL(fileURLWithPath: "\(dirForTestProfile)/browser.db")
let enumerator = FileManager.default.enumerator(atPath: dirForTestProfile)
let filePaths = enumerator?.allObjects as! [String]
filePaths.filter { $0.contains(".db") }.forEach { item in
try! FileManager.default.removeItem(at: URL(fileURLWithPath: "\(dirForTestProfile)/\(item)"))
}
try! FileManager.default.copyItem(at: input, to: output)
}
if arg.starts(with: LaunchArguments.LoadTabsStateArchive) {
if launchArguments.contains(LaunchArguments.ClearProfile) {
fatalError("Clearing profile and loading a TabsState.Archive is not a supported combination.")
}
// Grab the name of file in the bundle's test-fixtures dir, and copy it to the runtime app dir.
let filenameArchive = arg.replacingOccurrences(of: LaunchArguments.LoadTabsStateArchive, with: "")
let input = URL(fileURLWithPath: Bundle(for: TestAppDelegate.self).path(forResource: filenameArchive, ofType: nil, inDirectory: "test-fixtures")!)
try? FileManager.default.createDirectory(atPath: dirForTestProfile, withIntermediateDirectories: false, attributes: nil)
let output = URL(fileURLWithPath: "\(dirForTestProfile)/tabsState.archive")
let enumerator = FileManager.default.enumerator(atPath: dirForTestProfile)
let filePaths = enumerator?.allObjects as! [String]
filePaths.filter { $0.contains(".archive") }.forEach { item in
try! FileManager.default.removeItem(at: URL(fileURLWithPath: "\(dirForTestProfile)/\(item)"))
}
try! FileManager.default.copyItem(at: input, to: output)
}
}
if launchArguments.contains(LaunchArguments.ClearProfile) {
// Use a clean profile for each test session.
log.debug("Deleting all files in 'Documents' directory to clear the profile")
profile = BrowserProfile(localName: "testProfile", syncDelegate: application.syncDelegate, clear: true)
} else {
profile = BrowserProfile(localName: "testProfile", syncDelegate: application.syncDelegate)
}
if launchArguments.contains(LaunchArguments.SkipAddingGoogleTopSite) {
profile.prefs.setBool(true, forKey: PrefsKeys.GoogleTopSiteHideKey)
}
// Don't show the ETP Coversheet New page.
if launchArguments.contains(LaunchArguments.SkipETPCoverSheet) {
profile.prefs.setString(ETPCoverSheetShowType.DoNotShow.rawValue, forKey: PrefsKeys.KeyETPCoverSheetShowType)
}
// Don't show the What's New page.
if launchArguments.contains(LaunchArguments.SkipWhatsNew) {
profile.prefs.setInt(1, forKey: PrefsKeys.KeyLastVersionNumber)
}
if launchArguments.contains(LaunchArguments.SkipDefaultBrowserOnboarding) {
profile.prefs.setBool(true, forKey: PrefsKeys.KeyDidShowDefaultBrowserOnboarding)
}
// Skip the intro when requested by for example tests or automation
if launchArguments.contains(LaunchArguments.SkipIntro) {
profile.prefs.setInt(1, forKey: PrefsKeys.IntroSeen)
}
// Change to 0 to deactivate chron tabs
if launchArguments.contains(LaunchArguments.ChronTabs) {
profile.prefs.setInt(1, forKey: PrefsKeys.ChronTabsPrefKey)
}
if launchArguments.contains(LaunchArguments.StageServer) {
profile.prefs.setInt(1, forKey: PrefsKeys.UseStageServer)
}
self.profile = profile
return profile
}
override func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// If the app is running from a XCUITest reset all settings in the app
if ProcessInfo.processInfo.arguments.contains(LaunchArguments.ClearProfile) {
resetApplication()
}
Tab.ChangeUserAgent.clear()
return super.application(application, willFinishLaunchingWithOptions: launchOptions)
}
/**
Use this to reset the application between tests.
**/
func resetApplication() {
log.debug("Wiping everything for a clean start.")
// Clear image cache
SDImageCache.shared.clearDisk()
SDImageCache.shared.clearMemory()
// Clear the cookie/url cache
URLCache.shared.removeAllCachedResponses()
let storage = HTTPCookieStorage.shared
if let cookies = storage.cookies {
for cookie in cookies {
storage.deleteCookie(cookie)
}
}
// Clear the documents directory
let rootPath = appRootDir()
let manager = FileManager.default
let documents = URL(fileURLWithPath: rootPath)
let docContents = try! manager.contentsOfDirectory(atPath: rootPath)
for content in docContents {
do {
try manager.removeItem(at: documents.appendingPathComponent(content))
} catch {
log.debug("Couldn't delete some document contents.")
}
}
}
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Speed up the animations to 100 times as fast.
defer { application.keyWindow?.layer.speed = 100.0 }
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func appRootDir() -> String {
var rootPath = ""
let sharedContainerIdentifier = AppInfo.sharedContainerIdentifier
if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: sharedContainerIdentifier) {
rootPath = url.path
} else {
rootPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
}
return rootPath
}
}