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
@testable import Storage
8
import Sync
9
import UIKit
10
11
import XCTest
12
13
class MockBrowserProfile: BrowserProfile {
14
var peekSyncManager: BrowserSyncManager {
15
return self.syncManager as! BrowserSyncManager
16
}
17
18
var peekTabs: SQLiteRemoteClientsAndTabs {
19
return self.remoteClientsAndTabs as! SQLiteRemoteClientsAndTabs
20
}
21
}
22
23
class MockEngineStateChanges: EngineStateChanges {
24
var collections: [String] = []
25
var enabled: [String] = []
26
var disabled: [String] = []
27
var clearWasCalled: Bool = false
28
29
func collectionsThatNeedLocalReset() -> [String] {
30
return self.collections
31
}
32
33
func enginesEnabled() -> [String] {
34
return self.enabled
35
}
36
37
func enginesDisabled() -> [String] {
38
return self.disabled
39
}
40
41
func clearLocalCommands() {
42
clearWasCalled = true
43
}
44
}
45
46
func assertClientsHaveGUIDsFromStorage(_ storage: RemoteClientsAndTabs, expected: [GUID]) {
47
let recs = storage.getClients().value.successValue
48
XCTAssertNotNil(recs)
49
XCTAssertEqual(expected, recs!.map { $0.guid! })
50
}
51
52
class ResetTests: XCTestCase {
53
func testResetting() {
54
let profile = MockBrowserProfile(localName: "testResetTests")
55
56
// Add a client.
57
let tabs = profile.peekTabs
58
XCTAssertTrue(tabs.insertOrUpdateClient(RemoteClient(guid: "abcdefghijkl", name: "Remote", modified: Date.now(), type: "mobile", formfactor: "tablet", os: "Windows", version: "55.0.1a", fxaDeviceId: "fxa1")).value.isSuccess)
59
_ = tabs.replaceRemoteDevices([RemoteDevice(id: "fxa1", name: "Device 1", type: "desktop", isCurrentDevice: false, lastAccessTime: 123, availableCommands: [:])]).succeeded()
60
61
// Verify that it's there.
62
assertClientsHaveGUIDsFromStorage(tabs, expected: ["abcdefghijkl"])
63
64
// Tell the sync manager that "clients" has changed syncID.
65
let e = MockEngineStateChanges()
66
e.collections.append("clients")
67
68
XCTAssertTrue(profile.peekSyncManager.takeActionsOnEngineStateChanges(e).value.isSuccess)
69
70
// We threw away the command.
71
XCTAssertTrue(e.clearWasCalled)
72
73
// And now we have no local clients.
74
let empty = tabs.getClients().value.successValue
75
XCTAssertNotNil(empty)
76
XCTAssertEqual(empty!, [])
77
}
78
}