From d20eb976481b8376211b68d2a000ef6fbb1ec172 Mon Sep 17 00:00:00 2001 From: Nathan Young Date: Sun, 8 Nov 2026 22:12:36 -0500 Subject: [PATCH] Observe AppKit window release after the run loop settles The macOS display contract reached its lifecycle step and failed with "VM normal completion must release its toolkit window". Creation was already observed correctly, so the provider releases the window; AppKit completes the teardown on the run loop, and counting in the same turn still lists it. The observer now gives the loop one bounded nonblocking turn before counting, which is the test accommodating AppKit's bookkeeping rather than the provider holding anything. The fixture also gains an --input-probe mode. macOS needs accessibility trust to post events to another process, which a hosted runner does not grant, so the interaction suite cannot drive the provider from outside; posting into this process needs no permission. The probe types through the provider's own pump and reports which of the text, toggle and click events arrived, with the application's active and key state, so a failure names itself instead of arriving as a deadline. It is a separate workflow step rather than part of --display, so unproven input handling cannot fail an otherwise green contract. Co-Authored-By: Claude Opus 5 --- .github/workflows/macos-appkit-provider.yml | 5 ++ tests/ui_registry_observer.hpp | 12 ++- tests/ui_runtime_appkit_tests.mm | 84 +++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/.github/workflows/macos-appkit-provider.yml b/.github/workflows/macos-appkit-provider.yml index ff3ee39..83c1d85 100644 --- a/.github/workflows/macos-appkit-provider.yml +++ b/.github/workflows/macos-appkit-provider.yml @@ -72,6 +72,11 @@ jobs: - name: Provider contract against a real session timeout-minutes: 5 run: ./build/ci/bin/nil_ui_runtime_appkit_tests --display + # Cross-process input needs accessibility trust a runner does not grant, + # so real keyboard input is exercised inside the process instead. + - name: Real keyboard input, in process + timeout-minutes: 5 + run: ./build/ci/bin/nil_ui_runtime_appkit_tests --input-probe display: if: ${{ inputs.scope == 'display' }} diff --git a/tests/ui_registry_observer.hpp b/tests/ui_registry_observer.hpp index b185cc7..b1a8b22 100644 --- a/tests/ui_registry_observer.hpp +++ b/tests/ui_registry_observer.hpp @@ -77,7 +77,17 @@ class UiRegistryObserver { #if defined(__linux__) unsigned count() const { return size_(list_()); } #elif defined(__APPLE__) - unsigned count() const { return static_cast(NSApp ? NSApp.windows.count : 0); } + unsigned count() const { + // AppKit completes window teardown on the run loop, so a window the + // provider has already released is still listed if it is counted in the + // same turn. Give the loop one bounded, nonblocking turn first; this is + // the observer accommodating AppKit's bookkeeping, not the provider + // holding anything. + @autoreleasepool { + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]]; + } + return static_cast(NSApp ? NSApp.windows.count : 0); + } #elif defined(_WIN32) unsigned count() const { unsigned total = 0; diff --git a/tests/ui_runtime_appkit_tests.mm b/tests/ui_runtime_appkit_tests.mm index e2db66e..ec7d626 100644 --- a/tests/ui_runtime_appkit_tests.mm +++ b/tests/ui_runtime_appkit_tests.mm @@ -19,6 +19,7 @@ #include #include +#import #import namespace { @@ -158,6 +159,83 @@ void contract() { rejected(context.close(window), "handle", "closing twice is an error"); } +// Real keyboard input, delivered in process. macOS requires accessibility +// trust to post events to another process, which a hosted runner does not +// grant, so the language-level contract cannot drive the interaction suite +// there; posting into this process needs no such permission, and the provider +// pump is still what has to route it. +void input_probe(bool verbose) { + @autoreleasepool { + ny::ui::Context context; + const auto window = ok(context.window("NIL AppKit input probe", 420, 240), "create probe window"); + const auto column = ok(context.widget("column", window, ""), "create probe column"); + const auto entry = ok(context.widget("entry", column, ""), "create probe entry"); + const auto box = ok(context.widget("checkbox", column, "Enable setting"), "create probe checkbox"); + const auto button = ok(context.widget("button", column, "Finish probe"), "create probe button"); + ok(context.set_number(window, "visible", 1), "show probe window"); + ok(context.set_number(entry, "focus", 1), "focus probe entry"); + + NSWindow *top = nil; + for (NSWindow *candidate in NSApp.windows) + if ([candidate.title isEqualToString:@"NIL AppKit input probe"]) { top = candidate; break; } + require(top != nil, "probe window is discoverable among the application windows"); + // Key events reach the key window's responder chain, so the application + // has to be active for AppKit to route them at all. + [NSApp activateIgnoringOtherApps:YES]; + if (verbose) + std::cout << "input probe: active=" << (NSApp.active ? 1 : 0) + << " key=" << (top.keyWindow ? 1 : 0) + << " responder=" << (top.firstResponder + ? [NSStringFromClass([top.firstResponder class]) UTF8String] : "none") + << "\n"; + + const auto post = [&](NSString *characters, unsigned short code) { + for (NSEventType type : {NSEventTypeKeyDown, NSEventTypeKeyUp}) { + NSEvent *event = [NSEvent keyEventWithType:type + location:NSZeroPoint + modifierFlags:0 + timestamp:[[NSProcessInfo processInfo] systemUptime] + windowNumber:top.windowNumber + context:nil + characters:characters + charactersIgnoringModifiers:characters + isARepeat:NO + keyCode:code]; + if (event) [NSApp postEvent:event atStart:NO]; + } + }; + for (NSString *character in @[@"M", @"a", @"t", @"r", @"i", @"x"]) post(character, 0); + post(@"\t", 48); // kVK_Tab + post(@" ", 49); // kVK_Space + post(@"\t", 48); + post(@" ", 49); + + bool saw_text = false, saw_toggle = false, saw_click = false; + std::string observed; + for (int attempt = 0; attempt < 400 && !saw_click; ++attempt) { + const auto polled = context.poll(window); + if (!polled) break; + if (!polled.number) { [NSThread sleepForTimeInterval:0.01]; continue; } + const auto kind = ok(context.event_number(polled.number, "kind"), "probe event kind"); + const auto target = ok(context.event_number(polled.number, "target"), "probe event target"); + if (kind == 2 && target == entry) { + observed = context.event_text(polled.number).text; + if (observed == "Matrix") saw_text = true; + } else if (kind == 3 && target == box && + ok(context.event_number(polled.number, "checked"), "probe checked") == 1) saw_toggle = true; + else if (kind == 1 && target == button) saw_click = true; + ok(context.release_event(polled.number), "release probe event"); + } + if (verbose) + std::cout << "input probe: text=" << saw_text << " toggle=" << saw_toggle + << " click=" << saw_click << " entry=\"" << observed << "\"\n"; + ok(context.close(window), "close probe window"); + require(saw_text, "posted characters reach the focused entry"); + require(saw_toggle, "Tab then Space reaches the checkbox"); + require(saw_click, "Tab then Space reaches the button"); + } +} + } // namespace int main(int argc, char **argv) { @@ -168,6 +246,12 @@ int main(int argc, char **argv) { return 0; } const bool desktop = window_server(); + if (mode == "--input-probe") { + require(desktop, "--input-probe requires a window server session"); + input_probe(true); + std::cout << "UI runtime: " << checks << " checks passed (--input-probe)\n"; + return 0; + } if (mode == "--display") { require(desktop, "--display requires a window server session"); contract();