121 lines
6.4 KiB
Swift
121 lines
6.4 KiB
Swift
import UIKit
|
|
import XCTest
|
|
@testable import suixinkan
|
|
|
|
/// 使用挂起的内存读取验证无感刷新,以及刷新与用户操作交错时的防重和时序。
|
|
@MainActor
|
|
final class DroneDockSilentRefreshTests: XCTestCase {
|
|
/// 正常刷新不改变开始/安全按钮可用性;用户点击后才锁定,旧请求迟到不覆盖开拍后快照。
|
|
func testButtonsRemainEnabledAndStartWinsOverPendingRead() async throws {
|
|
let service = try SilentRefreshService()
|
|
let context = DroneDockShootingTaskTestData.context()
|
|
let model = DroneDockShootingTaskViewModel(context: context, taskID: 501,
|
|
order: try DroneDockShootingTaskTestData.order(), service: service, startService: service,
|
|
commandService: service, currentContext: { context })
|
|
let controller = DroneDockShootingTaskViewController(viewModel: model, simulationRecordsProvider: { "本地模拟" })
|
|
let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first
|
|
let previous = scene?.windows.first { $0.isKeyWindow }
|
|
let window = scene.map { UIWindow(windowScene: $0) } ?? UIWindow(frame: UIScreen.main.bounds)
|
|
window.rootViewController = UINavigationController(rootViewController: controller)
|
|
window.makeKeyAndVisible()
|
|
defer {
|
|
service.finishRead()
|
|
window.isHidden = true; window.rootViewController = nil; previous?.makeKeyAndVisible()
|
|
}
|
|
try await wait { model.canStartShooting }
|
|
let primary = try XCTUnwrap(views(controller.view).first { $0.accessibilityIdentifier == "drone.task.remote.primary" } as? UIButton)
|
|
let pause = try XCTUnwrap(views(controller.view).first { $0.accessibilityIdentifier == "drone.task.remote.pause" } as? UIControl)
|
|
let refresh = try XCTUnwrap(controller.navigationItem.rightBarButtonItems?.first {
|
|
$0.accessibilityIdentifier == "drone.task.remote.navigationRefresh"
|
|
})
|
|
XCTAssertTrue(primary.isEnabled)
|
|
XCTAssertTrue(pause.isEnabled)
|
|
_ = UIApplication.shared.sendAction(try XCTUnwrap(refresh.action), to: refresh.target, from: refresh, for: nil)
|
|
try await wait { model.isLoading && service.reads == 2 }
|
|
XCTAssertTrue(primary.isEnabled)
|
|
XCTAssertTrue(pause.isEnabled)
|
|
XCTAssertTrue(refresh.isEnabled)
|
|
XCTAssertTrue(model.canStartShooting)
|
|
XCTAssertTrue(model.canSendCommand(.pause))
|
|
primary.sendActions(for: .touchUpInside)
|
|
primary.sendActions(for: .touchUpInside)
|
|
XCTAssertFalse(primary.isEnabled)
|
|
try await wait { model.hasSubmittedStart && !model.isStartingShooting }
|
|
XCTAssertEqual(service.starts, 1)
|
|
XCTAssertEqual(service.reads, 3, "提交后必须重新回查,不能被先前的 isLoading 跳过")
|
|
XCTAssertEqual(model.presentation.phase, .shooting)
|
|
service.finishRead()
|
|
try await Task.sleep(for: .milliseconds(40))
|
|
XCTAssertEqual(model.presentation.phase, .shooting, "迟到的 ready 响应不能倒退页面")
|
|
XCTAssertEqual(service.starts, 1)
|
|
}
|
|
|
|
/// 无权限和真实状态变化仍立即撤销按钮许可,无感只适用于请求本身。
|
|
func testNewGateAndPermissionDenialStillDisableActions() async throws {
|
|
let service = try SilentRefreshService()
|
|
let context = DroneDockShootingTaskTestData.context()
|
|
let model = DroneDockShootingTaskViewModel(context: context, taskID: 501,
|
|
order: try DroneDockShootingTaskTestData.order(), service: service, startService: service,
|
|
commandService: service, currentContext: { context })
|
|
await model.refresh()
|
|
XCTAssertTrue(model.canStartShooting)
|
|
model.applyTaskSnapshot(try SilentRefreshService.snapshot(shooting: true))
|
|
XCTAssertFalse(model.canStartShooting)
|
|
XCTAssertTrue(model.canSendCommand(.pause))
|
|
model.stopReading(message: "无权订阅该飞行任务")
|
|
XCTAssertFalse(model.canSendCommand(.pause))
|
|
await model.startShooting()
|
|
XCTAssertEqual(service.starts, 0)
|
|
}
|
|
|
|
private func wait(_ condition: () -> Bool) async throws {
|
|
for _ in 0..<200 {
|
|
if condition() { return }
|
|
try await Task.sleep(for: .milliseconds(10))
|
|
}
|
|
XCTFail("本地模拟状态未就绪")
|
|
}
|
|
|
|
private func views(_ root: UIView) -> [UIView] { [root] + root.subviews.flatMap(views) }
|
|
}
|
|
|
|
/// 第二次读取刻意忽略取消并迟到返回旧快照,第三次回查立即返回操作后状态。
|
|
@MainActor
|
|
private final class SilentRefreshService: DroneDockShootingServing, DroneDockStartShooting, DroneDockCommandSending {
|
|
var reads = 0
|
|
var starts = 0
|
|
private var continuation: CheckedContinuation<DroneDockShootingResponse, Never>?
|
|
private let ready: DroneDockShootingResponse
|
|
init() throws { ready = try Self.snapshot(shooting: false) }
|
|
|
|
func shooting(taskID: Int, liveBattery: Bool) async throws -> DroneDockShootingResponse {
|
|
XCTAssertEqual(taskID, 501); XCTAssertFalse(liveBattery)
|
|
reads += 1
|
|
if reads == 2 { return await withCheckedContinuation { continuation = $0 } }
|
|
return starts == 0 ? ready : try Self.snapshot(shooting: true)
|
|
}
|
|
|
|
func startShooting(taskID: Int) async throws -> DroneDockStartShootResponse {
|
|
starts += 1
|
|
return try JSONDecoder().decode(DroneDockStartShootResponse.self, from: Data("""
|
|
{"order_flight_task_id":501,"plane_id":58,"order_flight_task":{"id":501,"order_number":"TEST-TASK-PAGE-001","scenic_id":128}}
|
|
""".utf8))
|
|
}
|
|
|
|
func sendCommand(taskID: Int, command: DroneDockSafetyCommand) async throws -> DroneDockCommandResponse {
|
|
XCTFail("本测试不提交安全指令")
|
|
throw CancellationError()
|
|
}
|
|
|
|
func finishRead() { continuation?.resume(returning: ready); continuation = nil }
|
|
|
|
static func snapshot(shooting: Bool) throws -> DroneDockShootingResponse {
|
|
try DroneDockShootingTaskTestData.response(changes: [
|
|
"order_flight_task": ["id": 501, "order_number": "TEST-TASK-PAGE-001", "scenic_id": 128, "status": 20],
|
|
"task": ["id": 501, "status": 20], "current_plane": ["id": 58],
|
|
"execute_gate": ["can_execute": !shooting, "primary_action": shooting ? "shooting" : "start_shoot"],
|
|
"ui": ["phase": shooting ? "shooting" : "arrived_pending_start", "commands": [["command": "flighttask_pause"], ["command": "return_home"]]]
|
|
])
|
|
}
|
|
}
|