import XCTest /// 固定布局(pre-paginated)书籍的旋转回归: /// 每次旋转会使预加载缓存签名失效并批量丢弃 WKWebView 页面, /// 历史上曾因 script message handler 保留环导致 WebView 泄漏累积, /// 多次旋转后在 CA commit 阶段 abort(CA::Render::Encoder::grow)。 final class FixedLayoutRotationTests: XCTestCase { private let app = XCUIApplication() override func setUpWithError() throws { continueAfterFailure = false XCUIDevice.shared.orientation = .portrait } override func tearDownWithError() throws { XCUIDevice.shared.orientation = .portrait } func testFixedLayoutBookSurvivesRepeatedRotation() { app.launchAndOpenSampleBook(bookTitleQuery: "熊爷爷") app.waitForReader() for _ in 0..<3 { XCUIDevice.shared.orientation = .landscapeLeft RunLoop.current.run(until: Date().addingTimeInterval(2)) let landscapeState = app.waitForDemoReaderState( timeout: 5, description: "固定版式横屏保持单页" ) { $0.pagesPerScreen == 1 } XCTAssertEqual(landscapeState.pagesPerScreen, 1, "交互型固定版式书横屏不应启用双页") XCUIDevice.shared.orientation = .portrait RunLoop.current.run(until: Date().addingTimeInterval(2)) } XCTAssertEqual(app.state, .runningForeground, "反复旋转后 app 已退出(疑似崩溃)") app.waitForReader() } func testFixedLayoutCachedPageCanReturnAfterPageTurn() { app.launchAndOpenSampleBook(bookTitleQuery: "熊爷爷") app.waitForReader() let content = app.otherElements[IDs.readerContent] XCTAssertTrue(content.waitForExistence(timeout: 5), "固定版式阅读区域未出现") let initialState = app.waitForDemoReaderState(timeout: 5, description: "固定版式初始页") { $0.page != nil } let initialPage = initialState.page ?? 0 content.swipeLeft() let nextState = app.waitForDemoReaderState(timeout: 8, description: "固定版式向后翻页") { state in guard let page = state.page else { return false } return page != initialPage } content.swipeRight() let returnedState = app.waitForDemoReaderState(timeout: 8, description: "固定版式返回缓存页") { $0.page == initialPage } XCTAssertNotEqual(nextState.page, returnedState.page, "固定版式返回缓存页后页码未恢复") XCTAssertTrue(content.exists, "返回缓存页后阅读区域不应消失") } }