| 1 | import Foundation |
| 2 | import XCTest |
| 3 | @testable import DocsKit |
| 4 | |
| 5 | /// The surface across text views and model updates (Fable on #170, |
| 6 | /// findings 2 and 3). |
| 7 | @MainActor final class SurfaceLifecycleTests: XCTestCase { |
| 8 | private func projection() -> CRDTProjection { |
| 9 | SurfaceFixture.projection([SurfaceFixture.heading("h", "Title"), SurfaceFixture.paragraph("p", "Body text."), |
| 10 | SurfaceFixture.list("l", "bullet_list", ["one", "two"])]) |
| 11 | } |
| 12 | |
| 13 | /// The host is rebuilt without the model opening again (an iPad |
| 14 | /// split collapsing, a restructured window): the new text view shows |
| 15 | /// the document at once, with no further publication, and the old one |
| 16 | /// no longer edits through the surface. |
| 17 | func testASecondTextViewIsDrawnWhole() throws { |
| 18 | let state = SurfaceState() |
| 19 | state.publish(projection(), generation: 1, dirty: nil) |
| 20 | let second = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| 21 | state.surface.attach(second) |
| 22 | XCTAssertTrue(state.surface.textView === second) |
| 23 | SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), projection(), "the second text view") |
| 24 | XCTAssertNil(state.text.delegate, "the first text view is let go") |
| 25 | // And it keeps drawing: a later publication reaches the second view. |
| 26 | var changed = projection() |
| 27 | changed.children[1] = SurfaceFixture.paragraph("p", "Body text, changed.") |
| 28 | state.publish(changed, generation: 1, dirty: ["p"]) |
| 29 | SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), changed, "a publication after the second attach") |
| 30 | } |
| 31 | |
| 32 | /// The text view is attached inside SwiftUI's update (makeNSView / |
| 33 | /// makeUIView). It is drawn there, so the first frame is final, but the |
| 34 | /// model hears nothing until the next turn: no observed state is |
| 35 | /// assigned during a view update (the 0.1.7/0.1.8 rule), not the |
| 36 | /// rendered generation and not the document's image sources. |
| 37 | func testAttachingTellsTheModelOnlyAfterTheUpdate() async throws { |
| 38 | let image = CRDTNode(id: "i", type: "paragraph", children: [CRDTNode(id: "img", type: "image", attrs: ["src": "https://example.invalid/a.png", "alt": "A"])]) |
| 39 | let document = SurfaceFixture.projection([SurfaceFixture.paragraph("p", "Before."), image]) |
| 40 | let surface = DocumentSurface() |
| 41 | var inUpdate = false |
| 42 | var duringUpdate: [String] = [], after: [String] = [] |
| 43 | var hooks = DocumentSurface.Hooks() |
| 44 | hooks.didRender = { _ in (inUpdate ? { duringUpdate.append("didRender") } : { after.append("didRender") })() } |
| 45 | hooks.imageSourcesChanged = { _ in (inUpdate ? { duringUpdate.append("imageSources") } : { after.append("imageSources") })() } |
| 46 | surface.hooks = hooks |
| 47 | surface.apply(DocumentSurface.Publication(generation: 1, projection: document, revision: 1, dirty: nil)) |
| 48 | let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| 49 | inUpdate = true |
| 50 | surface.attach(text) |
| 51 | inUpdate = false |
| 52 | XCTAssertEqual(duringUpdate, [], "nothing reaches the model inside the update") |
| 53 | XCTAssertTrue(Keyboard.storage(of: text).string.hasPrefix("Before."), "drawn at once") |
| 54 | for _ in 0..<20 where after.count < 2 { try await Task.sleep(for: .milliseconds(5)) } |
| 55 | XCTAssertEqual(Set(after), ["didRender", "imageSources"], "the model hears both on the next turn") |
| 56 | } |
| 57 | |
| 58 | /// A report deferred from an attach belongs to the document drawn then: |
| 59 | /// if the model resets the surface (another document opens) in the same |
| 60 | /// turn, the old document's image sources and generation never reach it |
| 61 | /// (Fable on #170, final check, finding 2). |
| 62 | func testADeferredReportIsDroppedAfterAReset() async throws { |
| 63 | let image = CRDTNode(id: "i", type: "paragraph", children: [CRDTNode(id: "img", type: "image", attrs: ["src": "https://example.invalid/a.png", "alt": "A"])]) |
| 64 | let surface = DocumentSurface() |
| 65 | var sources: [[String]] = [], rendered: [UInt64] = [] |
| 66 | var hooks = DocumentSurface.Hooks() |
| 67 | hooks.didRender = { rendered.append($0) } |
| 68 | hooks.imageSourcesChanged = { sources.append($0) } |
| 69 | surface.hooks = hooks |
| 70 | surface.apply(DocumentSurface.Publication(generation: 3, projection: SurfaceFixture.projection([image]), revision: 1, dirty: nil)) |
| 71 | let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| 72 | surface.attach(text) |
| 73 | surface.reset() |
| 74 | for _ in 0..<10 { try await Task.sleep(for: .milliseconds(5)) } |
| 75 | XCTAssertEqual(rendered, [], "the old document's generation is not reported after the reset") |
| 76 | XCTAssertEqual(sources.last ?? [], [], "the last image sources the model hears are the reset's, none") |
| 77 | } |
| 78 | |
| 79 | /// The old text view is already gone (its host released it) when the |
| 80 | /// new one attaches: the new one is still drawn whole (Fable on #170, |
| 81 | /// final check, finding 3). |
| 82 | func testANewTextViewIsDrawnWholeWhenTheOldOneIsGone() async throws { |
| 83 | let surface = DocumentSurface() |
| 84 | var hooks = DocumentSurface.Hooks(); hooks.canRender = { _ in true } |
| 85 | surface.hooks = hooks |
| 86 | autoreleasepool { |
| 87 | let first = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| 88 | surface.attach(first) |
| 89 | surface.apply(DocumentSurface.Publication(generation: 1, projection: projection(), revision: 1, dirty: nil)) |
| 90 | XCTAssertFalse(Keyboard.storage(of: first).string.isEmpty) |
| 91 | } |
| 92 | for _ in 0..<50 where surface.textView != nil { try await Task.sleep(for: .milliseconds(10)) } |
| 93 | XCTAssertNil(surface.textView, "the first view is gone") |
| 94 | let second = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| 95 | surface.attach(second) |
| 96 | SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), projection(), "a new view after the old one is gone") |
| 97 | } |
| 98 | } |
| 99 | |