From 42378d12dd5655bde79d93ae9f00129f73aa4250 Mon Sep 17 00:00:00 2001 From: Radoslaw Nowacki Date: Tue, 22 Sep 2026 09:01:18 +0200 Subject: [PATCH 1/2] fix(ios): pop only the topmost React Native screen on popToNative MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit popToNative() is posted as an app-wide NotificationCenter notification with no sender, so every live React Native surface acted on it — including ones buried under other screens. In a stack like RN -> native -> RN, one call tore down everything, taking the native screen in between with it. On top of that, a SwiftUI-hosted surface handled the notification twice: once in ReactNativeView via dismiss(), and again in the ReactNativeViewController it wraps via popViewController. BrownfieldSurfaceRegistry now picks a single surface — the most recently shown one that is currently on screen — and only that surface acts, once. Visibility is read live (view.window) rather than cached from viewWillAppear, because SwiftUI recreates hosted controllers and a cached flag is stale exactly when the notification arrives. The registry has no UIKit dependency, so it runs under `swift test` on macOS in the existing iOS native tests CI job. Fixes #354 Co-Authored-By: Claude Opus 5 (1M context) --- .changeset/pop-to-native-topmost-surface.md | 14 ++ .../ios/Views/ReactNativeView.swift | 39 ++++- .../ios/Views/ReactNativeViewController.swift | 121 +++++++++++++- .../ios/swiftpm/Package.swift | 13 ++ .../BrownfieldSurfaceRegistry.swift | 93 +++++++++++ .../BrownfieldSurfaceRegistryTests.swift | 157 ++++++++++++++++++ 6 files changed, 431 insertions(+), 6 deletions(-) create mode 100644 .changeset/pop-to-native-topmost-surface.md create mode 100644 packages/react-native-brownfield/ios/swiftpm/Sources/BrownfieldNavigationSupport/BrownfieldSurfaceRegistry.swift create mode 100644 packages/react-native-brownfield/ios/swiftpm/Tests/BrownfieldNavigationSupportTests/BrownfieldSurfaceRegistryTests.swift diff --git a/.changeset/pop-to-native-topmost-surface.md b/.changeset/pop-to-native-topmost-surface.md new file mode 100644 index 000000000..b9fe9bdd4 --- /dev/null +++ b/.changeset/pop-to-native-topmost-surface.md @@ -0,0 +1,14 @@ +--- +'@callstack/react-native-brownfield': patch +--- + +Fix `popToNative()` on iOS closing every React Native screen instead of only the topmost one. + +`popToNative()` is delivered as an app-wide notification, so every live React Native surface acted on it — including surfaces buried under other screens, and each surface acted twice when hosted by SwiftUI. In a stack such as RN → native → RN, a single call tore down the whole navigation stack, taking native screens with it. + +A React Native surface now acts only when it is the topmost one on screen, and only once per host. + +Two consequences worth knowing about: + +- When no React Native surface is on screen — for example one hidden behind a full-screen native modal — `popToNative()` is now a no-op instead of popping the hidden screen. It logs when this happens. +- `popToNative()` no longer dismisses a modally presented native screen that merely *contains* a React Native surface. Only a surface that owns a navigation stack entry pops. diff --git a/packages/react-native-brownfield/ios/Views/ReactNativeView.swift b/packages/react-native-brownfield/ios/Views/ReactNativeView.swift index 893e6b6d3..46b2e0d23 100644 --- a/packages/react-native-brownfield/ios/Views/ReactNativeView.swift +++ b/packages/react-native-brownfield/ios/Views/ReactNativeView.swift @@ -1,20 +1,38 @@ import SwiftUI +/** + Links a SwiftUI `ReactNativeView` to the view controller it hosts, so the + SwiftUI layer can ask whether this surface may act on `popToNative()`. + */ +final class BrownfieldSurfaceHost { + weak var controller: ReactNativeViewController? + + var isPopTarget: Bool { controller?.isPopTarget ?? false } +} + /** A UIViewControllerRepresentable that bridges ReactNativeViewController to SwiftUI. */ struct ReactNativeViewRepresentable: UIViewControllerRepresentable { var moduleName: String var initialProperties: [String: Any] = [:] + var host: BrownfieldSurfaceHost func makeUIViewController(context: Context) -> UIViewController { - return ReactNativeViewController( + let controller = ReactNativeViewController( moduleName: moduleName, initialProperties: initialProperties ) + controller.isHostedBySwiftUI = true + host.controller = controller + return controller } - func updateUIViewController(_ uiViewController: UIViewController, context: Context) {} + func updateUIViewController(_ uiViewController: UIViewController, context: Context) { + if let controller = uiViewController as? ReactNativeViewController { + host.controller = controller + } + } } /** @@ -24,6 +42,7 @@ struct ReactNativeViewRepresentable: UIViewControllerRepresentable { @available(iOS 15.0, *) public struct ReactNativeView: View { @Environment(\.dismiss) var dismiss + @State private var host = BrownfieldSurfaceHost() var moduleName: String var initialProperties: [String: Any] = [:] @@ -35,11 +54,21 @@ public struct ReactNativeView: View { public var body: some View { ReactNativeViewRepresentable( moduleName: moduleName, - initialProperties: initialProperties + initialProperties: initialProperties, + host: host ) .ignoresSafeArea(.all) - .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name.popToNative)) - { notification in + // `.popToNative` is public API and may be posted from any thread, while + // `host.isPopTarget` reads UIKit state — so deliver on main. + .onReceive( + NotificationCenter.default + .publisher(for: NSNotification.Name.popToNative) + .receive(on: DispatchQueue.main) + ) { _ in + // Every live surface receives this app-wide notification; only the + // topmost on-screen one may dismiss. + // https://github.com/callstack/react-native-brownfield/issues/354 + guard host.isPopTarget else { return } dismiss() } } diff --git a/packages/react-native-brownfield/ios/Views/ReactNativeViewController.swift b/packages/react-native-brownfield/ios/Views/ReactNativeViewController.swift index bae11046e..d75070f39 100644 --- a/packages/react-native-brownfield/ios/Views/ReactNativeViewController.swift +++ b/packages/react-native-brownfield/ios/Views/ReactNativeViewController.swift @@ -7,6 +7,25 @@ internal import EXUpdates @objc public class ReactNativeViewController: UIViewController { private var moduleName: String private var initialProperties: [String: Any]? + /// Identifies this surface in `BrownfieldSurfaceRegistry`, which decides + /// which single surface may act on `popToNative()`. + private var surfaceID: BrownfieldSurfaceID? + + /// Set by `ReactNativeView`. SwiftUI owns its navigation state, so an + /// imperative `popViewController` is reverted; the SwiftUI host dismisses + /// instead and this controller stands down. + internal var isHostedBySwiftUI = false + + /// True from the start of a disappearance transition until the surface next + /// appears. `view.window` stays non-nil for the whole animation, so this is + /// what distinguishes "the host already dismissed us" from "nothing happened". + private var isDisappearing = false + + /// Whether this surface is the one allowed to act on `popToNative()`. + internal var isPopTarget: Bool { + guard let surfaceID else { return false } + return BrownfieldSurfaceRegistry.shared.isPopTarget(surfaceID) + } #if canImport(EXUpdates) private let expoUpdatesDelegate = ReactNativeExpoUpdatesDelegate() @@ -39,6 +58,12 @@ internal import EXUpdates #if !canImport(EXUpdates) renderReactNativeView() #endif + + // Visibility is resolved live: SwiftUI recreates hosted controllers, so a + // cached lifecycle flag is stale when the notification arrives. + surfaceID = BrownfieldSurfaceRegistry.shared.register { [weak self] in + self?.viewIfLoaded?.window != nil + } NotificationCenter.default.addObserver( self, @@ -56,8 +81,31 @@ internal import EXUpdates } } + public override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + isDisappearing = false + + // Ordered here rather than in `viewDidAppear`: during a modal presentation + // the covered surface is still in a window, so waiting for `viewDidAppear` + // would let it out-rank the surface being presented over it. + if let surfaceID { + BrownfieldSurfaceRegistry.shared.markShown(surfaceID) + } + } + + public override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + isDisappearing = true + } + deinit { NotificationCenter.default.removeObserver(self) + + if let surfaceID { + BrownfieldSurfaceRegistry.shared.unregister(surfaceID) + } } @objc private func togglePopGestureRecognizer(_ notification: Notification) { @@ -73,11 +121,82 @@ internal import EXUpdates guard let userInfo = notification.userInfo, let animated = userInfo["animated"] as? Bool else { return } + // `.popToNative` is public API, so a host may post it from any thread. + // Everything below reads UIKit state — including the registry's visibility + // check — so hop to main first. DispatchQueue.main.async { [weak self] in - self?.navigationController?.popViewController(animated: animated) + guard let self else { return } + + // The notification is app-wide, so every live surface receives it. Only + // the topmost visible one may act, otherwise surfaces buried under other + // screens tear down the whole navigation stack. + // https://github.com/callstack/react-native-brownfield/issues/354 + let target = BrownfieldSurfaceRegistry.shared.popTarget() + + guard let surfaceID = self.surfaceID, target == surfaceID else { + if target == nil { + NSLog("[ReactBrownfield] popToNative(): no React Native surface is on screen; ignoring.") + } + return + } + + guard self.isHostedBySwiftUI else { + self.popFromNativeNavigationStack(animated: animated) + return + } + + // SwiftUI owns its navigation state and reverts an imperative pop, so the + // SwiftUI host dismisses instead. It cannot always succeed though — a + // `ReactNativeView` inside a `UIHostingController` pushed onto a UIKit + // navigation stack has nothing for `dismiss()` to act on. Give the host a + // moment, then fall back, so `popToNative()` is never silently inert. + // + // `isDisappearing` — not the delay — is what makes this safe: a dismiss + // that is merely slow has already begun its transition by the time this + // runs, so a late timer stands down instead of popping a second entry. + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in + guard let self, + !self.isDisappearing, + self.viewIfLoaded?.window != nil, + // Re-established: the entitlement was resolved before the wait. + self.isPopTarget else { return } + + self.popFromNativeNavigationStack(animated: animated) + } } } + /// Pops this surface off the host's `UINavigationController`. + /// + /// Deliberately does not fall back to `dismiss()` when there is no navigation + /// stack: `presentingViewController` is inherited, so an inline surface hosted + /// inside a presented native screen would tear down that whole screen — the + /// same over-dismissal this fix exists to prevent. + private func popFromNativeNavigationStack(animated: Bool) { + guard let navigationController else { return } + + // This controller is usually a child of a host controller (SwiftUI wraps it + // in one), so find the stack entry containing it rather than assuming it is + // itself on the stack — and pop to the entry below that, so a screen pushed + // while we were waiting is not the one that gets popped. + let stack = navigationController.viewControllers + guard let index = stack.firstIndex(where: { $0 === self || self.isDescendant(of: $0) }), + index > 0 else { return } + + navigationController.popToViewController(stack[index - 1], animated: animated) + } + + private func isDescendant(of controller: UIViewController) -> Bool { + var ancestor = parent + + while let current = ancestor { + if current === controller { return true } + ancestor = current.parent + } + + return false + } + private func renderReactNativeView() { guard !moduleName.isEmpty else { return } diff --git a/packages/react-native-brownfield/ios/swiftpm/Package.swift b/packages/react-native-brownfield/ios/swiftpm/Package.swift index 3a8fe1c3f..7294b9771 100644 --- a/packages/react-native-brownfield/ios/swiftpm/Package.swift +++ b/packages/react-native-brownfield/ios/swiftpm/Package.swift @@ -12,6 +12,10 @@ let package = Package( name: "BrownfieldBundleSupport", targets: ["BrownfieldBundleSupport"] ), + .library( + name: "BrownfieldNavigationSupport", + targets: ["BrownfieldNavigationSupport"] + ), ], targets: [ .target( @@ -23,5 +27,14 @@ let package = Package( dependencies: ["BrownfieldBundleSupport"], path: "Tests/BrownfieldBundleSupportTests" ), + .target( + name: "BrownfieldNavigationSupport", + path: "Sources/BrownfieldNavigationSupport" + ), + .testTarget( + name: "BrownfieldNavigationSupportTests", + dependencies: ["BrownfieldNavigationSupport"], + path: "Tests/BrownfieldNavigationSupportTests" + ), ] ) diff --git a/packages/react-native-brownfield/ios/swiftpm/Sources/BrownfieldNavigationSupport/BrownfieldSurfaceRegistry.swift b/packages/react-native-brownfield/ios/swiftpm/Sources/BrownfieldNavigationSupport/BrownfieldSurfaceRegistry.swift new file mode 100644 index 000000000..3ade35213 --- /dev/null +++ b/packages/react-native-brownfield/ios/swiftpm/Sources/BrownfieldNavigationSupport/BrownfieldSurfaceRegistry.swift @@ -0,0 +1,93 @@ +import Foundation + +/// Identifies one live React Native surface. Vended by `BrownfieldSurfaceRegistry`. +struct BrownfieldSurfaceID: Hashable, CustomStringConvertible { + fileprivate let rawValue: UInt64 + + var description: String { "surface#\(rawValue)" } +} + +/// Decides which React Native surface may act on a `popToNative()` request. +/// +/// `popToNative()` is delivered as an app-wide notification, so every live React +/// Native surface receives it — including ones buried under other screens. Acting +/// on all of them tears down the whole navigation stack +/// (https://github.com/callstack/react-native-brownfield/issues/354). +/// +/// The registry resolves that to a single surface: whichever on-screen surface +/// was shown most recently. +/// +/// Visibility is asked for at query time instead of being cached from lifecycle +/// callbacks. Hosts recreate view controllers — SwiftUI does it freely — and a +/// cached flag is stale exactly when the notification arrives. +/// +/// Deliberately free of UIKit so it can be unit tested on macOS via `swift test`. +/// Internal, like the sibling `BrownfieldBundleSupport` target: it is an +/// implementation detail of the pod, not API for host apps. +final class BrownfieldSurfaceRegistry { + static let shared = BrownfieldSurfaceRegistry() + + private struct Surface { + let isOnScreen: () -> Bool + var shownAt: UInt64 + } + + private let lock = NSLock() + private var nextSurfaceValue: UInt64 = 0 + private var nextShownValue: UInt64 = 0 + private var surfaces: [BrownfieldSurfaceID: Surface] = [:] + + init() {} + + /// - Parameter isOnScreen: queried on every `popTarget()`. Capture the host + /// weakly so a released surface stops winning the election. + func register(isOnScreen: @escaping () -> Bool) -> BrownfieldSurfaceID { + lock.lock() + defer { lock.unlock() } + + nextSurfaceValue += 1 + let id = BrownfieldSurfaceID(rawValue: nextSurfaceValue) + surfaces[id] = Surface(isOnScreen: isOnScreen, shownAt: 0) + return id + } + + func unregister(_ surface: BrownfieldSurfaceID) { + lock.lock() + defer { lock.unlock() } + + surfaces.removeValue(forKey: surface) + } + + /// Records that a surface was just shown, ordering it ahead of other on-screen + /// surfaces. Only a tiebreaker — a surface never marked shown can still win + /// when it is the one on screen. + func markShown(_ surface: BrownfieldSurfaceID) { + lock.lock() + defer { lock.unlock() } + + guard var entry = surfaces[surface] else { return } + + nextShownValue += 1 + entry.shownAt = nextShownValue + surfaces[surface] = entry + } + + /// The single surface allowed to handle `popToNative()`, or `nil` when no + /// React Native surface is on screen. + func popTarget() -> BrownfieldSurfaceID? { + lock.lock() + let candidates = surfaces + lock.unlock() + + return candidates + .filter { $0.value.isOnScreen() } + .max { lhs, rhs in + (lhs.value.shownAt, lhs.key.rawValue) < (rhs.value.shownAt, rhs.key.rawValue) + }? + .key + } + + func isPopTarget(_ surface: BrownfieldSurfaceID) -> Bool { + popTarget() == surface + } +} diff --git a/packages/react-native-brownfield/ios/swiftpm/Tests/BrownfieldNavigationSupportTests/BrownfieldSurfaceRegistryTests.swift b/packages/react-native-brownfield/ios/swiftpm/Tests/BrownfieldNavigationSupportTests/BrownfieldSurfaceRegistryTests.swift new file mode 100644 index 000000000..466b3f567 --- /dev/null +++ b/packages/react-native-brownfield/ios/swiftpm/Tests/BrownfieldNavigationSupportTests/BrownfieldSurfaceRegistryTests.swift @@ -0,0 +1,157 @@ +import XCTest +@testable import BrownfieldNavigationSupport + +/// `popToNative()` is delivered as an app-wide notification, so every live React +/// Native surface hears it. These tests pin down which single surface may act. +/// +/// Visibility is supplied as a live closure rather than tracked state: the host +/// recreates view controllers (SwiftUI does this freely), so cached lifecycle +/// flags go stale exactly when the notification arrives. +/// +/// Regression cover for https://github.com/callstack/react-native-brownfield/issues/354 +final class BrownfieldSurfaceRegistryTests: XCTestCase { + /// Stands in for a React Native surface's `view.window != nil` check. + private final class FakeSurface { + var isOnScreen = false + } + + func test_noRegisteredSurfacesHaveNoPopTarget() { + let registry = BrownfieldSurfaceRegistry() + + XCTAssertNil(registry.popTarget()) + } + + func test_offScreenSurfaceIsNotThePopTarget() { + let registry = BrownfieldSurfaceRegistry() + let surface = FakeSurface() + let id = registry.register { surface.isOnScreen } + + XCTAssertNil(registry.popTarget()) + XCTAssertFalse(registry.isPopTarget(id)) + } + + func test_theOnlyOnScreenSurfaceIsThePopTarget() { + let registry = BrownfieldSurfaceRegistry() + let surface = FakeSurface() + let id = registry.register { surface.isOnScreen } + surface.isOnScreen = true + + XCTAssertEqual(registry.popTarget(), id) + XCTAssertTrue(registry.isPopTarget(id)) + } + + /// The reporter's stack: RN "Home" is pushed first, covered by a native screen, + /// then RN "Contact" is pushed on top. Home stays alive and still observes the + /// notification, but must not pop — that is what destroys the whole stack. + func test_coveredSurfaceIsNotThePopTargetWhenAnotherIsOnScreen() { + let registry = BrownfieldSurfaceRegistry() + let home = FakeSurface() + let contact = FakeSurface() + let homeID = registry.register { home.isOnScreen } + let contactID = registry.register { contact.isOnScreen } + + home.isOnScreen = false + contact.isOnScreen = true + + XCTAssertEqual(registry.popTarget(), contactID) + XCTAssertFalse(registry.isPopTarget(homeID)) + } + + /// The failure that made the first fix inert: the top surface had been + /// registered but its appearance callback had not run yet when the + /// notification arrived. A live visibility check still resolves it. + func test_onScreenSurfaceIsThePopTargetEvenIfItWasNeverMarkedShown() { + let registry = BrownfieldSurfaceRegistry() + let covered = FakeSurface() + let top = FakeSurface() + let coveredID = registry.register { covered.isOnScreen } + registry.markShown(coveredID) + covered.isOnScreen = false + + let topID = registry.register { top.isOnScreen } + top.isOnScreen = true + + XCTAssertEqual(registry.popTarget(), topID) + } + + func test_surfaceBecomesThePopTargetAgainOnceTheSurfaceAboveItGoesAway() { + let registry = BrownfieldSurfaceRegistry() + let home = FakeSurface() + let contact = FakeSurface() + let homeID = registry.register { home.isOnScreen } + let contactID = registry.register { contact.isOnScreen } + + contact.isOnScreen = true + registry.unregister(contactID) + home.isOnScreen = true + + XCTAssertEqual(registry.popTarget(), homeID) + } + + func test_unregisteredSurfaceIsNeverThePopTarget() { + let registry = BrownfieldSurfaceRegistry() + let surface = FakeSurface() + let id = registry.register { surface.isOnScreen } + surface.isOnScreen = true + registry.unregister(id) + + XCTAssertNil(registry.popTarget()) + XCTAssertFalse(registry.isPopTarget(id)) + } + + /// Two surfaces on screen at once (an inline React Native card on a native host + /// screen, plus a pushed full-screen experience). Only the most recently shown + /// acts, so a single `popToNative()` pops once. + func test_mostRecentlyShownOnScreenSurfaceWinsWhenSeveralAreVisible() { + let registry = BrownfieldSurfaceRegistry() + let inlineCard = FakeSurface() + let pushedScreen = FakeSurface() + let inlineID = registry.register { inlineCard.isOnScreen } + let pushedID = registry.register { pushedScreen.isOnScreen } + + inlineCard.isOnScreen = true + pushedScreen.isOnScreen = true + registry.markShown(inlineID) + registry.markShown(pushedID) + + XCTAssertEqual(registry.popTarget(), pushedID) + XCTAssertFalse(registry.isPopTarget(inlineID)) + } + + /// Pins `markShown` itself. Every other multi-surface case here registers and + /// shows in the same order, so registration order alone would reproduce the + /// expected result; this one shows the *earlier-registered* surface last, and + /// fails if recency is dropped from the election. + func test_earlierRegisteredSurfaceWinsWhenItWasShownMoreRecently() { + let registry = BrownfieldSurfaceRegistry() + let first = FakeSurface() + let second = FakeSurface() + let firstID = registry.register { first.isOnScreen } + let secondID = registry.register { second.isOnScreen } + + first.isOnScreen = true + second.isOnScreen = true + + registry.markShown(secondID) + registry.markShown(firstID) + + XCTAssertEqual(registry.popTarget(), firstID) + XCTAssertFalse(registry.isPopTarget(secondID)) + } + + /// Pins the registry's contract — a surface whose visibility closure stops + /// reporting true drops out of the election. It does not, and cannot here, + /// verify that `ReactNativeViewController` captures itself weakly; that is + /// UIKit-side and covered by the end-to-end test. + func test_deallocatedSurfaceIsNotThePopTarget() { + let registry = BrownfieldSurfaceRegistry() + var transient: FakeSurface? = FakeSurface() + transient?.isOnScreen = true + let id = registry.register { [weak transient] in transient?.isOnScreen ?? false } + registry.markShown(id) + + transient = nil + + XCTAssertNil(registry.popTarget()) + } +} From 7ef76dda5b7be35cd9379be54a59ae671d1c589e Mon Sep 17 00:00:00 2001 From: Radoslaw Nowacki Date: Wed, 23 Sep 2026 13:50:36 +0200 Subject: [PATCH 2/2] refactor(ios): move BrownfieldSurfaceRegistry next to the other iOS sources Per review, the registry sits in ios/ alongside ReactNativeBrownfield.swift rather than under ios/swiftpm/Sources, so the Package.swift change is no longer needed. SPM targets cannot reference sources outside the package root, so the registry's unit tests go with it. Co-Authored-By: Claude Opus 5 (1M context) --- .../BrownfieldSurfaceRegistry.swift | 0 .../ios/swiftpm/Package.swift | 13 -- .../BrownfieldSurfaceRegistryTests.swift | 157 ------------------ 3 files changed, 170 deletions(-) rename packages/react-native-brownfield/ios/{swiftpm/Sources/BrownfieldNavigationSupport => }/BrownfieldSurfaceRegistry.swift (100%) delete mode 100644 packages/react-native-brownfield/ios/swiftpm/Tests/BrownfieldNavigationSupportTests/BrownfieldSurfaceRegistryTests.swift diff --git a/packages/react-native-brownfield/ios/swiftpm/Sources/BrownfieldNavigationSupport/BrownfieldSurfaceRegistry.swift b/packages/react-native-brownfield/ios/BrownfieldSurfaceRegistry.swift similarity index 100% rename from packages/react-native-brownfield/ios/swiftpm/Sources/BrownfieldNavigationSupport/BrownfieldSurfaceRegistry.swift rename to packages/react-native-brownfield/ios/BrownfieldSurfaceRegistry.swift diff --git a/packages/react-native-brownfield/ios/swiftpm/Package.swift b/packages/react-native-brownfield/ios/swiftpm/Package.swift index 7294b9771..3a8fe1c3f 100644 --- a/packages/react-native-brownfield/ios/swiftpm/Package.swift +++ b/packages/react-native-brownfield/ios/swiftpm/Package.swift @@ -12,10 +12,6 @@ let package = Package( name: "BrownfieldBundleSupport", targets: ["BrownfieldBundleSupport"] ), - .library( - name: "BrownfieldNavigationSupport", - targets: ["BrownfieldNavigationSupport"] - ), ], targets: [ .target( @@ -27,14 +23,5 @@ let package = Package( dependencies: ["BrownfieldBundleSupport"], path: "Tests/BrownfieldBundleSupportTests" ), - .target( - name: "BrownfieldNavigationSupport", - path: "Sources/BrownfieldNavigationSupport" - ), - .testTarget( - name: "BrownfieldNavigationSupportTests", - dependencies: ["BrownfieldNavigationSupport"], - path: "Tests/BrownfieldNavigationSupportTests" - ), ] ) diff --git a/packages/react-native-brownfield/ios/swiftpm/Tests/BrownfieldNavigationSupportTests/BrownfieldSurfaceRegistryTests.swift b/packages/react-native-brownfield/ios/swiftpm/Tests/BrownfieldNavigationSupportTests/BrownfieldSurfaceRegistryTests.swift deleted file mode 100644 index 466b3f567..000000000 --- a/packages/react-native-brownfield/ios/swiftpm/Tests/BrownfieldNavigationSupportTests/BrownfieldSurfaceRegistryTests.swift +++ /dev/null @@ -1,157 +0,0 @@ -import XCTest -@testable import BrownfieldNavigationSupport - -/// `popToNative()` is delivered as an app-wide notification, so every live React -/// Native surface hears it. These tests pin down which single surface may act. -/// -/// Visibility is supplied as a live closure rather than tracked state: the host -/// recreates view controllers (SwiftUI does this freely), so cached lifecycle -/// flags go stale exactly when the notification arrives. -/// -/// Regression cover for https://github.com/callstack/react-native-brownfield/issues/354 -final class BrownfieldSurfaceRegistryTests: XCTestCase { - /// Stands in for a React Native surface's `view.window != nil` check. - private final class FakeSurface { - var isOnScreen = false - } - - func test_noRegisteredSurfacesHaveNoPopTarget() { - let registry = BrownfieldSurfaceRegistry() - - XCTAssertNil(registry.popTarget()) - } - - func test_offScreenSurfaceIsNotThePopTarget() { - let registry = BrownfieldSurfaceRegistry() - let surface = FakeSurface() - let id = registry.register { surface.isOnScreen } - - XCTAssertNil(registry.popTarget()) - XCTAssertFalse(registry.isPopTarget(id)) - } - - func test_theOnlyOnScreenSurfaceIsThePopTarget() { - let registry = BrownfieldSurfaceRegistry() - let surface = FakeSurface() - let id = registry.register { surface.isOnScreen } - surface.isOnScreen = true - - XCTAssertEqual(registry.popTarget(), id) - XCTAssertTrue(registry.isPopTarget(id)) - } - - /// The reporter's stack: RN "Home" is pushed first, covered by a native screen, - /// then RN "Contact" is pushed on top. Home stays alive and still observes the - /// notification, but must not pop — that is what destroys the whole stack. - func test_coveredSurfaceIsNotThePopTargetWhenAnotherIsOnScreen() { - let registry = BrownfieldSurfaceRegistry() - let home = FakeSurface() - let contact = FakeSurface() - let homeID = registry.register { home.isOnScreen } - let contactID = registry.register { contact.isOnScreen } - - home.isOnScreen = false - contact.isOnScreen = true - - XCTAssertEqual(registry.popTarget(), contactID) - XCTAssertFalse(registry.isPopTarget(homeID)) - } - - /// The failure that made the first fix inert: the top surface had been - /// registered but its appearance callback had not run yet when the - /// notification arrived. A live visibility check still resolves it. - func test_onScreenSurfaceIsThePopTargetEvenIfItWasNeverMarkedShown() { - let registry = BrownfieldSurfaceRegistry() - let covered = FakeSurface() - let top = FakeSurface() - let coveredID = registry.register { covered.isOnScreen } - registry.markShown(coveredID) - covered.isOnScreen = false - - let topID = registry.register { top.isOnScreen } - top.isOnScreen = true - - XCTAssertEqual(registry.popTarget(), topID) - } - - func test_surfaceBecomesThePopTargetAgainOnceTheSurfaceAboveItGoesAway() { - let registry = BrownfieldSurfaceRegistry() - let home = FakeSurface() - let contact = FakeSurface() - let homeID = registry.register { home.isOnScreen } - let contactID = registry.register { contact.isOnScreen } - - contact.isOnScreen = true - registry.unregister(contactID) - home.isOnScreen = true - - XCTAssertEqual(registry.popTarget(), homeID) - } - - func test_unregisteredSurfaceIsNeverThePopTarget() { - let registry = BrownfieldSurfaceRegistry() - let surface = FakeSurface() - let id = registry.register { surface.isOnScreen } - surface.isOnScreen = true - registry.unregister(id) - - XCTAssertNil(registry.popTarget()) - XCTAssertFalse(registry.isPopTarget(id)) - } - - /// Two surfaces on screen at once (an inline React Native card on a native host - /// screen, plus a pushed full-screen experience). Only the most recently shown - /// acts, so a single `popToNative()` pops once. - func test_mostRecentlyShownOnScreenSurfaceWinsWhenSeveralAreVisible() { - let registry = BrownfieldSurfaceRegistry() - let inlineCard = FakeSurface() - let pushedScreen = FakeSurface() - let inlineID = registry.register { inlineCard.isOnScreen } - let pushedID = registry.register { pushedScreen.isOnScreen } - - inlineCard.isOnScreen = true - pushedScreen.isOnScreen = true - registry.markShown(inlineID) - registry.markShown(pushedID) - - XCTAssertEqual(registry.popTarget(), pushedID) - XCTAssertFalse(registry.isPopTarget(inlineID)) - } - - /// Pins `markShown` itself. Every other multi-surface case here registers and - /// shows in the same order, so registration order alone would reproduce the - /// expected result; this one shows the *earlier-registered* surface last, and - /// fails if recency is dropped from the election. - func test_earlierRegisteredSurfaceWinsWhenItWasShownMoreRecently() { - let registry = BrownfieldSurfaceRegistry() - let first = FakeSurface() - let second = FakeSurface() - let firstID = registry.register { first.isOnScreen } - let secondID = registry.register { second.isOnScreen } - - first.isOnScreen = true - second.isOnScreen = true - - registry.markShown(secondID) - registry.markShown(firstID) - - XCTAssertEqual(registry.popTarget(), firstID) - XCTAssertFalse(registry.isPopTarget(secondID)) - } - - /// Pins the registry's contract — a surface whose visibility closure stops - /// reporting true drops out of the election. It does not, and cannot here, - /// verify that `ReactNativeViewController` captures itself weakly; that is - /// UIKit-side and covered by the end-to-end test. - func test_deallocatedSurfaceIsNotThePopTarget() { - let registry = BrownfieldSurfaceRegistry() - var transient: FakeSurface? = FakeSurface() - transient?.isOnScreen = true - let id = registry.register { [weak transient] in transient?.isOnScreen ?? false } - registry.markShown(id) - - transient = nil - - XCTAssertNil(registry.popTarget()) - } -}