阅读器修改

This commit is contained in:
shenlei
2026-10-23 19:29:45 +09:00
parent e59bad9abf
commit 4388831abb
14 changed files with 536 additions and 81 deletions
@@ -26,7 +26,8 @@ extension XCUIApplication {
allowsInspectableWebViews: Bool = false,
enablesVerboseWebLogs: Bool = false,
searchKeyword: String? = nil,
pdfTextSource: String? = nil
pdfTextSource: String? = nil,
pdfSpeechWhitePageProbe: Int? = nil
) {
var args = ["--demo-book-title", bookTitleQuery]
if resetsReaderState {
@@ -62,6 +63,9 @@ extension XCUIApplication {
if let pdfTextSource {
args += ["--demo-pdf-text-source", pdfTextSource]
}
if let pdfSpeechWhitePageProbe {
args += ["--demo-pdf-speech-whitepage-probe", "\(pdfSpeechWhitePageProbe)"]
}
launchArguments = args
launch()
}
@@ -205,4 +205,71 @@ final class LocationPersistenceTests: XCTestCase {
}
XCTAssertEqual(restoredState.page, 3, "字体变化后,通过高亮面板跳转仍应回到原始高亮页")
}
// MARK: - PDF 阅读进度恢复
//
// 回归点:`RDPDFReaderViewController.viewDidLoad` 里 `reloadData()` 会同步触发
// `pageNum(0)` → `saveReadingPage(0)`,把待恢复的页覆盖成 0,导致 `restoreReadingPage`
// 永远读到第 1 页。修复:`reloadData()` 前先读恢复页,恢复窗口内跳过写盘。
// 前置:Demo 侧 `PDFDemoReaderViewController` 现已把 `RDPDFReaderPersistenceStore`
// 同时作为 `persistence:` 传入(此前只传了 `annotationPersistence:`)。
func testPDFReadingPositionRestoredOnReopen() throws {
app.launchAndOpenSampleBook(
bookTitleQuery: "英汉大词典",
pageNumber: 4,
resetsReaderState: true,
pdfTextSource: "none"
)
app.waitForReader(timeout: 30)
let moved = app.waitForDemoReaderState(timeout: 20, description: "PDF 跳到第 4 页") { $0.page == 4 }
XCTAssertEqual(moved.page, 4)
// 翻页写盘有 1s 防抖,再留出后台 IO 队列写文件的时间。
RunLoop.current.run(until: Date().addingTimeInterval(2.5))
app.showReaderChromeIfNeeded()
let back = app.buttons[IDs.readerBack]
XCTAssertTrue(back.waitForExistence(timeout: 5), "返回按钮应出现")
back.tap()
XCTAssertTrue(app.tables[IDs.demoBooksTable].waitForExistence(timeout: 8), "应返回书库列表")
RunLoop.current.run(until: Date().addingTimeInterval(1.5))
// 重开必须保留状态,displayType 不传:避免 Demo 的 applyDemoDisplayType 把
// 状态标签重置成 page=1 干扰断言。
app.launchAndOpenSampleBook(
bookTitleQuery: "英汉大词典",
resetsReaderState: false,
pdfTextSource: "none"
)
app.waitForReader(timeout: 30)
let restored = app.waitForDemoReaderState(timeout: 15, description: "PDF 恢复到第 4 页") { $0.page == 4 }
XCTAssertEqual(restored.page, 4, "重开 PDF 应恢复上次阅读页,而不是被开局 reloadData 覆盖成第 1 页")
}
func testPDFResetStateClearsPersistedReadingPage() throws {
app.launchAndOpenSampleBook(
bookTitleQuery: "英汉大词典",
pageNumber: 5,
resetsReaderState: true,
pdfTextSource: "none"
)
app.waitForReader(timeout: 30)
app.waitForDemoReaderState(timeout: 20, description: "PDF 跳到第 5 页") { $0.page == 5 }
RunLoop.current.run(until: Date().addingTimeInterval(2.5))
app.showReaderChromeIfNeeded()
let back = app.buttons[IDs.readerBack]
if back.waitForExistence(timeout: 5) { back.tap() }
RunLoop.current.run(until: Date().addingTimeInterval(1.0))
app.launchAndOpenSampleBook(
bookTitleQuery: "英汉大词典",
resetsReaderState: true,
pdfTextSource: "none"
)
app.waitForReader(timeout: 30)
let state = app.waitForDemoReaderState(timeout: 15, description: "reset 后回到第 1 页") { $0.page != nil }
XCTAssertEqual(state.page, 1, "--demo-reset-state 应清掉 reader-state.json,重开回到第 1 页")
}
}
@@ -35,11 +35,12 @@ final class PDFPageTurnBlankPageTests: XCTestCase {
// MARK: - 步骤
private func openDictionaryPDF(displayType: String) {
private func openDictionaryPDF(displayType: String, textSource: String? = nil) {
app.launchAndOpenSampleBook(
bookTitleQuery: "英汉大词典",
displayType: displayType,
resetsReaderState: true
resetsReaderState: true,
pdfTextSource: textSource
)
app.waitForReader(timeout: 30)
app.waitForReaderPage(1, timeout: 30)
@@ -144,4 +145,108 @@ final class PDFPageTurnBlankPageTests: XCTestCase {
}
return Double(darkCount) / Double(side * side)
}
// MARK: - 主题切换:变换缓存失效正确性
//
// 回归点:`pageImageTransform` 结果现按 (页码, 主题 id) 缓存。若失效点漏接,
// 切主题 / 翻页 / 内存回收后会显示上一个主题算出的陈旧帧。此用例churn 页面并
// 在夜间/浅色间往返,确认没有陈旧帧、也没有因缓存导致的白页。
// 注:XCUITest 无法可靠地测毫秒级耗时;这里以“缓存正确性”作为性能修复的回归护栏。
func testDarkThemeSurvivesPageChurnWithoutStaleOrBlankFrames() {
openDictionaryPDF(displayType: "horizontalscroll", textSource: "none")
let lightRatio = currentInkRatio()
XCTAssertGreaterThan(lightRatio, blankInkRatioThreshold, "首页不应是白页")
XCTAssertLessThan(lightRatio, 0.35, "浅色主题下正文页不应接近全黑(ink=\(lightRatio))")
switchTheme(toIdentifier: 5) // 夜间:contentBackgroundColor 亮度 < 0.5 → 整页反相
let darkRatio = currentInkRatio()
XCTAssertGreaterThan(darkRatio, 0.5, "切到夜间主题后正文页应被反相为深色(ink=\(darkRatio))")
app.hideReaderChromeIfNeeded()
// 前翻 3 页:每页首次 configure 命中/回填变换缓存;翻页同时触发
// trimPageDescriptorCache 的 renderedImageCache 失效路径。
for page in 2...4 {
turnPageAndWait(toPage: page, forward: true)
let r = currentInkRatio()
XCTAssertGreaterThan(r, blankInkRatioThreshold, "夜间主题前翻后出现白页(page \(page),ink=\(r))")
XCTAssertGreaterThan(r, 0.4, "夜间主题前翻后出现未反相的浅色帧(page \(page),ink=\(r))")
}
for page in stride(from: 3, through: 1, by: -1) {
turnPageAndWait(toPage: page, forward: false)
let r = currentInkRatio()
XCTAssertGreaterThan(r, 0.4, "夜间主题回翻后出现未反相的浅色帧(page \(page),ink=\(r))")
}
switchTheme(toIdentifier: 0) // 浅色
let restoredLight = currentInkRatio()
XCTAssertLessThan(
restoredLight, 0.35,
"切回浅色主题后应立即用浅色帧;陈旧的深色变换缓存未随 apply(theme) 清除(ink=\(restoredLight))"
)
}
// MARK: - 朗读预取遇到暂时无图的页,不应把该页永久变白
//
// 回归点:`speechTextRuns(at:)` 之前会无条件把 provider 返回的 descriptor 写进
// `pageDescriptors`,即使 `image == nil`。之后翻到该页时 `configure` 的
// `guard descriptor == nil else { … return }` 短路,页面永久白屏、也不重试。
// 修复:仅在 `descriptor.image != nil` 时写缓存。
func testSpeechPrefetchDoesNotBlankPageWhenImageArrivesLate() {
// probe=3:宿主 Provider 首次被请求 index 3(第 4 页)时返回 image=nil,
// 之后返回真实图;阅读器加载后用 speechTextRuns 预取到 index 4。
app.launchAndOpenSampleBook(
bookTitleQuery: "英汉大词典",
displayType: "horizontalscroll",
resetsReaderState: true,
pdfTextSource: "none",
pdfSpeechWhitePageProbe: 3
)
app.waitForReader(timeout: 30)
app.waitForReaderPage(1, timeout: 30)
_ = app.waitForDemoReaderState(timeout: 30, description: "朗读预取完成") { $0["speechProbe"] == "done" }
app.hideReaderChromeIfNeeded()
// 翻到第 4 页(index 3)——被预取“污染”的那一页。
for page in 2...4 {
turnPageAndWait(toPage: page, forward: true)
}
let ratio = currentInkRatio()
XCTAssertGreaterThan(
ratio, blankInkRatioThreshold,
"朗读预取到暂时无图的页后,翻到该页仍是白页(ink=\(ratio))——speechTextRuns 不应缓存无图 descriptor"
)
}
// MARK: - 辅助
private func currentInkRatio() -> Double {
RunLoop.current.run(until: Date().addingTimeInterval(0.4))
return inkRatio(of: XCUIScreen.main.screenshot().image)
}
private func turnPageAndWait(toPage page: Int, forward: Bool) {
turnPage(forward: forward)
app.waitForDemoReaderState(timeout: 15, description: "page=\(page)") { $0.page == page }
// 翻页动画 + 异步页面图装配完成后再取样。
RunLoop.current.run(until: Date().addingTimeInterval(1.2))
}
private func switchTheme(toIdentifier identifier: Int) {
app.showReaderChromeIfNeeded()
let settings = app.buttons[IDs.readerSettings]
XCTAssertTrue(settings.waitForExistence(timeout: 5), "设置按钮应存在")
settings.tap()
let themeButton = app.buttons[IDs.settingsTheme(identifier)]
XCTAssertTrue(themeButton.waitForExistence(timeout: 5), "主题按钮 \(identifier) 应存在")
themeButton.tap()
let done = app.buttons[IDs.settingsDone]
if done.waitForExistence(timeout: 3) { done.tap() }
// 面板关闭动画 + 当前页按新主题重绘。
RunLoop.current.run(until: Date().addingTimeInterval(1.0))
}
}