Testing continuity on the iPhone Duo simulator
Three layers, from cheapest to closest to a real fold:
- a UI test that flips the size class from inside the app;
- a UI test that waits while you press the pose buttons and rotate in Device Hub;
- a manual pass in Device Hub through the matrix below.
What Xcode 27.1 can and can't do from code
| Action | From code? | How |
|---|---|---|
| Rotate | Not on the closed Duo | Device Hub's rotate button. Measured: on the closed Duo simulator, setting XCUIDevice.shared.orientation to landscape changed the device orientation but not the app's 466 × 678 pt window (this skill's run and duo-testing's). Open: unverified. From inside the app, measured: requestGeometryUpdate(_:errorHandler:) on the open inner display failed with "The current windowing mode does not allow for programmatic changes to interface orientation." |
| Close, open, partially open | Not from a test | Device Hub buttons only. XCUIDevice has no fold or hinge property, and simctl and devicectl have no pose command (checked in Xcode 27.1). A Mac tool outside the test can press the buttons through the Accessibility API, with the user's Accessibility permission (see duo-setup). |
| Any hinge angle | No | Hold Option and drag the slider under the bezel |
| Split View | No | Drag the app by its home indicator to one side (Tech Talk 111461) |
| Size class change | Yes, inside your app | The debug flip below |
| Two-finger gestures | No | Device Hub can't send them (Xcode 27 known issue); test on a device |
Manual pass in Device Hub
Run the app on the iPhone Duo simulator from Xcode; it opens in Device Hub. The three pose buttons (Closed, Partially Open, Open) sit under the bezel, to the right of the rotate controls. Click the rotate button to rotate left and Option-click it to rotate right (Interacting with your app in Device Hub). Device > Slow Animations helps you watch each step.
Set up this state first, then run each sequence and check every column after each step:
| Set up | Check after each step |
|---|---|
| Scroll a long list or grid halfway | The same item is at the top |
| Select an item and push one screen in the detail | Same item, same pushed screen; Back still works |
| Type in a field, select a word, keep the keyboard up | Same text, same selection; note what the keyboard does |
| Fill half a form | Every field kept |
| Start video or audio | Still playing, same time, no restart |
| Start an upload | Still running, finishes once |
| Open a sheet | Still open, same content |
| Sign in | Still signed in, no second sign-in screen |
Sequences (each arrow is one button press or rotation):
- Open → Closed → Open
- Open → Partially Open → Open
- Closed portrait → rotate → Open → Closed
- Open → rotate → Closed → rotate → Open
- Open → Split View on one side → Closed → Open
- Open → Closed → Open → Closed → Open, quickly, while scrolling
- Open → Closed while typing, then Open while typing
Android's large-screen quality tests call these state and combination checks (adaptive app quality, T-Config_State and T-Config_Combinations). The idea carries over; the rules are Android's.
The debug flip: app side
A launch switch, ignored in release builds, adds two things at the window root:
- a button that overrides the horizontal size class, so your own size-class code and
NavigationSplitViewtake the same path a fold takes; - hidden labels: a one-line summary of the state that must survive, the current size class, and the window's size in points.
import SwiftUI
enum ContinuityTesting {
#if DEBUG
static let isOn = ProcessInfo.processInfo.arguments.contains("-continuityTesting")
#else
static let isOn = false
#endif
}
struct ContinuityHarness: ViewModifier {
let summary: String // built from your model
@State private var override: UserInterfaceSizeClass?
@State private var size = CGSize.zero
@Environment(\.horizontalSizeClass) private var real
func body(content: Content) -> some View {
if ContinuityTesting.isOn {
content
.environment(\.horizontalSizeClass, override ?? real)
.frame(maxWidth: .infinity, maxHeight: .infinity) // fill the window, however small the content
.background {
Color.clear
.onGeometryChange(for: CGSize.self) { $0.size } action: { size = $0 }
.ignoresSafeArea() // keep this last: the reader then sees the whole window
}
.overlay(alignment: .bottomLeading) {
VStack(alignment: .leading) {
Button("Flip size class") {
override = (override ?? real) == .compact ? .regular : .compact
}
.accessibilityIdentifier("continuity.flip")
Text((override ?? real) == .compact ? "compact" : "regular")
.accessibilityIdentifier("continuity.sizeClass")
Text("\(Int(size.width))x\(Int(size.height))")
.accessibilityIdentifier("continuity.windowSize")
Text(summary)
.accessibilityIdentifier("continuity.summary")
}
.font(.caption2)
.opacity(0.02) // present for tests, invisible to people
}
} else {
content
}
}
}
extension View {
func continuityHarness(summary: String) -> some View {
modifier(ContinuityHarness(summary: summary))
}
}Use it on the window root, with a summary built from the model:
import SwiftUI
import Observation
@MainActor @Observable
final class ReaderModel {
var selection: Int?
var topItem: Int?
var draft = ""
var isPlaying = false
var summary: String {
"sel=\(selection.map(String.init) ?? "-") top=\(topItem.map(String.init) ?? "-") "
+ "draft=\(draft.count) playing=\(isPlaying)"
}
}
struct ReaderRoot: View {
@State private var model = ReaderModel()
var body: some View {
Text("Your root view")
.environment(model)
.continuityHarness(summary: model.summary)
}
}- The
ifin the harness reads a launch switch that never changes while the app runs, so it never changes identity. - The size label must show the window, not your content or the safe area. Measured on the closed Duo simulator: the window was 466 × 678 pt with 84 pt right and 34 pt bottom safe-area insets. A reader on the content saw the text's own 108 × 20 pt; a reader placed after
.ignoresSafeArea()saw 382 × 644 pt; the reader above, inside.ignoresSafeArea(), saw 466 × 678 pt. - Measured: this kind of override made
NavigationSplitViewcollapse and expand, and switchedif/elsebranches, on the iOS 27.1 simulator (evidence.md). - The override changes the size class only. It doesn't move the window to the other display, resize it, or touch the keyboard. Keep the Device Hub pass.
- UIKit: set
traitOverrides.horizontalSizeClasson the root view controller instead (traitOverrides, iOS 17). Measured: it ranregisterForTraitChangeshandlers and changed a compositional layout's column count.
The UI tests
The first test runs on its own. It doesn't rotate, because rotation from a UI test doesn't resize the app on the closed Duo (see the table above). The second test waits for you to fold and rotate in Device Hub, so it is skipped unless you ask for it.
import XCTest
// `nonisolated` keeps XCTestCase's initializers valid when the target defaults to MainActor isolation.
nonisolated final class ContinuityUITests: XCTestCase {
@MainActor
private func launch() -> XCUIApplication {
let app = XCUIApplication()
app.launchArguments += ["-continuityTesting"]
app.launch()
return app
}
@MainActor
private func waitForChange(of element: XCUIElement, from old: String, timeout: TimeInterval) -> Bool {
let changed = XCTNSPredicateExpectation(predicate: NSPredicate(format: "label != %@", old), object: element)
return XCTWaiter().wait(for: [changed], timeout: timeout) == .completed
}
/// Flip the size class there and back, and check the summary never changes.
@MainActor
func testStateSurvivesSizeClassFlip() throws {
let app = launch()
let summary = app.staticTexts["continuity.summary"]
let sizeClass = app.staticTexts["continuity.sizeClass"]
XCTAssertTrue(summary.waitForExistence(timeout: 10))
// Build up state here: scroll, select, type, start playback.
let before = summary.label
let start = sizeClass.label
let flipped = start == "compact" ? "regular" : "compact"
app.buttons["continuity.flip"].tap()
XCTAssertTrue(sizeClass.wait(for: \.label, toEqual: flipped, timeout: 5))
XCTAssertEqual(summary.label, before, "state changed on a size-class flip")
app.buttons["continuity.flip"].tap()
XCTAssertTrue(sizeClass.wait(for: \.label, toEqual: start, timeout: 5))
XCTAssertEqual(summary.label, before, "state changed on the flip back")
}
/// Real fold and rotation. Run with TEST_RUNNER_DUO_MANUAL_FOLD=1 and use Device Hub when asked.
/// Works from either pose: it asks for the other pose first, then a rotation, then the first pose again.
@MainActor
func testStateSurvivesRealFold() throws {
try XCTSkipUnless(ProcessInfo.processInfo.environment["DUO_MANUAL_FOLD"] == "1",
"Manual test: needs someone to press the pose buttons in Device Hub.")
let app = launch()
let summary = app.staticTexts["continuity.summary"]
let sizeClass = app.staticTexts["continuity.sizeClass"]
let windowSize = app.staticTexts["continuity.windowSize"]
XCTAssertTrue(summary.waitForExistence(timeout: 10))
let before = summary.label
let start = sizeClass.label // "compact" when closed, "regular" when open
let other = start == "compact" ? "regular" : "compact"
var size = windowSize.label
XCTContext.runActivity(named: start == "compact" ? "Press Open in Device Hub" : "Press Closed in Device Hub") { _ in
XCTAssertTrue(sizeClass.wait(for: \.label, toEqual: other, timeout: 60))
XCTAssertTrue(waitForChange(of: windowSize, from: size, timeout: 5), "the fold did not resize the window")
XCTAssertEqual(summary.label, before, "state changed on the first fold")
}
size = windowSize.label
XCTContext.runActivity(named: "Click the rotate button in Device Hub") { _ in
XCTAssertTrue(waitForChange(of: windowSize, from: size, timeout: 60), "rotation did not resize the window")
XCTAssertEqual(summary.label, before, "state changed on rotation")
}
XCTContext.runActivity(named: start == "compact" ? "Press Closed in Device Hub" : "Press Open in Device Hub") { _ in
XCTAssertTrue(sizeClass.wait(for: \.label, toEqual: start, timeout: 60))
XCTAssertEqual(summary.label, before, "state changed on the second fold")
}
}
}Run them:
# Automatic test
DEVELOPER_DIR=/Applications/Xcode-27.1.app/Contents/Developer xcodebuild test \
-scheme YourApp -destination 'platform=iOS Simulator,name=iPhone Duo,OS=27.1' \
-parallel-testing-enabled NO \
-only-testing:YourAppUITests/ContinuityUITests/testStateSurvivesSizeClassFlip
# Manual fold test: keep Device Hub visible; fold and rotate when the test asks
TEST_RUNNER_DUO_MANUAL_FOLD=1 DEVELOPER_DIR=/Applications/Xcode-27.1.app/Contents/Developer xcodebuild test \
-scheme YourApp -destination 'platform=iOS Simulator,name=iPhone Duo,OS=27.1' \
-parallel-testing-enabled NO \
-only-testing:YourAppUITests/ContinuityUITests/testStateSurvivesRealFold-parallel-testing-enabled NOkeeps the run on the simulator you see in Device Hub. With parallel testing on, xcodebuild may run the tests on a clone: one more simulator on disk, in its own pose, where the manual test waits for a fold nobody can make.xcodebuildpasses anyTEST_RUNNER_variable to the test runner with the prefix removed (man xcodebuild, Xcode 27.1).- wait(for:toEqual:timeout:) polls a key path until it matches. XCTSkipUnless keeps the manual test out of normal runs.
- The summary should hold only what must not change. Leave the size class out of it.
- Measured: the automatic test passed on a closed iPhone Duo simulator (iOS 27.1), in an app target with Swift 6 and MainActor default isolation. It found the labels at 2% opacity. The manual test skipped itself when the variable was unset.
- Unverified: the manual test's fold and rotate steps, because they need a person at Device Hub. Whether Device Hub's rotate button resizes your app depends on the display and your supported orientations (Tech Talk 111461). Delete the rotate step if your app doesn't rotate in that pose.
duo-testinghas general tips for UI tests on the Duo simulator.
Quick checks without a test target
- Print the state summary from
onChange(of: sizeClass)and watch the console while you press the Device Hub buttons. - Add
let _ = Self._printChanges()to a view body while debugging. It prints why the body ran.@identityin that line means the view got a new identity. Measured: after a flip, a view inside anif/elseprinted@self, @identity, __count changed., and the same view insideAnyLayoutprinted@self changed.The underscore marks it as not public API: use it in debug builds only, and don't parse its output.