Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Tests: render in windows that are on no display

v0lumehiad45d14c9b7e
diff --git a/frontend/Tests/NX2AppleTests/BuildFailureTests.swift b/frontend/Tests/NX2AppleTests/BuildFailureTests.swift
index 68b2e48c..2fd1e047 100644
--- a/frontend/Tests/NX2AppleTests/BuildFailureTests.swift
+++ b/frontend/Tests/NX2AppleTests/BuildFailureTests.swift
@@ -268,7 +268,7 @@ final class BuildFailureTests: XCTestCase {
app.setActivationPolicy(.accessory)
func capture(_ name: String, width: CGFloat, height: CGFloat, _ view: some View) throws {
let host = NSHostingView(rootView: view.preferredColorScheme(.dark).frame(width: width, height: height))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: width, height: height), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: width, height: height))
window.contentView = host
window.appearance = NSAppearance(named: .darkAqua)
window.orderFront(nil)
diff --git a/frontend/Tests/NX2AppleTests/BuildNotificationsTests.swift b/frontend/Tests/NX2AppleTests/BuildNotificationsTests.swift
index a28f20af..421a6ac6 100644
--- a/frontend/Tests/NX2AppleTests/BuildNotificationsTests.swift
+++ b/frontend/Tests/NX2AppleTests/BuildNotificationsTests.swift
@@ -63,7 +63,7 @@ final class BuildNotificationsTests: XCTestCase {
model.deps = [BuildModel.Dep(id: "test", name: "Xcode", hint: "", present: true, installCmd: [])]
let library = GameLibrary(file: temporary.appendingPathComponent("preview.json"))
let host = NSHostingView(rootView: ContentView(model: model, library: library, initialPage: .settings, performStartupChecks: false))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1440, height: 1500), styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 1440, height: 1500), styleMask: [.titled, .closable, .resizable])
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.5))
diff --git a/frontend/Tests/NX2AppleTests/CheatFilesTests.swift b/frontend/Tests/NX2AppleTests/CheatFilesTests.swift
index 351158ba..acd7fc72 100644
--- a/frontend/Tests/NX2AppleTests/CheatFilesTests.swift
+++ b/frontend/Tests/NX2AppleTests/CheatFilesTests.swift
@@ -408,7 +408,7 @@ final class CheatFilesTests: XCTestCase {
func shot(_ name: String, _ view: some View, height: CGFloat = 700) throws {
let host = NSHostingView(rootView: view.environmentObject(PadFocus()))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 600, height: height), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 600, height: height))
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.6))
diff --git a/frontend/Tests/NX2AppleTests/ConsoleLiveTests.swift b/frontend/Tests/NX2AppleTests/ConsoleLiveTests.swift
index 5b183d6b..97a21b80 100644
--- a/frontend/Tests/NX2AppleTests/ConsoleLiveTests.swift
+++ b/frontend/Tests/NX2AppleTests/ConsoleLiveTests.swift
@@ -171,7 +171,7 @@ import SwiftUI
func testEnteringAndLeavingConsoleModeRestoresTheWindowInOrder() async throws {
var calls: [Bool] = []
var finishEntering: (() -> Void)?
- let window = NSWindow(contentRect: .zero, styleMask: [.titled], backing: .buffered, defer: true)
+ let window = OffscreenWindow(size: .zero)
let coordinator = ConsoleCoordinator(window: { window }, setFullScreen: { _, on, done in calls.append(on); if on { finishEntering = done } else { done() } }, fadeSeconds: 0.05)
coordinator.enter()
XCTAssertFalse(coordinator.active, "not while the guided setup shows")
diff --git a/frontend/Tests/NX2AppleTests/ConsolePadTests.swift b/frontend/Tests/NX2AppleTests/ConsolePadTests.swift
index 520e48c1..3ea12241 100644
--- a/frontend/Tests/NX2AppleTests/ConsolePadTests.swift
+++ b/frontend/Tests/NX2AppleTests/ConsolePadTests.swift
@@ -62,8 +62,7 @@ final class ConsolePadTests: XCTestCase {
let driver = MockConsoleDriver(scenario: .full, animated: false)
let gamepad = LauncherGamepad()
let host = NSHostingView(rootView: ConsoleView(driver: driver, gamepad: gamepad).frame(width: 1280, height: 800))
- let window = NSWindow(contentRect: NSRect(x: -10000, y: 0, width: 1280, height: 800), styleMask: [.borderless], backing: .buffered, defer: false)
- window.isReleasedWhenClosed = false
+ let window = OffscreenWindow(size: NSSize(width: 1280, height: 800), styleMask: [.borderless])
window.contentView = host
window.orderBack(nil)
defer { window.orderOut(nil); window.contentView = nil; window.close() }
@@ -102,8 +101,7 @@ final class ConsolePadTests: XCTestCase {
if name == "options" { driver.openOptions("demo.0") }
if name == "home-4pads" { driver.back() }
let host = NSHostingView(rootView: ConsoleView(driver: driver, lang: .en, glyphs: .init(family: family)).transaction { $0.disablesAnimations = true }.frame(width: width, height: height))
- let window = NSWindow(contentRect: NSRect(x: -10000, y: 0, width: width, height: height), styleMask: [.borderless], backing: .buffered, defer: false)
- window.isReleasedWhenClosed = false
+ let window = OffscreenWindow(size: NSSize(width: width, height: height), styleMask: [.borderless])
window.contentView = host
window.orderBack(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.25))
@@ -187,8 +185,7 @@ final class ConsolePadTests: XCTestCase {
func shot(_ view: AnyView, _ name: String, _ width: CGFloat, _ height: CGFloat) throws {
// a titled window, as the launcher's own screenshots use: the launcher draws under its transparent title bar
let host = NSHostingView(rootView: view)
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: width, height: height), styleMask: [.titled], backing: .buffered, defer: false)
- window.isReleasedWhenClosed = false
+ let window = OffscreenWindow(size: NSSize(width: width, height: height))
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(1.0))
diff --git a/frontend/Tests/NX2AppleTests/ConsoleTests.swift b/frontend/Tests/NX2AppleTests/ConsoleTests.swift
index e328b6db..260b35a6 100644
--- a/frontend/Tests/NX2AppleTests/ConsoleTests.swift
+++ b/frontend/Tests/NX2AppleTests/ConsoleTests.swift
@@ -213,8 +213,7 @@ final class ConsoleTests: XCTestCase {
driver.back()
let view = ContentView(model: model, library: library, performStartupChecks: false, controllerDriver: AnyConsoleDriver(driver))
let host = NSHostingView(rootView: view)
- let window = NSWindow(contentRect: NSRect(x: -10000, y: 0, width: width, height: 800), styleMask: [.borderless], backing: .buffered, defer: false)
- window.isReleasedWhenClosed = false
+ let window = OffscreenWindow(size: NSSize(width: width, height: 800), styleMask: [.borderless])
window.contentView = host
window.orderBack(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.2))
@@ -259,8 +258,7 @@ final class ConsoleTests: XCTestCase {
view = AnyView(ConsoleConnectionPrompt(lang: lang, enterConsole: {}, dismissPrompt: {}).environmentObject(PadFocus()).preferredColorScheme(.dark))
} else { view = AnyView(ConsoleView(driver: driver, lang: lang).transaction { $0.disablesAnimations = true }) }
let host = NSHostingView(rootView: view.frame(width: width, height: height))
- let window = NSWindow(contentRect: NSRect(x: -10000, y: 0, width: width, height: height), styleMask: [.borderless], backing: .buffered, defer: false)
- window.isReleasedWhenClosed = false
+ let window = OffscreenWindow(size: NSSize(width: width, height: height), styleMask: [.borderless])
window.contentView = host
window.orderBack(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.12))
@@ -285,8 +283,7 @@ final class ConsoleTests: XCTestCase {
let (width, height): (CGFloat, CGFloat) = (1280, 800)
let driver = MockConsoleDriver(scenario: .full, lang: .en, animated: false)
let host = NSHostingView(rootView: ConsoleView(driver: driver, lang: .en).transaction { $0.disablesAnimations = true }.frame(width: width, height: height))
- let window = NSWindow(contentRect: NSRect(x: -10000, y: 0, width: width, height: height), styleMask: [.borderless], backing: .buffered, defer: false)
- window.isReleasedWhenClosed = false
+ let window = OffscreenWindow(size: NSSize(width: width, height: height), styleMask: [.borderless])
window.contentView = host
window.orderBack(nil)
defer { window.orderOut(nil); window.contentView = nil; window.close() }
diff --git a/frontend/Tests/NX2AppleTests/DropFilesTests.swift b/frontend/Tests/NX2AppleTests/DropFilesTests.swift
index 7497131d..6511b24a 100644
--- a/frontend/Tests/NX2AppleTests/DropFilesTests.swift
+++ b/frontend/Tests/NX2AppleTests/DropFilesTests.swift
@@ -220,7 +220,7 @@ final class DropFilesTests: XCTestCase {
.modifier(GameFilesDrop(enabled: true, label: model.t("drop.hint"), targeted: true) { _ in })
.frame(width: 1100, height: 760).background(LauncherPalette.background).environment(\.colorScheme, .dark)
let host = NSHostingView(rootView: page)
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1100, height: 760), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 1100, height: 760))
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.5))
diff --git a/frontend/Tests/NX2AppleTests/FrameTimesTests.swift b/frontend/Tests/NX2AppleTests/FrameTimesTests.swift
index cca294e3..b05d4d43 100644
--- a/frontend/Tests/NX2AppleTests/FrameTimesTests.swift
+++ b/frontend/Tests/NX2AppleTests/FrameTimesTests.swift
@@ -179,7 +179,7 @@ final class FrameTimesTests: XCTestCase {
let model = BuildModel()
model.lang = .en
let host = NSHostingView(rootView: LaunchOptionsView(m: model, library: library, gameID: library.games[0].id).environmentObject(PadFocus()).environmentObject(model.localizer))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 900, height: 1300), styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 900, height: 1300), styleMask: [.titled, .closable, .resizable])
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(1.0))
@@ -192,7 +192,7 @@ final class FrameTimesTests: XCTestCase {
let rows = NSHostingView(rootView: VStack(spacing: 0) {
ForEach(FrameTimes.recordings(in: folder), id: \.self) { FrameTimesRecordingView(file: $0, t: model.t) }
}.frame(width: 760).background(Color(white: 0.09)))
- let rowsWindow = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 760, height: 520), styleMask: [.titled], backing: .buffered, defer: false)
+ let rowsWindow = OffscreenWindow(size: NSSize(width: 760, height: 520))
rowsWindow.contentView = rows
rowsWindow.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(1.0))
diff --git a/frontend/Tests/NX2AppleTests/KeyCheckTests.swift b/frontend/Tests/NX2AppleTests/KeyCheckTests.swift
index 366291a2..206aa978 100644
--- a/frontend/Tests/NX2AppleTests/KeyCheckTests.swift
+++ b/frontend/Tests/NX2AppleTests/KeyCheckTests.swift
@@ -183,7 +183,7 @@ final class KeyCheckTests: XCTestCase {
model.slots[1].paths = withUpdate ? [update] : []
model.slots[3].paths = [try keysFile(keys)]
let host = NSHostingView(rootView: ContentView(model: model, library: library, initialPage: .builds, performStartupChecks: false))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1440, height: 1024), styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 1440, height: 1024), styleMask: [.titled, .closable, .resizable])
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.8))
diff --git a/frontend/Tests/NX2AppleTests/LauncherControlWidthTests.swift b/frontend/Tests/NX2AppleTests/LauncherControlWidthTests.swift
index a5cef2ce..db6a4f62 100644
--- a/frontend/Tests/NX2AppleTests/LauncherControlWidthTests.swift
+++ b/frontend/Tests/NX2AppleTests/LauncherControlWidthTests.swift
@@ -166,7 +166,7 @@ final class LauncherControlWidthTests: XCTestCase {
.environment(\.measuresLauncherControls, true)
.onPreferenceChange(LauncherControlSamples.self) { samples.all = $0 }
let host = NSHostingView(rootView: view)
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1100, height: 2600), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 1100, height: 2600))
window.contentView = host
window.orderFront(nil)
defer { window.orderOut(nil) }
@@ -204,7 +204,7 @@ final class LauncherControlWidthTests: XCTestCase {
let samples = Samples()
let host = NSHostingView(rootView: view.launcherRowControls().environment(\.measuresLauncherControls, true)
.onPreferenceChange(LauncherControlSamples.self) { samples.all = $0 }.frame(width: 400, height: 200))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 200), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 400, height: 200))
window.contentView = host
window.orderFront(nil)
defer { window.orderOut(nil) }
@@ -277,7 +277,7 @@ final class LauncherControlWidthTests: XCTestCase {
.environment(\.measuresLauncherControls, true)
.onPreferenceChange(LauncherControlSamples.self) { samples.all = $0 }
let host = NSHostingView(rootView: view)
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 600, height: 700), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 600, height: 700))
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.3))
diff --git a/frontend/Tests/NX2AppleTests/LauncherDropdownTests.swift b/frontend/Tests/NX2AppleTests/LauncherDropdownTests.swift
index 2441b03a..7ea7d106 100644
--- a/frontend/Tests/NX2AppleTests/LauncherDropdownTests.swift
+++ b/frontend/Tests/NX2AppleTests/LauncherDropdownTests.swift
@@ -97,7 +97,7 @@ final class LauncherDropdownTests: XCTestCase {
}
.padding(24)
let host = NSHostingView(rootView: ZStack { LauncherPalette.background; rows }.preferredColorScheme(.dark).frame(width: width, height: height))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: width, height: height), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: width, height: height))
window.contentView = host
window.appearance = NSAppearance(named: .darkAqua)
window.orderFront(nil)
diff --git a/frontend/Tests/NX2AppleTests/LauncherPerformanceTests.swift b/frontend/Tests/NX2AppleTests/LauncherPerformanceTests.swift
index 08067467..97d64d19 100644
--- a/frontend/Tests/NX2AppleTests/LauncherPerformanceTests.swift
+++ b/frontend/Tests/NX2AppleTests/LauncherPerformanceTests.swift
@@ -140,9 +140,7 @@ final class LauncherPerformanceTests: XCTestCase {
library.selectedID = world.selected
let pad = LauncherGamepad()
let host = NSHostingView(rootView: ContentView(model: model, library: library, performStartupChecks: false, gamepad: pad))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 900), styleMask: [.titled, .closable, .resizable],
- backing: .buffered, defer: false)
- window.isReleasedWhenClosed = false
+ let window = OffscreenWindow(size: NSSize(width: 1280, height: 900), styleMask: [.titled, .closable, .resizable])
window.contentView = host
window.orderFront(nil)
defer { window.orderOut(nil) }
diff --git a/frontend/Tests/NX2AppleTests/LibraryTests.swift b/frontend/Tests/NX2AppleTests/LibraryTests.swift
index 562beede..86f339d2 100644
--- a/frontend/Tests/NX2AppleTests/LibraryTests.swift
+++ b/frontend/Tests/NX2AppleTests/LibraryTests.swift
@@ -631,7 +631,7 @@ final class LibraryTests: XCTestCase {
model.slots[3].paths = [keys.path]
let library = GameLibrary(file: temporary.appendingPathComponent("library.json"))
let host = NSHostingView(rootView: ContentView(model: model, library: library, initialPage: .builds, performStartupChecks: false))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1100, height: 800), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 1100, height: 800))
window.contentView = host
window.orderFront(nil)
defer { window.orderOut(nil) }
@@ -702,7 +702,7 @@ final class LibraryTests: XCTestCase {
/// The view at 2× in a window-shaped frame: rounded corners, the traffic lights, a soft shadow, transparent around.
func shot<V: View>(_ name: String, _ view: V, size: NSSize, trafficLights: Bool = true) throws {
let host = NSHostingView(rootView: view)
- let window = NSWindow(contentRect: NSRect(origin: .zero, size: size), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: size)
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(1.2)) // artwork, layout
@@ -809,8 +809,7 @@ final class LibraryTests: XCTestCase {
let view = ContentView(model: model, library: store, initialPage: page, activeBuildID: activeID, performStartupChecks: false, search: search,
olderTools: older, manageOpen: manageOpen, padRing: padRing)
let host = NSHostingView(rootView: view)
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: width, height: height),
- styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: width, height: height), styleMask: [.titled, .closable, .resizable])
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.5))
@@ -910,7 +909,7 @@ final class LibraryTests: XCTestCase {
model.running = running
let sheet = NSHostingView(rootView: FreeSpaceView(m: model, game: library.games[0], survey: measured, chosen: [.extracted, .oldShaders])
.environmentObject(PadFocus()).environmentObject(model.localizer))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 560, height: 690), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 560, height: 690))
window.contentView = sheet
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.4))
@@ -927,7 +926,7 @@ final class LibraryTests: XCTestCase {
try FileManager.default.copyItem(at: URL(fileURLWithPath: json), to: dm.appendingPathComponent("launcher.json"))
if let fps = LaunchSetting.read(app: first.appPath)?.first(where: { $0.env == "DM_FPS_TARGET" }) { SavedChoices(app: first.appPath, titleID: first.titleID).set(fps, "60") }
let host = NSHostingView(rootView: LaunchOptionsView(m: model, library: library, gameID: first.id).environmentObject(PadFocus()).environmentObject(model.localizer))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 600, height: 700), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 600, height: 700))
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.5))
@@ -958,7 +957,7 @@ final class LibraryTests: XCTestCase {
if let up = ControlLayout.functions.first(where: { $0.id == "RUp" }) { layout.assign(up, key: 34) }
layout.setSends("A", "B"); layout.setSends("B", "A")
let host = NSHostingView(rootView: ControlsView(layout: layout).environmentObject(PadFocus()).environmentObject(model.localizer))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 600, height: 700), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 600, height: 700))
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.5))
@@ -975,7 +974,7 @@ final class LibraryTests: XCTestCase {
model.slots[0].paths = []
let view = ContentView(model: model, library: crowded, performStartupChecks: false)
let crowdedHost = NSHostingView(rootView: view)
- let crowdedWindow = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1280, height: 900), styleMask: [.titled], backing: .buffered, defer: false)
+ let crowdedWindow = OffscreenWindow(size: NSSize(width: 1280, height: 900))
crowdedWindow.contentView = crowdedHost
crowdedWindow.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.5))
@@ -998,7 +997,7 @@ final class LibraryTests: XCTestCase {
LauncherProgressBar(value: nil, height: 6).frame(width: 180)
}.padding(30).background(LauncherPalette.background))
bars.frame = NSRect(x: 0, y: 0, width: 540, height: 130)
- let barsWindow = NSWindow(contentRect: bars.frame, styleMask: [.titled], backing: .buffered, defer: false)
+ let barsWindow = OffscreenWindow(size: bars.frame.size)
barsWindow.contentView = bars
barsWindow.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.3))
@@ -1024,7 +1023,7 @@ final class LibraryTests: XCTestCase {
layout.setPadMode(mode)
let focus = PadFocus()
let host = NSHostingView(rootView: ControlsView(layout: layout).environmentObject(focus).environmentObject(model.localizer))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 600, height: 700), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 600, height: 700))
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.4))
@@ -1045,7 +1044,7 @@ final class LibraryTests: XCTestCase {
DiagnosticsButton(m: model, showCopied: true)
}.launcherRowControls().padding(24).background(LauncherPalette.background).environmentObject(PadFocus()).environmentObject(model.localizer))
buttons.frame = NSRect(x: 0, y: 0, width: 420, height: 90)
- let window = NSWindow(contentRect: buttons.frame, styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: buttons.frame.size)
window.contentView = buttons
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.3))
@@ -1082,7 +1081,7 @@ final class LibraryTests: XCTestCase {
[AppleDevice(id: "00008150-AAAA", name: "Zijad\u{2019}s iPhone", model: "iPhone 17", os: "27.0", reachable: true),
AppleDevice(id: "00008150-BBBB", name: "Zijad\u{2019}s iPad", model: "iPad Pro", os: "27.0", reachable: true)]
}).environmentObject(focus).environmentObject(model.localizer))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 600, height: 520), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 600, height: 520))
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.6))
diff --git a/frontend/Tests/NX2AppleTests/OffscreenWindow.swift b/frontend/Tests/NX2AppleTests/OffscreenWindow.swift
new file mode 100644
index 00000000..6d01acb8
--- /dev/null
+++ b/frontend/Tests/NX2AppleTests/OffscreenWindow.swift
@@ -0,0 +1,63 @@
+import AppKit
+import CoreGraphics
+import XCTest
+
+/// The window every rendering test uses. A test needs a live window so SwiftUI lays out and `cacheDisplay` draws, never one anybody can see.
+/// It sits far outside every display, is never key or main, and AppKit may not pull it back onto a screen. Ordering it in does not
+/// activate the app. The titled style stays the default because the launcher draws under its transparent title bar.
+final class OffscreenWindow: NSWindow {
+ /// Far outside any arrangement of displays, in both axes.
+ static let origin = NSPoint(x: -100_000, y: -100_000)
+
+ init(size: NSSize, styleMask: NSWindow.StyleMask = [.titled]) {
+ super.init(contentRect: NSRect(origin: Self.origin, size: size), styleMask: styleMask, backing: .buffered, defer: false)
+ isReleasedWhenClosed = false
+ // a titled window was already moved onto a screen by the initializer, so put it back
+ setFrame(NSRect(origin: Self.origin, size: frame.size), display: false)
+ }
+
+ override var canBecomeKey: Bool { false }
+ override var canBecomeMain: Bool { false }
+ /// A titled window is moved back onto a screen when it is ordered in. This one stays where it is.
+ override func constrainFrameRect(_ frameRect: NSRect, to screen: NSScreen?) -> NSRect { frameRect }
+
+ /// The windows of this process that a person could see: shown, and with their frame on a display.
+ @MainActor static func visibleOnScreen() -> [NSWindow] {
+ let displays = NSScreen.screens.map(\.frame)
+ return NSApp.windows.filter { window in window.isVisible && displays.contains { $0.intersects(window.frame) } }
+ }
+
+ /// The same question asked of the window server: windows of this process on a display, whatever AppKit thinks.
+ static func serverWindowsOnDisplays() -> [[String: Any]] {
+ let displays = NSScreen.screens.map(\.frame)
+ let mainHeight = NSScreen.screens.first?.frame.height ?? 0
+ let list = (CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as? [[String: Any]]) ?? []
+ return list.filter { info in
+ guard (info[kCGWindowOwnerPID as String] as? Int32) == ProcessInfo.processInfo.processIdentifier,
+ let bounds = (info[kCGWindowBounds as String] as? NSDictionary).flatMap({ CGRect(dictionaryRepresentation: $0 as CFDictionary) }) else { return false }
+ // CGWindow bounds have the origin at the top left of the main display, NSScreen frames at the bottom left: test both
+ let flipped = CGRect(x: bounds.minX, y: mainHeight - bounds.maxY, width: bounds.width, height: bounds.height)
+ return displays.contains { $0.intersects(bounds) || $0.intersects(flipped) }
+ }
+ }
+}
+
+final class OffscreenWindowTests: XCTestCase {
+ /// Windows of the helper are shown (as the render tests do) and must be on no display, for AppKit and for the window server.
+ @MainActor func testRenderWindowsAreNeverOnAnyDisplay() {
+ NSApplication.shared.setActivationPolicy(.accessory)
+ let windows = [OffscreenWindow(size: NSSize(width: 1440, height: 1500)),
+ OffscreenWindow(size: NSSize(width: 1280, height: 800), styleMask: [.borderless]),
+ OffscreenWindow(size: NSSize(width: 900, height: 1300), styleMask: [.titled, .closable, .resizable])]
+ for window in windows {
+ window.contentView = NSView(frame: NSRect(origin: .zero, size: window.contentLayoutRect.size))
+ window.orderFront(nil)
+ }
+ defer { for window in windows { window.orderOut(nil); window.contentView = nil; window.close() } }
+ RunLoop.main.run(until: Date().addingTimeInterval(0.3))
+ for window in windows { XCTAssertTrue(window.isVisible, "ordered in, so layout and drawing work") }
+ XCTAssertEqual(OffscreenWindow.visibleOnScreen(), [], "no window of the tests is on a display")
+ XCTAssertEqual(OffscreenWindow.serverWindowsOnDisplays().count, 0, "the window server shows none either")
+ XCTAssertNil(NSApp.keyWindow)
+ }
+}
diff --git a/frontend/Tests/NX2AppleTests/OnboardingTests.swift b/frontend/Tests/NX2AppleTests/OnboardingTests.swift
index c2288cab..1a658664 100644
--- a/frontend/Tests/NX2AppleTests/OnboardingTests.swift
+++ b/frontend/Tests/NX2AppleTests/OnboardingTests.swift
@@ -190,8 +190,7 @@ final class OnboardingTests: XCTestCase {
let driver = MockOnboardingDriver(state: s, lang: lang, animated: false)
let view = OnboardingView(driver: driver, lang: lang)
let host = NSHostingView(rootView: view)
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 900, height: 680),
- styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 900, height: 680), styleMask: [.titled, .closable, .resizable])
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.4))
@@ -214,8 +213,7 @@ final class OnboardingTests: XCTestCase {
s.background = [.python: .done, .dotnet: .running(0.5), .tools: .queued]
let driver = MockOnboardingDriver(state: s, lang: .en, animated: false)
let host = NSHostingView(rootView: OnboardingView(driver: driver, lang: .en))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 900, height: 680),
- styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 900, height: 680), styleMask: [.titled, .closable, .resizable])
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.4))
@@ -236,8 +234,7 @@ final class OnboardingTests: XCTestCase {
s.osTooOldForNVN = true; s.xcodeTooOldForNVN = true
let driver = MockOnboardingDriver(state: s, lang: lang, animated: false)
let host = NSHostingView(rootView: OnboardingView(driver: driver, lang: lang))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 900, height: 680),
- styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: 900, height: 680), styleMask: [.titled, .closable, .resizable])
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.4))
diff --git a/frontend/Tests/NX2AppleTests/ReleaseNotesTests.swift b/frontend/Tests/NX2AppleTests/ReleaseNotesTests.swift
index 111c25df..7a4142f2 100644
--- a/frontend/Tests/NX2AppleTests/ReleaseNotesTests.swift
+++ b/frontend/Tests/NX2AppleTests/ReleaseNotesTests.swift
@@ -207,7 +207,7 @@ final class ReleaseNotesTests: XCTestCase {
m.appUpdate = AppUpdate.Release(tag: tag, url: "", digest: nil, notes: notes)
let width: CGFloat = 560, height: CGFloat = 640
let host = NSHostingView(rootView: UpdateSheet(m: m).environmentObject(m.localizer).frame(width: width, height: height))
- let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: width, height: height), styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: NSSize(width: width, height: height))
window.contentView = host
window.appearance = NSAppearance(named: .darkAqua)
window.orderFront(nil)
diff --git a/frontend/Tests/NX2AppleTests/StaleFrameworksTests.swift b/frontend/Tests/NX2AppleTests/StaleFrameworksTests.swift
index 9d8d3861..2a818dd1 100644
--- a/frontend/Tests/NX2AppleTests/StaleFrameworksTests.swift
+++ b/frontend/Tests/NX2AppleTests/StaleFrameworksTests.swift
@@ -150,7 +150,7 @@ final class StaleFrameworksTests: XCTestCase {
NSApplication.shared.setActivationPolicy(.accessory)
let host = NSHostingView(rootView: alert)
host.frame = NSRect(origin: .zero, size: host.fittingSize)
- let window = NSWindow(contentRect: host.frame, styleMask: [.titled], backing: .buffered, defer: false)
+ let window = OffscreenWindow(size: host.frame.size)
window.contentView = host
window.orderFront(nil)
RunLoop.main.run(until: Date().addingTimeInterval(0.4))