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 Foundation
7
import Account
8
import Shared
9
import Storage
10
import Sync
11
import XCTest
12
13
open class MockSyncManager: SyncManager {
14
open var isSyncing = false
15
open var lastSyncFinishTime: Timestamp?
16
open var syncDisplayState: SyncDisplayState?
17
18
open func hasSyncedHistory() -> Deferred<Maybe<Bool>> {
19
return deferMaybe(true)
20
}
21
22
private func completedWithStats(collection: String) -> Deferred<Maybe<SyncStatus>> {
23
return deferMaybe(SyncStatus.completed(SyncEngineStatsSession(collection: collection)))
24
}
25
26
open func syncClients() -> SyncResult { return completedWithStats(collection: "mock_clients") }
27
open func syncClientsThenTabs() -> SyncResult { return completedWithStats(collection: "mock_clientsandtabs") }
28
open func syncHistory() -> SyncResult { return completedWithStats(collection: "mock_history") }
29
open func syncLogins() -> SyncResult { return completedWithStats(collection: "mock_logins") }
30
open func syncBookmarks() -> SyncResult { return completedWithStats(collection: "mock_bookmarks") }
31
open func syncEverything(why: SyncReason) -> Success {
32
return succeed()
33
}
34
open func syncNamedCollections(why: SyncReason, names: [String]) -> Success {
35
return succeed()
36
}
37
open func beginTimedSyncs() {}
38
open func endTimedSyncs() {}
39
open func applicationDidBecomeActive() {
40
self.beginTimedSyncs()
41
}
42
open func applicationDidEnterBackground() {
43
self.endTimedSyncs()
44
}
45
46
open func onNewProfile() {
47
}
48
49
open func onAddedAccount() -> Success {
50
return succeed()
51
}
52
open func onRemovedAccount(_ account: Account.FirefoxAccount?) -> Success {
53
return succeed()
54
}
55
56
open func hasSyncedLogins() -> Deferred<Maybe<Bool>> {
57
return deferMaybe(true)
58
}
59
}
60
61
open class MockTabQueue: TabQueue {
62
open func addToQueue(_ tab: ShareItem) -> Success {
63
return succeed()
64
}
65
66
open func getQueuedTabs() -> Deferred<Maybe<Cursor<ShareItem>>> {
67
return deferMaybe(ArrayCursor<ShareItem>(data: []))
68
}
69
70
open func clearQueuedTabs() -> Success {
71
return succeed()
72
}
73
}
74
75
open class MockPanelDataObservers: PanelDataObservers {
76
override init(profile: Client.Profile) {
77
super.init(profile: profile)
78
self.activityStream = MockActivityStreamDataObserver(profile: profile)
79
}
80
}
81
82
open class MockActivityStreamDataObserver: DataObserver {
83
public func refreshIfNeeded(forceTopSites topSites: Bool) {
84
}
85
86
public var profile: Client.Profile
87
public weak var delegate: DataObserverDelegate?
88
89
init(profile: Client.Profile) {
90
self.profile = profile
91
}
92
}
93
94
class MockFiles: FileAccessor {
95
init() {
96
let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
97
super.init(rootPath: (docPath as NSString).appendingPathComponent("testing"))
98
}
99
}
100
101
open class MockProfile: Client.Profile {
102
// Read/Writeable properties for mocking
103
public var recommendations: HistoryRecommendations
104
public var places: RustPlaces
105
public var files: FileAccessor
106
public var history: BrowserHistory & SyncableHistory & ResettableSyncStorage
107
public var logins: RustLogins
108
public var syncManager: SyncManager!
109
110
fileprivate var legacyPlaces: BrowserHistory & Favicons & SyncableHistory & ResettableSyncStorage & HistoryRecommendations
111
112
public lazy var panelDataObservers: PanelDataObservers = {
113
return MockPanelDataObservers(profile: self)
114
}()
115
116
var db: BrowserDB
117
var readingListDB: BrowserDB
118
119
fileprivate let name: String = "mockaccount"
120
121
init(databasePrefix: String = "mock") {
122
files = MockFiles()
123
syncManager = MockSyncManager()
124
let loginsDatabasePath = URL(fileURLWithPath: (try! files.getAndEnsureDirectory()), isDirectory: true).appendingPathComponent("\(databasePrefix)_logins.db").path
125
logins = RustLogins(databasePath: loginsDatabasePath, encryptionKey: "AAAAAAAA")
126
db = BrowserDB(filename: "\(databasePrefix).db", schema: BrowserSchema(), files: files)
127
readingListDB = BrowserDB(filename: "\(databasePrefix)_ReadingList.db", schema: ReadingListSchema(), files: files)
128
let placesDatabasePath = URL(fileURLWithPath: (try! files.getAndEnsureDirectory()), isDirectory: true).appendingPathComponent("\(databasePrefix)_places.db").path
129
places = RustPlaces(databasePath: placesDatabasePath)
130
legacyPlaces = SQLiteHistory(db: self.db, prefs: MockProfilePrefs())
131
recommendations = legacyPlaces
132
history = legacyPlaces
133
}
134
135
public func localName() -> String {
136
return name
137
}
138
139
public func _reopen() {
140
isShutdown = false
141
142
db.reopenIfClosed()
143
_ = logins.reopenIfClosed()
144
_ = places.reopenIfClosed()
145
}
146
147
public func _shutdown() {
148
isShutdown = true
149
150
db.forceClose()
151
_ = logins.forceClose()
152
_ = places.forceClose()
153
}
154
155
public var isShutdown: Bool = false
156
157
public var favicons: Favicons {
158
return self.legacyPlaces
159
}
160
161
lazy public var queue: TabQueue = {
162
return MockTabQueue()
163
}()
164
165
lazy public var metadata: Metadata = {
166
return SQLiteMetadata(db: self.db)
167
}()
168
169
lazy public var isChinaEdition: Bool = {
170
return Locale.current.identifier == "zh_CN"
171
}()
172
173
lazy public var certStore: CertStore = {
174
return CertStore()
175
}()
176
177
lazy public var searchEngines: SearchEngines = {
178
return SearchEngines(prefs: self.prefs, files: self.files)
179
}()
180
181
lazy public var prefs: Prefs = {
182
return MockProfilePrefs()
183
}()
184
185
lazy public var readingList: ReadingList = {
186
return SQLiteReadingList(db: self.readingListDB)
187
}()
188
189
lazy public var recentlyClosedTabs: ClosedTabsStore = {
190
return ClosedTabsStore(prefs: self.prefs)
191
}()
192
193
internal lazy var remoteClientsAndTabs: RemoteClientsAndTabs = {
194
return SQLiteRemoteClientsAndTabs(db: self.db)
195
}()
196
197
fileprivate lazy var syncCommands: SyncCommands = {
198
return SQLiteRemoteClientsAndTabs(db: self.db)
199
}()
200
201
public lazy var accountConfiguration: FirefoxAccountConfiguration = {
202
return ProductionFirefoxAccountConfiguration(prefs: self.prefs)
203
}()
204
var account: Account.FirefoxAccount?
205
206
public func hasAccount() -> Bool {
207
return account != nil
208
}
209
210
public func hasSyncableAccount() -> Bool {
211
return account?.actionNeeded == FxAActionNeeded.none
212
}
213
214
public func getAccount() -> Account.FirefoxAccount? {
215
return account
216
}
217
218
public func setAccount(_ account: Account.FirefoxAccount) {
219
self.account = account
220
self.syncManager.onAddedAccount()
221
}
222
223
public func flushAccount() {}
224
225
public func removeAccount() {
226
let old = self.account
227
self.account = nil
228
self.syncManager.onRemovedAccount(old)
229
}
230
231
public func getClients() -> Deferred<Maybe<[RemoteClient]>> {
232
return deferMaybe([])
233
}
234
235
public func getCachedClients() -> Deferred<Maybe<[RemoteClient]>> {
236
return deferMaybe([])
237
}
238
239
public func getClientsAndTabs() -> Deferred<Maybe<[ClientAndTabs]>> {
240
return deferMaybe([])
241
}
242
243
public func getCachedClientsAndTabs() -> Deferred<Maybe<[ClientAndTabs]>> {
244
return deferMaybe([])
245
}
246
247
public func cleanupHistoryIfNeeded() {}
248
249
public func storeTabs(_ tabs: [RemoteTab]) -> Deferred<Maybe<Int>> {
250
return deferMaybe(0)
251
}
252
253
public func sendItem(_ item: ShareItem, toDevices devices: [RemoteDevice]) -> Success {
254
return succeed()
255
}
256
}