GroveEngine/tests/integration/IT_014_ui_module_integration.cpp
StillHammer 1b7703f07b feat(IIO)!: BREAKING CHANGE - Callback-based message dispatch
## Breaking Change

IIO API redesigned from manual pull+if-forest to callback dispatch.
All modules must update their subscribe() calls to pass handlers.

### Before (OLD API)
```cpp
io->subscribe("input:mouse");

void process(...) {
    while (io->hasMessages()) {
        auto msg = io->pullMessage();
        if (msg.topic == "input:mouse") {
            handleMouse(msg);
        } else if (msg.topic == "input:keyboard") {
            handleKeyboard(msg);
        }
    }
}
```

### After (NEW API)
```cpp
io->subscribe("input:mouse", [this](const Message& msg) {
    handleMouse(msg);
});

void process(...) {
    while (io->hasMessages()) {
        io->pullAndDispatch();  // Callbacks invoked automatically
    }
}
```

## Changes

**Core API (include/grove/IIO.h)**
- Added: `using MessageHandler = std::function<void(const Message&)>`
- Changed: `subscribe()` now requires `MessageHandler` callback parameter
- Changed: `subscribeLowFreq()` now requires `MessageHandler` callback
- Removed: `pullMessage()`
- Added: `pullAndDispatch()` - pulls and auto-dispatches to handlers

**Implementation (src/IntraIO.cpp)**
- Store callbacks in `Subscription.handler`
- `pullAndDispatch()` matches topic against ALL subscriptions (not just first)
- Fixed: Regex pattern compilation supports both wildcards (*) and regex (.*)
- Performance: ~1000 msg/s throughput (unchanged from before)

**Files Updated**
- 31 test/module files migrated to callback API (via parallel agents)
- 8 documentation files updated (DEVELOPER_GUIDE, USER_GUIDE, module READMEs)

## Bugs Fixed During Migration

1. **pullAndDispatch() early return bug**: Was only calling FIRST matching handler
   - Fix: Loop through ALL subscriptions, invoke all matching handlers

2. **Regex pattern compilation bug**: Pattern "player:.*" failed to match
   - Fix: Detect ".*" in pattern → use as regex, otherwise escape and convert wildcards

## Testing

 test_11_io_system: PASSED (IIO pub/sub, pattern matching, batching)
 test_threaded_module_system: 6/6 PASSED
 test_threaded_stress: 5/5 PASSED (50 modules, 100x reload, concurrent ops)
 test_12_datanode: PASSED
 10 TopicTree scenarios: 10/10 PASSED
 benchmark_e2e: ~1000 msg/s throughput

Total: 23+ tests passing

## Performance Impact

No performance regression from callback dispatch:
- IIO throughput: ~1000 msg/s (same as before)
- ThreadedModuleSystem: Speedup ~1.0x (barrier pattern expected)

## Migration Guide

For all modules using IIO:

1. Update subscribe() calls to include handler lambda
2. Replace pullMessage() loops with pullAndDispatch()
3. Move topic-specific logic from if-forest into callbacks

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-19 14:19:27 +07:00

268 lines
12 KiB
C++

