test: 添加 Input 模块单元测试
- 创建 tests/Input 目录和 CMakeLists.txt - 添加 28 个测试用例覆盖: - InputManager: Singleton, Initialize/Shutdown, 按键状态, 鼠标, 轴, 按钮 - InputAxis: 默认构造, 正负键, SetValue - InputEvent: KeyEvent, MouseButtonEvent, MouseMoveEvent, MouseWheelEvent, TextInputEvent
This commit is contained in:
423
tests/Input/test_input_manager.cpp
Normal file
423
tests/Input/test_input_manager.cpp
Normal file
@@ -0,0 +1,423 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <XCEngine/Input/InputManager.h>
|
||||
#include <XCEngine/Input/InputTypes.h>
|
||||
#include <XCEngine/Input/InputEvent.h>
|
||||
#include <XCEngine/Input/InputAxis.h>
|
||||
#include <XCEngine/Math/Vector2.h>
|
||||
#include <XCEngine/Containers/String.h>
|
||||
|
||||
using namespace XCEngine::Input;
|
||||
using namespace XCEngine::Math;
|
||||
using namespace XCEngine::Containers;
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(InputManager, Singleton) {
|
||||
InputManager& mgr1 = InputManager::Get();
|
||||
InputManager& mgr2 = InputManager::Get();
|
||||
EXPECT_EQ(&mgr1, &mgr2);
|
||||
}
|
||||
|
||||
TEST(InputManager, InitializeShutdown) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, IsKeyDown) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
EXPECT_FALSE(mgr.IsKeyDown(KeyCode::A));
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::A, false, false, false, false, false);
|
||||
EXPECT_TRUE(mgr.IsKeyDown(KeyCode::A));
|
||||
|
||||
mgr.ProcessKeyUp(KeyCode::A, false, false, false, false);
|
||||
EXPECT_FALSE(mgr.IsKeyDown(KeyCode::A));
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, IsKeyPressed) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
EXPECT_FALSE(mgr.IsKeyPressed(KeyCode::A));
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::A, false, false, false, false, false);
|
||||
EXPECT_TRUE(mgr.IsKeyPressed(KeyCode::A));
|
||||
|
||||
mgr.Update(0.016f);
|
||||
EXPECT_FALSE(mgr.IsKeyPressed(KeyCode::A));
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, IsKeyUp) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
EXPECT_TRUE(mgr.IsKeyUp(KeyCode::A));
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::A, false, false, false, false, false);
|
||||
EXPECT_FALSE(mgr.IsKeyUp(KeyCode::A));
|
||||
|
||||
mgr.ProcessKeyUp(KeyCode::A, false, false, false, false);
|
||||
EXPECT_TRUE(mgr.IsKeyUp(KeyCode::A));
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, KeyEventModifiers) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
bool eventFired = false;
|
||||
KeyEvent capturedEvent{};
|
||||
|
||||
uint64_t id = mgr.OnKeyEvent().Subscribe([&eventFired, &capturedEvent](const KeyEvent& e) {
|
||||
eventFired = true;
|
||||
capturedEvent = e;
|
||||
});
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::A, false, true, true, false, false);
|
||||
EXPECT_TRUE(eventFired);
|
||||
EXPECT_EQ(capturedEvent.keyCode, KeyCode::A);
|
||||
EXPECT_TRUE(capturedEvent.alt);
|
||||
EXPECT_TRUE(capturedEvent.ctrl);
|
||||
EXPECT_FALSE(capturedEvent.shift);
|
||||
EXPECT_FALSE(capturedEvent.meta);
|
||||
EXPECT_EQ(capturedEvent.type, KeyEvent::Type::Down);
|
||||
|
||||
mgr.OnKeyEvent().Unsubscribe(id);
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, KeyEventRepeat) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
bool eventFired = false;
|
||||
KeyEvent capturedEvent{};
|
||||
|
||||
uint64_t id = mgr.OnKeyEvent().Subscribe([&eventFired, &capturedEvent](const KeyEvent& e) {
|
||||
eventFired = true;
|
||||
capturedEvent = e;
|
||||
});
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::A, true, false, false, false, false);
|
||||
EXPECT_TRUE(eventFired);
|
||||
EXPECT_EQ(capturedEvent.type, KeyEvent::Type::Repeat);
|
||||
|
||||
mgr.OnKeyEvent().Unsubscribe(id);
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, MousePosition) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
mgr.ProcessMouseMove(100, 200, 10, -5);
|
||||
|
||||
Vector2 pos = mgr.GetMousePosition();
|
||||
EXPECT_EQ(pos.x, 100.0f);
|
||||
EXPECT_EQ(pos.y, 200.0f);
|
||||
|
||||
Vector2 delta = mgr.GetMouseDelta();
|
||||
EXPECT_EQ(delta.x, 10.0f);
|
||||
EXPECT_EQ(delta.y, -5.0f);
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, MouseButton) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
EXPECT_FALSE(mgr.IsMouseButtonDown(MouseButton::Left));
|
||||
|
||||
mgr.ProcessMouseButton(MouseButton::Left, true, 100, 200);
|
||||
EXPECT_TRUE(mgr.IsMouseButtonDown(MouseButton::Left));
|
||||
|
||||
mgr.ProcessMouseButton(MouseButton::Left, false, 100, 200);
|
||||
EXPECT_FALSE(mgr.IsMouseButtonDown(MouseButton::Left));
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, MouseButtonClicked) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
EXPECT_FALSE(mgr.IsMouseButtonClicked(MouseButton::Left));
|
||||
|
||||
mgr.ProcessMouseButton(MouseButton::Left, true, 100, 200);
|
||||
EXPECT_TRUE(mgr.IsMouseButtonClicked(MouseButton::Left));
|
||||
|
||||
mgr.Update(0.016f);
|
||||
EXPECT_FALSE(mgr.IsMouseButtonClicked(MouseButton::Left));
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, MouseButtonEvent) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
bool eventFired = false;
|
||||
MouseButtonEvent capturedEvent{};
|
||||
|
||||
uint64_t id = mgr.OnMouseButton().Subscribe([&eventFired, &capturedEvent](const MouseButtonEvent& e) {
|
||||
eventFired = true;
|
||||
capturedEvent = e;
|
||||
});
|
||||
|
||||
mgr.ProcessMouseButton(MouseButton::Left, true, 100, 200);
|
||||
EXPECT_TRUE(eventFired);
|
||||
EXPECT_EQ(capturedEvent.button, MouseButton::Left);
|
||||
EXPECT_EQ(capturedEvent.position.x, 100.0f);
|
||||
EXPECT_EQ(capturedEvent.position.y, 200.0f);
|
||||
EXPECT_EQ(capturedEvent.type, MouseButtonEvent::Pressed);
|
||||
|
||||
mgr.OnMouseButton().Unsubscribe(id);
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, MouseMoveEvent) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
bool eventFired = false;
|
||||
MouseMoveEvent capturedEvent{};
|
||||
|
||||
uint64_t id = mgr.OnMouseMove().Subscribe([&eventFired, &capturedEvent](const MouseMoveEvent& e) {
|
||||
eventFired = true;
|
||||
capturedEvent = e;
|
||||
});
|
||||
|
||||
mgr.ProcessMouseMove(100, 200, 10, -5);
|
||||
EXPECT_TRUE(eventFired);
|
||||
EXPECT_EQ(capturedEvent.position.x, 100.0f);
|
||||
EXPECT_EQ(capturedEvent.position.y, 200.0f);
|
||||
EXPECT_EQ(capturedEvent.delta.x, 10.0f);
|
||||
EXPECT_EQ(capturedEvent.delta.y, -5.0f);
|
||||
|
||||
mgr.OnMouseMove().Unsubscribe(id);
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, MouseWheelEvent) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
bool eventFired = false;
|
||||
MouseWheelEvent capturedEvent{};
|
||||
|
||||
uint64_t id = mgr.OnMouseWheel().Subscribe([&eventFired, &capturedEvent](const MouseWheelEvent& e) {
|
||||
eventFired = true;
|
||||
capturedEvent = e;
|
||||
});
|
||||
|
||||
mgr.ProcessMouseWheel(1.0f, 100, 200);
|
||||
EXPECT_TRUE(eventFired);
|
||||
EXPECT_EQ(capturedEvent.delta, 1.0f);
|
||||
|
||||
mgr.OnMouseWheel().Unsubscribe(id);
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, DefaultAxes) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
float h = mgr.GetAxis("Horizontal");
|
||||
EXPECT_GE(h, -1.0f);
|
||||
EXPECT_LE(h, 1.0f);
|
||||
|
||||
float v = mgr.GetAxis("Vertical");
|
||||
EXPECT_GE(v, -1.0f);
|
||||
EXPECT_LE(v, 1.0f);
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, DefaultButtons) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
EXPECT_FALSE(mgr.GetButton("Jump"));
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::Space, false, false, false, false, false);
|
||||
EXPECT_TRUE(mgr.GetButton("Jump"));
|
||||
|
||||
mgr.ProcessKeyUp(KeyCode::Space, false, false, false, false);
|
||||
EXPECT_FALSE(mgr.GetButton("Jump"));
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, GetButtonDown) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
EXPECT_FALSE(mgr.GetButtonDown("Fire1"));
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::LeftCtrl, false, false, false, false, false);
|
||||
EXPECT_TRUE(mgr.GetButtonDown("Fire1"));
|
||||
|
||||
mgr.Update(0.016f);
|
||||
EXPECT_FALSE(mgr.GetButtonDown("Fire1"));
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, GetButtonUp) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::LeftCtrl, false, false, false, false, false);
|
||||
EXPECT_TRUE(mgr.GetButton("Fire1"));
|
||||
|
||||
mgr.ProcessKeyUp(KeyCode::LeftCtrl, false, false, false, false);
|
||||
EXPECT_TRUE(mgr.GetButtonUp("Fire1"));
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, RegisterAxis) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
mgr.ClearAxes();
|
||||
|
||||
InputAxis axis("TestAxis", KeyCode::W, KeyCode::S);
|
||||
mgr.RegisterAxis(axis);
|
||||
|
||||
EXPECT_EQ(mgr.GetAxis("TestAxis"), 0.0f);
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::W, false, false, false, false, false);
|
||||
EXPECT_EQ(mgr.GetAxis("TestAxis"), 1.0f);
|
||||
|
||||
mgr.ProcessKeyUp(KeyCode::W, false, false, false, false);
|
||||
EXPECT_EQ(mgr.GetAxis("TestAxis"), 0.0f);
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::S, false, false, false, false, false);
|
||||
EXPECT_EQ(mgr.GetAxis("TestAxis"), -1.0f);
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, GetAxisRaw) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
mgr.ClearAxes();
|
||||
InputAxis axis("RawTest", KeyCode::D, KeyCode::A);
|
||||
mgr.RegisterAxis(axis);
|
||||
|
||||
mgr.ProcessKeyDown(KeyCode::D, false, false, false, false, false);
|
||||
EXPECT_EQ(mgr.GetAxisRaw("RawTest"), 1.0f);
|
||||
|
||||
mgr.Update(0.016f);
|
||||
EXPECT_EQ(mgr.GetAxisRaw("RawTest"), 0.0f);
|
||||
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputManager, TextInputEvent) {
|
||||
InputManager& mgr = InputManager::Get();
|
||||
mgr.Initialize(nullptr);
|
||||
|
||||
bool eventFired = false;
|
||||
TextInputEvent capturedEvent{};
|
||||
|
||||
uint64_t id = mgr.OnTextInput().Subscribe([&eventFired, &capturedEvent](const TextInputEvent& e) {
|
||||
eventFired = true;
|
||||
capturedEvent = e;
|
||||
});
|
||||
|
||||
mgr.ProcessTextInput('A');
|
||||
EXPECT_TRUE(eventFired);
|
||||
EXPECT_EQ(capturedEvent.character, 'A');
|
||||
|
||||
mgr.OnTextInput().Unsubscribe(id);
|
||||
mgr.Shutdown();
|
||||
}
|
||||
|
||||
TEST(InputAxis, DefaultConstruction) {
|
||||
InputAxis axis;
|
||||
EXPECT_EQ(axis.GetValue(), 0.0f);
|
||||
EXPECT_EQ(axis.GetPositiveKey(), KeyCode::None);
|
||||
EXPECT_EQ(axis.GetNegativeKey(), KeyCode::None);
|
||||
}
|
||||
|
||||
TEST(InputAxis, PositiveNegativeKeys) {
|
||||
InputAxis axis("Test", KeyCode::W, KeyCode::S);
|
||||
EXPECT_EQ(axis.GetPositiveKey(), KeyCode::W);
|
||||
EXPECT_EQ(axis.GetNegativeKey(), KeyCode::S);
|
||||
}
|
||||
|
||||
TEST(InputAxis, SetValue) {
|
||||
InputAxis axis;
|
||||
axis.SetValue(0.5f);
|
||||
EXPECT_EQ(axis.GetValue(), 0.5f);
|
||||
}
|
||||
|
||||
TEST(InputEvent, KeyEventConstruction) {
|
||||
KeyEvent event;
|
||||
event.keyCode = KeyCode::A;
|
||||
event.alt = false;
|
||||
event.ctrl = false;
|
||||
event.shift = false;
|
||||
event.meta = false;
|
||||
event.type = KeyEvent::Type::Down;
|
||||
EXPECT_EQ(event.keyCode, KeyCode::A);
|
||||
EXPECT_FALSE(event.alt);
|
||||
EXPECT_FALSE(event.ctrl);
|
||||
EXPECT_FALSE(event.shift);
|
||||
EXPECT_FALSE(event.meta);
|
||||
EXPECT_EQ(event.type, KeyEvent::Type::Down);
|
||||
}
|
||||
|
||||
TEST(InputEvent, MouseButtonEventConstruction) {
|
||||
MouseButtonEvent event;
|
||||
event.button = MouseButton::Left;
|
||||
event.position.x = 100.0f;
|
||||
event.position.y = 200.0f;
|
||||
event.type = MouseButtonEvent::Pressed;
|
||||
EXPECT_EQ(event.button, MouseButton::Left);
|
||||
EXPECT_EQ(event.position.x, 100.0f);
|
||||
EXPECT_EQ(event.position.y, 200.0f);
|
||||
EXPECT_EQ(event.type, MouseButtonEvent::Pressed);
|
||||
}
|
||||
|
||||
TEST(InputEvent, MouseMoveEventConstruction) {
|
||||
MouseMoveEvent event;
|
||||
event.position.x = 100.0f;
|
||||
event.position.y = 200.0f;
|
||||
event.delta.x = 10.0f;
|
||||
event.delta.y = -5.0f;
|
||||
EXPECT_EQ(event.position.x, 100.0f);
|
||||
EXPECT_EQ(event.position.y, 200.0f);
|
||||
EXPECT_EQ(event.delta.x, 10.0f);
|
||||
EXPECT_EQ(event.delta.y, -5.0f);
|
||||
}
|
||||
|
||||
TEST(InputEvent, MouseWheelEventConstruction) {
|
||||
MouseWheelEvent event;
|
||||
event.position.x = 100.0f;
|
||||
event.position.y = 200.0f;
|
||||
event.delta = 1.5f;
|
||||
EXPECT_EQ(event.position.x, 100.0f);
|
||||
EXPECT_EQ(event.position.y, 200.0f);
|
||||
EXPECT_EQ(event.delta, 1.5f);
|
||||
}
|
||||
|
||||
TEST(InputEvent, TextInputEventConstruction) {
|
||||
TextInputEvent event;
|
||||
event.character = 'X';
|
||||
EXPECT_EQ(event.character, 'X');
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user