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) } 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 } } }