feat: EPUB 阅读器搜索、选中注释、书签 chrome 状态及大量重构优化
- 新增 RDEPUBReaderSearchCoordinator 与 RDEPUBSelectionState 管理搜索和选中状态 - 新增 BookmarkChromeStateTests、NavigationBackwardTests、SelectionAnnotateTests 等 UI 测试 - 新增多个边界测试 epub 样本(损坏结构、空归档、缺失文件、流式外链验证) - 重构阅读器 chrome 状态管理,统一 tool bar 与 search bar 交互 - 优化大书分页缓存策略(RDEPUBChapterSummaryDiskCache、RDEPUBPageCountCache) - 移除废弃的 RDEPUBLocationConverter 和 RDEPUBPageBreakPolicy - 更新 epub-bridge.js 与 JS bridge 通信协议 - 全面更新现有 UI 测试以适配新的 helper 和状态管理
This commit is contained in:
@@ -247,6 +247,189 @@ final class SearchTests: XCTestCase {
|
||||
XCTAssertNotEqual(countLabel.label, "0/0", "搜索状态应在工具栏恢复后保留")
|
||||
}
|
||||
|
||||
// MARK: - 搜索高亮字符验证
|
||||
|
||||
/// 搜索匹配文本应与搜索关键字一致
|
||||
/// 验证当前匹配项的高亮确实覆盖了搜索的关键字
|
||||
func testSearchMatchTextEqualsKeyword() {
|
||||
let keyword = "辽"
|
||||
app.launchAndOpenSampleBook(bookTitleQuery: "宝山辽墓材料与释读", searchKeyword: keyword)
|
||||
app.waitForReader()
|
||||
app.showReaderChromeIfNeeded()
|
||||
|
||||
// 等待搜索结果
|
||||
let countLabel = app.staticTexts[IDs.searchCount]
|
||||
XCTAssertTrue(countLabel.waitForExistence(timeout: 10), "搜索计数应出现")
|
||||
XCTAssertNotEqual(countLabel.label, "0/0", "关键词 '\(keyword)' 应有匹配")
|
||||
|
||||
// 验证当前匹配的文本就是搜索关键字
|
||||
let state = app.waitForDemoReaderState(timeout: 5, description: "searchMatchText exists") {
|
||||
$0.searchMatchText != nil && $0.searchMatchText != "none"
|
||||
}
|
||||
XCTAssertEqual(state.searchMatchText, keyword,
|
||||
"当前搜索匹配文本应为 '\(keyword)',实际:\(state.searchMatchText ?? "nil")")
|
||||
}
|
||||
|
||||
/// 重排后搜索匹配文本仍应与关键字一致
|
||||
func testSearchMatchTextAfterRepagination() {
|
||||
let keyword = "辽"
|
||||
app.launchAndOpenSampleBook(bookTitleQuery: "宝山辽墓材料与释读", searchKeyword: keyword)
|
||||
app.waitForReader()
|
||||
app.showReaderChromeIfNeeded()
|
||||
|
||||
// 等待搜索结果
|
||||
let countLabel = app.staticTexts[IDs.searchCount]
|
||||
XCTAssertTrue(countLabel.waitForExistence(timeout: 10), "搜索计数应出现")
|
||||
|
||||
// 改变字体触发重排
|
||||
app.showReaderChromeIfNeeded()
|
||||
let settingsButton = app.buttons[IDs.readerSettings]
|
||||
XCTAssertTrue(settingsButton.waitForExistence(timeout: 3))
|
||||
settingsButton.tap()
|
||||
|
||||
let fontIncrease = app.buttons[IDs.settingsFontIncrease]
|
||||
XCTAssertTrue(fontIncrease.waitForExistence(timeout: 3))
|
||||
fontIncrease.tap()
|
||||
fontIncrease.tap()
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
|
||||
let doneButton = app.buttons[IDs.settingsDone]
|
||||
if doneButton.waitForExistence(timeout: 3) {
|
||||
doneButton.tap()
|
||||
}
|
||||
Thread.sleep(forTimeInterval: 1)
|
||||
|
||||
// 重新打开搜索并导航到匹配
|
||||
app.showReaderChromeIfNeeded()
|
||||
let prevButton = app.buttons[IDs.searchPrevious]
|
||||
let nextButton = app.buttons[IDs.searchNext]
|
||||
if prevButton.waitForExistence(timeout: 3), prevButton.isEnabled {
|
||||
prevButton.tap()
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
}
|
||||
if nextButton.waitForExistence(timeout: 3), nextButton.isEnabled {
|
||||
nextButton.tap()
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
}
|
||||
|
||||
// 验证重排后匹配文本仍正确
|
||||
let state = app.waitForDemoReaderState(timeout: 5, description: "searchMatchText after repagination") {
|
||||
$0.searchMatchText != nil && $0.searchMatchText != "none"
|
||||
}
|
||||
XCTAssertEqual(state.searchMatchText, keyword,
|
||||
"重排后搜索匹配文本应为 '\(keyword)',实际:\(state.searchMatchText ?? "nil")")
|
||||
}
|
||||
|
||||
// MARK: - 重排后搜索跳转正确性
|
||||
|
||||
/// 重排后搜索跳转页码应正确(多匹配覆盖)
|
||||
/// 回归:chapterOffset(for:) 曾优先使用 row/column 查表而非存储的 chapterOffset,
|
||||
/// 导致重新排版后搜索跳转到错误的页面
|
||||
func testSearchNavigationAfterRepagination() {
|
||||
app.launchAndOpenSampleBook(searchKeyword: "的")
|
||||
app.waitForReader()
|
||||
app.showReaderChromeIfNeeded()
|
||||
|
||||
// 等待搜索结果
|
||||
let countLabel = app.staticTexts[IDs.searchCount]
|
||||
XCTAssertTrue(countLabel.waitForExistence(timeout: 10), "搜索计数应出现")
|
||||
XCTAssertNotEqual(countLabel.label, "0/0", "关键词 '的' 应有匹配")
|
||||
|
||||
// 解析匹配总数
|
||||
let countText = countLabel.label
|
||||
let parts = countText.split(separator: "/")
|
||||
guard parts.count == 2, let total = Int(parts[1]), total >= 3 else {
|
||||
XCTSkip("需要至少 3 个匹配才能测试多页跳转")
|
||||
return
|
||||
}
|
||||
|
||||
// 改变字体大小触发重新排版
|
||||
app.showReaderChromeIfNeeded()
|
||||
let settingsButton = app.buttons[IDs.readerSettings]
|
||||
XCTAssertTrue(settingsButton.waitForExistence(timeout: 3), "设置按钮应存在")
|
||||
settingsButton.tap()
|
||||
|
||||
let fontIncrease = app.buttons[IDs.settingsFontIncrease]
|
||||
XCTAssertTrue(fontIncrease.waitForExistence(timeout: 3), "字号增大按钮应存在")
|
||||
fontIncrease.tap()
|
||||
fontIncrease.tap()
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
|
||||
let doneButton = app.buttons[IDs.settingsDone]
|
||||
if doneButton.waitForExistence(timeout: 3) {
|
||||
doneButton.tap()
|
||||
}
|
||||
Thread.sleep(forTimeInterval: 1)
|
||||
|
||||
// 测试多个匹配位置的跳转正确性
|
||||
let nextButton = app.buttons[IDs.searchNext]
|
||||
let prevButton = app.buttons[IDs.searchPrevious]
|
||||
XCTAssertTrue(nextButton.waitForExistence(timeout: 3), "下一个按钮应存在")
|
||||
XCTAssertTrue(prevButton.waitForExistence(timeout: 3), "上一个按钮应存在")
|
||||
|
||||
// 导航到第 2 个匹配,记录页码
|
||||
nextButton.tap()
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
let stateMatch2 = app.waitForDemoReaderState(timeout: 5, description: "match 2 page") {
|
||||
($0.page ?? 0) > 0
|
||||
}
|
||||
let pageAtMatch2 = stateMatch2.page ?? 1
|
||||
|
||||
// 翻到其他页
|
||||
let content = app.otherElements[IDs.readerContent].firstMatch
|
||||
if content.waitForExistence(timeout: 3) {
|
||||
for _ in 0..<4 {
|
||||
content.swipeLeft()
|
||||
Thread.sleep(forTimeInterval: 0.3)
|
||||
}
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
}
|
||||
|
||||
// 跳转到第 3 个匹配
|
||||
nextButton.tap()
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
let stateMatch3 = app.waitForDemoReaderState(timeout: 5, description: "match 3 page") {
|
||||
($0.page ?? 0) > 0
|
||||
}
|
||||
let pageAtMatch3 = stateMatch3.page ?? 1
|
||||
|
||||
// 再翻到其他页
|
||||
if content.waitForExistence(timeout: 3) {
|
||||
for _ in 0..<4 {
|
||||
content.swipeLeft()
|
||||
Thread.sleep(forTimeInterval: 0.3)
|
||||
}
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
}
|
||||
|
||||
// 跳回第 2 个匹配,验证页码不变
|
||||
prevButton.tap()
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
let stateBack2 = app.waitForDemoReaderState(timeout: 5, description: "back to match 2") {
|
||||
$0.page == pageAtMatch2
|
||||
}
|
||||
XCTAssertEqual(stateBack2.page, pageAtMatch2,
|
||||
"重排后搜索跳回第 2 个匹配应回到第 \(pageAtMatch2) 页,实际:\(stateBack2.page ?? -1)")
|
||||
|
||||
// 再翻到其他页
|
||||
if content.waitForExistence(timeout: 3) {
|
||||
for _ in 0..<4 {
|
||||
content.swipeLeft()
|
||||
Thread.sleep(forTimeInterval: 0.3)
|
||||
}
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
}
|
||||
|
||||
// 跳到第 3 个匹配,验证页码不变
|
||||
nextButton.tap()
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
let stateBack3 = app.waitForDemoReaderState(timeout: 5, description: "back to match 3") {
|
||||
$0.page == pageAtMatch3
|
||||
}
|
||||
XCTAssertEqual(stateBack3.page, pageAtMatch3,
|
||||
"重排后搜索跳到第 3 个匹配应回到第 \(pageAtMatch3) 页,实际:\(stateBack3.page ?? -1)")
|
||||
}
|
||||
|
||||
// MARK: - 辅助方法
|
||||
|
||||
private func openSearchBar() {
|
||||
|
||||
Reference in New Issue
Block a user