ReadViewSDK/ReadViewDemo/ReadViewDemoUITests/ReaderUITests/PDFDrawingModeTests.swift

89 lines
3.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import XCTest
///
///
final class PDFDrawingModeTests: XCTestCase {
private let app = XCUIApplication()
override func setUpWithError() throws {
continueAfterFailure = false
XCUIDevice.shared.orientation = .portrait
}
override func tearDownWithError() throws {
XCUIDevice.shared.orientation = .portrait
}
func testLandscapeSinglePageScrollsOnlyWhenDrawingToolIsDeselected() {
app.launchAndOpenSampleBook(bookTitleQuery: "英汉大词典", displayType: "pagecurl")
app.waitForReader()
XCUIDevice.shared.orientation = .landscapeLeft
let content = app.otherElements[IDs.readerContent].firstMatch
XCTAssertTrue(content.waitForExistence(timeout: 8), "PDF 横屏阅读页面未出现")
XCTAssertTrue(waitForLandscapeLayout(), "PDF 未进入横屏单页布局")
app.showReaderChromeIfNeeded(requiredButtonIDs: [IDs.pdfDrawing])
let drawing = app.buttons[IDs.pdfDrawing]
XCTAssertTrue(drawing.waitForExistence(timeout: 5), "画笔入口未出现")
drawing.tap()
let pen = app.buttons[IDs.pdfDrawingPen]
let eraser = app.buttons[IDs.pdfDrawingEraser]
XCTAssertTrue(pen.waitForExistence(timeout: 5), "画笔工具未出现")
XCTAssertTrue(eraser.exists, "橡皮擦工具未出现")
let firstScrollablePage = app.currentDemoReaderState()?.page
XCTAssertNotNil(firstScrollablePage, "未取得初始 PDF 页码")
guard let firstScrollablePage else { return }
XCTAssertTrue(
scrollToAnotherPage(content, from: firstScrollablePage),
"未选中工具时,手机横屏单页无法连续竖滑"
)
let lockedPage = app.currentDemoReaderState()?.page
XCTAssertNotNil(lockedPage, "滚动后未取得 PDF 页码")
guard let lockedPage else { return }
pen.tap()
assertPageRemains(lockedPage, afterSwiping: content, message: "选中画笔后仍触发了页面滚动")
eraser.tap()
assertPageRemains(lockedPage, afterSwiping: content, message: "切换至橡皮擦后仍触发了页面滚动")
eraser.tap()
XCTAssertTrue(
scrollToAnotherPage(content, from: lockedPage),
"取消橡皮擦选中后,手机横屏单页没有恢复连续竖滑"
)
}
private func waitForLandscapeLayout() -> Bool {
let deadline = Date().addingTimeInterval(8)
while Date() < deadline {
let frame = app.windows.firstMatch.frame
if frame.width > frame.height { return true }
RunLoop.current.run(until: Date().addingTimeInterval(0.1))
}
return false
}
private func scrollToAnotherPage(_ content: XCUIElement, from page: Int) -> Bool {
for _ in 0..<5 {
content.swipeUp()
let deadline = Date().addingTimeInterval(1.5)
while Date() < deadline {
if let current = app.currentDemoReaderState()?.page, current != page { return true }
RunLoop.current.run(until: Date().addingTimeInterval(0.1))
}
}
return false
}
private func assertPageRemains(_ page: Int, afterSwiping content: XCUIElement, message: String) {
for _ in 0..<3 { content.swipeUp() }
RunLoop.current.run(until: Date().addingTimeInterval(0.8))
XCTAssertEqual(app.currentDemoReaderState()?.page, page, message)
}
}