|
| 1 | +import XCTest |
| 2 | +#if canImport(UIKit) |
| 3 | +import UIKit |
| 4 | +#endif |
| 5 | + |
| 6 | +/// One capture of the display that actually hosts the target app, with the facts needed to read its |
| 7 | +/// pixels without guessing a panel: which display was captured, how many pixels the encoded image |
| 8 | +/// holds, and how many of them represent one point. |
| 9 | +struct CapturedAppScreen { |
| 10 | + let image: RunnerImage |
| 11 | + let displayID: UInt |
| 12 | + let pixelWidth: Int |
| 13 | + let pixelHeight: Int |
| 14 | + let pixelsPerPoint: Double |
| 15 | +} |
| 16 | + |
| 17 | +/// Why the runner could not name the display hosting the target app. Carried as a reason code |
| 18 | +/// rather than as message text so a required capture can fail closed with a code the host keys on, |
| 19 | +/// and an optional observation can report "unknown" without parsing a sentence (#2728). |
| 20 | +enum RunnerAppScreenCaptureFailure: String, Error { |
| 21 | + case unresolvedWindow = "APP_SCREEN_WINDOW_UNRESOLVED" |
| 22 | + case unresolvedScreen = "APP_SCREEN_UNRESOLVED" |
| 23 | + case unrenderableImage = "APP_SCREEN_CAPTURE_UNRENDERABLE" |
| 24 | + |
| 25 | + init(_ failure: RunnerApplicationScreenFailure) { |
| 26 | + switch failure { |
| 27 | + case .unresolvedWindow: |
| 28 | + self = .unresolvedWindow |
| 29 | + default: |
| 30 | + self = .unresolvedScreen |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + var message: String { |
| 35 | + switch self { |
| 36 | + case .unresolvedWindow: |
| 37 | + return "Neither the target app nor the system surface resolved a window, so no display could be named." |
| 38 | + case .unresolvedScreen: |
| 39 | + return "The resolved app window did not report the display it is shown on." |
| 40 | + case .unrenderableImage: |
| 41 | + return "The resolved display handed back an image that could not be drawn upright." |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + var hint: String { |
| 46 | + switch self { |
| 47 | + case .unresolvedWindow: |
| 48 | + return "The capture follows the display that owns a window, so this is a runtime that is answering no window for any process; runner.log names the window frame and the query answer it refused." |
| 49 | + case .unresolvedScreen: |
| 50 | + return "Retry the capture. If it persists, the XCTest runtime is not reporting the window's display; runner.log names the window frame it resolved." |
| 51 | + case .unrenderableImage: |
| 52 | + return "Retry the capture. The display was resolved, so this is the capture itself failing, not a panel question." |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#if canImport(UIKit) && os(iOS) |
| 58 | +extension RunnerTests { |
| 59 | + /// Captures the display hosting `app` instead of `XCUIScreen.main`. |
| 60 | + /// |
| 61 | + /// On a foldable, `XCUIScreen.main` names one fixed panel while the app can be on the other, and |
| 62 | + /// the panel that is dark still answers with a valid PNG of black — a capture that looks like |
| 63 | + /// evidence and proves nothing. The window's own screen is the only source that follows the app, |
| 64 | + /// and on a panel the system presents turned it hands back an image whose buffer is sideways, so |
| 65 | + /// the pixels are redrawn upright at that image's own logical size and scale before anyone |
| 66 | + /// encodes or compares them (#2728). |
| 67 | + /// |
| 68 | + /// Never activates `app`: an observation must not change which app is foregrounded just to get a |
| 69 | + /// screen, so the caller passes the app context it already intends to observe. |
| 70 | + func captureResolvedAppScreen( |
| 71 | + app: XCUIApplication |
| 72 | + ) -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure> { |
| 73 | + var outcome: Result<CapturedAppScreen, RunnerAppScreenCaptureFailure> = .failure( |
| 74 | + .unresolvedScreen |
| 75 | + ) |
| 76 | + let capture = { |
| 77 | + outcome = Self.resolveCapturedAppScreen(app: app) |
| 78 | + } |
| 79 | + if Thread.isMainThread { |
| 80 | + capture() |
| 81 | + } else { |
| 82 | + DispatchQueue.main.sync(execute: capture) |
| 83 | + } |
| 84 | + return outcome |
| 85 | + } |
| 86 | + |
| 87 | + /// Captures the display a caller should observe, asking `app` first and the system surface second. |
| 88 | + /// |
| 89 | + /// A capture with no session app, or with one that is not running, still has a window on screen: |
| 90 | + /// the home screen is SpringBoard's window, and its panel is the lit one. Asking the system surface |
| 91 | + /// keeps that capture honest — it names a display a window actually occupies instead of reaching |
| 92 | + /// for `XCUIScreen.main`, which on a foldable is one fixed panel whether or not it is dark (#2728). |
| 93 | + /// |
| 94 | + /// Only an unresolved *window* moves on to the system surface. A window that resolved and then |
| 95 | + /// refused to name its display is the failure the host has to see, and no second question about a |
| 96 | + /// different process should bury it. |
| 97 | + func captureObservedScreen(app: XCUIApplication) -> Result< |
| 98 | + CapturedAppScreen, RunnerAppScreenCaptureFailure |
| 99 | + > { |
| 100 | + selectObservedScreenCapture( |
| 101 | + resolving: { captureResolvedAppScreen(app: app) }, |
| 102 | + fallingBack: { captureResolvedAppScreen(app: springboard) } |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + /// The target rule, kept apart from the two queries so the rule itself is testable: an unresolved |
| 107 | + /// window asks the system surface, and nothing else does. |
| 108 | + func selectObservedScreenCapture( |
| 109 | + resolving: () -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure>, |
| 110 | + fallingBack: () -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure> |
| 111 | + ) -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure> { |
| 112 | + let outcome = resolving() |
| 113 | + if case .failure(.unresolvedWindow) = outcome { |
| 114 | + return fallingBack() |
| 115 | + } |
| 116 | + return outcome |
| 117 | + } |
| 118 | + |
| 119 | + private static func resolveCapturedAppScreen( |
| 120 | + app: XCUIApplication |
| 121 | + ) -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure> { |
| 122 | + var screen: AnyObject? |
| 123 | + var displayID: UInt = 0 |
| 124 | + var resolution: RunnerApplicationScreenFailure = .unresolvedScreen |
| 125 | + guard RunnerResolveApplicationScreen(app, &screen, &displayID, &resolution), |
| 126 | + let captureScreen = screen as? XCUIScreen |
| 127 | + else { |
| 128 | + return .failure(RunnerAppScreenCaptureFailure(resolution)) |
| 129 | + } |
| 130 | + let image = captureScreen.screenshot().image |
| 131 | + guard let upright = runnerUprightCaptureImage(image) else { |
| 132 | + return .failure(.unrenderableImage) |
| 133 | + } |
| 134 | + // A scale that is not a finite positive number cannot be reported to a host that divides by it, |
| 135 | + // and cannot be drawn at, so the capture is refused rather than encoded with a value JSON cannot |
| 136 | + // carry (#2728). |
| 137 | + guard upright.scale.isFinite, upright.scale > 0 else { |
| 138 | + return .failure(.unrenderableImage) |
| 139 | + } |
| 140 | + guard let cgImage = runnerCGImage(from: upright) else { |
| 141 | + return .failure(.unrenderableImage) |
| 142 | + } |
| 143 | + return .success( |
| 144 | + CapturedAppScreen( |
| 145 | + image: upright, |
| 146 | + displayID: displayID, |
| 147 | + pixelWidth: cgImage.width, |
| 148 | + pixelHeight: cgImage.height, |
| 149 | + pixelsPerPoint: Double(upright.scale) |
| 150 | + ) |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + /// Redraws a turned capture so its encoded rows run the way the panel is presented, and leaves an |
| 155 | + /// already-upright capture untouched so a single-panel capture keeps the exact bytes it produced |
| 156 | + /// before panels existed. The draw box is the image's own oriented logical size and the renderer's |
| 157 | + /// scale is the image's own scale: neither comes from a nominal panel size, and the one-pixel |
| 158 | + /// difference between what XCTest hands back and what CoreDevice calls the panel is preserved |
| 159 | + /// rather than stretched away (#2728). |
| 160 | + private static func runnerUprightCaptureImage(_ image: UIImage) -> UIImage? { |
| 161 | + if image.imageOrientation == .up { |
| 162 | + return image |
| 163 | + } |
| 164 | + guard image.size.width > 0, image.size.height > 0, image.scale.isFinite, image.scale > 0 else { |
| 165 | + return nil |
| 166 | + } |
| 167 | + let format = UIGraphicsImageRendererFormat() |
| 168 | + format.scale = image.scale |
| 169 | + let renderer = UIGraphicsImageRenderer(size: image.size, format: format) |
| 170 | + return renderer.image { _ in |
| 171 | + image.draw(in: CGRect(origin: .zero, size: image.size)) |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | +#endif |
0 commit comments