- 完善双路 RTC 接收、首帧切换及拍摄结束后的本地播放释放 - 对齐无人机状态枚举与正常拍完返航标记,新增到达后本地倒计时 - 补充准备页电量与前往回执,进入任务时移除准备页路由 - 对齐拍摄中和拍摄完成原型布局,移除详情按钮 - 补充隔离测试、接口记录与项目约定 验证:最近 157 项相关模拟器隔离测试通过,Release 无签名编译通过;未调用真实飞控或图传。
210 lines
11 KiB
Swift
210 lines
11 KiB
Swift
import UIKit
|
|
import XCTest
|
|
@testable import suixinkan
|
|
|
|
/// 使用纯内存准备/召唤替身验证准备页替换路由,不连接真实设备或图传。
|
|
@MainActor
|
|
final class DroneDockPreparationRouteTests: XCTestCase {
|
|
private weak var previousKeyWindow: UIWindow?
|
|
|
|
/// 正常业务栈移除准备页,任务返回原工作台;更早的导航页面仍完整保留。
|
|
func testSuccessfulSummonReplacesPreparationAndReturnsToSameWorkbench() async throws {
|
|
for hasEarlierRoot in [false, true] {
|
|
let creation = DroneDockTaskCreationTestService(result: .success(try DroneDockTaskCreationTestData.response()))
|
|
let shoot = DroneDockShootTestService(result: .success(try DroneDockShootTestData.response()))
|
|
let task = PreparationRouteTaskStub(taskID: 501)
|
|
var destinationCount = 0
|
|
let project = try DroneDockPreparationViewFixtures.projectEnvelope()
|
|
let (preparation, projects) = try DroneDockPreparationViewFixtures.controller(
|
|
context: DroneDockPreparationViewFixtures.context(), order: DroneDockPreparationViewFixtures.order(),
|
|
responses: [project, project], creationService: creation, shootService: shoot,
|
|
taskControllerFactory: { id, order, context in
|
|
destinationCount += 1
|
|
XCTAssertEqual(id, 501)
|
|
XCTAssertEqual(order.orderNumber, "TEST-PREPARATION-001")
|
|
XCTAssertEqual(context.scenicID, 128)
|
|
return task
|
|
})
|
|
let workbench = UIViewController()
|
|
workbench.title = "机巢工作台(隔离替身)"
|
|
let earlier = UIViewController()
|
|
let prefix = hasEarlierRoot ? [earlier, workbench] : [workbench]
|
|
let navigation = UINavigationController()
|
|
navigation.setViewControllers(prefix + [preparation], animated: false)
|
|
let window = makeWindow(navigation)
|
|
defer { close(window) }
|
|
let summon = try button(in: preparation.view, id: "drone.preparation.summon")
|
|
try await settle(window) { summon.isEnabled }
|
|
summon.sendActions(for: .touchUpInside)
|
|
summon.sendActions(for: .touchUpInside)
|
|
try await settle(window) { navigation.topViewController === task && navigation.transitionCoordinator == nil }
|
|
XCTAssertEqual(navigation.viewControllers.count, prefix.count + 1)
|
|
XCTAssertTrue(navigation.viewControllers.dropLast().elementsEqual(prefix, by: { $0 === $1 }))
|
|
XCTAssertFalse(navigation.viewControllers.contains { $0 === preparation })
|
|
XCTAssertEqual(creation.requests.count, 1)
|
|
XCTAssertEqual(shoot.requests, [501])
|
|
XCTAssertEqual(destinationCount, 1)
|
|
XCTAssertEqual(projects.requests.count, 2)
|
|
|
|
XCTAssertTrue(navigation.popViewController(animated: false) === task)
|
|
try await settle(window) { navigation.topViewController === workbench }
|
|
XCTAssertTrue(navigation.viewControllers.elementsEqual(prefix, by: { $0 === $1 }))
|
|
summon.sendActions(for: .touchUpInside)
|
|
try await Task.sleep(for: .milliseconds(80))
|
|
XCTAssertTrue(navigation.topViewController === workbench, "已移除准备页的旧按钮不能再次导航")
|
|
XCTAssertEqual(destinationCount, 1)
|
|
XCTAssertEqual(creation.requests.count, 1)
|
|
XCTAssertEqual(shoot.requests, [501])
|
|
XCTAssertEqual(projects.requests.count, 2, "返回工作台不应重新激活已移除准备页的读取")
|
|
}
|
|
}
|
|
|
|
/// 已创建的结束任务仅在用户点击查看时替换准备页,不重发创建或召唤。
|
|
func testExplicitReviewOfCreatedTaskAlsoRemovesPreparation() async throws {
|
|
let creation = DroneDockTaskCreationTestService(result: .success(try DroneDockTaskCreationTestData.response()))
|
|
let shoot = DroneDockShootTestService(result: .failure(APIError.serverCode(100099,
|
|
"任务已结束,无法召唤;失败后请传 restart=true 重新召唤")))
|
|
let task = PreparationRouteTaskStub(taskID: 501)
|
|
var destinationCount = 0
|
|
let project = try DroneDockPreparationViewFixtures.projectEnvelope()
|
|
let (preparation, _) = try DroneDockPreparationViewFixtures.controller(
|
|
context: DroneDockPreparationViewFixtures.context(), order: DroneDockPreparationViewFixtures.order(),
|
|
responses: [project, project], creationService: creation, shootService: shoot,
|
|
taskControllerFactory: { id, _, _ in
|
|
destinationCount += 1
|
|
XCTAssertEqual(id, 501)
|
|
return task
|
|
})
|
|
let workbench = UIViewController()
|
|
workbench.title = "机巢工作台(隔离替身)"
|
|
let navigation = UINavigationController()
|
|
navigation.setViewControllers([workbench, preparation], animated: false)
|
|
let window = makeWindow(navigation)
|
|
defer { close(window) }
|
|
let summon = try button(in: preparation.view, id: "drone.preparation.summon")
|
|
try await settle(window) { summon.isEnabled }
|
|
summon.sendActions(for: .touchUpInside)
|
|
try await settle(window) { preparation.viewModel.summonRequiresTaskReview && summon.isEnabled }
|
|
XCTAssertTrue(navigation.topViewController === preparation)
|
|
XCTAssertEqual(destinationCount, 0)
|
|
XCTAssertEqual(summon.title(for: .normal), "查看拍摄任务")
|
|
summon.sendActions(for: .touchUpInside)
|
|
summon.sendActions(for: .touchUpInside)
|
|
try await settle(window) { navigation.topViewController === task && navigation.transitionCoordinator == nil }
|
|
XCTAssertTrue(navigation.viewControllers.elementsEqual([workbench, task], by: { $0 === $1 }))
|
|
XCTAssertEqual(destinationCount, 1)
|
|
XCTAssertEqual(creation.requests.count, 1)
|
|
XCTAssertEqual(shoot.requests, [501])
|
|
navigation.popViewController(animated: false)
|
|
try await settle(window) { navigation.topViewController === workbench }
|
|
XCTAssertEqual(navigation.viewControllers.count, 1)
|
|
}
|
|
|
|
/// 已离开准备页后返回的迟到召唤回执不能覆盖工作台或再次压入任务页。
|
|
func testLateSummonReceiptAfterLeavingPreparationDoesNotNavigate() async throws {
|
|
let creation = DroneDockTaskCreationTestService(result: .success(try DroneDockTaskCreationTestData.response()))
|
|
let shoot = PreparationRouteDeferredShoot()
|
|
var destinationCount = 0
|
|
let project = try DroneDockPreparationViewFixtures.projectEnvelope()
|
|
let (preparation, _) = try DroneDockPreparationViewFixtures.controller(
|
|
context: DroneDockPreparationViewFixtures.context(), order: DroneDockPreparationViewFixtures.order(),
|
|
responses: [project, project], creationService: creation, shootService: shoot,
|
|
taskControllerFactory: { id, _, _ in
|
|
destinationCount += 1
|
|
return PreparationRouteTaskStub(taskID: id)
|
|
})
|
|
let workbench = UIViewController()
|
|
workbench.title = "机巢工作台(隔离替身)"
|
|
let navigation = UINavigationController()
|
|
navigation.setViewControllers([workbench, preparation], animated: false)
|
|
let window = makeWindow(navigation)
|
|
defer { shoot.finish(); close(window) }
|
|
let summon = try button(in: preparation.view, id: "drone.preparation.summon")
|
|
try await settle(window) { summon.isEnabled }
|
|
summon.sendActions(for: .touchUpInside)
|
|
summon.sendActions(for: .touchUpInside)
|
|
try await settle(window) { shoot.isPending }
|
|
XCTAssertEqual(creation.requests.count, 1)
|
|
XCTAssertEqual(shoot.requests, [501])
|
|
navigation.popViewController(animated: false)
|
|
try await settle(window) { navigation.topViewController === workbench }
|
|
shoot.finish()
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
XCTAssertTrue(navigation.topViewController === workbench)
|
|
XCTAssertEqual(navigation.viewControllers.count, 1)
|
|
XCTAssertEqual(destinationCount, 0)
|
|
XCTAssertEqual(creation.requests.count, 1)
|
|
XCTAssertEqual(shoot.requests, [501])
|
|
}
|
|
|
|
private func button(in root: UIView, id: String) throws -> UIButton {
|
|
try XCTUnwrap(views(root).first { $0.accessibilityIdentifier == id } as? UIButton)
|
|
}
|
|
|
|
private func views(_ root: UIView) -> [UIView] { [root] + root.subviews.flatMap { self.views($0) } }
|
|
|
|
private func makeWindow(_ navigation: UINavigationController) -> UIWindow {
|
|
let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first
|
|
previousKeyWindow = scene?.windows.first { $0.isKeyWindow }
|
|
let window = scene.map { UIWindow(windowScene: $0) } ?? UIWindow(frame: UIScreen.main.bounds)
|
|
window.rootViewController = navigation
|
|
window.makeKeyAndVisible()
|
|
return window
|
|
}
|
|
|
|
private func close(_ window: UIWindow) {
|
|
window.rootViewController?.dismiss(animated: false)
|
|
window.isHidden = true
|
|
window.rootViewController = nil
|
|
previousKeyWindow?.makeKeyAndVisible()
|
|
}
|
|
|
|
private func settle(_ window: UIWindow, until condition: () -> Bool) async throws {
|
|
let deadline = ContinuousClock.now.advanced(by: .seconds(4))
|
|
while ContinuousClock.now < deadline {
|
|
window.layoutIfNeeded()
|
|
if condition() {
|
|
try await Task.sleep(for: .milliseconds(80))
|
|
window.layoutIfNeeded()
|
|
if condition() { return }
|
|
}
|
|
try await Task.sleep(for: .milliseconds(10))
|
|
}
|
|
XCTFail("隔离准备页路由未达到预期状态")
|
|
throw NSError(domain: "DroneDockPreparationRouteTests", code: 1)
|
|
}
|
|
}
|
|
|
|
/// 只承接任务身份的空任务页,不运行任何状态请求或设备服务。
|
|
@MainActor
|
|
private final class PreparationRouteTaskStub: UIViewController {
|
|
let taskID: Int
|
|
|
|
init(taskID: Int) {
|
|
self.taskID = taskID
|
|
super.init(nibName: nil, bundle: nil)
|
|
title = "拍摄任务(隔离替身)"
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
}
|
|
|
|
/// 故意在页面离开后再完成回执,用于验证取消和导航身份检查,不包含网络传输。
|
|
@MainActor
|
|
private final class PreparationRouteDeferredShoot: DroneDockShootServing {
|
|
private(set) var requests: [Int] = []
|
|
private var continuation: CheckedContinuation<DroneDockShootResponse, Error>?
|
|
var isPending: Bool { continuation != nil }
|
|
|
|
func shoot(taskID: Int) async throws -> DroneDockShootResponse {
|
|
requests.append(taskID)
|
|
return try await withCheckedThrowingContinuation { continuation = $0 }
|
|
}
|
|
|
|
func finish() {
|
|
continuation?.resume(returning: DroneDockShootResponse(dataJSON: #"{"source":"local_mock"}"#))
|
|
continuation = nil
|
|
}
|
|
}
|