feat: 实现 Window 与 InputModule 消息集成
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
set(INPUT_TEST_SOURCES
|
||||
test_input_manager.cpp
|
||||
test_windows_input_module.cpp
|
||||
)
|
||||
|
||||
add_executable(xcengine_input_tests ${INPUT_TEST_SOURCES})
|
||||
|
||||
194
tests/Input/test_windows_input_module.cpp
Normal file
194
tests/Input/test_windows_input_module.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <XCEngine/Input/InputManager.h>
|
||||
#include <XCEngine/Input/InputModule.h>
|
||||
#include <XCEngine/Input/Platform/WindowsInputModule.h>
|
||||
#include <XCEngine/Math/Vector2.h>
|
||||
#include <XCEngine/Containers/String.h>
|
||||
|
||||
using namespace XCEngine::Input;
|
||||
using namespace XCEngine::Math;
|
||||
using namespace XCEngine::Containers;
|
||||
using namespace XCEngine::Input::Platform;
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(WindowsInputModule, Construction) {
|
||||
WindowsInputModule module;
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, InitializeShutdown) {
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
module.Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, HandleKeyDown) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
|
||||
EXPECT_FALSE(InputManager::Get().IsKeyDown(KeyCode::A));
|
||||
|
||||
module.HandleMessage(0, 0x0100, 'A', 0);
|
||||
|
||||
EXPECT_TRUE(InputManager::Get().IsKeyDown(KeyCode::A));
|
||||
|
||||
module.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, HandleKeyUp) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
|
||||
module.HandleMessage(0, 0x0100, 'A', 0);
|
||||
EXPECT_TRUE(InputManager::Get().IsKeyDown(KeyCode::A));
|
||||
|
||||
module.HandleMessage(0, 0x0101, 'A', 0);
|
||||
EXPECT_FALSE(InputManager::Get().IsKeyDown(KeyCode::A));
|
||||
|
||||
module.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, HandleMouseMove) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
|
||||
module.HandleMessage(0, 0x0200, 0, 0x00320078);
|
||||
|
||||
Vector2 pos = InputManager::Get().GetMousePosition();
|
||||
EXPECT_EQ(pos.x, 120.0f);
|
||||
EXPECT_EQ(pos.y, 50.0f);
|
||||
|
||||
module.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, HandleLeftMouseButtonDown) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
|
||||
module.HandleMessage(0, 0x0201, 0, 0x00320078);
|
||||
|
||||
EXPECT_TRUE(InputManager::Get().IsMouseButtonDown(MouseButton::Left));
|
||||
|
||||
module.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, HandleLeftMouseButtonUp) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
|
||||
module.HandleMessage(0, 0x0201, 0, 0x00320078);
|
||||
EXPECT_TRUE(InputManager::Get().IsMouseButtonDown(MouseButton::Left));
|
||||
|
||||
module.HandleMessage(0, 0x0202, 0, 0x00320078);
|
||||
EXPECT_FALSE(InputManager::Get().IsMouseButtonDown(MouseButton::Left));
|
||||
|
||||
module.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, HandleRightMouseButton) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
|
||||
module.HandleMessage(0, 0x0204, 0, 0x00320078);
|
||||
EXPECT_TRUE(InputManager::Get().IsMouseButtonDown(MouseButton::Right));
|
||||
|
||||
module.HandleMessage(0, 0x0205, 0, 0x00320078);
|
||||
EXPECT_FALSE(InputManager::Get().IsMouseButtonDown(MouseButton::Right));
|
||||
|
||||
module.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, HandleMiddleMouseButton) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
|
||||
module.HandleMessage(0, 0x0207, 0, 0x00320078);
|
||||
EXPECT_TRUE(InputManager::Get().IsMouseButtonDown(MouseButton::Middle));
|
||||
|
||||
module.HandleMessage(0, 0x0208, 0, 0x00320078);
|
||||
EXPECT_FALSE(InputManager::Get().IsMouseButtonDown(MouseButton::Middle));
|
||||
|
||||
module.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, HandleMouseWheel) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
|
||||
module.HandleMessage(0, 0x020A, 0x00030000, 0x00780078);
|
||||
|
||||
float scrollDelta = InputManager::Get().GetMouseScrollDelta();
|
||||
EXPECT_EQ(scrollDelta, 0.025f);
|
||||
|
||||
module.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, HandleTextInput) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module;
|
||||
module.Initialize(nullptr);
|
||||
|
||||
bool eventFired = false;
|
||||
char capturedChar = 0;
|
||||
|
||||
uint64_t id = InputManager::Get().OnTextInput().Subscribe([&eventFired, &capturedChar](const TextInputEvent& e) {
|
||||
eventFired = true;
|
||||
capturedChar = e.character;
|
||||
});
|
||||
|
||||
module.HandleMessage(0, 0x0102, 'X', 0);
|
||||
|
||||
EXPECT_TRUE(eventFired);
|
||||
EXPECT_EQ(capturedChar, 'X');
|
||||
|
||||
InputManager::Get().OnTextInput().Unsubscribe(id);
|
||||
module.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
TEST(WindowsInputModule, MultipleModules) {
|
||||
InputManager::Get().Initialize(nullptr);
|
||||
|
||||
WindowsInputModule module1;
|
||||
WindowsInputModule module2;
|
||||
|
||||
module1.Initialize(nullptr);
|
||||
module2.Initialize(nullptr);
|
||||
|
||||
module1.HandleMessage(0, 0x0100, 'A', 0);
|
||||
EXPECT_TRUE(InputManager::Get().IsKeyDown(KeyCode::A));
|
||||
|
||||
module2.HandleMessage(0, 0x0100, 'B', 0);
|
||||
EXPECT_TRUE(InputManager::Get().IsKeyDown(KeyCode::B));
|
||||
|
||||
module1.Shutdown();
|
||||
module2.Shutdown();
|
||||
InputManager::Get().Shutdown();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user