feat(drone-dock): 搭建素材库预览选择与下载演示

This commit is contained in:
2026-10-23 17:11:54 +08:00
parent 7834b8bcbc
commit 837623ee7f
24 changed files with 3214 additions and 5 deletions
@@ -0,0 +1,192 @@
import AVFoundation
import AVKit
import UIKit
import XCTest
@testable import suixinkan
/// 在真机窗口验证素材预览的就绪过滤、初始页、静帧说明和关闭路径。
@MainActor
final class DroneMaterialPreviewViewTests: XCTestCase {
private weak var previousKeyWindow: UIWindow?
/// 本地演示视频随应用打包,点击播放才呈现播放器,不访问远程媒体。
func testBundledDemoVideoIsPlayableAndOpensOnlyAfterTap() async throws {
let url = try XCTUnwrap(Bundle.main.url(forResource: "drone_material_demo", withExtension: "mp4"))
XCTAssertTrue(url.isFileURL)
let playable = try await AVURLAsset(url: url).load(.isPlayable)
XCTAssertTrue(playable)
let material = DroneMaterial(id: "local-video", title: "演示视频", kind: .video,
previewURL: url, durationText: "00:03")
let controller = DroneMaterialPreviewViewController(items: [material], initialMaterialID: material.id)
let window = makeWindow(root: controller)
defer { close(window) }
try await settle(window: window) {
self.find(in: controller.view, identifier: "drone.material.preview.play") != nil
}
XCTAssertNil(controller.presentedViewController)
let play = try XCTUnwrap(find(in: controller.view, identifier: "drone.material.preview.play") as? UIButton)
assertHitTestReaches(play, in: window)
play.sendActions(for: .touchUpInside)
try await settle(window: window) { controller.presentedViewController is AVPlayerViewController }
let player = try XCTUnwrap(controller.presentedViewController as? AVPlayerViewController)
XCTAssertEqual((player.player?.currentItem?.asset as? AVURLAsset)?.url, url)
player.player?.pause()
player.dismiss(animated: false)
}
/// 指定素材从过滤后的正确页打开,真实本地图可见并保留预览截图。
func testReadyMaterialsOpenAtRequestedPageAndRenderLocalImage() async throws {
let items = [
DroneMaterial(id: "photo-01", title: "素材01", previewAssetName: "drone_demo_mountain"),
DroneMaterial(id: "failed-02", title: "素材02", uploadState: .failed),
DroneMaterial(id: "photo-03", title: "素材03", previewAssetName: "drone_demo_pagoda")
]
let controller = DroneMaterialPreviewViewController(items: items, initialMaterialID: "photo-03")
let window = makeWindow(root: controller)
defer { close(window) }
try await settle(window: window) {
self.previewImage(in: controller.view, title: "素材03", window: window) != nil
}
let labels = visibleViews(in: controller.view).compactMap { $0 as? UILabel }
XCTAssertTrue(labels.contains { $0.text == "2 / 2" })
XCTAssertTrue(labels.contains { $0.text == "照片 · 本地演示图片" })
XCTAssertFalse(labels.contains { $0.text?.contains("素材02") == true })
let image = try XCTUnwrap(previewImage(in: controller.view, title: "素材03", window: window))
XCTAssertNotNil(image.image)
XCTAssertEqual(image.accessibilityLabel, "素材03")
XCTAssertGreaterThan(image.bounds.width, 100)
XCTAssertGreaterThan(image.bounds.height, 100)
assertHitTestReaches(image, in: window)
let close = try XCTUnwrap(find(in: controller.view, identifier: "drone.material.preview.close") as? UIButton)
assertVisible(close, in: window)
XCTAssertFalse(visibleViews(in: controller.view).contains {
!$0.translatesAutoresizingMaskIntoConstraints && !String(describing: type(of: $0)).hasPrefix("_") && $0.hasAmbiguousLayout
})
capture(window, named: "机巢-素材预览-就绪图片与页码")
}
/// 无本地片源的视频明确标识为静帧,关闭后回到原页面而不触发播放或其他操作。
func testVideoWithoutLocalClipShowsHonestStillFrameAndCloses() async throws {
let material = DroneMaterial(id: "video-01", title: "素材视频01", kind: .video,
previewAssetName: "drone_demo_mountain", previewURL: nil)
let host = UIViewController()
host.view.backgroundColor = .systemBackground
host.view.accessibilityIdentifier = "drone.material.preview.testHost"
let window = makeWindow(root: host)
defer { close(window) }
let controller = DroneMaterialPreviewViewController(items: [material], initialMaterialID: material.id)
host.present(controller, animated: false)
try await settle(window: window) {
controller.view.window != nil && controller.presentingViewController === host
&& self.previewImage(in: controller.view, title: material.title, window: window) != nil
}
let labels = visibleViews(in: controller.view).compactMap { $0 as? UILabel }
XCTAssertTrue(labels.contains { $0.text == "视频 · 本地演示静帧 · 暂无演示视频" })
XCTAssertFalse(visibleViews(in: controller.view).contains { $0.accessibilityIdentifier == "drone.material.preview.play" })
XCTAssertNil(controller.presentedViewController)
capture(window, named: "机巢-素材预览-视频静帧明确说明")
let close = try XCTUnwrap(find(in: controller.view, identifier: "drone.material.preview.close") as? UIButton)
assertHitTestReaches(close, in: window)
close.sendActions(for: .touchUpInside)
try await settle(window: window) { host.presentedViewController == nil }
XCTAssertTrue(window.rootViewController === host)
XCTAssertNotNil(host.view.window)
}
/// 使用真机当前场景创建隔离窗口,保持测试前的主窗口可恢复。
private func makeWindow(root controller: UIViewController) -> 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(frame: UIScreen.main.bounds)
window.frame = scene?.coordinateSpace.bounds ?? UIScreen.main.bounds
window.rootViewController = controller
window.makeKeyAndVisible()
window.layoutIfNeeded()
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()
window.rootViewController?.view.layoutIfNeeded()
if condition() {
try await Task.sleep(nanoseconds: 250_000_000)
window.layoutIfNeeded()
return
}
try await Task.sleep(nanoseconds: 20_000_000)
}
XCTFail("素材预览未在预期时间内完成布局或关闭")
throw NSError(domain: "DroneMaterialPreviewViewTests", code: 1)
}
private func allViews(in view: UIView) -> [UIView] {
[view] + view.subviews.flatMap { allViews(in: $0) }
}
private func visibleViews(in view: UIView) -> [UIView] {
allViews(in: view).filter {
!isHiddenByAncestor($0) && !$0.bounds.isEmpty && $0.convert($0.bounds, to: view).intersects(view.bounds)
}
}
private func find(in view: UIView, identifier: String) -> UIView? {
allViews(in: view).first { $0.accessibilityIdentifier == identifier }
}
/// 等待有真实图像尺寸且中心触摸可达的当前页,不能仅以标题出现判定预览完成。
private func previewImage(in view: UIView, title: String, window: UIWindow) -> UIImageView? {
visibleViews(in: view).compactMap { $0 as? UIImageView }.first {
$0.accessibilityIdentifier == "drone.material.preview.image" && $0.accessibilityLabel == title
&& $0.image != nil && $0.bounds.width > 100 && $0.bounds.height > 100
&& isHitTestReachable($0, in: window)
}
}
private func isHitTestReachable(_ view: UIView, in window: UIWindow) -> Bool {
let center = view.convert(CGPoint(x: view.bounds.midX, y: view.bounds.midY), to: window)
guard window.bounds.contains(center), let hit = window.hitTest(center, with: nil) else { return false }
return hit === view || hit.isDescendant(of: view)
}
/// 验证实际触摸命中目标或其子视图,捕获空尺寸、裁切或透明覆盖导致的假可见内容。
private func assertHitTestReaches(_ view: UIView, in window: UIWindow, file: StaticString = #filePath, line: UInt = #line) {
XCTAssertTrue(isHitTestReachable(view, in: window), "触摸无法到达 \(view.accessibilityIdentifier ?? String(describing: type(of: view)))", file: file, line: line)
}
private func isHiddenByAncestor(_ view: UIView) -> Bool {
if view.isHidden || view.alpha < 0.01 { return true }
return view.superview.map { isHiddenByAncestor($0) } ?? false
}
private func assertVisible(_ button: UIButton, in window: UIWindow, file: StaticString = #filePath, line: UInt = #line) {
XCTAssertTrue(button.isEnabled, file: file, line: line)
let frame = button.convert(button.bounds, to: window)
XCTAssertGreaterThanOrEqual(frame.height, 44, file: file, line: line)
XCTAssertGreaterThanOrEqual(frame.width, 44, file: file, line: line)
XCTAssertTrue(window.bounds.contains(frame), file: file, line: line)
assertHitTestReaches(button, in: window, file: file, line: line)
}
/// 把预览的静态画面保留到真机测试 xcresult 中,供按原型复核。
private func capture(_ window: UIWindow, named name: String) {
let image = UIGraphicsImageRenderer(bounds: window.bounds).image { _ in
window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
}
let attachment = XCTAttachment(image: image)
attachment.name = name
attachment.lifetime = .keepAlways
add(attachment)
}
}