2023-11-10 15:38:54 +00:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2023-11-10 15:38:54 +00:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2023-11-10 15:38:54 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
class AppLockUITests: XCTestCase {
|
|
|
|
var app: XCUIApplication!
|
|
|
|
|
|
|
|
enum Step {
|
|
|
|
static let placeholder = 0
|
|
|
|
static let lockScreen = 1
|
2023-11-10 17:21:02 +00:00
|
|
|
static let failedUnlock = 2
|
|
|
|
static let logoutAlert = 3
|
|
|
|
static let forcedLogout = 4
|
2023-11-10 15:38:54 +00:00
|
|
|
static let unlocked = 99
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFlowEnabled() async throws {
|
|
|
|
// Given an app with screen lock enabled.
|
|
|
|
let client = try UITestsSignalling.Client(mode: .tests)
|
|
|
|
app = Application.launch(.appLockFlow)
|
|
|
|
await client.waitForApp()
|
|
|
|
|
|
|
|
// Blank form representing an unlocked app.
|
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.unlocked)
|
|
|
|
|
|
|
|
// When backgrounding the app.
|
|
|
|
try client.send(.notification(name: UIApplication.didEnterBackgroundNotification))
|
|
|
|
|
|
|
|
// Then the placeholder screen should obscure the content.
|
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.placeholder)
|
|
|
|
|
|
|
|
// When foregrounding the app.
|
2023-12-12 16:08:27 +02:00
|
|
|
try client.send(.notification(name: UIApplication.willEnterForegroundNotification))
|
|
|
|
try? await Task.sleep(for: .milliseconds(100))
|
2023-11-15 15:31:35 +00:00
|
|
|
try client.send(.notification(name: UIApplication.didBecomeActiveNotification))
|
2023-11-10 15:38:54 +00:00
|
|
|
|
|
|
|
// Then the Lock Screen should be shown to enter a PIN.
|
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.lockScreen)
|
|
|
|
|
|
|
|
// When entering a PIN
|
|
|
|
enterPIN()
|
|
|
|
|
|
|
|
// Then the app should be unlocked again.
|
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.unlocked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFlowDisabled() async throws {
|
|
|
|
// Given an app with screen lock enabled.
|
|
|
|
let client = try UITestsSignalling.Client(mode: .tests)
|
|
|
|
app = Application.launch(.appLockFlowDisabled)
|
|
|
|
await client.waitForApp()
|
|
|
|
|
|
|
|
// Blank form representing an unlocked app.
|
2025-01-08 10:55:26 +02:00
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.unlocked, delay: .seconds(0.5))
|
2023-11-10 15:38:54 +00:00
|
|
|
|
|
|
|
// When backgrounding the app.
|
|
|
|
try client.send(.notification(name: UIApplication.didEnterBackgroundNotification))
|
|
|
|
|
|
|
|
// Then the app should remain unlocked.
|
2025-01-08 10:55:26 +02:00
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.unlocked, delay: .seconds(0.5))
|
2023-11-10 15:38:54 +00:00
|
|
|
|
|
|
|
// When foregrounding the app.
|
2023-12-12 16:08:27 +02:00
|
|
|
try client.send(.notification(name: UIApplication.willEnterForegroundNotification))
|
|
|
|
try? await Task.sleep(for: .milliseconds(100))
|
2023-11-15 15:31:35 +00:00
|
|
|
try client.send(.notification(name: UIApplication.didBecomeActiveNotification))
|
2023-11-10 15:38:54 +00:00
|
|
|
|
|
|
|
// Then the app should still remain unlocked.
|
2025-01-08 10:55:26 +02:00
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.unlocked, delay: .seconds(0.5))
|
2023-11-10 15:38:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 17:21:02 +00:00
|
|
|
func testWrongPIN() async throws {
|
|
|
|
// Given an app with screen lock enabled that is ready to unlock.
|
|
|
|
let client = try UITestsSignalling.Client(mode: .tests)
|
|
|
|
app = Application.launch(.appLockFlow)
|
|
|
|
await client.waitForApp()
|
|
|
|
|
2025-01-08 10:55:26 +02:00
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.unlocked, delay: .seconds(0.5))
|
2023-11-10 17:21:02 +00:00
|
|
|
try client.send(.notification(name: UIApplication.didEnterBackgroundNotification))
|
2023-11-17 17:07:27 +00:00
|
|
|
try await Task.sleep(for: .milliseconds(500)) // Don't overwrite the previous signal immediately.
|
2023-12-12 16:08:27 +02:00
|
|
|
|
|
|
|
try client.send(.notification(name: UIApplication.willEnterForegroundNotification))
|
|
|
|
try? await Task.sleep(for: .milliseconds(100))
|
2023-11-15 15:31:35 +00:00
|
|
|
try client.send(.notification(name: UIApplication.didBecomeActiveNotification))
|
2023-12-12 16:08:27 +02:00
|
|
|
|
2025-01-08 10:55:26 +02:00
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.lockScreen, delay: .seconds(0.5))
|
2023-11-10 17:21:02 +00:00
|
|
|
|
|
|
|
// When entering an incorrect PIN
|
|
|
|
enterWrongPIN()
|
|
|
|
|
|
|
|
// Then the app should remain locked with a warning.
|
2025-01-08 10:55:26 +02:00
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.failedUnlock, delay: .seconds(0.5))
|
2023-11-10 17:21:02 +00:00
|
|
|
|
|
|
|
// When entering it incorrectly twice more.
|
|
|
|
enterWrongPIN()
|
|
|
|
enterWrongPIN()
|
|
|
|
|
|
|
|
// Then then the app should sign the user out.
|
2025-01-08 10:55:26 +02:00
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.logoutAlert, delay: .seconds(0.5))
|
2023-11-10 17:21:02 +00:00
|
|
|
app.alerts.element.buttons[A11yIdentifiers.alertInfo.primaryButton].tap()
|
2025-01-08 10:55:26 +02:00
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.forcedLogout, delay: .seconds(0.5))
|
2023-11-10 17:21:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 15:31:35 +00:00
|
|
|
func testResignActive() async throws {
|
|
|
|
// Given an app with screen lock enabled.
|
|
|
|
let client = try UITestsSignalling.Client(mode: .tests)
|
|
|
|
app = Application.launch(.appLockFlow)
|
|
|
|
await client.waitForApp()
|
|
|
|
|
|
|
|
// Blank form representing an unlocked app.
|
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.unlocked)
|
|
|
|
|
|
|
|
// When the app resigns active but doesn't enter the background.
|
|
|
|
try client.send(.notification(name: UIApplication.willResignActiveNotification))
|
|
|
|
|
|
|
|
// Then the placeholder screen should obscure the content.
|
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.placeholder)
|
|
|
|
|
|
|
|
// When the app becomes active again.
|
2023-12-12 16:08:27 +02:00
|
|
|
try client.send(.notification(name: UIApplication.willEnterForegroundNotification))
|
|
|
|
try? await Task.sleep(for: .milliseconds(100))
|
2023-11-15 15:31:35 +00:00
|
|
|
try client.send(.notification(name: UIApplication.didBecomeActiveNotification))
|
|
|
|
|
|
|
|
// Then the app should not have become unlock.
|
|
|
|
try await app.assertScreenshot(.appLockFlow, step: Step.unlocked)
|
|
|
|
}
|
|
|
|
|
2023-11-10 15:38:54 +00:00
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
func enterPIN() {
|
|
|
|
app.buttons[A11yIdentifiers.appLockScreen.numpad(2)].tap()
|
|
|
|
app.buttons[A11yIdentifiers.appLockScreen.numpad(0)].tap()
|
|
|
|
app.buttons[A11yIdentifiers.appLockScreen.numpad(2)].tap()
|
|
|
|
app.buttons[A11yIdentifiers.appLockScreen.numpad(3)].tap()
|
|
|
|
}
|
2023-11-10 17:21:02 +00:00
|
|
|
|
|
|
|
func enterWrongPIN() {
|
|
|
|
app.buttons[A11yIdentifiers.appLockScreen.numpad(0)].tap()
|
|
|
|
app.buttons[A11yIdentifiers.appLockScreen.numpad(0)].tap()
|
|
|
|
app.buttons[A11yIdentifiers.appLockScreen.numpad(0)].tap()
|
|
|
|
app.buttons[A11yIdentifiers.appLockScreen.numpad(0)].tap()
|
|
|
|
}
|
2023-11-10 15:38:54 +00:00
|
|
|
}
|