Files
XCEngine/tests/NewEditor/test_imgui_xcui_input_adapter.cpp

89 lines
2.8 KiB
C++

#include <gtest/gtest.h>
#include "XCUIBackend/ImGuiXCUIInputAdapter.h"
#include <XCEngine/Input/InputTypes.h>
namespace {
using XCEngine::Editor::XCUIBackend::ImGuiXCUIInputAdapter;
using XCEngine::Editor::XCUIBackend::XCUIInputBridgeCaptureOptions;
using XCEngine::Input::KeyCode;
class ImGuiContextScope {
public:
ImGuiContextScope() {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
}
~ImGuiContextScope() {
ImGui::DestroyContext();
}
};
void PrepareImGui(float width = 1024.0f, float height = 768.0f) {
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(width, height);
io.DeltaTime = 1.0f / 60.0f;
}
} // namespace
TEST(ImGuiXCUIInputAdapterTest, CaptureSnapshotMapsImGuiStateIntoXCUIFrameSnapshot) {
ImGuiContextScope contextScope;
PrepareImGui(800.0f, 600.0f);
ImGuiIO& io = ImGui::GetIO();
io.WantCaptureMouse = true;
io.WantCaptureKeyboard = true;
io.WantTextInput = true;
io.AddMousePosEvent(120.0f, 72.0f);
io.AddMouseButtonEvent(0, true);
io.AddMouseWheelEvent(0.0f, 1.0f);
io.AddKeyEvent(ImGuiKey_LeftCtrl, true);
io.AddKeyEvent(ImGuiKey_P, true);
io.AddInputCharacter('p');
ImGui::NewFrame();
XCUIInputBridgeCaptureOptions options = {};
options.pointerOffset = XCEngine::UI::UIPoint(20.0f, 12.0f);
options.windowFocused = false;
options.timestampNanoseconds = 99u;
const auto snapshot = ImGuiXCUIInputAdapter::CaptureSnapshot(io, options);
ImGui::EndFrame();
EXPECT_FLOAT_EQ(snapshot.pointerPosition.x, 100.0f);
EXPECT_FLOAT_EQ(snapshot.pointerPosition.y, 60.0f);
EXPECT_TRUE(snapshot.pointerInside);
EXPECT_TRUE(snapshot.pointerButtonsDown[0]);
EXPECT_FLOAT_EQ(snapshot.wheelDelta.x, 0.0f);
EXPECT_FLOAT_EQ(snapshot.wheelDelta.y, 1.0f);
EXPECT_TRUE(snapshot.modifiers.control);
EXPECT_FALSE(snapshot.windowFocused);
EXPECT_TRUE(snapshot.wantCaptureMouse);
EXPECT_TRUE(snapshot.wantCaptureKeyboard);
EXPECT_TRUE(snapshot.wantTextInput);
EXPECT_EQ(snapshot.timestampNanoseconds, 99u);
EXPECT_TRUE(snapshot.IsKeyDown(static_cast<std::int32_t>(KeyCode::LeftCtrl)));
EXPECT_TRUE(snapshot.IsKeyDown(static_cast<std::int32_t>(KeyCode::P)));
ASSERT_EQ(snapshot.characters.size(), 1u);
EXPECT_EQ(snapshot.characters[0], static_cast<std::uint32_t>('p'));
}
TEST(ImGuiXCUIInputAdapterTest, MapKeyCodeReturnsXCUIKeyCodesForNamedKeys) {
EXPECT_EQ(
ImGuiXCUIInputAdapter::MapKeyCode(ImGuiKey_A),
static_cast<std::int32_t>(KeyCode::A));
EXPECT_EQ(
ImGuiXCUIInputAdapter::MapKeyCode(ImGuiKey_F12),
static_cast<std::int32_t>(KeyCode::F12));
EXPECT_EQ(
ImGuiXCUIInputAdapter::MapKeyCode(ImGuiKey_None),
static_cast<std::int32_t>(KeyCode::None));
}