Repository Public MIT
1// ORIX Calc2// File: Packages/ORIXFeatureStatistics/Tests/ORIXFeatureStatisticsTests/RenderTests.swift3// Description: Renders every section off-screen at phone and tablet sizes (light, dark, accessibility text) as a smoke test; the images go to the temporary directory for inspection.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7#if canImport(UIKit)8import SwiftUI9import Testing10import UIKit11import ORIXUI12@testable import ORIXFeatureStatistics1314@MainActor @Suite struct RenderTests {15 struct Shot {16 var name: String17 var size: CGSize18 var section: StatSection19 var dark = false20 var textSize: DynamicTypeSize = .large21 var tool: SimTool = .monteCarlo22 var empty = false23 /// Renders a tall canvas so that the whole scrolling content is visible.24 var tall = false25 /// Hosts the content without the navigation bar (whose material does not render off-screen), to see the section bar.26 var bare = false27 }2829 static let phone = CGSize(width: 393, height: 852)30 static let shots: [Shot] = [31 Shot(name: "phone-data", size: phone, section: .data, tall: true),32 Shot(name: "phone-data-dark", size: phone, section: .data, dark: true, bare: true),33 Shot(name: "phone-data-empty", size: phone, section: .data, empty: true, bare: true),34 Shot(name: "phone-distributions", size: phone, section: .distributions, tall: true),35 Shot(name: "phone-inference", size: phone, section: .inference, tall: true),36 Shot(name: "phone-inference-ax3", size: phone, section: .inference, textSize: .accessibility3, tall: true),37 Shot(name: "phone-regression", size: phone, section: .regression, tall: true),38 Shot(name: "phone-regression-dark", size: phone, section: .regression, dark: true, tall: true),39 Shot(name: "phone-simulation", size: phone, section: .simulation, tall: true),40 Shot(name: "phone-simulation-bootstrap", size: phone, section: .simulation, tool: .bootstrap, tall: true),41 Shot(name: "phone-data-ax3", size: phone, section: .data, textSize: .accessibility3, tall: true),42 Shot(name: "phone-landscape-distributions", size: CGSize(width: 852, height: 393), section: .distributions, bare: true),43 Shot(name: "pad-landscape-data", size: CGSize(width: 1366, height: 1024), section: .data),44 Shot(name: "pad-landscape-inference-dark", size: CGSize(width: 1366, height: 1024), section: .inference, dark: true),45 Shot(name: "pad-landscape-regression", size: CGSize(width: 1366, height: 1024), section: .regression, tall: true),46 Shot(name: "pad-portrait-distributions", size: CGSize(width: 834, height: 1194), section: .distributions),47 Shot(name: "pad-portrait-simulation", size: CGSize(width: 834, height: 1194), section: .simulation, tool: .sampling),48 Shot(name: "pad-landscape-simulation-scenarios", size: CGSize(width: 1366, height: 1024), section: .simulation, tool: .scenarios),49 ]5051 @Test func rendersEveryLayout() async throws {52 let directory = FileManager.default.temporaryDirectory.appendingPathComponent("orix-statistics-shots", isDirectory: true)53 try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)54 for shot in RenderTests.shots {55 let app = makeApp()56 app.registerNatives(StatisticsNatives.all)57 let store = StatisticsStore()58 if !shot.empty { await store.loadDemo(context: app.engine.context) }59 store.section = shot.section60 store.simulation.state.tool = shot.tool61 let size = shot.tall ? CGSize(width: shot.size.width, height: shot.size.width >= 700 ? 2600 : 5200) : shot.size62 let compactHeight = shot.size.height < 50063 let root = Group {64 if shot.bare { StatisticsRoot(store: store) } else {65 NavigationStack { StatisticsRoot(store: store).navigationTitle(shot.section.longTitle).navigationBarTitleDisplayMode(.inline) }66 }67 }68 .tint(Stat.tint)69 .environment(app)70 .environment(\.horizontalSizeClass, shot.size.width >= 700 && !compactHeight ? .regular : .compact)71 .environment(\.verticalSizeClass, compactHeight ? .compact : .regular)72 .environment(\.dynamicTypeSize, shot.textSize)73 .preferredColorScheme(shot.dark ? .dark : .light)74 let controller = UIHostingController(rootView: root)75 controller.overrideUserInterfaceStyle = shot.dark ? .dark : .light76 let window = UIWindow(frame: CGRect(origin: .zero, size: size))77 window.rootViewController = controller78 window.isHidden = false79 window.layoutIfNeeded()80 try await Task.sleep(for: .milliseconds(500))81 let image = UIGraphicsImageRenderer(size: size).image { context in82 window.layer.render(in: context.cgContext)83 }84 let data = try #require(image.pngData())85 #expect(data.count > 1000)86 try data.write(to: directory.appendingPathComponent(shot.name + ".png"))87 window.isHidden = true88 window.rootViewController = nil89 }90 print("ORIX Statistics render images: \(directory.path)")91 }9293 /// The public screen in demo mode appears, computes, and leaves the user's saved state and history alone.94 @Test func publicScreenDemoDoesNotTouchSavedState() async throws {95 let app = makeApp()96 var saved = StatSnapshot()97 saved.section = .regression98 saved.data.dataset = SampleDatasets.bacteria99 app.saveState(saved, key: Stat.stateKey)100 let controller = UIHostingController(rootView: StatisticsScreen(section: "inference", demo: true).environment(app))101 let window = UIWindow(frame: CGRect(origin: .zero, size: RenderTests.phone))102 window.rootViewController = controller103 window.isHidden = false104 window.layoutIfNeeded()105 try await Task.sleep(for: .milliseconds(1500))106 window.isHidden = true107 window.rootViewController = nil108 try await Task.sleep(for: .milliseconds(300))109 #expect(app.loadState(StatSnapshot.self, key: Stat.stateKey) == saved)110 #expect(app.history.isEmpty)111 }112113 /// Without the demo flag, the screen restores the saved state and saves it back when it disappears.114 @Test func publicScreenRestoresAndSaves() async throws {115 let app = makeApp()116 var saved = StatSnapshot()117 saved.data.dataset = SampleDatasets.bacteria118 saved.data.selected = 1119 app.saveState(saved, key: Stat.stateKey)120 let controller = UIHostingController(rootView: StatisticsScreen(section: "distributions").environment(app))121 let window = UIWindow(frame: CGRect(origin: .zero, size: RenderTests.phone))122 window.rootViewController = controller123 window.isHidden = false124 window.layoutIfNeeded()125 try await Task.sleep(for: .milliseconds(1800))126 // The debounced auto-save wrote the restored state with the section requested by the host.127 let now = try #require(app.loadState(StatSnapshot.self, key: Stat.stateKey))128 #expect(now.section == .distributions)129 #expect(now.data.dataset == SampleDatasets.bacteria && now.data.selected == 1)130 window.isHidden = true131 window.rootViewController = nil132 }133}134#endif135