/**
* Integration Test IT_014: UIModule Full Integration
*
* Tests complete UI system integration:
* - BgfxRenderer module (rendering backend)
* - UIModule (UI widgets and layout)
* - TestControllerModule (simulates game logic)
*
* Verifies:
* - All modules load and communicate via IIO
* - UI events trigger game logic
* - Game logic updates UI state
* - Mouse/keyboard input flows correctly
* - Tooltips and scrolling work
*/
#include <catch2/catch_test_macros.hpp>
#include <grove/ModuleLoader.h>
#include <grove/IntraIOManager.h>
#include <grove/IntraIO.h>
#include <grove/JsonDataNode.h>
#include <thread>
#include <chrono>
#include <iostream>
using namespace grove;
TEST_CASE("IT_014: UIModule Full Integration", "[integration][ui][phase7]") {
std::cout << "\n========================================\n";
std::cout << "IT_014: UIModule Full Integration Test\n";
std::cout << "========================================\n\n";
auto& ioManager = IntraIOManager::getInstance();
// Create IIO instances for each module
auto rendererIO = ioManager.createInstance("bgfx_renderer");
auto uiIO = ioManager.createInstance("ui_module");
auto gameIO = ioManager.createInstance("game_logic");
SECTION("Load all modules") {
ModuleLoader rendererLoader;
ModuleLoader uiLoader;
ModuleLoader gameLoader;
std::string rendererPath = "../modules/libBgfxRenderer.so";
std::string uiPath = "../modules/libUIModule.so";
std::string gamePath = "./libTestControllerModule.so";
#ifdef _WIN32
rendererPath = "../modules/BgfxRenderer.dll";
uiPath = "../modules/UIModule.dll";
gamePath = "./TestControllerModule.dll";
#endif
SECTION("Load BgfxRenderer") {
std::unique_ptr<IModule> renderer;
REQUIRE_NOTHROW(renderer = rendererLoader.load(rendererPath, "bgfx_renderer"));
REQUIRE(renderer != nullptr);
// Configure headless renderer
JsonDataNode config("config");
config.setInt("windowWidth", 800);
config.setInt("windowHeight", 600);
config.setString("backend", "noop"); // Headless mode
config.setBool("vsync", false);
REQUIRE_NOTHROW(renderer->setConfiguration(config, rendererIO.get(), nullptr));
std::cout << "✅ BgfxRenderer loaded and configured\n";
SECTION("Load UIModule") {
std::unique_ptr<IModule> uiModule;
REQUIRE_NOTHROW(uiModule = uiLoader.load(uiPath, "ui_module"));
REQUIRE(uiModule != nullptr);
// Configure UI with test layout
JsonDataNode uiConfig("config");
uiConfig.setInt("windowWidth", 800);
uiConfig.setInt("windowHeight", 600);
uiConfig.setString("layoutFile", "../../assets/ui/test_widgets.json");
uiConfig.setInt("baseLayer", 1000);
REQUIRE_NOTHROW(uiModule->setConfiguration(uiConfig, uiIO.get(), nullptr));
std::cout << "✅ UIModule loaded and configured\n";
SECTION("Load TestControllerModule") {
std::unique_ptr<IModule> gameModule;
REQUIRE_NOTHROW(gameModule = gameLoader.load(gamePath, "game_logic"));
REQUIRE(gameModule != nullptr);
// Configure game controller
JsonDataNode gameConfig("config");
gameConfig.setString("uiInstanceId", "ui_module");
REQUIRE_NOTHROW(gameModule->setConfiguration(gameConfig, gameIO.get(), nullptr));
std::cout << "✅ TestControllerModule loaded and configured\n";
SECTION("Run integration loop") {
std::cout << "\n--- Running Integration Loop ---\n";
// Check if renderer is healthy before using it
auto rendererHealth = renderer->getHealthStatus();
bool rendererOK = rendererHealth &&
rendererHealth->getString("status", "") == "healthy";
if (!rendererOK) {
std::cout << "⚠️ Renderer not healthy (expected for noop backend), skipping renderer process calls\n";
}
int clickCount = 0;
int actionCount = 0;
int valueChangeCount = 0;
int hoverCount = 0;
// Subscribe to events we want to verify with callbacks
gameIO->subscribe("ui:click", [&](const Message& msg) {
clickCount++;
});
gameIO->subscribe("ui:action", [&](const Message& msg) {
actionCount++;
});
gameIO->subscribe("ui:value_changed", [&](const Message& msg) {
valueChangeCount++;
});
gameIO->subscribe("ui:hover", [&](const Message& msg) {
bool enter = msg.data->getBool("enter", false);
if (enter) {
hoverCount++;
}
});
// Simulate 60 frames (~1 second at 60fps)
for (int frame = 0; frame < 60; frame++) {
// Simulate mouse movement
if (frame == 10) {
auto mouseMove = std::make_unique<JsonDataNode>("mouse_move");
mouseMove->setDouble("x", 400.0);
mouseMove->setDouble("y", 300.0);
uiIO->publish("input:mouse:move", std::move(mouseMove));
}
// Simulate mouse click on button
if (frame == 20) {
auto mouseDown = std::make_unique<JsonDataNode>("mouse_button");
mouseDown->setInt("button", 0);
mouseDown->setBool("pressed", true);
mouseDown->setDouble("x", 400.0);
mouseDown->setDouble("y", 300.0);
uiIO->publish("input:mouse:button", std::move(mouseDown));
}
if (frame == 22) {
auto mouseUp = std::make_unique<JsonDataNode>("mouse_button");
mouseUp->setInt("button", 0);
mouseUp->setBool("pressed", false);
mouseUp->setDouble("x", 400.0);
mouseUp->setDouble("y", 300.0);
uiIO->publish("input:mouse:button", std::move(mouseUp));
}
// Simulate mouse wheel
if (frame == 30) {
auto mouseWheel = std::make_unique<JsonDataNode>("mouse_wheel");
mouseWheel->setDouble("delta", 1.0);
uiIO->publish("input:mouse:wheel", std::move(mouseWheel));
}
// Process all modules
JsonDataNode frameInput("input");
frameInput.setDouble("deltaTime", 1.0 / 60.0);
uiModule->process(frameInput);
gameModule->process(frameInput);
// Only call renderer if it's healthy
if (rendererOK) {
renderer->process(frameInput);
}
// Dispatch events (callbacks handle counting and logging)
while (gameIO->hasMessages() > 0) {
gameIO->pullAndDispatch();
}
// Small delay to simulate real-time
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
std::cout << "\n--- Integration Loop Complete ---\n";
std::cout << "Events received:\n";
std::cout << " Clicks: " << clickCount << "\n";
std::cout << " Actions: " << actionCount << "\n";
std::cout << " Value changes: " << valueChangeCount << "\n";
std::cout << " Hovers: " << hoverCount << "\n";
// In headless mode without renderer, we might not get events
// The important thing is that the loop ran without crashing
std::cout << "\n✅ Integration loop successful (60 frames processed without crash)\n";
}
SECTION("Verify module health") {
auto uiHealth = uiModule->getHealthStatus();
REQUIRE(uiHealth != nullptr);
std::string uiStatus = uiHealth->getString("status", "");
REQUIRE((uiStatus == "healthy" || uiStatus == "running"));
auto gameHealth = gameModule->getHealthStatus();
REQUIRE(gameHealth != nullptr);
std::string gameStatus = gameHealth->getString("status", "");
REQUIRE((gameStatus == "healthy" || gameStatus == "running"));
std::cout << "✅ All modules healthy (UI: " << uiStatus << ", Game: " << gameStatus << ")\n";
}
SECTION("Test state save/restore") {
// Get state from game module
auto state = gameModule->getState();
REQUIRE(state != nullptr);
// Modify and restore
gameModule->setState(*state);
std::cout << "✅ State save/restore works\n";
}
// Cleanup game module BEFORE leaving scope
std::cout << "Shutting down game module...\n";
gameModule->shutdown();
gameModule.reset(); // Explicitly reset before unload
gameLoader.unload();
std::cout << "Game module unloaded\n";
}
// Cleanup UI module BEFORE leaving scope
std::cout << "Shutting down UI module...\n";
uiModule->shutdown();
uiModule.reset(); // Explicitly reset before unload
uiLoader.unload();
std::cout << "UI module unloaded\n";
}
// Cleanup renderer BEFORE leaving scope
std::cout << "Shutting down renderer...\n";
renderer->shutdown();
renderer.reset(); // Explicitly reset before unload
rendererLoader.unload();
std::cout << "Renderer unloaded\n";
}
}
// Small delay to let background threads settle
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Cleanup IIO instances
std::cout << "Removing IIO instances...\n";
ioManager.removeInstance("bgfx_renderer");
ioManager.removeInstance("ui_module");
ioManager.removeInstance("game_logic");
std::cout << "IIO instances removed\n";
std::cout << "\n========================================\n";
std::cout << "✅ IT_014 PASSED - Full Integration OK\n";
std::cout << "========================================\n";
}