import XCTest final class SettingsEffectTests: XCTestCase { private let app = XCUIApplication() override func setUpWithError() throws { continueAfterFailure = false } func testFontSizeChangeUpdatesContent() throws { app.launchAndOpenSampleBook() app.waitForReader(timeout: 12) app.waitForDemoReaderState(timeout: 5, description: "page exists") { $0.page != nil } app.showReaderChromeIfNeeded() XCTAssertTrue(app.buttons[IDs.readerSettings].waitForExistence(timeout: 3), "设置按钮不存在") app.buttons[IDs.readerSettings].tap() XCTAssertTrue(app.waitForSettingsPanel(), "设置面板未出现") XCTAssertTrue(app.increaseSettingsFontSize(), "字号滑块不存在") XCTAssertTrue(app.buttons[IDs.settingsDone].waitForExistence(timeout: 3), "完成按钮不存在") app.buttons[IDs.settingsDone].tap() app.waitForDemoReaderState(timeout: 8, description: "reader=opened") { $0.isOpened } } func testThemeSwitchChangesBackground() throws { app.launchAndOpenSampleBook() app.waitForReader(timeout: 12) app.showReaderChromeIfNeeded() app.buttons[IDs.readerSettings].tap() XCTAssertTrue(app.waitForSettingsPanel(), "设置面板未出现") let darkThemeButton = app.buttons[IDs.settingsTheme(5)] if darkThemeButton.waitForExistence(timeout: 3) { darkThemeButton.tap() } app.buttons[IDs.settingsDone].tap() let contentView = app.otherElements[IDs.readerContentView] XCTAssertTrue(contentView.waitForExistence(timeout: 5), "主题切换后内容区应存在") } func testLineHeightChangeUpdatesContent() throws { app.launchAndOpenSampleBook() app.waitForReader(timeout: 12) app.showReaderChromeIfNeeded() app.buttons[IDs.readerSettings].tap() XCTAssertTrue(app.waitForSettingsPanel()) app.selectSettingsPillOption(pillIdentifier: IDs.settingsLineHeight, optionTitle: "标准") app.buttons[IDs.settingsDone].tap() app.waitForDemoReaderState(timeout: 8, description: "reader=opened") { $0.isOpened } } func testDisplayTypeChangeRePaginates() throws { app.launchAndOpenSampleBook(displayType: "horizontalscroll") app.waitForReader(timeout: 12) app.waitForDemoReaderState(timeout: 5, description: "display=horizontalScroll") { $0.display == "horizontalScroll" } app.showReaderChromeIfNeeded() app.buttons[IDs.readerSettings].tap() XCTAssertTrue(app.waitForSettingsPanel()) app.selectSettingsPillOption(pillIdentifier: IDs.settingsDisplayType, optionTitle: "仿真") app.buttons[IDs.settingsDone].tap() app.waitForDemoReaderState(timeout: 8, description: "display changed") { $0.display != nil } } func testDefaultBodyPaginationDoesNotEnableWidowOrphanCompaction() throws { app.launchAndOpenSampleBook() app.waitForReader(timeout: 12) let state = app.waitForDemoReaderState(timeout: 8, description: "默认正文分页策略") { state in state.avoidWidows != nil && state.avoidOrphans != nil } XCTAssertEqual(state.avoidWidows, 0, "默认正文分页不应启用 widow compaction,避免页尾明显留白") XCTAssertEqual(state.avoidOrphans, 0, "默认正文分页不应启用 orphan compaction,避免提前换页") } func testSettingsPersistAfterReopen() throws { app.launchAndOpenSampleBook() app.waitForReader(timeout: 12) app.showReaderChromeIfNeeded() app.buttons[IDs.readerSettings].tap() XCTAssertTrue(app.sliders[IDs.settingsFontSize].waitForExistence(timeout: 5)) let fontValueLabel = app.staticTexts[IDs.settingsFontValue] XCTAssertTrue(fontValueLabel.waitForExistence(timeout: 3)) let originalFontValue = fontValueLabel.label XCTAssertTrue(app.increaseSettingsFontSize(), "字号滑块应存在") let newFontValue = fontValueLabel.label XCTAssertNotEqual(newFontValue, originalFontValue, "增大字号后 label 应变化") app.buttons[IDs.settingsDone].tap() app.buttons[IDs.readerBack].tap() // 必须保留状态:默认的 resetsReaderState 会清掉刚存下的设置, // 那样这个用例永远测不到持久化 app.launchAndOpenSampleBook(resetsReaderState: false) app.waitForReader(timeout: 12) app.showReaderChromeIfNeeded() app.buttons[IDs.readerSettings].tap() let persistedFontValue = app.staticTexts[IDs.settingsFontValue] if persistedFontValue.waitForExistence(timeout: 5) { XCTAssertEqual(persistedFontValue.label, newFontValue, "重新打开后字号应持久化为之前的值") } app.buttons[IDs.settingsDone].tap() } func testThemeSelectionPersistsAfterReopen() throws { app.launchAndOpenSampleBook(resetsReaderState: true) app.waitForReader(timeout: 12) app.showReaderChromeIfNeeded() app.buttons[IDs.readerSettings].tap() let darkThemeButton = app.buttons[IDs.settingsTheme(5)] XCTAssertTrue(darkThemeButton.waitForExistence(timeout: 5)) darkThemeButton.tap() XCTAssertEqual(darkThemeButton.value as? String, "selected", "切换主题后当前主题按钮应为选中态") app.buttons[IDs.settingsDone].tap() app.showReaderChromeIfNeeded() app.buttons[IDs.readerBack].tap() app.launchAndOpenSampleBook(resetsReaderState: false) app.waitForReader(timeout: 12) app.showReaderChromeIfNeeded() app.buttons[IDs.readerSettings].tap() let reopenedDarkThemeButton = app.buttons[IDs.settingsTheme(5)] XCTAssertTrue(reopenedDarkThemeButton.waitForExistence(timeout: 5)) XCTAssertEqual(reopenedDarkThemeButton.value as? String, "selected", "主题应在重启后保持选中") app.buttons[IDs.settingsDone].tap() } func testFontChoiceChangePersistsAfterReopen() throws { app.launchAndOpenSampleBook(resetsReaderState: true) app.waitForReader(timeout: 12) app.showReaderChromeIfNeeded() app.buttons[IDs.readerSettings].tap() let fontChoiceControl = app.buttons[IDs.settingsFontChoice] XCTAssertTrue(fontChoiceControl.waitForExistence(timeout: 5)) XCTAssertTrue( app.selectSettingsPillOption(pillIdentifier: IDs.settingsFontChoice, optionTitle: "宋体"), "字体应可切换到宋体" ) XCTAssertEqual(fontChoiceControl.value as? String, "宋体", "切换字体后当前字体值应更新") app.buttons[IDs.settingsDone].tap() app.showReaderChromeIfNeeded() app.buttons[IDs.readerBack].tap() app.launchAndOpenSampleBook(resetsReaderState: false) app.waitForReader(timeout: 12) app.showReaderChromeIfNeeded() app.buttons[IDs.readerSettings].tap() let reopenedFontChoiceControl = app.buttons[IDs.settingsFontChoice] XCTAssertTrue(reopenedFontChoiceControl.waitForExistence(timeout: 5)) XCTAssertEqual(reopenedFontChoiceControl.value as? String, "宋体", "字体选择应在重启后保持") app.buttons[IDs.settingsDone].tap() } }