import XCTest final class LocationPersistenceTests: XCTestCase { private let app = XCUIApplication() override func setUpWithError() throws { continueAfterFailure = false } func testReadingPositionRestoredOnReopen() throws { app.launchAndOpenSampleBook() app.waitForReader(timeout: 12) _ = app.waitForDemoReaderState(timeout: 5, description: "存在页码") { $0.page != nil } let paging = app.collectionViews[IDs.readerPaging] if paging.waitForExistence(timeout: 5) { paging.swipeLeft() } let stateAfterSwipe = app.waitForDemoReaderState(timeout: 8, description: "翻页后页码变化") { state in (state.page ?? 0) > 1 } let pageAfterSwipe = stateAfterSwipe.page ?? 0 guard pageAfterSwipe > 1 else { throw XCTSkip("当前翻页模式下未能翻到第 2 页") } let backButton = app.buttons[IDs.readerBack] if backButton.waitForExistence(timeout: 3) { backButton.tap() } app.launchAndOpenSampleBook() app.waitForReader(timeout: 12) let restoredState = app.waitForDemoReaderState(timeout: 8, description: "恢复阅读位置") { state in state.page != nil } let restoredPage = restoredState.page ?? 0 XCTAssertEqual(restoredPage, pageAfterSwipe, "重新打开后应恢复到上次阅读页码") } func testResetStateLaunchesAtFirstPage() throws { app.launchAndOpenSampleBook(resetsReaderState: true) app.waitForReader(timeout: 12) let state = app.waitForDemoReaderState(timeout: 5, description: "首开第 1 页") { state in state.page != nil } let page = state.page ?? 0 XCTAssertEqual(page, 1, "重置状态后应从第 1 页开始") } func testPositionSurvivesFontChange() throws { app.launchAndOpenSampleBook() app.waitForReader(timeout: 12) let paging = app.collectionViews[IDs.readerPaging] if paging.waitForExistence(timeout: 5) { paging.swipeLeft() } app.waitForDemoReaderState(timeout: 5, description: "page exists") { $0.page != nil } app.showReaderChromeIfNeeded() if app.buttons[IDs.readerSettings].waitForExistence(timeout: 3) { app.buttons[IDs.readerSettings].tap() } if app.buttons[IDs.settingsFontIncrease].waitForExistence(timeout: 5) { app.buttons[IDs.settingsFontIncrease].tap() } if app.buttons[IDs.settingsDone].waitForExistence(timeout: 3) { app.buttons[IDs.settingsDone].tap() } let backButton = app.buttons[IDs.readerBack] if backButton.waitForExistence(timeout: 3) { backButton.tap() } app.launchAndOpenSampleBook() app.waitForReader(timeout: 12) app.waitForDemoReaderState(timeout: 8, description: "reader=opened") { $0.isOpened } } func testBookmarkAndHighlightPersistAcrossReopen() throws { app.launchAndOpenSampleBook(resetsReaderState: true) app.waitForReader(timeout: 12) app.showReaderChromeIfNeeded() XCTAssertTrue(app.buttons[IDs.readerBookmark].waitForExistence(timeout: 3)) app.buttons[IDs.readerBookmark].tap() app.waitForDemoReaderState(timeout: 5, description: "bookmarks=1") { $0.bookmarks == 1 } app.hideReaderChromeIfNeeded() let content = app.otherElements[IDs.readerSelectionText].firstMatch XCTAssertTrue(content.waitForExistence(timeout: 5)) let start = content.coordinate(withNormalizedOffset: CGVector(dx: 0.2, dy: 0.5)) let end = content.coordinate(withNormalizedOffset: CGVector(dx: 0.55, dy: 0.5)) start.press(forDuration: 0.8, thenDragTo: end) XCTAssertTrue(app.menuItems["高亮"].waitForExistence(timeout: 3)) app.menuItems["高亮"].tap() app.waitForDemoReaderState(timeout: 8, description: "highlights=1") { $0.highlights == 1 } app.showReaderChromeIfNeeded() app.buttons[IDs.readerBack].tap() app.launchAndOpenSampleBook(resetsReaderState: false) app.waitForReader(timeout: 12) let state = app.waitForDemoReaderState(timeout: 8, description: "bookmark/highlight restored") { state in state.bookmarks == 1 && state.highlights == 1 } XCTAssertEqual(state.bookmarks, 1) XCTAssertEqual(state.highlights, 1) } }