Files
XCEngine/tests/NewEditor/test_imgui_xcui_input_adapter.cpp

92 lines
3.1 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;
unsigned char* fontPixels = nullptr;
int fontWidth = 0;
int fontHeight = 0;
io.Fonts->GetTexDataAsRGBA32(&fontPixels, &fontWidth, &fontHeight);
io.Fonts->SetTexID(static_cast<ImTextureID>(1));
}
} // namespace
TEST(ImGuiXCUIInputAdapterTest, CaptureSnapshotMapsImGuiStateIntoXCUIFrameSnapshot) {
ImGuiContextScope contextScope;
PrepareImGui(800.0f, 600.0f);
ImGuiIO& io = ImGui::GetIO();
io.MousePos = ImVec2(120.0f, 72.0f);
io.MouseDown[0] = true;
io.MouseWheel = 1.0f;
io.WantCaptureMouse = true;
io.WantCaptureKeyboard = true;
io.WantTextInput = true;
io.KeyCtrl = true;
io.KeysData[ImGuiKey_LeftCtrl - ImGuiKey_NamedKey_BEGIN].Down = true;
io.KeysData[ImGuiKey_P - ImGuiKey_NamedKey_BEGIN].Down = true;
io.InputQueueCharacters.resize(0);
io.InputQueueCharacters.push_back(static_cast<ImWchar>('p'));
XCUIInputBridgeCaptureOptions options = {};
options.pointerOffset = XCEngine::UI::UIPoint(20.0f, 12.0f);
options.windowFocused = false;
options.timestampNanoseconds = 99u;
const auto snapshot = ImGuiXCUIInputAdapter::CaptureSnapshot(io, options);
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));
}