|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include <gtest/gtest.h> |
4 | | -#include <memory> |
5 | 4 | #include <functional> |
6 | 5 | #include <chrono> |
7 | 6 | #include <thread> |
|
11 | 10 | #include "TestConfig.h" |
12 | 11 | #include "TestElements.h" |
13 | 12 | #include "../../src/Mouse.h" |
14 | | -#include "../../src/Utils.h" |
15 | 13 |
|
16 | 14 | namespace RobotTest { |
17 | | - |
18 | | -/** |
19 | | - * @brief Test fixture for SDL-based Robot tests |
20 | | - * |
21 | | - * This fixture handles SDL initialization, window creation, and |
22 | | - * cleanup for all Robot CPP SDL-based tests. |
23 | | - */ |
24 | | -class RobotSDLTest : public ::testing::Test { |
25 | | -protected: |
26 | | - void SetUp() override { |
27 | | - // Initialize test config with reasonable defaults |
28 | | - config_ = std::make_unique<TestConfig>(); |
29 | | - |
30 | | - // Initialize SDL, window, and renderer |
31 | | - context_ = std::make_unique<TestContext>(*config_); |
32 | | - |
33 | | - // Wait for window to be ready |
34 | | - context_->prepareForTests(); |
35 | | - SDL_Delay(static_cast<Uint32>(config_->setupDelay.count())); |
36 | | - } |
37 | | - |
38 | | - void TearDown() override { |
39 | | - // Clear all test elements |
40 | | - testElements_.clear(); |
41 | | - |
42 | | - // TestContext destructor will handle SDL cleanup |
43 | | - context_.reset(); |
44 | | - } |
45 | | - |
46 | 15 | /** |
47 | | - * @brief Creates a drag element and adds it to the test elements collection |
48 | | - * @return Pointer to the created drag element (owned by the fixture) |
| 16 | + * @brief Test fixture for SDL-based Robot tests |
| 17 | + * |
| 18 | + * This fixture handles SDL initialization, window creation, and |
| 19 | + * cleanup for all Robot CPP SDL-based tests. |
49 | 20 | */ |
50 | | - DragElement* createDragElement(int x, int y, int width, int height, |
51 | | - Color color, const std::string& name) { |
52 | | - auto element = std::make_unique<DragElement>( |
53 | | - SDL_Rect{x, y, width, height}, color, name); |
54 | | - |
55 | | - auto* rawPtr = element.get(); |
56 | | - testElements_.push_back(std::move(element)); |
57 | | - return rawPtr; |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * @brief Runs the event loop for a specified duration |
62 | | - * @param duration How long to process events |
63 | | - */ |
64 | | - void processEventsFor(std::chrono::milliseconds duration) { |
65 | | - auto startTime = std::chrono::steady_clock::now(); |
66 | | - bool running = true; |
67 | | - |
68 | | - while (running && |
69 | | - (std::chrono::steady_clock::now() - startTime < duration)) { |
70 | | - |
71 | | - // Process pending SDL events |
72 | | - context_->handleEvents(running); |
| 21 | + class RobotSDLTest : public ::testing::Test { |
| 22 | + protected: |
| 23 | + void SetUp() override { |
| 24 | + // Initialize test config with reasonable defaults |
| 25 | + config_ = std::make_unique<TestConfig>(); |
| 26 | + |
| 27 | + // Initialize SDL, window, and renderer |
| 28 | + context_ = std::make_unique<TestContext>(*config_); |
| 29 | + |
| 30 | + // Wait for window to be ready |
| 31 | + context_->prepareForTests(); |
| 32 | + SDL_Delay(static_cast<Uint32>(config_->setupDelay.count())); |
| 33 | + } |
73 | 34 |
|
74 | | - // Render current state |
75 | | - context_->renderFrame([this](SDL_Renderer* renderer) { |
76 | | - renderTestElements(renderer); |
77 | | - }); |
| 35 | + void TearDown() override { |
| 36 | + // Clear all test elements |
| 37 | + testElements_.clear(); |
78 | 38 |
|
79 | | - // Limit frame rate |
80 | | - SDL_Delay(static_cast<Uint32>(config_->frameDelay.count())); |
| 39 | + // TestContext destructor will handle SDL cleanup |
| 40 | + context_.reset(); |
81 | 41 | } |
82 | | - } |
83 | | - |
84 | | - /** |
85 | | - * @brief Converts window coordinates to screen coordinates |
86 | | - */ |
87 | | - Robot::Point windowToScreen(int x, int y) const { |
88 | | - int windowX, windowY; |
89 | | - SDL_GetWindowPosition(context_->getWindow(), &windowX, &windowY); |
90 | | - return {x + windowX, y + windowY}; |
91 | | - } |
92 | 42 |
|
93 | | - /** |
94 | | - * @brief Performs a mouse drag operation |
95 | | - * @param startPoint Starting point in window coordinates |
96 | | - * @param endPoint Ending point in window coordinates |
97 | | - */ |
98 | | - void performMouseDrag(const SDL_Point& startPoint, const SDL_Point& endPoint) { |
99 | | - // Convert to screen coordinates |
100 | | - Robot::Point startPos = windowToScreen(startPoint.x, startPoint.y); |
101 | | - Robot::Point endPos = windowToScreen(endPoint.x, endPoint.y); |
| 43 | + /** |
| 44 | + * @brief Creates a drag element and adds it to the test elements collection |
| 45 | + * @return Pointer to the created drag element (owned by the fixture) |
| 46 | + */ |
| 47 | + DragElement *createDragElement(int x, int y, int width, int height, |
| 48 | + Color color, const std::string &name) { |
| 49 | + auto element = std::make_unique<DragElement>( |
| 50 | + SDL_Rect{x, y, width, height}, color, name); |
| 51 | + |
| 52 | + auto *rawPtr = element.get(); |
| 53 | + testElements_.push_back(std::move(element)); |
| 54 | + return rawPtr; |
| 55 | + } |
102 | 56 |
|
103 | | - std::cout << "Moving to start position: " << startPos.x << ", " << startPos.y << std::endl; |
| 57 | + /** |
| 58 | + * @brief Runs the event loop for a specified duration |
| 59 | + * @param duration How long to process events |
| 60 | + */ |
| 61 | + void processEventsFor(std::chrono::milliseconds duration) { |
| 62 | + auto startTime = std::chrono::steady_clock::now(); |
| 63 | + bool running = true; |
| 64 | + |
| 65 | + while (running && |
| 66 | + (std::chrono::steady_clock::now() - startTime < duration)) { |
| 67 | + // Process pending SDL events |
| 68 | + context_->handleEvents(running); |
| 69 | + |
| 70 | + // Render current state |
| 71 | + context_->renderFrame([this](SDL_Renderer *renderer) { |
| 72 | + renderTestElements(renderer); |
| 73 | + }); |
| 74 | + |
| 75 | + // Limit frame rate |
| 76 | + SDL_Delay(static_cast<Uint32>(config_->frameDelay.count())); |
| 77 | + } |
| 78 | + } |
104 | 79 |
|
105 | | - // Move to start position |
106 | | - Robot::Mouse::Move(startPos); |
107 | | - Robot::delay(static_cast<unsigned int>(config_->actionDelay.count())); |
| 80 | + /** |
| 81 | + * @brief Converts window coordinates to screen coordinates |
| 82 | + */ |
| 83 | + Robot::Point windowToScreen(int x, int y) const { |
| 84 | + int windowX, windowY; |
| 85 | + SDL_GetWindowPosition(context_->getWindow(), &windowX, &windowY); |
| 86 | + return {x + windowX, y + windowY}; |
| 87 | + } |
108 | 88 |
|
109 | | - // Press mouse button |
110 | | - Robot::Mouse::ToggleButton(true, Robot::MouseButton::LEFT_BUTTON); |
111 | | - Robot::delay(static_cast<unsigned int>(config_->actionDelay.count())); |
| 89 | + /** |
| 90 | + * @brief Performs a mouse drag operation |
| 91 | + * @param startPoint Starting point in window coordinates |
| 92 | + * @param endPoint Ending point in window coordinates |
| 93 | + */ |
| 94 | + void performMouseDrag(const SDL_Point &startPoint, const SDL_Point &endPoint) { |
| 95 | + // Convert to screen coordinates |
| 96 | + Robot::Point startPos = windowToScreen(startPoint.x, startPoint.y); |
| 97 | + Robot::Point endPos = windowToScreen(endPoint.x, endPoint.y); |
112 | 98 |
|
113 | | - // Move to end position |
114 | | - std::cout << "Moving to end position: " << endPos.x << ", " << endPos.y << std::endl; |
115 | | - Robot::Mouse::Move(endPos); |
116 | | - Robot::delay(static_cast<unsigned int>(config_->actionDelay.count())); |
| 99 | + std::cout << "Moving to start position: " << startPos.x << ", " << startPos.y << std::endl; |
117 | 100 |
|
118 | | - // Release mouse button |
119 | | - Robot::Mouse::ToggleButton(false, Robot::MouseButton::LEFT_BUTTON); |
| 101 | + // Move to start position |
| 102 | + Robot::Mouse::MoveSmooth(startPos); |
120 | 103 |
|
121 | | - // Process events to ensure drag is applied |
122 | | - processEventsFor(std::chrono::milliseconds(500)); |
123 | | - } |
| 104 | + // Perform the drag operation using the library's DragSmooth method |
| 105 | + std::cout << "Performing smooth drag to end position: " << endPos.x << ", " << endPos.y << std::endl; |
| 106 | + Robot::Mouse::DragSmooth(endPos); |
124 | 107 |
|
125 | | - /** |
126 | | - * @brief Renders all test elements |
127 | | - */ |
128 | | - void renderTestElements(SDL_Renderer* renderer) { |
129 | | - for (const auto& element : testElements_) { |
130 | | - element->draw(renderer); |
| 108 | + // Process events to ensure drag is applied |
| 109 | + processEventsFor(std::chrono::milliseconds(1500)); |
131 | 110 | } |
132 | 111 |
|
133 | | - // Draw mouse cursor position |
134 | | - drawMousePosition(renderer); |
135 | | - } |
| 112 | + /** |
| 113 | + * @brief Renders all test elements |
| 114 | + */ |
| 115 | + void renderTestElements(SDL_Renderer *renderer) { |
| 116 | + for (const auto &element: testElements_) { |
| 117 | + element->draw(renderer); |
| 118 | + } |
136 | 119 |
|
137 | | - /** |
138 | | - * @brief Draws the current mouse position on screen |
139 | | - */ |
140 | | - void drawMousePosition(SDL_Renderer* renderer) { |
141 | | - // Get window position |
142 | | - int windowX, windowY; |
143 | | - SDL_GetWindowPosition(context_->getWindow(), &windowX, &windowY); |
144 | | - |
145 | | - // Get global mouse position |
146 | | - Robot::Point globalMousePos = Robot::Mouse::GetPosition(); |
147 | | - |
148 | | - // Calculate local mouse position (relative to window) |
149 | | - int localMouseX = globalMousePos.x - windowX; |
150 | | - int localMouseY = globalMousePos.y - windowY; |
151 | | - |
152 | | - // Draw mouse position indicator - a red crosshair |
153 | | - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); |
154 | | - SDL_RenderDrawLine(renderer, localMouseX - 10, localMouseY, localMouseX + 10, localMouseY); |
155 | | - SDL_RenderDrawLine(renderer, localMouseX, localMouseY - 10, localMouseX, localMouseY + 10); |
156 | | - } |
| 120 | + // Draw mouse cursor position |
| 121 | + drawMousePosition(renderer); |
| 122 | + } |
157 | 123 |
|
158 | | - std::unique_ptr<TestConfig> config_; |
159 | | - std::unique_ptr<TestContext> context_; |
160 | | - std::vector<std::unique_ptr<TestElement>> testElements_; |
161 | | -}; |
| 124 | + /** |
| 125 | + * @brief Draws the current mouse position on screen |
| 126 | + */ |
| 127 | + void drawMousePosition(SDL_Renderer *renderer) { |
| 128 | + // Get window position |
| 129 | + int windowX, windowY; |
| 130 | + SDL_GetWindowPosition(context_->getWindow(), &windowX, &windowY); |
| 131 | + |
| 132 | + // Get global mouse position |
| 133 | + Robot::Point globalMousePos = Robot::Mouse::GetPosition(); |
| 134 | + |
| 135 | + // Calculate local mouse position (relative to window) |
| 136 | + int localMouseX = globalMousePos.x - windowX; |
| 137 | + int localMouseY = globalMousePos.y - windowY; |
| 138 | + |
| 139 | + // Draw mouse position indicator - a red crosshair |
| 140 | + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); |
| 141 | + SDL_RenderDrawLine(renderer, localMouseX - 10, localMouseY, localMouseX + 10, localMouseY); |
| 142 | + SDL_RenderDrawLine(renderer, localMouseX, localMouseY - 10, localMouseX, localMouseY + 10); |
| 143 | + } |
162 | 144 |
|
| 145 | + std::unique_ptr<TestConfig> config_; |
| 146 | + std::unique_ptr<TestContext> context_; |
| 147 | + std::vector<std::unique_ptr<TestElement> > testElements_; |
| 148 | + }; |
163 | 149 | } // namespace RobotTest |
0 commit comments