Sync build 64 manual reader and qualification tests
This commit is contained in:
1 parent
8236ca17c6
commit
4abc194001
5 files changed
+272
-76
No files matched your search
@@ -1,20 +1,21 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
import XCTest
|
||||
import PDFKit
|
||||
import SwiftUI
|
||||
import CryptoKit
|
||||
|
||||
@MainActor
|
||||
final class SoftwareLibraryManualTests: XCTestCase {
|
||||
private func fixture() throws -> (SoftwareLibraryAsset, Data) {
|
||||
private func fixture(id: String = "manual", pages: Int = 2, size: NSSize = NSSize(width: 300, height: 500)) throws -> (SoftwareLibraryAsset, Data) {
|
||||
let pdf = PDFDocument()
|
||||
for _ in 0..<2 {
|
||||
let image = NSImage(size: NSSize(width: 300, height: 500))
|
||||
image.lockFocus(); NSColor.white.setFill(); NSRect(x: 0, y: 0, width: 300, height: 500).fill(); image.unlockFocus()
|
||||
for _ in 0..<pages {
|
||||
let image = NSImage(size: size)
|
||||
image.lockFocus(); NSColor.white.setFill(); NSRect(origin: .zero, size: size).fill(); image.unlockFocus()
|
||||
pdf.insert(try XCTUnwrap(PDFPage(image: image)), at: pdf.pageCount)
|
||||
}
|
||||
let bytes = try XCTUnwrap(pdf.dataRepresentation())
|
||||
let hash = SHA256.hash(data: bytes).map { String(format: "%02x", $0) }.joined()
|
||||
var asset = SoftwareLibraryAsset(id: "manual", name: "Manual", kind: "manual", sourceURL: URL(string: "https://colorcomputerarchive.com/repo/Documents/Manuals/Games/Manual.pdf")!, sha256: hash, byteSize: Int64(bytes.count), credit: "Color Computer Archive", matchStatus: "accepted", matchReason: "Reviewed")
|
||||
var asset = SoftwareLibraryAsset(id: id, name: "Manual", kind: "manual", sourceURL: URL(string: "https://colorcomputerarchive.com/repo/Documents/Manuals/Games/Manual.pdf")!, sha256: hash, byteSize: Int64(bytes.count), credit: "Color Computer Archive", matchStatus: "accepted", matchReason: "Reviewed")
|
||||
asset.manualURL = URL(string: "https://vccplus.brianholman.com/dl/software/CoCoLibrary/Documents/Manuals/Games/Manual.pdf")
|
||||
return (asset, bytes)
|
||||
}
|
||||
@@ -98,6 +99,110 @@ final class SoftwareLibraryManualTests: XCTestCase {
|
||||
model.page = 999; model.goToPage(); XCTAssertEqual(model.page, 2)
|
||||
model.page = -1; model.goToPage(); XCTAssertEqual(model.page, 1)
|
||||
}
|
||||
func testFreshPDFStartsAtCoverAndRestoresOnlyTheSavedReadingPosition() async throws {
|
||||
// At PDFKit's default scale this viewport fits more than two of the
|
||||
// small pages. Its midpoint and the top of its first page can disagree.
|
||||
let (asset, bytes) = try fixture(pages: 4, size: NSSize(width: 320, height: 294))
|
||||
let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
||||
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
|
||||
defer { try? FileManager.default.removeItem(at: root) }
|
||||
try bytes.write(to: root.appendingPathComponent(try XCTUnwrap(asset.sha256) + ".pdf"))
|
||||
let key = "software-manual:" + asset.id
|
||||
let frame = NSRect(x: 0, y: 0, width: 900, height: 850)
|
||||
|
||||
func waitFor(_ message: String, _ condition: () -> Bool) async throws {
|
||||
let deadline = ContinuousClock.now + .seconds(8)
|
||||
while !condition() {
|
||||
guard ContinuousClock.now < deadline else {
|
||||
XCTFail(message)
|
||||
throw CocoaError(.fileReadUnknown)
|
||||
}
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
}
|
||||
}
|
||||
func attach(_ model: SoftwareLibraryManualModel) -> NSWindow {
|
||||
let window = NSWindow(contentRect: frame, styleMask: [.titled], backing: .buffered, defer: false)
|
||||
let host = NSHostingView(rootView: SoftwareLibraryManualView(model: model))
|
||||
window.contentView = host
|
||||
host.layoutSubtreeIfNeeded()
|
||||
window.displayIfNeeded()
|
||||
return window
|
||||
}
|
||||
|
||||
let saved: LibraryReadingPosition
|
||||
do {
|
||||
let model = SoftwareLibraryManualModel()
|
||||
let store = LibraryContinuityStore(directory: root)
|
||||
model.continuity = store
|
||||
let window = attach(model)
|
||||
defer { window.contentView = nil }
|
||||
XCTAssertNil(store.reading(for: key))
|
||||
model.open(asset, directory: root)
|
||||
try await waitFor("Fresh PDF did not fit its cover in the attached reader") {
|
||||
model.pdfView.window === window && model.pdfView.autoScales &&
|
||||
model.pdfView.scaleFactor > 1 && model.pdfView.currentDestination?.page != nil
|
||||
}
|
||||
XCTAssertEqual(model.page, 1)
|
||||
let document = try XCTUnwrap(model.document)
|
||||
XCTAssertTrue(model.pdfView.currentDestination?.page === document.page(at: 0))
|
||||
model.goToPage(model.page + 1)
|
||||
try await waitFor("Next skipped the second page") {
|
||||
model.page == 2 && model.pdfView.currentDestination?.page === document.page(at: 1)
|
||||
}
|
||||
model.goToPage(model.page - 1)
|
||||
try await waitFor("Previous did not return to the cover") {
|
||||
model.page == 1 && model.pdfView.currentDestination?.page === document.page(at: 0)
|
||||
}
|
||||
|
||||
model.pdfView.autoScales = false
|
||||
model.pdfView.scaleFactor = 4
|
||||
let second = try XCTUnwrap(document.page(at: 1))
|
||||
model.pdfView.go(to: PDFDestination(page: second, at: CGPoint(x: 60, y: 205)))
|
||||
try await waitFor("The user-selected location was not observed") { model.page == 2 }
|
||||
model.cancel()
|
||||
await store.flush()
|
||||
saved = try XCTUnwrap(store.reading(for: key))
|
||||
XCTAssertEqual(saved.page, 2)
|
||||
XCTAssertEqual(saved.autoScales, false)
|
||||
XCTAssertEqual(try XCTUnwrap(saved.zoom), 4, accuracy: 0.01)
|
||||
XCTAssertGreaterThan(saved.offset, 0.1)
|
||||
XCTAssertLessThan(saved.offset, 0.95)
|
||||
}
|
||||
|
||||
// Closing a different reader while its PDF has loaded but the view has
|
||||
// not been attached must not replace the last real position.
|
||||
let unopened = SoftwareLibraryManualModel()
|
||||
let previousStore = LibraryContinuityStore(directory: root)
|
||||
unopened.continuity = previousStore
|
||||
unopened.open(asset, directory: root)
|
||||
try await waitFor("Detached PDF failed to open") { unopened.document != nil }
|
||||
unopened.cancel()
|
||||
XCTAssertEqual(previousStore.reading(for: key), saved)
|
||||
|
||||
let restored = SoftwareLibraryManualModel()
|
||||
let reopenedStore = LibraryContinuityStore(directory: root)
|
||||
restored.continuity = reopenedStore
|
||||
XCTAssertEqual(reopenedStore.reading(for: key), saved)
|
||||
let window = attach(restored)
|
||||
defer { window.contentView = nil }
|
||||
restored.open(asset, directory: root)
|
||||
try await waitFor("Stored PDF page and zoom were lost during attachment") {
|
||||
restored.page == 2 && !restored.pdfView.autoScales &&
|
||||
abs(restored.pdfView.scaleFactor - 4) < 0.01
|
||||
}
|
||||
let (other, otherBytes) = try fixture(id: "another-manual", pages: 3)
|
||||
try otherBytes.write(to: root.appendingPathComponent(try XCTUnwrap(other.sha256) + ".pdf"))
|
||||
restored.open(other, directory: root)
|
||||
try await waitFor("Switching manuals did not open a fitted cover") {
|
||||
restored.readingReady && restored.page == 1 && restored.pdfView.autoScales
|
||||
}
|
||||
XCTAssertEqual(restored.document?.pageCount, 3)
|
||||
let reopened = try XCTUnwrap(reopenedStore.reading(for: key))
|
||||
XCTAssertEqual(reopened.page, saved.page)
|
||||
XCTAssertEqual(reopened.horizontalOffset, saved.horizontalOffset, accuracy: 0.01)
|
||||
XCTAssertEqual(reopened.offset, saved.offset, accuracy: 0.01)
|
||||
}
|
||||
|
||||
func testCompanionManualChecksRegularFileBeforeOpening() async throws {
|
||||
let (asset, bytes) = try fixture()
|
||||
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
||||
|
||||
Reference in new issue
Block a user