Commit 476f5358 authored by Mladen Jakovljevic's avatar Mladen Jakovljevic

Scalable solution for solution #3.

parent 2d867850
......@@ -13,39 +13,47 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static let uiTestingKeyPrefix = "UI-TestingKey_"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if AppDelegate.isUITestingEnabled {
setBoolDefaults(environmentKey: "keepScreenOnEnvKey", defaultsKey: ViewController.keepScreenOnKey)
setBoolDefaults(environmentKey: "autoRefreshEnvKey", defaultsKey: ViewController.autoRefreshKey)
setUserDefaults()
}
return true
}
static var isUITestingEnabled: Bool {
get {
return ProcessInfo.processInfo.arguments.contains("UI-Testing")
}
}
private func setBoolDefaults(environmentKey: String, defaultsKey: String) {
let environment = ProcessInfo.processInfo.environment
if let boolArgument = environment[environmentKey] {
if boolArgument == "YES" {
UserDefaults.standard.set(true, forKey: defaultsKey)
} else if boolArgument == "NO" {
UserDefaults.standard.set(false, forKey: defaultsKey)
private func setUserDefaults() {
for (key, value)
in ProcessInfo.processInfo.environment
where key.hasPrefix(AppDelegate.uiTestingKeyPrefix) {
// Truncate "UI-TestingKey_" part
let userDefaultsKey = key.truncateUITestingKey()
switch value {
case "YES":
UserDefaults.standard.set(true, forKey: userDefaultsKey)
case "NO":
UserDefaults.standard.set(false, forKey: userDefaultsKey)
default:
UserDefaults.standard.set(value, forKey: userDefaultsKey)
}
}
}
private func setStringDefaults(environmentKey: String, defaultsKey: String) {
let environment = ProcessInfo.processInfo.environment
if let stringArgument = environment[environmentKey] {
UserDefaults.standard.set(stringArgument, forKey: defaultsKey)
}
extension String {
func truncateUITestingKey() -> String {
if let range = self.range(of: AppDelegate.uiTestingKeyPrefix) {
let userDefaultsKey = self[range.upperBound...]
return String(userDefaultsKey)
}
return self
}
}
}
\ No newline at end of file
......@@ -12,8 +12,8 @@ class StateUITests: XCTestCase {
let app = XCUIApplication()
static let keepScreenOnEnvKey = "keepScreenOnEnvKey"
static let autoRefreshEnvKey = "autoRefreshEnvKey"
static let keepScreenOnEnvKey = "UI-TestingKey_keepScreenOn"
static let autoRefreshEnvKey = "UI-TestingKey_autoRefresh"
override func setUp() {
super.setUp()
......@@ -26,7 +26,7 @@ class StateUITests: XCTestCase {
app.launch()
app.switches["keepScreenOnSwitch"].tap()
XCTAssertTrue(app.staticTexts["Screen stays turned on"].exists,
"Error: Turnixng switch on doesn't change caption text.")
"Error: Turning switch on doesn't change caption text.")
sleep(1)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment