Commit cff027da authored by Mladen Jakovljevic's avatar Mladen Jakovljevic

Mocking UserDefaults keys using launchArguments.

parent 2d867850
......@@ -14,38 +14,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if AppDelegate.isUITestingEnabled {
setBoolDefaults(environmentKey: "keepScreenOnEnvKey", defaultsKey: ViewController.keepScreenOnKey)
setBoolDefaults(environmentKey: "autoRefreshEnvKey", defaultsKey: ViewController.autoRefreshKey)
}
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 setStringDefaults(environmentKey: String, defaultsKey: String) {
let environment = ProcessInfo.processInfo.environment
if let stringArgument = environment[environmentKey] {
UserDefaults.standard.set(stringArgument, forKey: defaultsKey)
}
}
}
......@@ -12,26 +12,27 @@ class StateUITests: XCTestCase {
let app = XCUIApplication()
static let keepScreenOnEnvKey = "keepScreenOnEnvKey"
static let autoRefreshEnvKey = "autoRefreshEnvKey"
// MARK: Copy/pasted ViewController.keepScreenOnKey and ViewController.autoRefreshKey
// Note minus (-) sign before the key.
static let keepScreenOnUserDefaultsKey = "-keepScreenOn"
static let autoRefreshUserDefaultsKey = "-autoRefresh"
override func setUp() {
super.setUp()
continueAfterFailure = false
app.launchArguments += ["UI-Testing"]
}
func testTurningOnKeepScreenOnSwitch() {
app.launchEnvironment[StateUITests.keepScreenOnEnvKey] = "NO"
app.launchArguments += [StateUITests.keepScreenOnUserDefaultsKey, "NO"]
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)
}
func testCaptionTextForTurnedOnKeepScreenOnSwitch() {
app.launchEnvironment[StateUITests.keepScreenOnEnvKey] = "YES"
app.launchArguments += [StateUITests.keepScreenOnUserDefaultsKey, "YES"]
app.launch()
XCTAssertTrue(app.staticTexts["Screen stays turned on"].exists,
"Caption text for Keep Screen On switch is not correct on app startup.")
......@@ -39,7 +40,7 @@ class StateUITests: XCTestCase {
}
func testTurningOnAutoRefreshSwitch() {
app.launchEnvironment[StateUITests.autoRefreshEnvKey] = "NO"
app.launchArguments += [StateUITests.autoRefreshUserDefaultsKey, "NO"]
app.launch()
app.switches["autoRefreshSwitch"].tap()
XCTAssertTrue(app.staticTexts["Refreshing on start activated"].exists,
......@@ -48,7 +49,7 @@ class StateUITests: XCTestCase {
}
func testCaptionTextForTurnedOnAutoRefreshSwitch() {
app.launchEnvironment[StateUITests.autoRefreshEnvKey] = "YES"
app.launchArguments += [StateUITests.autoRefreshUserDefaultsKey, "YES"]
app.launch()
XCTAssertTrue(app.staticTexts["Refreshing on start activated"].exists,
"Caption text for Auto Refresh switch is not correct on app startup.")
......@@ -56,8 +57,8 @@ class StateUITests: XCTestCase {
}
func testContinueButtonEnabled() {
app.launchEnvironment[StateUITests.keepScreenOnEnvKey] = "NO"
app.launchEnvironment[StateUITests.autoRefreshEnvKey] = "NO"
app.launchArguments += [StateUITests.keepScreenOnUserDefaultsKey, "NO"]
app.launchArguments += [StateUITests.autoRefreshUserDefaultsKey, "NO"]
app.launch()
app.switches["autoRefreshSwitch"].tap()
app.switches["keepScreenOnSwitch"].tap()
......
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