183 lines
11 KiB
Swift
183 lines
11 KiB
Swift
import Photos
|
|
import UIKit
|
|
import XCTest
|
|
@testable import suixinkan
|
|
|
|
/// 用正式素材页的隔离场景检查窄屏布局与交互,所有媒体都来自随包资源。
|
|
@MainActor
|
|
final class DroneDockMaterialsSimulationViewTests: XCTestCase {
|
|
private weak var previousKeyWindow: UIWindow?
|
|
|
|
/// 十种真实接口边界状态均保留固定底栏,内容和操作区不互相遮挡。
|
|
func testAllMockStatesKeepFooterAndContentSeparateOnPhoneWidths() async throws {
|
|
for width in [375.0, 402.0] {
|
|
for scenario in ["ready", "uploading", "uploadFailed", "mixed", "empty", "loading", "error", "taskUploading", "taskFailed", "film"] {
|
|
let controller = try XCTUnwrap(DroneDockMaterialsSimulation.makeController(scenario: scenario) as? DroneDockMaterialsViewController)
|
|
let window = show(controller, width: width)
|
|
defer { close(window) }
|
|
try await settle(window) {
|
|
if scenario == "loading" { return controller.viewModel.isLoading }
|
|
if scenario == "error" { return controller.viewModel.detailErrorMessage != nil }
|
|
return controller.viewModel.total != nil
|
|
}
|
|
let list = try XCTUnwrap(find(controller.view, "drone.materials.remote.list") as? UICollectionView)
|
|
let footer = try XCTUnwrap(find(controller.view, "drone.materials.remote.footer"))
|
|
let button = try XCTUnwrap(find(footer, "drone.materials.remote.download") as? UIButton)
|
|
XCTAssertLessThanOrEqual(list.frame.maxY, footer.frame.minY + 1, scenario)
|
|
XCTAssertGreaterThanOrEqual(button.bounds.height, 44, scenario)
|
|
XCTAssertLessThanOrEqual(footer.frame.maxY, controller.view.bounds.maxY + 1, scenario)
|
|
XCTAssertFalse(button.isEnabled, scenario)
|
|
XCTAssertEqual(controller.viewModel.selectedCount, 0)
|
|
for thumbnail in views(list).filter({ $0.accessibilityIdentifier == "drone.materials.remote.thumbnail" }) {
|
|
XCTAssertEqual(thumbnail.bounds.height, thumbnail.bounds.width * 9 / 16, accuracy: 1)
|
|
}
|
|
if scenario == "uploading" || scenario == "uploadFailed" || scenario.hasPrefix("task") {
|
|
XCTAssertTrue(controller.viewModel.selectableMaterialIDs.isEmpty, scenario)
|
|
}
|
|
let image = UIGraphicsImageRenderer(bounds: window.bounds).image { _ in
|
|
window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
|
|
}
|
|
let attachment = XCTAttachment(image: image)
|
|
attachment.name = "订单素材库-\(scenario)-\(Int(width))"
|
|
attachment.lifetime = .keepAlways
|
|
add(attachment)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 选择不打开预览,预览不改变选择;返回时继续显示固定底栏中的已选数量。
|
|
func testLocalSelectionAndPreviewAreIndependent() async throws {
|
|
let controller = try XCTUnwrap(DroneDockMaterialsSimulation.makeController(scenario: "ready") as? DroneDockMaterialsViewController)
|
|
let window = show(controller, width: 402)
|
|
defer { close(window) }
|
|
try await settle(window) { controller.viewModel.total == 3 }
|
|
let selection = try XCTUnwrap(find(controller.view, "drone.materials.remote.select.903.5") as? UIButton)
|
|
selection.sendActions(for: .touchUpInside)
|
|
XCTAssertEqual(controller.viewModel.selectedCount, 1)
|
|
XCTAssertEqual(controller.navigationController?.viewControllers.count, 1)
|
|
window.layoutIfNeeded()
|
|
let selectedImage = UIGraphicsImageRenderer(bounds: window.bounds).image { _ in
|
|
window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
|
|
}
|
|
let selectedAttachment = XCTAttachment(image: selectedImage)
|
|
selectedAttachment.name = "订单素材库-selected-402"
|
|
selectedAttachment.lifetime = .keepAlways
|
|
add(selectedAttachment)
|
|
let preview = try XCTUnwrap(find(controller.view, "drone.materials.remote.file.903.5.preview") as? UIButton)
|
|
preview.sendActions(for: .touchUpInside)
|
|
let navigation = try XCTUnwrap(controller.navigationController)
|
|
try await settle(window) { navigation.viewControllers.count == 2 }
|
|
XCTAssertEqual(controller.viewModel.selectedCount, 1)
|
|
navigation.popViewController(animated: false)
|
|
try await settle(window) { navigation.topViewController === controller }
|
|
XCTAssertEqual(controller.viewModel.selectedCount, 1)
|
|
let download = try XCTUnwrap(find(controller.view, "drone.materials.remote.download") as? UIButton)
|
|
XCTAssertTrue(download.isEnabled)
|
|
}
|
|
|
|
/// 默认演示使用 mock.invalid 与随包素材,完成文案不声称写入相册,也不申请相册权限。
|
|
func testDefaultMockDownloadCompletesAsSimulationWithoutPhotoPermissionChange() async throws {
|
|
let authorizationBefore = PHPhotoLibrary.authorizationStatus(for: .addOnly)
|
|
let controller = try XCTUnwrap(DroneDockMaterialsSimulation.makeController(arguments: ["--dock-materials-mock=ready"]) as? DroneDockMaterialsViewController)
|
|
let window = show(controller, width: 402)
|
|
defer { close(window) }
|
|
try await settle(window) { controller.viewModel.total == 3 }
|
|
XCTAssertEqual(controller.navigationItem.prompt, "本地演示 · 未联网")
|
|
XCTAssertTrue(controller.viewModel.tasks.flatMap(\.materials).allSatisfy { $0.previewURL?.host == "mock.invalid" })
|
|
try XCTUnwrap(find(controller.view, "drone.materials.remote.select.903.5") as? UIButton).sendActions(for: .touchUpInside)
|
|
try await settle(window) { controller.viewModel.selectedCount == 1 }
|
|
try XCTUnwrap(find(controller.view, "drone.materials.remote.download") as? UIButton).sendActions(for: .touchUpInside)
|
|
try await settle(window) { controller.downloadViewModel.phase == .finished }
|
|
XCTAssertEqual(controller.downloadViewModel.completedCount, 1)
|
|
XCTAssertEqual(controller.downloadViewModel.failedCount, 0)
|
|
let labels = views(controller.view).compactMap { ($0 as? UILabel)?.text }
|
|
XCTAssertTrue(labels.contains { $0.contains("演示完成") })
|
|
XCTAssertTrue(labels.contains { $0.contains("未下载或保存真实文件") })
|
|
XCTAssertFalse(labels.contains { $0.hasPrefix("已保存") })
|
|
XCTAssertNil(controller.presentedViewController)
|
|
XCTAssertEqual(PHPhotoLibrary.authorizationStatus(for: .addOnly), authorizationBefore)
|
|
}
|
|
|
|
/// 显式回环参数只改变固定测试媒体地址;这里只检查模型,不触发下载或 Photos。
|
|
func testLocalDownloadFlagOnlyBuildsCanonicalLoopbackMediaURLs() async throws {
|
|
for base in ["http://127.0.0.1:18765", "http://127.0.0.1:18765/"] {
|
|
let controller = try XCTUnwrap(DroneDockMaterialsSimulation.makeController(arguments: [
|
|
"--dock-materials-mock=ready", "--dock-materials-download-local=\(base)"
|
|
]) as? DroneDockMaterialsViewController)
|
|
await controller.viewModel.refresh()
|
|
XCTAssertEqual(controller.navigationItem.prompt, "本地测试素材 · 无业务请求")
|
|
let files = controller.viewModel.tasks.flatMap(\.materials)
|
|
XCTAssertEqual(files.count, 6)
|
|
for file in files {
|
|
XCTAssertEqual(file.fileURL, "http://127.0.0.1:18765/\(file.fileType == 1 ? "demo.jpg" : "demo.mp4")")
|
|
}
|
|
XCTAssertEqual(controller.downloadViewModel.phase, .idle)
|
|
XCTAssertEqual(controller.downloadViewModel.completedCount, 0)
|
|
}
|
|
}
|
|
|
|
/// 非回环、隐式端口、路径、鉴权与查询等参数全部回退纯模拟,不能借调试入口访问任意地址。
|
|
func testInvalidLocalDownloadFlagsFallBackToIsolatedMockURLs() async throws {
|
|
let invalidBases = [
|
|
"https://127.0.0.1:18765", "http://localhost:18765", "http://192.168.1.10:18765",
|
|
"http://example.com:18765", "http://127.0.0.1", "http://127.0.0.1:0",
|
|
"http://127.0.0.1:65536", "http://127.0.0.1:18765/files",
|
|
"http://user:pass@127.0.0.1:18765", "http://127.0.0.1:18765?source=remote",
|
|
"http://127.0.0.1:18765#fragment", "file:///tmp/materials"
|
|
]
|
|
for base in invalidBases {
|
|
let controller = try XCTUnwrap(DroneDockMaterialsSimulation.makeController(arguments: [
|
|
"--dock-materials-mock=ready", "--dock-materials-download-local=\(base)"
|
|
]) as? DroneDockMaterialsViewController)
|
|
await controller.viewModel.refresh()
|
|
XCTAssertEqual(controller.navigationItem.prompt, "本地演示 · 未联网", base)
|
|
XCTAssertTrue(controller.viewModel.tasks.flatMap(\.materials).allSatisfy { $0.previewURL?.host == "mock.invalid" }, base)
|
|
XCTAssertEqual(controller.downloadViewModel.phase, .idle)
|
|
}
|
|
let mixed = try XCTUnwrap(DroneDockMaterialsSimulation.makeController(arguments: [
|
|
"--dock-materials-mock=mixed", "--dock-materials-download-local=http://127.0.0.1:18765"
|
|
]) as? DroneDockMaterialsViewController)
|
|
await mixed.viewModel.refresh()
|
|
XCTAssertTrue(mixed.viewModel.tasks.flatMap(\.materials).compactMap(\.previewURL).allSatisfy { $0.host == "mock.invalid" })
|
|
XCTAssertEqual(mixed.navigationItem.prompt, "本地演示 · 未联网")
|
|
XCTAssertNil(DroneDockMaterialsSimulation.makeController(arguments: ["--dock-materials-download-local=http://127.0.0.1:18765"]))
|
|
}
|
|
|
|
private func show(_ controller: UIViewController, width: CGFloat) -> UIWindow {
|
|
let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
|
|
let scene = scenes.first { $0.activationState == .foregroundActive } ?? scenes.first
|
|
previousKeyWindow = scene?.windows.first { $0.isKeyWindow }
|
|
let window = scene.map { UIWindow(windowScene: $0) } ?? UIWindow()
|
|
window.frame = CGRect(x: 0, y: 0, width: width, height: width == 375 ? 812 : 874)
|
|
window.overrideUserInterfaceStyle = .light
|
|
window.rootViewController = UINavigationController(rootViewController: controller)
|
|
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 {
|
|
for _ in 0..<150 {
|
|
window.layoutIfNeeded()
|
|
if condition() {
|
|
// 等待 Diffable 的淡入结束,避免截图叠加上一帧的加载占位。
|
|
try await Task.sleep(nanoseconds: 400_000_000)
|
|
window.layoutIfNeeded()
|
|
if condition() { return }
|
|
}
|
|
try await Task.sleep(nanoseconds: 15_000_000)
|
|
}
|
|
XCTFail("素材演示未完成状态展示")
|
|
throw NSError(domain: "DroneDockMaterialsSimulationViewTests", code: 1)
|
|
}
|
|
|
|
private func views(_ root: UIView) -> [UIView] { [root] + root.subviews.flatMap { views($0) } }
|
|
private func find(_ root: UIView, _ identifier: String) -> UIView? { views(root).first { $0.accessibilityIdentifier == identifier } }
|
|
}
|