Files
suixinkan_uikit/suixinkanTests/ScenicMaterialsFooterLayoutTests.swift
T
lujiuyin b95b251de1 feat: 完善机巢模拟调试与素材交付交互
增加禁飞期间的设备指令拦截及本地模拟拍摄流程;适配订单素材库和景区空境库,完善素材筛选、预览与下载;订单交付入口改为方式选择弹窗并复用云端上传。补充单元测试、接口记录和模拟器截图。
2026-10-30 09:59:11 +08:00

255 lines
14 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import UIKit
import XCTest
@testable import suixinkan
/// 验证景区素材下载底栏在窄屏及状态切换后仍能完整展示摘要并正确响应操作。
@MainActor
final class ScenicMaterialsFooterLayoutTests: XCTestCase {
/// 选择、下载、暂停、失败和完成状态均不裁切文字、进度或操作按钮。
func testFooterStatesFitCompactAndRegularWidths() throws {
for width in [320.0, 375.0, 393.0] {
let footer = ScenicMaterialsFooterView()
for state in 0..<6 {
apply(state, to: footer)
layout(footer, width: width)
XCTAssertGreaterThan(footer.bounds.height, 0)
let primary: UIButton = try control("scenic.materials.primary", in: footer)
XCTAssertGreaterThanOrEqual(primary.bounds.height, 44)
try assertCompleteSingleLineTitle(primary)
let visibleControls = allViews(footer).filter {
($0.accessibilityIdentifier?.hasPrefix("scenic.materials.") == true) && isVisible($0, within: footer)
}
for view in visibleControls {
let rect = view.convert(view.bounds, to: footer)
XCTAssertGreaterThan(rect.width, 0, "状态 \(state):\(view.accessibilityIdentifier ?? "") 无宽度")
XCTAssertGreaterThan(rect.height, 0)
XCTAssertGreaterThanOrEqual(rect.minX, -0.5)
XCTAssertGreaterThanOrEqual(rect.minY, -0.5)
XCTAssertLessThanOrEqual(rect.maxX, width + 0.5)
XCTAssertLessThanOrEqual(rect.maxY, footer.bounds.maxY + 0.5)
if let label = view as? UILabel, !(label.text ?? "").isEmpty {
let needed = label.sizeThatFits(CGSize(width: label.bounds.width, height: .greatestFiniteMagnitude))
XCTAssertLessThanOrEqual(needed.height, label.bounds.height + 1, "状态 \(state):文字被裁切")
}
if let button = view as? UIButton { try assertCompleteSingleLineTitle(button) }
}
}
}
}
/// 真正挂载窗口后,初始完成态及从选择态切入完成态都显示完整按钮文字,防止内部标题被约束挤成窄列。
func testButtonTitlesStayReadableInWindowAcrossInitialAndTransitionedStates() async throws {
for width in [320.0, 375.0, 393.0] {
for states in [[5], [0, 5], [1, 2, 3, 4, 5, 1, 5]] {
let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
.first { $0.activationState == .foregroundActive }
?? UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first
let 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
let controller = UIViewController()
let footer = ScenicMaterialsFooterView()
// 在入窗前设置首个状态,覆盖直接打开“下载完成”演示时的初次布局。
apply(try XCTUnwrap(states.first), to: footer)
window.rootViewController = controller
controller.loadViewIfNeeded()
controller.view.addSubview(footer)
footer.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
footer.widthAnchor.constraint(equalToConstant: width),
footer.centerXAnchor.constraint(equalTo: controller.view.centerXAnchor),
footer.bottomAnchor.constraint(equalTo: controller.view.bottomAnchor)
])
window.makeKeyAndVisible()
defer {
window.isHidden = true
window.rootViewController = nil
previousKeyWindow?.makeKeyAndVisible()
}
for state in states {
apply(state, to: footer)
window.setNeedsLayout()
window.layoutIfNeeded()
try await Task.sleep(nanoseconds: 20_000_000)
controller.view.layoutIfNeeded()
footer.layoutIfNeeded()
XCTAssertTrue(footer.window === window)
XCTAssertEqual(footer.bounds.width, width, accuracy: 0.5)
let primary: UIButton = try control("scenic.materials.primary", in: footer)
try assertCompleteSingleLineTitle(primary)
if let secondary = allViews(footer).first(where: {
$0.accessibilityIdentifier == "scenic.materials.secondary"
}) as? UIButton, isVisible(secondary, within: footer) {
try assertCompleteSingleLineTitle(secondary)
}
if state == 5 {
XCTAssertEqual(primary.titleLabel?.text, "继续选择素材")
XCTAssertGreaterThan(primary.bounds.width, width * 0.8)
}
}
}
}
}
/// 下载摘要和取消位于同一行,进度条与通栏按钮依次放在其下方。
func testDownloadingKeepsCancelBesideSummaryAndPrimaryBelowProgress() throws {
let footer = ScenicMaterialsFooterView()
apply(2, to: footer)
layout(footer, width: 375)
let title: UILabel = try control("scenic.materials.footer.title", in: footer)
let cancel: UIButton = try control("scenic.materials.secondary", in: footer)
let progress: UIProgressView = try control("scenic.materials.footer.progress", in: footer)
let primary: UIButton = try control("scenic.materials.primary", in: footer)
let titleRect = title.convert(title.bounds, to: footer)
let cancelRect = cancel.convert(cancel.bounds, to: footer)
let progressRect = progress.convert(progress.bounds, to: footer)
let primaryRect = primary.convert(primary.bounds, to: footer)
XCTAssertEqual(titleRect.midY, cancelRect.midY, accuracy: 1)
XCTAssertLessThanOrEqual(titleRect.maxX, cancelRect.minX)
XCTAssertLessThanOrEqual(max(titleRect.maxY, cancelRect.maxY), progressRect.minY)
XCTAssertLessThan(progressRect.maxY, primaryRect.minY)
XCTAssertEqual(primaryRect.minX, progressRect.minX, accuracy: 1)
XCTAssertEqual(primaryRect.maxX, progressRect.maxX, accuracy: 1)
}
/// 失败时继续选择和重试并排;完成后仅保留通栏主操作,回到选择态不残留旧进度。
func testResultAndSelectionTransitionsRemoveStaleControls() throws {
let footer = ScenicMaterialsFooterView()
apply(1, to: footer)
layout(footer, width: 320)
let selectionHeight = footer.bounds.height
for _ in 0..<3 {
apply(2, to: footer)
layout(footer, width: 320)
let progress: UIProgressView = try control("scenic.materials.footer.progress", in: footer)
apply(4, to: footer)
layout(footer, width: 320)
let secondary: UIButton = try control("scenic.materials.secondary", in: footer)
let primary: UIButton = try control("scenic.materials.primary", in: footer)
let secondaryRect = secondary.convert(secondary.bounds, to: footer)
let primaryRect = primary.convert(primary.bounds, to: footer)
XCTAssertTrue(isVisible(secondary, within: footer))
XCTAssertEqual(secondaryRect.midY, primaryRect.midY, accuracy: 1)
XCTAssertLessThan(secondaryRect.maxX, primaryRect.minX)
XCTAssertEqual(secondaryRect.width, primaryRect.width, accuracy: 1)
apply(5, to: footer)
layout(footer, width: 320)
XCTAssertFalse(isVisible(secondary, within: footer))
let detail: UILabel = try control("scenic.materials.footer.detail", in: footer)
XCTAssertTrue(detail.text?.contains("未写入系统相册") == true)
XCTAssertGreaterThan(primary.bounds.width, primaryRect.width * 1.8)
apply(1, to: footer)
layout(footer, width: 320)
XCTAssertEqual(footer.bounds.height, selectionHeight, accuracy: 1)
XCTAssertEqual(primary.title(for: .normal), "下载所选(2)")
let activity: UIActivityIndicatorView = try control("scenic.materials.footer.activity", in: footer)
XCTAssertFalse(isVisible(secondary, within: footer))
XCTAssertFalse(isVisible(progress, within: footer))
XCTAssertFalse(activity.isAnimating)
XCTAssertFalse(isVisible(activity, within: footer))
}
}
/// 忙碌时主操作禁用而取消可用;暂停及失败状态继续派发各自的操作回调。
func testBusyPrimaryCannotDispatchButCancelAndResumeCan() throws {
let footer = ScenicMaterialsFooterView()
var primaryCalls = 0
var secondaryCalls = 0
footer.onPrimaryTap = { primaryCalls += 1 }
footer.onSecondaryTap = { secondaryCalls += 1 }
apply(2, to: footer)
layout(footer, width: 393)
let primary: UIButton = try control("scenic.materials.primary", in: footer)
let secondary: UIButton = try control("scenic.materials.secondary", in: footer)
let activity: UIActivityIndicatorView = try control("scenic.materials.footer.activity", in: footer)
XCTAssertFalse(primary.isEnabled)
XCTAssertEqual(primary.backgroundColor, ScenicMaterialsStyle.primary)
XCTAssertTrue(activity.isAnimating)
XCTAssertTrue(secondary.isEnabled)
primary.sendActions(for: .touchUpInside)
secondary.sendActions(for: .touchUpInside)
XCTAssertEqual(primaryCalls, 0)
XCTAssertEqual(secondaryCalls, 1)
for state in [3, 4] {
apply(state, to: footer)
layout(footer, width: 393)
XCTAssertTrue(primary.isEnabled)
XCTAssertFalse(activity.isAnimating)
primary.sendActions(for: .touchUpInside)
secondary.sendActions(for: .touchUpInside)
}
XCTAssertEqual(primaryCalls, 2)
XCTAssertEqual(secondaryCalls, 3)
}
private func apply(_ state: Int, to footer: ScenicMaterialsFooterView) {
switch state {
case 0, 1:
footer.configure(title: "已选 \(state == 0 ? 0 : 2) 段", detail: "本地演示 · 不下载或保存真实文件",
primaryTitle: state == 0 ? "下载所选" : "下载所选(2)", primaryEnabled: state == 1,
layout: .selection)
case 2:
footer.configure(title: "下载中 · 0/2 段", detail: nil, primaryTitle: "正在下载…", primaryEnabled: false,
secondaryTitle: "取消", progress: 0.35, layout: .progress, isBusy: true)
case 3:
footer.configure(title: "已暂停 · 已完成 1/2 段", detail: "已完成项已保留,请手动继续",
primaryTitle: "继续下载", primaryEnabled: true, secondaryTitle: "取消",
progress: 0.5, layout: .progress)
case 4:
footer.configure(title: "完成 1 段 · 失败 1 段", detail: "演示结果 · 未保存真实文件",
primaryTitle: "重试失败(1)", primaryEnabled: true, secondaryTitle: "继续选择",
layout: .result)
default:
footer.configure(title: "演示完成 2 段", detail: "未写入系统相册", primaryTitle: "继续选择素材",
primaryEnabled: true, layout: .result)
}
}
private func layout(_ footer: UIView, width: Double) {
let size = footer.systemLayoutSizeFitting(CGSize(width: width, height: 1),
withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel)
footer.frame = CGRect(origin: .zero, size: size)
footer.setNeedsLayout()
footer.layoutIfNeeded()
}
private func control<T: UIView>(_ identifier: String, in root: UIView) throws -> T {
try XCTUnwrap(allViews(root).first { $0.accessibilityIdentifier == identifier } as? T)
}
private func assertCompleteSingleLineTitle(_ button: UIButton,
file: StaticString = #filePath, line: UInt = #line) throws {
let label = try XCTUnwrap(button.titleLabel, file: file, line: line)
let title = try XCTUnwrap(button.currentTitle, file: file, line: line)
let font = try XCTUnwrap(label.font, file: file, line: line)
let measured = (title as NSString).size(withAttributes: [.font: font])
let rect = label.convert(label.bounds, to: button)
XCTAssertEqual(label.text, title, file: file, line: line)
XCTAssertEqual(label.numberOfLines, 1, "按钮应单行完整显示:\(title)", file: file, line: line)
XCTAssertTrue(isVisible(label, within: button), file: file, line: line)
XCTAssertGreaterThanOrEqual(rect.width + 1, measured.width, "按钮标题被挤窄:\(title)", file: file, line: line)
XCTAssertGreaterThanOrEqual(rect.height + 1, font.lineHeight, file: file, line: line)
XCTAssertGreaterThanOrEqual(rect.minX, -0.5, file: file, line: line)
XCTAssertGreaterThanOrEqual(rect.minY, -0.5, file: file, line: line)
XCTAssertLessThanOrEqual(rect.maxX, button.bounds.maxX + 0.5, file: file, line: line)
XCTAssertLessThanOrEqual(rect.maxY, button.bounds.maxY + 0.5, file: file, line: line)
let fitted = label.sizeThatFits(CGSize(width: rect.width, height: .greatestFiniteMagnitude))
XCTAssertLessThanOrEqual(fitted.height, font.lineHeight + 1, "按钮标题被挤成多行:\(title)", file: file, line: line)
}
private func allViews(_ view: UIView) -> [UIView] { [view] + view.subviews.flatMap { allViews($0) } }
private func isVisible(_ view: UIView, within root: UIView) -> Bool {
var current: UIView? = view
while let candidate = current {
if candidate.isHidden || candidate.alpha == 0 { return false }
if candidate === root { return true }
current = candidate.superview
}
return false
}
}