GroveEngine/tests/integration/IT_015_input_ui_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

113 lines
3.9 KiB
C++

/**
* Integration Test IT_015: UIModule Input Event Integration (Simplified)
*
* Tests input event processing by publishing IIO messages directly:
* - Direct IIO input event publishing (bypasses InputModule/SDL)
* - UIModule consumes input events and processes them
* - Verifies UI event generation
*
* Note: This test bypasses InputModule to avoid SDL dependencies.
* For full InputModule testing, see test_30_input_module.cpp
*/
#include <catch2/catch_test_macros.hpp>
#include <grove/ModuleLoader.h>
#include <grove/IntraIOManager.h>
#include <grove/IntraIO.h>
#include <grove/JsonDataNode.h>
#include <iostream>
using namespace grove;
TEST_CASE("IT_015: UIModule Input Integration", "[integration][input][ui][phase3]") {
std::cout << "\n========================================\n";
std::cout << "IT_015: Input → UI Integration Test\n";
std::cout << "========================================\n\n";
auto& ioManager = IntraIOManager::getInstance();
// Create IIO instances
auto inputPublisher = ioManager.createInstance("input_publisher");
auto uiIO = ioManager.createInstance("ui_module");
auto testIO = ioManager.createInstance("test_observer");
// Load UIModule
ModuleLoader uiLoader;
std::string uiPath = "../modules/libUIModule.so";
#ifdef _WIN32
uiPath = "../modules/libUIModule.dll";
#endif
std::unique_ptr<IModule> uiModule;
REQUIRE_NOTHROW(uiModule = uiLoader.load(uiPath, "ui_module"));
REQUIRE(uiModule != nullptr);
// Configure UIModule
JsonDataNode uiConfig("config");
uiConfig.setInt("windowWidth", 800);
uiConfig.setInt("windowHeight", 600);
uiConfig.setString("layoutFile", "../../assets/ui/test_buttons.json");
uiConfig.setInt("baseLayer", 1000);
REQUIRE_NOTHROW(uiModule->setConfiguration(uiConfig, uiIO.get(), nullptr));
std::cout << "✅ UIModule loaded\n\n";
int uiClicksReceived = 0;
int uiHoversReceived = 0;
// Subscribe to events with callbacks
testIO->subscribe("ui:click", [&](const Message& msg) {
uiClicksReceived++;
std::cout << "✅ Received ui:click event\n";
});
testIO->subscribe("ui:hover", [&](const Message& msg) {
uiHoversReceived++;
std::cout << "✅ Received ui:hover event\n";
});
testIO->subscribe("ui:action", [](const Message& msg) {
// Just acknowledge action events
});
// Publish input events via IIO (simulates InputModule output)
std::cout << "Publishing input events...\n";
// Mouse move to center
auto mouseMoveData = std::make_unique<JsonDataNode>("data");
mouseMoveData->setInt("x", 400);
mouseMoveData->setInt("y", 300);
inputPublisher->publish("input:mouse:move", std::move(mouseMoveData));
// Process UIModule
JsonDataNode inputData("input");
uiModule->process(inputData);
// Mouse click
auto mouseClickData = std::make_unique<JsonDataNode>("data");
mouseClickData->setInt("button", 0);
mouseClickData->setBool("pressed", true);
mouseClickData->setInt("x", 100);
mouseClickData->setInt("y", 100);
inputPublisher->publish("input:mouse:button", std::move(mouseClickData));
// Process UIModule again
uiModule->process(inputData);
// Dispatch UI events (callbacks handle counting)
while (testIO->hasMessages() > 0) {
testIO->pullAndDispatch();
}
std::cout << "\nResults:\n";
std::cout << " - UI clicks: " << uiClicksReceived << "\n";
std::cout << " - UI hovers: " << uiHoversReceived << "\n";
// Note: UI events depend on layout file, so we don't REQUIRE them
// This test mainly verifies that UIModule can be loaded and process input events
std::cout << "\n✅ IT_015: Integration test PASSED\n";
std::cout << "========================================\n\n";
// Cleanup
uiModule->shutdown();
}