Files
aevolution/tests/client_lifecycle.cpp
T
dingoandClaude Opus 5.5 bb8c2fc359 Fix M9.8c review findings in the engineering client
Resolve pointer ownership per SDL event so same-batch panel clicks, wheel and
click-to-focus typing never reach the world; keep the Tab toggle independent of
idempotent external capture release; own launcher phase and remote session
together so Local fixture ends any remote attempt; wrap notices beside a
reserved close column within a bounded stack. Adds client-panels test and
host/session, same-batch and host-order Tab regressions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 11:10:32 -04:00

453 lines
20 KiB
C++

#include "evo/client/application.hpp"
#include "render/sdl_renderer.hpp"
#include <SDL3/SDL.h>
#include <array>
#include <cmath>
#include <cstdio>
#include <filesystem>
#include <imgui.h>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
#include <vector>
void client_controls_tests();
namespace {
using namespace evo::client;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("client lifecycle line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
template <typename T, typename E> bool refused(const std::expected<T, E>& result, E error) {
return !result && result.error() == error;
}
evo::render::Frame fixture() {
const std::array<evo::render::Rectangle, 1> rectangles{{{0, 0, 32, 32, {255, 0, 0, 255}}}};
auto result = evo::render::Frame::create(rectangles);
CHECK(result);
return std::move(result.value());
}
SDL_Window* window() {
int count{};
SDL_Window** windows = SDL_GetWindows(&count);
SDL_Window* result = count == 1 ? windows[0] : nullptr;
SDL_free(static_cast<void*>(windows));
CHECK(result);
return result;
}
void send(SDL_EventType type, SDL_WindowID id) {
SDL_Event event{};
event.type = type;
if (type >= SDL_EVENT_RENDER_TARGETS_RESET && type <= SDL_EVENT_RENDER_DEVICE_LOST)
event.render.windowID = id;
else
event.window.windowID = id;
CHECK(SDL_PushEvent(&event));
}
void frame_contract() {
using namespace evo::render;
std::array<Rectangle, 1> r{{{1, 2, 3, 4, {5, 6, 7, 255}}}};
const auto frame = Frame::create(r);
CHECK(frame);
r[0].x = 100;
CHECK(frame->rectangles()[0].x == 1); // Owns copy; caller mutation cannot alter it.
r[0].x = std::numeric_limits<float>::quiet_NaN();
CHECK(refused(Frame::create(r), FrameError::invalid_geometry));
r[0].x = 0;
r[0].width = -1;
CHECK(refused(Frame::create(r), FrameError::invalid_geometry));
r[0].width = std::numeric_limits<float>::infinity();
CHECK(refused(Frame::create(r), FrameError::invalid_geometry));
std::vector<Rectangle> full(max_rectangles);
CHECK(Frame::create(full));
full.emplace_back();
CHECK(refused(Frame::create(full), FrameError::too_many_primitives));
}
void pixels(SDL_Window* native, const evo::render::Frame& frame, WindowState state) {
auto* renderer = SDL_GetRenderer(native);
CHECK(renderer);
// Exercise non-unit, anisotropic drawable scaling without requiring a HiDPI monitor.
state.logical_width = state.pixel_width / 2;
state.logical_height = state.pixel_height;
CHECK(detail::draw(renderer, frame, state));
const std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)> surface{
SDL_RenderReadPixels(renderer, nullptr), SDL_DestroySurface};
CHECK(surface);
Uint8 r{}, g{}, b{}, a{};
CHECK(SDL_ReadSurfacePixel(surface.get(), 48, 8, &r, &g, &b, &a));
CHECK(r == 255 && g == 0 && b == 0 && a == 255);
CHECK(SDL_ReadSurfacePixel(surface.get(), 80, 8, &r, &g, &b, &a));
CHECK(r == 0 && g == 0 && b == 0 && a == 255);
CHECK(SDL_ReadSurfacePixel(surface.get(), 8, 48, &r, &g, &b, &a));
CHECK(r == 0 && g == 0 && b == 0 && a == 255);
}
void lifecycle() {
const auto frame = fixture();
CHECK(refused(Application::open({0, 64, false, "software"}), Error::invalid_config));
CHECK(refused(Application::open({64, 64, false, "evo-invalid"}), Error::renderer));
CHECK(SDL_WasInit(0) == 0); // Failed partial initialization released SDL.
for (int round = 0; round < 32; ++round) {
auto result = Application::open({160, 120, false, "software"});
CHECK(result);
auto app = std::move(result.value());
CHECK(refused(Application::open({}), Error::already_open));
CHECK(app->step(frame) == Step::rendered);
auto* native = window();
pixels(native, frame, app->state());
const auto id = SDL_GetWindowID(native);
// Foreign close/reset must not affect the one owned window.
send(SDL_EVENT_WINDOW_CLOSE_REQUESTED, id + 1);
send(SDL_EVENT_RENDER_DEVICE_LOST, id + 1);
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
CHECK(app->step(frame) == Step::rendered);
CHECK(app->state().accepts_input());
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
CHECK(app->step(frame) == Step::rendered);
CHECK(!app->state().accepts_input());
send(SDL_EVENT_WINDOW_MINIMIZED, id);
CHECK(app->step(frame) == Step::paused);
send(SDL_EVENT_WINDOW_RESTORED, id);
CHECK(app->step(frame) == Step::rendered);
CHECK(SDL_SetWindowSize(native, 240, 180));
send(SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED, id);
send(SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED, id);
CHECK(app->step(frame) == Step::rendered);
int lw{}, lh{}, pw{}, ph{};
CHECK(SDL_GetWindowSize(native, &lw, &lh));
CHECK(SDL_GetWindowSizeInPixels(native, &pw, &ph));
const auto state = app->state();
CHECK(state.logical_width == lw && state.logical_height == lh);
CHECK(state.pixel_width == pw && state.pixel_height == ph);
CHECK(std::isfinite(state.content_scale) && state.content_scale > 0);
send(SDL_EVENT_RENDER_TARGETS_RESET, id);
CHECK(app->step(frame) == Step::rendered);
send(SDL_EVENT_RENDER_DEVICE_RESET, id);
CHECK(app->step(frame) == Step::rendered);
CHECK(SDL_HideWindow(native));
send(SDL_EVENT_WINDOW_HIDDEN, id);
CHECK(app->step(frame) == Step::paused);
CHECK(SDL_ShowWindow(native));
send(SDL_EVENT_WINDOW_SHOWN, id);
CHECK(app->step(frame) == Step::rendered);
if (round % 2 == 0) {
send(SDL_EVENT_RENDER_DEVICE_LOST, id);
CHECK(refused(app->step(frame), Error::device_lost));
CHECK(refused(app->step(frame), Error::device_lost));
CHECK(app->state().failed && !app->state().accepts_input());
} else {
send(SDL_EVENT_WINDOW_CLOSE_REQUESTED, id);
CHECK(app->step(frame) == Step::closed);
CHECK(app->step(frame) == Step::closed);
}
app.reset();
CHECK(SDL_WasInit(0) == 0);
}
auto hidden = Application::open({64, 64, true, "software"});
CHECK(hidden);
CHECK((*hidden)->step(frame) == Step::paused);
send(SDL_EVENT_QUIT, 0);
CHECK((*hidden)->step(frame) == Step::closed);
}
Intention required(std::expected<Intention, InputError> result) {
CHECK(result);
return result.value();
}
void native_input() {
const auto frame = fixture();
auto result = Application::open({320, 240, false, "software"});
CHECK(result);
auto app = std::move(result.value());
CHECK(app->poll_events());
const auto id = SDL_GetWindowID(window());
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
auto key = [id](SDL_Scancode scancode, bool down, bool repeat = false) {
SDL_Event event{};
event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP;
event.key.windowID = id;
event.key.scancode = scancode;
event.key.repeat = repeat;
CHECK(SDL_PushEvent(&event));
};
key(SDL_SCANCODE_W, true);
key(SDL_SCANCODE_RIGHT, true);
CHECK(app->poll_events());
const auto first = app->sample_intention(40);
CHECK(first && first->move_y == 1000 && first->sequence == 1 && first->tick == 40);
CHECK(app->take_navigation().x == 1);
key(SDL_SCANCODE_W, false);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(41)).move_y == 0);
key(SDL_SCANCODE_W, true);
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
key(SDL_SCANCODE_W, true, true);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(42)).move_y == 0 && app->take_navigation().x == 0);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
app->set_ui_capture(true);
key(SDL_SCANCODE_TAB, true); // Legacy shortcut cannot escape an external modal.
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && app->ui_captured());
CHECK(required(app->sample_intention(43)).move_y == 0);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
app->set_ui_capture(false);
CHECK(required(app->sample_intention(44)).move_y == 0);
CHECK(app->poll_events());
key(SDL_SCANCODE_TAB, true);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
CHECK(app->ui_captured() && required(app->sample_intention(45)).move_y == 0);
key(SDL_SCANCODE_TAB, true, true);
CHECK(app->poll_events());
CHECK(app->ui_captured());
key(SDL_SCANCODE_TAB, false);
key(SDL_SCANCODE_TAB, true);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
CHECK(!app->ui_captured() && required(app->sample_intention(46)).move_y == 1000);
// A saturated event batch cancels motion until the backlog drains.
for (int i = 0; i < 300; ++i)
key(SDL_SCANCODE_RIGHT, true);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(47)).move_y == 0 && app->take_navigation().x == 0);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(48)).move_y == 0 && app->take_navigation().x == 0);
SDL_Event wheel{};
wheel.type = SDL_EVENT_MOUSE_WHEEL;
wheel.wheel.windowID = id;
wheel.wheel.y = 2;
wheel.wheel.mouse_x = 123;
wheel.wheel.mouse_y = 45;
wheel.wheel.direction = SDL_MOUSEWHEEL_FLIPPED;
CHECK(SDL_PushEvent(&wheel));
CHECK(app->poll_events());
const auto navigation = app->take_navigation();
CHECK(navigation.wheel == -2 && navigation.pointer.x == 123 && navigation.pointer.y == 45);
app->set_ui_capture(true);
CHECK(SDL_PushEvent(&wheel));
CHECK(app->poll_events());
CHECK(app->take_navigation().wheel == 0);
app->set_ui_capture(false);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events());
key(SDL_SCANCODE_W, true);
send(SDL_EVENT_WINDOW_MINIMIZED, id);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(49)).move_y == 0);
send(SDL_EVENT_WINDOW_RESTORED, id);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(50)).move_y == 0);
app->set_ui_capture(true);
key(SDL_SCANCODE_W, true); // Queued while captured; releasing capture must not replay it.
app->set_ui_capture(false);
CHECK(app->poll_events());
CHECK(required(app->sample_intention(51)).move_y == 0);
CHECK(app->present(frame) == Step::rendered);
}
// M9.8c: the toolkit sees input first. A click that operates a widget never selects
// through it; keyboard focus in a text field suppresses world movement; releasing that
// focus requires a fresh key press (nothing is synthesized from held keys).
void toolkit_capture() {
const auto frame = fixture();
auto result = Application::open({320, 240, false, "software"});
CHECK(result);
auto app = std::move(result.value());
CHECK(app->poll_events());
const auto id = SDL_GetWindowID(window());
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
int presses = 0;
bool text_focus = false;
std::array<char, 32> text{}, field{};
ImVec2 field_min{}, field_max{};
app->set_overlay([&] {
ImGui::SetNextWindowPos({0, 0}, ImGuiCond_Always);
ImGui::SetNextWindowSize({150, 150}, ImGuiCond_Always);
ImGui::Begin("panel", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
if (ImGui::Button("Press", {130, 40}))
++presses;
ImGui::InputText("field", field.data(), field.size());
field_min = ImGui::GetItemRectMin();
field_max = ImGui::GetItemRectMax();
if (text_focus) {
ImGui::SetKeyboardFocusHere();
ImGui::InputText("name", text.data(), text.size());
}
ImGui::End();
});
auto mouse = [id](SDL_EventType type, float x, float y) {
SDL_Event event{};
event.type = type;
if (type == SDL_EVENT_MOUSE_MOTION) {
event.motion.windowID = id;
event.motion.x = x;
event.motion.y = y;
} else {
event.button.windowID = id;
event.button.button = SDL_BUTTON_LEFT;
event.button.down = type == SDL_EVENT_MOUSE_BUTTON_DOWN;
event.button.clicks = 1;
event.button.x = x;
event.button.y = y;
}
CHECK(SDL_PushEvent(&event));
};
const auto click = [&](float x, float y) {
mouse(SDL_EVENT_MOUSE_MOTION, x, y);
CHECK(app->poll_events() && app->present(frame) == Step::rendered); // Toolkit hovers.
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, x, y);
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, x, y);
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
return app->take_navigation().click;
};
// Over the widget: the button activates once, the world sees no click.
// The panel exists before the user clicks (as every application panel does).
CHECK(app->present(frame) == Step::rendered && app->present(frame) == Step::rendered);
CHECK(!click(60, 30) && presses == 1 && app->ui_wants_pointer());
// Open world: a click is delivered once, then consumed.
const auto world = click(260, 200);
CHECK(world && world->x == 260 && world->y == 200 && presses == 1);
CHECK(!app->take_navigation().click);
// A drag is not a click.
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, 260, 200);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, 290, 200);
CHECK(app->poll_events() && !app->take_navigation().click);
// Same batch, no settling frame: the pointer was last over the world, then moves
// onto the panel and clicks. Ownership is resolved per event, so the world sees
// nothing and the widget still activates on the toolkit's next frames.
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
CHECK(app->poll_events() && app->present(frame) == Step::rendered && !app->ui_wants_pointer());
mouse(SDL_EVENT_MOUSE_MOTION, 60, 30);
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, 60, 30);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, 60, 30);
CHECK(app->poll_events() && !app->take_navigation().click);
for (int i = 0; i < 5 && presses == 1; ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(presses == 2 && !app->take_navigation().click);
// Same for the wheel: moving onto the panel and scrolling never zooms the world.
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
CHECK(app->poll_events() && app->present(frame) == Step::rendered);
SDL_Event wheel{};
wheel.type = SDL_EVENT_MOUSE_WHEEL;
wheel.wheel.windowID = id;
wheel.wheel.y = 3;
wheel.wheel.mouse_x = 60;
wheel.wheel.mouse_y = 30;
mouse(SDL_EVENT_MOUSE_MOTION, 60, 30);
CHECK(SDL_PushEvent(&wheel));
CHECK(app->poll_events() && app->take_navigation().wheel == 0);
CHECK(app->present(frame) == Step::rendered);
// ...while the same wheel over the world still zooms.
wheel.wheel.mouse_x = 260;
wheel.wheel.mouse_y = 200;
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
CHECK(SDL_PushEvent(&wheel));
CHECK(app->poll_events() && app->take_navigation().wheel == 3);
CHECK(app->present(frame) == Step::rendered);
auto key = [id](SDL_Scancode scancode, bool down, bool repeat = false) {
SDL_Event event{};
event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP;
event.key.windowID = id;
event.key.scancode = scancode;
event.key.repeat = repeat;
CHECK(SDL_PushEvent(&event));
};
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && required(app->sample_intention(100)).move_y == 1000);
// A focused text field takes the keyboard: held movement stops, typing never moves.
text_focus = true;
// Focus requests take effect over the next toolkit frames; wait a bounded number.
for (int i = 0; i < 5 && !app->ui_captured(); ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(app->ui_captured());
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && app->ui_captured());
CHECK(required(app->sample_intention(101)).move_y == 0);
// Focus leaves the field: capture releases, but only a fresh press moves again.
text_focus = false; // The field is no longer submitted, so it loses focus.
for (int i = 0; i < 5 && app->ui_captured(); ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(!app->ui_captured());
CHECK(required(app->sample_intention(102)).move_y == 0);
CHECK(app->poll_events());
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && required(app->sample_intention(103)).move_y == 1000);
key(SDL_SCANCODE_W, false);
CHECK(app->poll_events() && required(app->sample_intention(104)).move_y == 0);
// Click-to-focus and typing in one batch: the field is not yet reported focused,
// yet keys after a press on the panel never move the world.
const float fx = (field_min.x + field_max.x) / 2, fy = (field_min.y + field_max.y) / 2;
mouse(SDL_EVENT_MOUSE_MOTION, fx, fy);
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, fx, fy);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, fx, fy);
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && !app->take_navigation().click);
CHECK(required(app->sample_intention(105)).move_y == 0);
for (int i = 0; i < 5 && !app->ui_captured(); ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(app->ui_captured() && required(app->sample_intention(106)).move_y == 0);
// A world click unfocuses the field; capture releases and fresh presses move again.
mouse(SDL_EVENT_MOUSE_MOTION, 260, 200);
mouse(SDL_EVENT_MOUSE_BUTTON_DOWN, 260, 200);
mouse(SDL_EVENT_MOUSE_BUTTON_UP, 260, 200);
CHECK(app->poll_events());
for (int i = 0; i < 5 && app->ui_captured(); ++i)
CHECK(app->present(frame) == Step::rendered && app->poll_events());
CHECK(!app->ui_captured());
static_cast<void>(app->take_navigation());
CHECK(app->poll_events());
key(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && required(app->sample_intention(107)).move_y == 1000);
key(SDL_SCANCODE_W, false);
CHECK(app->poll_events());
// Host order (main loop): poll -> set_ui_capture(false) outside modals -> sample ->
// present. The idempotent external release must not cancel the Tab toggle.
const auto host_frame = [&](std::uint64_t tick) {
CHECK(app->poll_events());
app->set_ui_capture(false);
const auto intention = required(app->sample_intention(tick));
CHECK(app->present(frame) == Step::rendered);
return intention;
};
key(SDL_SCANCODE_TAB, true);
key(SDL_SCANCODE_TAB, false);
CHECK(host_frame(110).move_y == 0 && app->ui_captured());
for (std::uint64_t tick = 111; tick < 115; ++tick) {
key(SDL_SCANCODE_W, true);
CHECK(host_frame(tick).move_y == 0 && app->ui_captured());
}
key(SDL_SCANCODE_TAB, true); // Second deliberate toggle; W stays held physically.
CHECK(host_frame(115).move_y == 0 && !app->ui_captured());
key(SDL_SCANCODE_W, true, true); // A repeat is not a fresh press.
CHECK(host_frame(116).move_y == 0);
key(SDL_SCANCODE_W, true);
CHECK(host_frame(117).move_y == 1000);
// Screenshot hook writes the composed frame (engineering visual review).
const auto shot = std::filesystem::temp_directory_path() / "evo-toolkit-capture.bmp";
CHECK(app->request_screenshot(shot.string()) && app->present(frame) == Step::rendered);
CHECK(std::filesystem::file_size(shot) > std::uintmax_t{320} * 240);
std::filesystem::remove(shot);
CHECK(!app->request_screenshot(""));
}
} // namespace
int main() try {
client_controls_tests();
frame_contract();
lifecycle();
native_input();
toolkit_capture();
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s: %s\n", e.what(), SDL_GetError());
return 1;
} catch (...) {
return 1;
}