// Which responder the share extension asks to open Cotton, and in which shape. // // The crash of 15 Sep 2026: one selector // (`openURL:options:completionHandler:`) is implemented by TWO classes that // take DIFFERENT third arguments, and the extension sent `UIApplication`'s // `NSDictionary` to whichever object answered first. On this host that is a // `UIScene`, so UIKit sent `universalLinksOnly` to an empty dictionary and the // extension died — every share, every type. // // These fixtures are fake responders: a class ancestry is all the rule reads, // so a test can hand it one without a share sheet, a window or a simulator. // Every value here is synthetic. @testable import CottonKit import XCTest final class ShareOpenerDispatchTests: XCTestCase { // Fake responders, as the runtime walk would report them: the object's own // class first, then every class above it. private let application = ["UIApplication", "UIResponder", "NSObject"] private let windowScene = ["UIWindowScene", "UIScene", "UIResponder", "NSObject"] private let viewController = ["ShareViewController", "UIViewController", "UIResponder", "NSObject"] private let view = ["UIView", "UIResponder", "NSObject"] /// Some host's own opener — it answers the selector and nothing says what /// its options argument is. private let hostResponder = ["HostShareResponder", "UIResponder", "NSObject"] // ── The kind, read off the class ────────────────────────────────────── func testAnApplicationTakesTheDictionaryForm() { XCTAssertEqual(shareOpenerKind(classAncestry: application), .application) XCTAssertEqual(ShareOpenerKind.application.optionsClassName, "NSDictionary") } /// **The crash.** A scene's method takes a `UISceneOpenExternalURLOptions`, /// and sending it a dictionary is `-[__NSDictionary0 universalLinksOnly]: /// unrecognized selector`. func testASceneTakesItsOwnOptionsObject() { XCTAssertEqual(shareOpenerKind(classAncestry: windowScene), .scene) XCTAssertEqual( ShareOpenerKind.scene.optionsClassName, "UISceneOpenExternalURLOptions", "a scene never gets the dictionary form — that is the abort") } /// A subclass is still what it inherits from, including the runtime's own /// KVO subclass, whose name is not `UIApplication` at all. func testASubclassIsReadOffItsAncestryAndNotItsOwnName() { XCTAssertEqual( shareOpenerKind(classAncestry: ["NSKVONotifying_UIApplication"] + application), .application) XCTAssertEqual(shareOpenerKind(classAncestry: windowScene), .scene) } /// Anything else is not classified, so it is never called: an unknown third /// argument is exactly what the abort was. func testEverythingElseHasNoKnownShape() { XCTAssertNil(shareOpenerKind(classAncestry: view)) XCTAssertNil(shareOpenerKind(classAncestry: viewController)) XCTAssertNil(shareOpenerKind(classAncestry: hostResponder)) XCTAssertNil(shareOpenerKind(classAncestry: [])) } // ── Which one on the chain is called ────────────────────────────────── private func candidate(_ ancestry: [String], opensURLs: Bool = true) -> ShareOpenerCandidate { ShareOpenerCandidate( opensURLs: opensURLs, kind: opensURLs ? shareOpenerKind(classAncestry: ancestry) : nil) } /// The chain as the extension walks it: itself, its view, its window, the /// scene. The scene is the only opener, and it is called as a scene. func testTheSceneIsUsedWhenItIsTheOnlyOpener() { let chain = [ candidate(viewController, opensURLs: false), candidate(view, opensURLs: false), candidate(windowScene), ] XCTAssertEqual(shareOpenerChoice(chain), 2) XCTAssertEqual(chain[2].kind, .scene) } /// An application is preferred wherever it sits — it is the documented /// opener for a URL leaving the process, and its `false` really means /// nothing will take it. func testAnApplicationIsPreferredOverASceneEarlierOnTheChain() { let chain = [ candidate(viewController, opensURLs: false), candidate(windowScene), candidate(application), ] XCTAssertEqual(shareOpenerChoice(chain), 2) XCTAssertEqual(chain[2].kind, .application) } /// A responder that answers the selector but is neither is SKIPPED, not /// guessed at — and the scene behind it still gets the knock. func testAnUnknownOpenerIsSkippedAndTheSceneBehindItAnswers() { let chain = [ candidate(hostResponder), candidate(windowScene), ] XCTAssertEqual(shareOpenerChoice(chain), 1) XCTAssertFalse(chain[0].opensURLs == false, "it does answer the selector") XCTAssertNil(chain[0].kind, "and nothing says what its options are") } /// Nobody to call is not an error: the bytes are staged and the mailbox /// entry is written, so the next foreground drains it /// (``ShareOpenOutcome/couldNotOpen(_:)``). func testNobodyOnTheChainIsCouldNotOpenAndNotACall() { XCTAssertNil(shareOpenerChoice([])) XCTAssertNil( shareOpenerChoice([ candidate(viewController, opensURLs: false), candidate(view, opensURLs: false), ])) XCTAssertNil( shareOpenerChoice([candidate(hostResponder)]), "an opener whose shape is unknown is nobody") } /// A responder that is a scene but does NOT answer the selector is not /// called either — the class says the shape, `responds(to:)` says whether /// there is anything to send. func testAKindWithoutTheSelectorIsNotCalled() { XCTAssertNil(shareOpenerChoice([candidate(windowScene, opensURLs: false)])) } // ── The names, against the real classes ─────────────────────────────── #if canImport(UIKit) /// The rule is a list of NAMES, so a UIKit rename would leave it /// quietly matching nothing. No instance is made: the class objects /// are all the ancestry walk ever reads. func testTheNamesAreTheRealUIKitClasses() { XCTAssertEqual( shareOpenerKind(classAncestry: ShareOpenerDispatchTests.ancestry(of: UIApplication.self)), .application) XCTAssertEqual( shareOpenerKind(classAncestry: ShareOpenerDispatchTests.ancestry(of: UIWindowScene.self)), .scene) XCTAssertNil( shareOpenerKind(classAncestry: ShareOpenerDispatchTests.ancestry(of: UIView.self))) } /// And the options names are the real options classes — the extension's /// two `typealias`es have to agree with what this enum says they are. func testTheOptionsNamesAreTheRealOptionsClasses() { XCTAssertEqual( NSStringFromClass(NSDictionary.self), ShareOpenerKind.application.optionsClassName) XCTAssertEqual( NSStringFromClass(UIScene.OpenExternalURLOptions.self), ShareOpenerKind.scene.optionsClassName) } /// The same walk the extension makes, over a class rather than an /// instance. private static func ancestry(of type: AnyClass) -> [String] { var names: [String] = [] var current: AnyClass? = type while let cls = current { names.append(NSStringFromClass(cls)) current = class_getSuperclass(cls) } return names } #endif }