| 1 | #if os(iOS) |
| 2 | import Foundation |
| 3 | import RastrilloApple |
| 4 | import RastrilloSnapshots |
| 5 | import RastrilloTestSupport |
| 6 | import SwiftUI |
| 7 | import UIKit |
| 8 | import XCTest |
| 9 | @testable import DocsKit |
| 10 | |
| 11 | /// The S3 iPhone document (spec 2026-11-22 §3, §11): the real |
| 12 | /// `DocsWorkspace` opening a fixture document on the editor snapshots' |
| 13 | /// in-memory server and pushing it, as a writer and as a reader; and the |
| 14 | /// Format sheet and the Share sheet as the sheet draws them. Light and dark |
| 15 | /// at the default size and AX3; the document, the sheets also in Arabic and |
| 16 | /// Russian. |
| 17 | final class DocsPhoneDocumentSnapshots: XCTestCase { |
| 18 | @MainActor func testPhoneDocumentScreens() async throws { |
| 19 | try XCTSkipUnless(ProcessInfo.processInfo.environment["RASTRILLO_SNAPSHOTS"] != nil, |
| 20 | "Run with native/scripts/snapshots.sh") |
| 21 | DocsFonts.register() |
| 22 | let settings = SnapshotSettings(tint: OficinaPalette.accent(.docs)) |
| 23 | await assertSnapshots(of: SnapshotCatalog(app: "docs-phone", variants: [.init(.light, .standard), .init(.dark, .standard)], |
| 24 | settings: settings, screens: Self.screens + Self.languageScreens)) |
| 25 | await assertSnapshots(of: SnapshotCatalog(app: "docs-phone-ax3", variants: [.init(.light, .accessibility), .init(.dark, .accessibility)], |
| 26 | settings: settings, screens: Self.screens)) |
| 27 | await PhoneDocumentScreen.stopAll() |
| 28 | } |
| 29 | |
| 30 | static let phone = SnapshotSize.fixed(width: 402, height: 874) |
| 31 | static let sheet = SnapshotSize.width(402) |
| 32 | |
| 33 | @MainActor static var screens: [SnapshotScreen] { |
| 34 | [ |
| 35 | SnapshotScreen("phone-document", mac: nil, iphone: phone) { PhoneDocumentScreen(reader: false) }, |
| 36 | SnapshotScreen("phone-reader", mac: nil, iphone: phone) { PhoneDocumentScreen(reader: true) }, |
| 37 | SnapshotScreen("phone-format-sheet", mac: nil, iphone: sheet) { PhoneFormatSheetScreen(language: "en") }, |
| 38 | SnapshotScreen("phone-share-sheet", mac: nil, iphone: sheet) { PhoneShareSheetScreen(language: "en") }, |
| 39 | ] |
| 40 | } |
| 41 | @MainActor static var languageScreens: [SnapshotScreen] { |
| 42 | ["ar", "ru"].flatMap { language in [ |
| 43 | SnapshotScreen("phone-document-" + language, mac: nil, iphone: phone) { PhoneDocumentScreen(reader: false, language: language) }, |
| 44 | SnapshotScreen("phone-format-sheet-" + language, mac: nil, iphone: sheet) { PhoneFormatSheetScreen(language: language) }, |
| 45 | SnapshotScreen("phone-share-sheet-" + language, mac: nil, iphone: sheet) { PhoneShareSheetScreen(language: language) }, |
| 46 | ] } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// The document as the iPhone shows it: the workspace opening "doc" and |
| 51 | /// pushing it, until its text is drawn and its title measured. |
| 52 | @MainActor struct PhoneDocumentScreen: View { |
| 53 | let reader: Bool |
| 54 | var language = "en" |
| 55 | @State private var server: EditorServer? |
| 56 | @State private var ready = false |
| 57 | private let probe = ViewProbe() |
| 58 | private static var opened: [EditorServer] = [] |
| 59 | static func stopAll() async { opened.removeAll() } |
| 60 | |
| 61 | var body: some View { |
| 62 | ZStack(alignment: .topLeading) { |
| 63 | if let server { |
| 64 | DocsWorkspace(client: NativeHTTP(origin: server.origin), transport: { server }, signOut: {}, documentID: "doc", team: "Tito", |
| 65 | connectivity: NativeConnectivity(source: ScriptedPathSource(.satisfied))) |
| 66 | .background { PhoneProbeView(probe: probe) } |
| 67 | } |
| 68 | if !ready { PhoneOpeningCounter() } |
| 69 | } |
| 70 | .environment(\.locale, Locale(identifier: language)) |
| 71 | .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) |
| 72 | .task { await open() } |
| 73 | } |
| 74 | |
| 75 | private func open() async { |
| 76 | do { |
| 77 | let server = EditorServer() |
| 78 | if reader { await server.readOnly() } |
| 79 | let fixture: EditorFixture = language == "ar" ? .arabic : language == "ru" ? .russian : .millbrook |
| 80 | try await server.seed(fixture.blocks) |
| 81 | Self.opened.append(server) |
| 82 | self.server = server |
| 83 | let deadline = ContinuousClock.now + .seconds(10) |
| 84 | while ContinuousClock.now < deadline { |
| 85 | if let text = probe.textView, text.textStorage.length > 100, text.headerHeight > 0, await server.openStreams == 1 { break } |
| 86 | try await Task.sleep(for: .milliseconds(20)) |
| 87 | } |
| 88 | guard let text = probe.textView, text.textStorage.length > 100 else { throw EditorScreenFailure("the document did not open") } |
| 89 | text.setContentOffset(CGPoint(x: 0, y: -text.adjustedContentInset.top), animated: false) |
| 90 | text.layoutIfNeeded() |
| 91 | try await Task.sleep(for: .milliseconds(400)) |
| 92 | ready = true |
| 93 | } catch { |
| 94 | print("phone snapshot did not open: \(error)") |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// The Format sheet on its own, as the sheet's medium detent shows it. |
| 100 | @MainActor struct PhoneFormatSheetScreen: View { |
| 101 | let language: String |
| 102 | @State private var model = DocsModel(transport: { nil }) |
| 103 | var body: some View { |
| 104 | DocsFormatSheet(model: model, close: {}) |
| 105 | .frame(height: 430, alignment: .top) |
| 106 | .background(Color(uiColor: .systemBackground)) |
| 107 | .environment(\.locale, Locale(identifier: language)) |
| 108 | .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /// The Share sheet's panel, as the sheet shows it: two people, the team |
| 113 | /// rung on, and the link published with the once-only URL showing. |
| 114 | @MainActor struct PhoneShareSheetScreen: View { |
| 115 | let language: String |
| 116 | var body: some View { |
| 117 | let information = DocsShareInformation(sqid: "doc", title: "A History of the Millbrook Waterworks", |
| 118 | grants: [DocsShareGrant(address: "bob@example.com", role: "writer"), |
| 119 | DocsShareGrant(address: "carol.long-address@example.org", role: "reader")], |
| 120 | published: true, public_url: "", visibility: "team", error: "") |
| 121 | let model = DocsSharePanelModel(sqid: "doc", title: information.title, information: information, |
| 122 | onceURL: URL(string: "https://docs.oficina.works/p/Hx7kQ2mVb9tLr4sN")) |
| 123 | DocsSharePanel(model: model, me: "alice@example.com", focusOnAppear: false, close: {}) |
| 124 | .background(Color(uiColor: .systemBackground)) |
| 125 | .environment(\.locale, Locale(identifier: language)) |
| 126 | .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @MainActor private struct PhoneOpeningCounter: View { |
| 131 | @State private var frame = 0 |
| 132 | var body: some View { |
| 133 | Text(verbatim: "opening \(frame)").padding(4).background(Color.red).foregroundStyle(.white) |
| 134 | .task { while !Task.isCancelled { frame &+= 1; try? await Task.sleep(for: .milliseconds(10)) } } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private struct PhoneProbeView: UIViewRepresentable { |
| 139 | let probe: ViewProbe |
| 140 | func makeUIView(context: Context) -> UIView { let view = UIView(); probe.view = view; return view } |
| 141 | func updateUIView(_ view: UIView, context: Context) {} |
| 142 | } |
| 143 | #endif |
| 144 | |