feat: 接入拍摄任务实时订阅并完善日志与封面
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
import Kingfisher
|
||||
import UIKit
|
||||
import XCTest
|
||||
@testable import suixinkan
|
||||
|
||||
/// 真实响应缺点位封面时,验证同点位样片回退与卡片复用;图片只读取预置内存缓存。
|
||||
@MainActor
|
||||
final class DroneDockPointCoverTests: XCTestCase {
|
||||
/// 点位封面优先于样片,去掉外层空白后保留 URL 查询参数。
|
||||
func testPointCoverTakesPriorityOverSampleCover() throws {
|
||||
let url = "https://images.example.invalid/spot.png?signature=test-value"
|
||||
let spot = try makeSpot(cover: " \n" + url + " \n", sampleCovers: ["https://images.example.invalid/sample.png"])
|
||||
XCTAssertEqual(spot.displayCoverURL?.absoluteString, url)
|
||||
XCTAssertEqual(spot.coverURL, " \n" + url + " \n", "不能改写原始接口字段")
|
||||
}
|
||||
|
||||
/// 兼容实际空字符串及缺失、null、无效协议,按航线顺序寻找第一张有效封面。
|
||||
func testMissingOrInvalidPointCoverFallsBackToFirstValidSample() throws {
|
||||
for cover: Any? in [nil, NSNull(), "", " \n", "/relative.png", "file:///tmp/local.png", "https://"] {
|
||||
let spot = try makeSpot(cover: cover, sampleCovers: ["", "file:///tmp/sample.png",
|
||||
" https://images.example.invalid/first.png ", "https://images.example.invalid/second.png"])
|
||||
XCTAssertEqual(spot.displayCoverURL?.absoluteString, "https://images.example.invalid/first.png")
|
||||
}
|
||||
let spot = try decode([
|
||||
"scenic_spot_id": 201, "scenic_spot_name": "测试点", "cover_url": "", "air_lines": [
|
||||
["air_line_id": 301, "air_line_name": "第一条", "samples": [["id": 401, "cover_url": ""]]],
|
||||
["air_line_id": 302, "air_line_name": "第二条", "samples": [["id": 402, "cover_url": "https://images.example.invalid/next-route.png"]]]
|
||||
]
|
||||
])
|
||||
XCTAssertEqual(spot.displayCoverURL?.lastPathComponent, "next-route.png")
|
||||
}
|
||||
|
||||
/// 无有效封面时保持占位,不把预览视频当图片,也不会借用其他点位图片。
|
||||
func testMissingCoverDoesNotUsePreviewOrAnotherPoint() throws {
|
||||
let empty = try decode([
|
||||
"scenic_spot_id": 201, "scenic_spot_name": "缺图点", "air_lines": [[
|
||||
"air_line_id": 301, "air_line_name": "测试航线", "samples": [[
|
||||
"id": 401, "preview_url": "https://images.example.invalid/video.mp4"
|
||||
]]
|
||||
]]
|
||||
])
|
||||
let other = try makeSpot(id: 202, sampleCovers: ["https://images.example.invalid/other.png"])
|
||||
XCTAssertNil(empty.displayCoverURL)
|
||||
XCTAssertEqual(other.displayCoverURL?.lastPathComponent, "other.png")
|
||||
XCTAssertNil(try makeSpot().displayCoverURL)
|
||||
}
|
||||
|
||||
/// 同一个卡片依次显示两个点位各自的缓存封面,切到缺图点或回收时清除旧图片与加载身份。
|
||||
func testCellDisplaysMatchingSampleImagesAndClearsThemOnReuse() async throws {
|
||||
let key = UUID().uuidString
|
||||
let firstURL = "https://images.example.invalid/\(key)-first.png"
|
||||
let secondURL = "https://images.example.invalid/\(key)-second.png"
|
||||
let firstImage = image(color: .red)
|
||||
let secondImage = image(color: .blue)
|
||||
ImageCache.default.store(firstImage, forKey: firstURL, toDisk: false, completionHandler: nil)
|
||||
ImageCache.default.store(secondImage, forKey: secondURL, toDisk: false, completionHandler: nil)
|
||||
defer {
|
||||
ImageCache.default.removeImage(forKey: firstURL, fromDisk: false)
|
||||
ImageCache.default.removeImage(forKey: secondURL, fromDisk: false)
|
||||
}
|
||||
XCTAssertNotNil(ImageCache.default.retrieveImageInMemoryCache(forKey: firstURL))
|
||||
XCTAssertNotNil(ImageCache.default.retrieveImageInMemoryCache(forKey: secondURL))
|
||||
|
||||
let cell = DronePreparationPointCell(frame: CGRect(x: 0, y: 0, width: 200, height: 64))
|
||||
let thumbnail = try XCTUnwrap(cell.contentView.subviews.first {
|
||||
$0.accessibilityIdentifier == "drone.preparation.point.cover"
|
||||
} as? UIImageView)
|
||||
cell.apply(spot: try makeSpot(cover: "", sampleCovers: [firstURL]), selected: true)
|
||||
try await assertImage(firstImage, in: thumbnail)
|
||||
cell.apply(spot: try makeSpot(id: 202, cover: "", sampleCovers: [secondURL]), selected: false)
|
||||
try await assertImage(secondImage, in: thumbnail)
|
||||
XCTAssertEqual(thumbnail.contentMode, .scaleAspectFill)
|
||||
|
||||
cell.apply(spot: try makeSpot(id: 203), selected: false)
|
||||
XCTAssertNil(thumbnail.kf.taskIdentifier)
|
||||
XCTAssertEqual(thumbnail.contentMode, .center)
|
||||
XCTAssertNotNil(thumbnail.image)
|
||||
XCTAssertNotEqual(thumbnail.image?.pngData(), secondImage.pngData())
|
||||
cell.prepareForReuse()
|
||||
XCTAssertNil(thumbnail.image)
|
||||
XCTAssertNil(thumbnail.kf.taskIdentifier)
|
||||
}
|
||||
|
||||
private func makeSpot(id: Int = 201, cover: Any? = nil, sampleCovers: [String] = []) throws -> DroneDockPurchasedSpot {
|
||||
var fields: [String: Any] = ["scenic_spot_id": id, "scenic_spot_name": "测试点\(id)", "available": true,
|
||||
"air_lines": [["air_line_id": 301, "air_line_name": "测试航线", "samples": sampleCovers.enumerated().map {
|
||||
["id": 401 + $0.offset, "cover_url": $0.element] as [String: Any]
|
||||
}]]]
|
||||
fields["cover_url"] = cover
|
||||
return try decode(fields)
|
||||
}
|
||||
|
||||
private func decode(_ fields: [String: Any]) throws -> DroneDockPurchasedSpot {
|
||||
try JSONDecoder().decode(DroneDockPurchasedSpot.self, from: JSONSerialization.data(withJSONObject: fields))
|
||||
}
|
||||
|
||||
private func image(color: UIColor) -> UIImage {
|
||||
UIGraphicsImageRenderer(size: CGSize(width: 48, height: 48)).image { context in
|
||||
color.setFill()
|
||||
context.fill(CGRect(x: 0, y: 0, width: 48, height: 48))
|
||||
}
|
||||
}
|
||||
|
||||
private func assertImage(_ expected: UIImage, in view: UIImageView) async throws {
|
||||
for _ in 0..<20 {
|
||||
if view.image?.pngData() == expected.pngData() { return }
|
||||
try await Task.sleep(nanoseconds: 50_000_000)
|
||||
}
|
||||
XCTFail("卡片应显示当前点位对应的样片封面")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user