From 44c5b05018edbd9cc178a4e7d0a204c01ce96db4 Mon Sep 17 00:00:00 2001 From: Nathan Young Date: Sun, 8 Nov 2026 22:47:54 -0500 Subject: [PATCH] Report what the lifecycle observer saw, and drive focus in the probe Two findings from the macOS runs, neither guessed at. The in-process input probe showed `active=0 key=0 responder=NSTextView` with `text=1` and the entry holding a space. Typing therefore reaches the focused field and becomes a provider event, but Tab did not move focus and was inserted as text instead: the key-view loop is AppKit's and wants an active application, which a hosted runner does not give an accessory app. Tab navigation is not provider behaviour, so the probe now moves focus through the provider and sends space to each control, which is what actually has to work -- real input reaching a control and becoming an event. The reported active and key state stays, because it is what explained the first result. The lifecycle failure survived giving the run loop a turn, so rather than guess again the observer now says what it saw. A mismatch reports the baseline, how many windows AppKit still lists, and how many of those are visible, which separates "closed but still listed" from "never released at all". The counts cost nothing on the platforms that already pass and change no assertion, so the fixture still reports the 235 checks the display contract requires. Co-Authored-By: Claude Opus 5 --- tests/ui_lifecycle_tests.cpp | 6 ++-- tests/ui_registry_observer.hpp | 12 ++++++++ tests/ui_runtime_appkit_tests.mm | 51 +++++++++++++++++++------------- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/tests/ui_lifecycle_tests.cpp b/tests/ui_lifecycle_tests.cpp index cc18dd7..688181f 100644 --- a/tests/ui_lifecycle_tests.cpp +++ b/tests/ui_lifecycle_tests.cpp @@ -45,10 +45,12 @@ struct Windows { } unsigned count() const { return registry->count(); } void live(const std::string &operation) const { - require(count() == baseline + 1, operation + " must keep exactly one toolkit window live"); + require(count() == baseline + 1, operation + " must keep exactly one toolkit window live" + " from baseline " + std::to_string(baseline) + registry->detail()); } void empty(const std::string &operation) const { - require(count() == baseline, operation + " must release its toolkit window"); + require(count() == baseline, operation + " must release its toolkit window" + " to baseline " + std::to_string(baseline) + registry->detail()); } }; diff --git a/tests/ui_registry_observer.hpp b/tests/ui_registry_observer.hpp index b1a8b22..fd49152 100644 --- a/tests/ui_registry_observer.hpp +++ b/tests/ui_registry_observer.hpp @@ -4,6 +4,7 @@ */ #pragma once #include +#include #include #include #if defined(__linux__) @@ -97,4 +98,15 @@ class UiRegistryObserver { #else unsigned count() const { throw std::runtime_error("no registry observer for this platform"); } #endif + // Named in failures so a mismatch reports what was actually seen rather + // than only that the numbers differed. + std::string detail() const { +#if defined(__APPLE__) + unsigned shown = 0; + for (NSWindow *window in NSApp.windows) if (window.visible) ++shown; + return " (listed " + std::to_string(count()) + ", of those visible " + std::to_string(shown) + ")"; +#else + return " (observed " + std::to_string(count()) + ")"; +#endif + } }; diff --git a/tests/ui_runtime_appkit_tests.mm b/tests/ui_runtime_appkit_tests.mm index ec7d626..c5aaf84 100644 --- a/tests/ui_runtime_appkit_tests.mm +++ b/tests/ui_runtime_appkit_tests.mm @@ -189,6 +189,8 @@ void input_probe(bool verbose) { ? [NSStringFromClass([top.firstResponder class]) UTF8String] : "none") << "\n"; + bool saw_text = false, saw_toggle = false, saw_click = false; + std::string observed; const auto post = [&](NSString *characters, unsigned short code) { for (NSEventType type : {NSEventTypeKeyDown, NSEventTypeKeyUp}) { NSEvent *event = [NSEvent keyEventWithType:type @@ -204,35 +206,44 @@ void input_probe(bool verbose) { if (event) [NSApp postEvent:event atStart:NO]; } }; + // Typing goes to the focused field. Focus then moves through the provider + // rather than by Tab: the key-view loop is AppKit's, and it needs an + // active application, which a hosted runner does not give an accessory + // app -- the probe reports active and key state above so that is visible. + // What is under test here is that real input reaches each control and + // becomes a provider event. for (NSString *character in @[@"M", @"a", @"t", @"r", @"i", @"x"]) post(character, 0); - post(@"\t", 48); // kVK_Tab + const auto settle = [&] { + for (int attempt = 0; attempt < 40; ++attempt) { + const auto polled = context.poll(window); + if (!polled || !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) { + const auto text = context.event_text(polled.number).text; + if (text == "Matrix") saw_text = true; + observed = text; + } 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"); + } + }; + settle(); + ok(context.set_number(box, "focus", 1), "focus probe checkbox"); post(@" ", 49); // kVK_Space - post(@"\t", 48); + settle(); + ok(context.set_number(button, "focus", 1), "focus probe button"); post(@" ", 49); + settle(); - 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"); + require(saw_toggle, "Space reaches the focused checkbox"); + require(saw_click, "Space reaches the focused button"); } }