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"); } }