forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupportManagerTests.swift
More file actions
125 lines (102 loc) · 4.44 KB
/
Copy pathSupportManagerTests.swift
File metadata and controls
125 lines (102 loc) · 4.44 KB
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
//
// SupportManagerTests.swift
// LoopTests
//
// Created by Rick Pasetto on 9/10/21.
// Copyright © 2021 LoopKit Authors. All rights reserved.
//
import XCTest
import LoopKit
import LoopKitUI
import SwiftUI
@testable import Loop
class SupportManagerTests: XCTestCase {
enum MockError: Error { case nothing }
class Mixin {
@ViewBuilder
func supportMenuItem(supportInfoProvider: SupportInfoProvider, urlHandler: @escaping (URL) -> Void) -> some View {}
func softwareUpdateView(bundleIdentifier: String, currentVersion: String, guidanceColors: GuidanceColors, openAppStore: (() -> Void)?) -> AnyView? {
nil
}
var mockResult: Result<VersionUpdate?, Error> = .success(.default)
func checkVersion(bundleIdentifier: String, currentVersion: String) async -> VersionUpdate? {
switch mockResult {
case .success(let update):
return update
case .failure:
return nil
}
}
weak var delegate: SupportUIDelegate?
}
class MockSupport: Mixin, SupportUI {
static var pluginIdentifier: String { "SupportManagerTestsMockSupport" }
override init() { super.init() }
required init?(rawState: RawStateValue) { super.init() }
var rawState: RawStateValue = [:]
func getScenarios(from scenarioURLs: [URL]) -> [LoopScenario] { [] }
func loopWillReset() {}
func loopDidReset() {}
func configurationMenuItems() -> [LoopKitUI.CustomMenuItem] { return [] }
}
class AnotherMockSupport: Mixin, SupportUI {
static var pluginIdentifier: String { "SupportManagerTestsAnotherMockSupport" }
override init() { super.init() }
required init?(rawState: RawStateValue) { super.init() }
var rawState: RawStateValue = [:]
func getScenarios(from scenarioURLs: [URL]) -> [LoopScenario] { [] }
func loopWillReset() {}
func loopDidReset() {}
func configurationMenuItems() -> [LoopKitUI.CustomMenuItem] { return [] }
}
class MockAlertIssuer: AlertIssuer {
func issueAlert(_ alert: LoopKit.Alert) {
}
func retractAlert(identifier: LoopKit.Alert.Identifier) {
}
}
class MockDeviceSupportDelegate: DeviceSupportDelegate {
var availableSupports: [LoopKitUI.SupportUI] = []
var pumpManagerStatus: LoopKit.PumpManagerStatus?
var cgmManagerStatus: LoopKit.CGMManagerStatus?
func generateDiagnosticReport(_ completion: @escaping (String) -> Void) {
completion("Mock Issue Report")
}
}
var supportManager: SupportManager!
var mockSupport: SupportManagerTests.MockSupport!
var mockAlertIssuer: MockAlertIssuer!
var pluginManager = PluginManager()
var mocKDeviceSupportDelegate = MockDeviceSupportDelegate()
override func setUp() {
mockAlertIssuer = MockAlertIssuer()
supportManager = SupportManager(pluginManager: pluginManager, deviceSupportDelegate: mocKDeviceSupportDelegate, staticSupportTypes: [], alertIssuer: mockAlertIssuer)
mockSupport = SupportManagerTests.MockSupport()
supportManager.addSupport(mockSupport)
}
func testVersionCheckOneService() async throws {
let result = await supportManager.checkVersion()
XCTAssertEqual(VersionUpdate.noUpdateNeeded, result)
mockSupport.mockResult = .success(.required)
let result2 = await supportManager.checkVersion()
XCTAssertEqual(.required, result2)
}
func testVersionCheckOneServiceError() async throws {
// Error doesn't really do anything but log
mockSupport.mockResult = .failure(MockError.nothing)
let result = await supportManager.checkVersion()
XCTAssertEqual(VersionUpdate.noUpdateNeeded, result)
}
func testVersionCheckMultipleServices() async throws {
let anotherSupport = AnotherMockSupport()
supportManager.addSupport(anotherSupport)
let result = await supportManager.checkVersion()
XCTAssertEqual(VersionUpdate.noUpdateNeeded, result)
anotherSupport.mockResult = .success(.required)
let result2 = await supportManager.checkVersion()
XCTAssertEqual(.required, result2)
let result3 = await supportManager.checkVersion()
mockSupport.mockResult = .success(.recommended)
XCTAssertEqual(.required, result3)
}
}