/
AutoMateLaunchEnvironments.swift
235 lines (206 loc) · 10.6 KB
/
AutoMateLaunchEnvironments.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// AutoMateLaunchEnvironments.swift
// AutoMate
//
// Created by Joanna Bednarz on 26/01/2017.
// Copyright © 2017 PGS Software. All rights reserved.
//
import Foundation
// MARK: - AutoMateLaunchEnvironment
/// Protocol adapted by all launch enviroment options predefined in `AutoMate`.
/// It also assures that default handling is provided by [AutoMate - AppBuddy](https://github.com/PGSSoft/AutoMate-AppBuddy).
public protocol AutoMateLaunchEnvironment {
// MARK: Properties
/// Readable unique key that is passed with launch enviroment.
static var key: AutoMateKey { get }
}
/// Default implementations for `AutoMateLaunchEnvironment` that can handle multiple resource values.
public extension AutoMateLaunchEnvironment where Self: LaunchEnvironmentWithMultipleValues, Self.Value == LaunchEnvironmentResourceValue {
// MARK: Properties
/// Implementation overriding `uniqueIdentifier` property from `LaunchOption` protocol to use `rawValue` of `AutoMateKey`.
var uniqueIdentifier: String {
return Self.key.rawValue
}
// MARK: Initialization
/// Intializes `AutoMateLaunchEnvironment` with tuples describing `LaunchEnvironmentResourceValue`.
///
/// - Parameter resources: `(String, String?)` tuples describing `LaunchEnvironmentResourceValue`
init(resources: (fileName: String, bundleName: String?)...) {
self.init(valuesCollection: resources.map(Value.init))
}
}
/// Default implementations for `AutoMateLaunchEnvironment` that handle single value.
public extension AutoMateLaunchEnvironment where Self: LaunchEnvironmentWithSingleValue {
// MARK: Properties
/// Implementation of `key` property from `LaunchEnvironmentWithSingleValue` protocol to use `rawValue` of `AutoMateKey`.
var key: String {
return Self.key.rawValue
}
}
// MARK: - Event Launch Environment
/// Launch environment supporting `EventKit` events.
/// Expects bundle and file name for every file containing data of events to be added into calendar at test launch.
/// Structure is defined in example project's file _events.json_.
///
/// **Example:**
///
/// ```swift
/// let recurringEvents: EventLaunchEnvironment = [ LaunchEnvironmentResourceValue(fileName: "monthly_events", bundleName: "Data") ]
/// let nearEvents = EventLaunchEnvironment(resources: (fileName: "todays_events", bundleName: "Test data"), (fileName: "this_week_events", bundleName: nil))
/// let nearEvents = EventLaunchEnvironment(shouldCleanBefore: true, resources: (fileName: "todays_events", bundleName: "Test data"), (fileName: "this_week_events", bundleName: nil))
/// ```
///
/// - warning:
/// Setting `shouldCleanBefore` to `true` will remove all events from a device.
public struct EventLaunchEnvironment: CleanableLaunchEnvironmentWithMultipleValues, AutoMateLaunchEnvironment {
// MARK: Typealiases
/// Defines associated type from `LaunchEnvironmentProtocol` to be `LaunchEnvironmentResourceValue`.
public typealias Value = LaunchEnvironmentResourceValue
// MARK: Properties
/// Defines `LaunchEnvironmentResourceValue` as `AutoMateKey.events`.
public static let key: AutoMateKey = .events
/// Array to store all resource values from which launch enviroment value is composed.
public let valuesCollection: [Value]
/// Flag that indicates if all `EKEvent`s should be removed before saving new ones.
public let shouldCleanBefore: Bool
// MARK: Initialization
/// Initializes `EventLaunchEnvironment` that can be passed to `TestLauncher`.
/// If handler is added to `LaunchEnvironmentManager` default handling is provided
/// by [AutoMate - AppBuddy](https://github.com/PGSSoft/AutoMate-AppBuddy).
///
/// - Parameters:
/// - shouldCleanBefore: `Bool` flag indicating if `EKEvent`s should be removed before saving new ones.
/// - valuesCollection: `Array` of all resource values to be passed.
public init(shouldCleanBefore: Bool, valuesCollection: [Value]) {
self.valuesCollection = valuesCollection
self.shouldCleanBefore = shouldCleanBefore
}
}
// MARK: - Reminder Launch Environment
/// Launch environment supporting `EventKit` reminders.
/// Expects bundle and file name for every file containing data of reminders to be added into calendar at test launch.
/// Structure is defined in example project's file _reminders.json_.
///
/// **Example:**
///
/// ```swift
/// let recurringReminders: ReminderLaunchEnvironment = [ LaunchEnvironmentResourceValue(fileName: "johnys_birthday_reminder", bundleName: "Data") ]
/// let highPriorityReminders = ReminderLaunchEnvironment(resources: (fileName: "automate_release_reminders", bundleName: "Test data"), (fileName: "wwdc_reminders", bundleName: nil))
/// let highPriorityReminders = ReminderLaunchEnvironment(shouldCleanBefore: true, resources: (fileName: "automate_release_reminders", bundleName: "Test data"), (fileName: "wwdc_reminders", bundleName: nil))
/// ```
///
/// - warning:
/// Setting `shouldCleanBefore` to `true` will remove all reminders from a device.
public struct ReminderLaunchEnvironment: CleanableLaunchEnvironmentWithMultipleValues, AutoMateLaunchEnvironment {
// MARK: Typealiases
/// Defines associated type from `LaunchEnvironmentProtocol` to be `LaunchEnvironmentResourceValue`.
public typealias Value = LaunchEnvironmentResourceValue
// MARK: Properties
/// Defines `LaunchEnvironmentResourceValue` as `AutoMateKey.reminders`.
public static let key: AutoMateKey = .reminders
/// Array to store all resource values from which launch enviroment value is composed.
public let valuesCollection: [Value]
/// Flag that indicates if all `EKReminder`s should be removed before saving new ones.
public let shouldCleanBefore: Bool
// MARK: Initialization
/// Initializes `EventLaunchEnvironment` that can be passed to `TestLauncher`.
/// If handler is added to `LaunchEnvironmentManager` default handling is provided
/// by [AutoMate - AppBuddy](https://github.com/PGSSoft/AutoMate-AppBuddy).
///
/// - Parameters:
/// - shouldCleanBefore: `Bool` flag indicating if `EKReminder`s should be removed before saving new ones.
/// - valuesCollection: `Array` of all resource values to be passed.
public init(shouldCleanBefore: Bool, valuesCollection: [Value]) {
self.valuesCollection = valuesCollection
self.shouldCleanBefore = shouldCleanBefore
}
}
// MARK: - Contacts Launch Environment
/// Launch environment supporting `Contacts`.
/// Expects bundle and file name for every file containing data of contacts to be added to address book at test launch.
/// Structure is defined in example project's file _contacts.json_.
///
/// **Example:**
///
/// ```swift
/// let johnContacts: ContactLaunchEnvironment = [ LaunchEnvironmentResourceValue(fileName: "john", bundleName: "Data") ]
/// let severalContacts = ContactLaunchEnvironment(resources: (fileName: "michael", bundleName: "Test data"), (fileName: "emma", bundleName: nil))
/// let severalContacts = ContactLaunchEnvironment(shouldCleanBefore: true, resources: (fileName: "michael", bundleName: "Test data"), (fileName: "emma", bundleName: nil))
/// ```
///
/// - warning:
/// Setting `shouldCleanBefore` to `true` will remove all contacts from a device.
public struct ContactLaunchEnvironment: CleanableLaunchEnvironmentWithMultipleValues, AutoMateLaunchEnvironment {
// MARK: Typealiases
/// Defines associated type from `LaunchEnvironmentProtocol` to be `LaunchEnvironmentResourceValue`.
public typealias Value = LaunchEnvironmentResourceValue
// MARK: Properties
/// Defines `LaunchEnvironmentResourceValue` as `AutoMateKey.contacts`.
public static let key: AutoMateKey = .contacts
/// Array to store all resource values from which launch enviroment value is composed.
public let valuesCollection: [LaunchEnvironmentResourceValue]
/// Flag that indicates if all `CNContact`s should be removed before saving new ones.
public let shouldCleanBefore: Bool
// MARK: Initialization
/// Initializes `EventLaunchEnvironment` that can be passed to `TestLauncher`.
/// If handler is added to `LaunchEnvironmentManager` default handling is provided
/// by [AutoMate - AppBuddy](https://github.com/PGSSoft/AutoMate-AppBuddy).
///
/// - Parameters:
/// - shouldCleanBefore: `Bool` flag indicating if `CNContact`s should be removed before saving new ones.
/// - valuesCollection: `Array` of all resource values to be passed.
public init(shouldCleanBefore: Bool, valuesCollection: [Value]) {
self.valuesCollection = valuesCollection
self.shouldCleanBefore = shouldCleanBefore
}
}
// MARK: - Turn off animation launch environment
/// Launch environment disabling `UIKit` animation.
///
/// **Example:**
///
/// ```swift
/// let disableAnimation = AnimationLaunchEnvironment()
/// ```
public struct AnimationLaunchEnvironment: LaunchEnvironmentWithSingleValue, AutoMateLaunchEnvironment {
// MARK: Typealiases
/// Defines associated type from `LaunchEnvironmentProtocol` to be `BooleanLaunchEnvironmentValue`.
public typealias Value = BooleanLaunchEnvironmentValue
// MARK: Properties
/// Defines `LaunchEnvironmentResourceValue` as `AutoMateKey.animation`.
public static let key: AutoMateKey = .animation
/// Value from which is used as launch enviroment value.
public var value: Value
// MARK: Initialization
/// Initialize launch option. By default disable animation (`false`).
///
/// - Parameter animation: `false` (default) if the animation should be disabled.
public init(animation: Value = false) {
value = animation
}
}
// MARK: - In in UI test launch environment
/// Launch environment informing application that is running in UI test.
///
/// **Example:**
///
/// ```swift
/// let isInUITest = IsInUITestLaunchEnvironment()
/// ```
public struct IsInUITestLaunchEnvironment: LaunchEnvironmentWithSingleValue, AutoMateLaunchEnvironment {
// MARK: Typealiases
/// Defines associated type from `LaunchEnvironmentProtocol` to be `BooleanLaunchEnvironmentValue`.
public typealias Value = BooleanLaunchEnvironmentValue
// MARK: Properties
/// Defines `LaunchEnvironmentResourceValue` as `AutoMateKey.animation`.
public static let key: AutoMateKey = .isInUITest
/// Value from which is used as launch enviroment value.
public var value: Value
// MARK: Initialization
/// Initialize launch option. By default inform application that it is running in UI test (`true`).
///
/// - Parameter running: `true` (default) if the application should know if it is running in UI test.
public init(inUITest: Value = true) {
value = inUITest
}
}