Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tests/ui_lifecycle_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
};

Expand Down
12 changes: 12 additions & 0 deletions tests/ui_registry_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#pragma once
#include <cwchar>
#include <string>
#include <iterator>
#include <stdexcept>
#if defined(__linux__)
Expand Down Expand Up @@ -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
}
};
51 changes: 31 additions & 20 deletions tests/ui_runtime_appkit_tests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
}
}

Expand Down
Loading