| new file mode 100644 |
| index 0000000..c33376b |
| --- /dev/null |
| +++ b/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift |
| @@ -0,0 +1,112 @@ |
| +#if os(macOS) |
| +import AppKit |
| +import Foundation |
| +import Observation |
| +import RastrilloApple |
| +import RastrilloNative |
| +import RastrilloTestSupport |
| +import Synchronization |
| +import SwiftUI |
| +import XCTest |
| +@testable import DocsKit |
| + |
| +/// S2a's chrome over S1a's surface (Fable on #170, findings 4 and 5): what |
| +/// the Format menu, the toolbar, the pill and the live word count read |
| +/// must follow an edit through observation, though the model's projection |
| +/// and selection are no longer observed. |
| +@MainActor final class SurfaceChromeTests: XCTestCase { |
| + private var model: DocsModel! |
| + private var text: DocsTextView! |
| + private var server: AccessServer! |
| + |
| + override func setUp() async throws { |
| + DocsFonts.register() |
| + server = AccessServer() |
| + try await server.inner.seed(LoopFixture.everything) |
| + let connectivity = NativeConnectivity(source: ScriptedPathSource(.satisfied)) |
| + let account = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: UUID().uuidString, connectivity: connectivity, transport: { [server] in server }) |
| + model = DocsModel(transport: { [server] in server }, connectivity: connectivity, offlineAccount: account) |
| + text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 640, height: 800)) |
| + model.surface.attach(text) |
| + await model.open("doc") |
| + model.suspendLiveUpdates() |
| + } |
| + override func tearDown() async throws { await model?.stop(); model = nil; text = nil; server = nil } |
| + |
| + private func observes(_ read: () -> Void, after change: () async throws -> Void) async throws -> Bool { |
| + let fired = Mutex(false) |
| + withObservationTracking(read) { fired.withLock { $0 = true } } |
| + try await change() |
| + return fired.withLock { $0 } |
| + } |
| + private func until(_ what: String, _ condition: () -> Bool) async throws { |
| + let deadline = ContinuousClock.now + .seconds(5) |
| + while !condition() { |
| + guard ContinuousClock.now < deadline else { throw LoopFailure("timed out waiting for \(what)") } |
| + try await Task.sleep(for: .milliseconds(5)) |
| + } |
| + } |
| + private func typeInTheMiddle(_ characters: String) { |
| + let line = (model.surface.content.string as NSString).range(of: LoopFixture.middle) |
| + Keyboard.select(NSRange(location: NSMaxRange(line), length: 0), in: text) |
| + Keyboard.type(characters, in: text) |
| + } |
| + |
| + /// The Format menu (DocsEditorCommands) is disabled from |
| + /// `DocumentCommands.isEnabled`: a key makes it false and the queue |
| + /// draining makes it true again, and each change reaches observation, |
| + /// so the menu's enablement updates. |
| + func testTheFormatMenusEnablementFollowsAnEdit() async throws { |
| + let commands = { [unowned self] in DocumentCommands(model: model, content: model.surface.content) } |
| + XCTAssertTrue(commands().isEnabled) |
| + let disabled = try await observes({ _ = commands().isEnabled }) { typeInTheMiddle("x") } |
| + XCTAssertTrue(disabled, "a key re-evaluates the menu") |
| + XCTAssertFalse(commands().isEnabled, "disabled while input is pending") |
| + let enabled = try await observes({ _ = commands().isEnabled }) { try await until("the queue drained") { !model.hasPendingInput } } |
| + XCTAssertTrue(enabled, "the queue draining re-evaluates the menu") |
| + try await until("enabled again") { commands().isEnabled } |
| + } |
| + |
| + /// Undo and redo read the observed history, set with each publication: |
| + /// it is current while input is still pending (the toolbar selection |
| + /// keeps its last state then), and for a reader, whose toolbar |
| + /// selection is never computed. |
| + func testHistoryIsCurrentWhileInputIsPendingAndForAReader() async throws { |
| + XCTAssertFalse(model.history.canUndo) |
| + var pendingAtPublication: Bool?, historyAtPublication: Bool? |
| + model.surface.willApply = { [unowned self] _ in |
| + if pendingAtPublication == nil { pendingAtPublication = model.hasPendingInput; historyAtPublication = model.history.canUndo } |
| + } |
| + typeInTheMiddle("y") |
| + try await until("the queue drained") { !model.hasPendingInput } |
| + XCTAssertEqual(pendingAtPublication, true, "the publication arrived with input pending") |
| + XCTAssertEqual(historyAtPublication, true, "and the history was already current") |
| + |
| + // A reader: access drops to read-only; a peer's edit still moves the history. |
| + await server.answer(200, canWrite: false) |
| + await model.revalidateAccess() |
| + XCTAssertEqual(model.documentAccess, .readOnly) |
| + XCTAssertNil(DocumentToolbarSelection(commands: DocumentCommands(model: model, content: model.surface.content)), "no toolbar selection for a reader") |
| + XCTAssertTrue(model.history.loaded) |
| + XCTAssertTrue(model.history.canUndo, "the reader's history is the projection's") |
| + } |
| + |
| + #if DOCS_PROBES |
| + /// The live word count recounts after a draw without rendering the |
| + /// document: it reads the projection's text and the text view's storage. |
| + func testTheLiveWordCountDoesNoWholeRender() async throws { |
| + try await Task.sleep(for: .milliseconds(20)) |
| + let sheet = DocsLiveWordCountSheet(model: model) {} |
| + DocsProbes.reset() |
| + let followed = try await observes({ _ = sheet.body }) { |
| + typeInTheMiddle("z") |
| + try await until("the queue drained") { !model.hasPendingInput } |
| + try await Task.sleep(for: .milliseconds(20)) |
| + } |
| + XCTAssertTrue(followed, "the sheet recounts after the draw") |
| + _ = sheet.body |
| + XCTAssertEqual(DocsProbes.wholeRenders, 0, "no whole render for the count") |
| + } |
| + #endif |
| +} |
| +#endif |