Skip to content

Commit 5871fa1

Browse files
committed
refactor: leaner mouse drag test implementation
1 parent 318ba51 commit 5871fa1

4 files changed

Lines changed: 138 additions & 144 deletions

File tree

tests/sdl/MouseTests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ TEST_F(MouseDragTest, CanDragElementToNewPosition) {
2121
"Drag Test Element"
2222
);
2323

24+
// Process events for a short while to ensure window is fully rendered
25+
processEventsFor(std::chrono::milliseconds(1000));
26+
2427
// Initial position of the element
2528
const SDL_Rect initialRect = dragElement->getRect();
2629

@@ -67,9 +70,16 @@ TEST_F(MouseDragTest, CanDragElementToNewPosition) {
6770
std::cout << " Drag end point: ("
6871
<< endPoint.x << ", " << endPoint.y << ")" << std::endl;
6972

73+
// Add extra render cycle before starting the operation
74+
processEventsFor(std::chrono::milliseconds(1000));
75+
7076
// Perform the drag operation
7177
performMouseDrag(startPoint, endPoint);
7278

79+
// Add additional delay after drag to ensure events are processed
80+
std::cout << "Waiting for drag operation to complete..." << std::endl;
81+
processEventsFor(std::chrono::milliseconds(2000));
82+
7383
// Get the element's position after the drag
7484
const SDL_Rect finalRect = dragElement->getRect();
7585
std::cout << " Final element position: ("

tests/sdl/RobotSDLTestFixture.h

Lines changed: 116 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <gtest/gtest.h>
4-
#include <memory>
54
#include <functional>
65
#include <chrono>
76
#include <thread>
@@ -11,153 +10,140 @@
1110
#include "TestConfig.h"
1211
#include "TestElements.h"
1312
#include "../../src/Mouse.h"
14-
#include "../../src/Utils.h"
1513

1614
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-
4615
/**
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.
4920
*/
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+
}
7334

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();
7838

79-
// Limit frame rate
80-
SDL_Delay(static_cast<Uint32>(config_->frameDelay.count()));
39+
// TestContext destructor will handle SDL cleanup
40+
context_.reset();
8141
}
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-
}
9242

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+
}
10256

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+
}
10479

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+
}
10888

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);
11298

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;
117100

118-
// Release mouse button
119-
Robot::Mouse::ToggleButton(false, Robot::MouseButton::LEFT_BUTTON);
101+
// Move to start position
102+
Robot::Mouse::MoveSmooth(startPos);
120103

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);
124107

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));
131110
}
132111

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+
}
136119

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+
}
157123

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+
}
162144

145+
std::unique_ptr<TestConfig> config_;
146+
std::unique_ptr<TestContext> context_;
147+
std::vector<std::unique_ptr<TestElement> > testElements_;
148+
};
163149
} // namespace RobotTest

tests/sdl/SDLTestMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int main(int argc, char** argv) {
3030
}
3131

3232
// Parse wait time
33-
int waitTime = 2000; // Default 2 seconds
33+
int waitTime = 2000;
3434
for (int i = 1; i < argc; ++i) {
3535
if (std::string(argv[i]) == "--wait-time" && i + 1 < argc) {
3636
waitTime = std::stoi(argv[i + 1]);

tests/sdl/TestConfig.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
#include <chrono>
44
#include <string>
5-
#include <vector>
6-
#include <optional>
75

86
namespace RobotTest {
9-
107
/**
118
* @brief Configuration for tests with default values
129
*/
@@ -18,13 +15,13 @@ namespace RobotTest {
1815

1916
// Test execution settings
2017
bool runTests = false;
21-
std::chrono::milliseconds initialWaitTime{2000};
22-
std::chrono::seconds testTimeout{30};
18+
std::chrono::milliseconds initialWaitTime{6000}; // Increased to 6 seconds
19+
std::chrono::seconds testTimeout{60}; // Increased to 60 seconds
2320

2421
// Delay settings for animation and visualization
25-
std::chrono::milliseconds frameDelay{16}; // ~60 FPS
26-
std::chrono::milliseconds setupDelay{500};
27-
std::chrono::milliseconds actionDelay{300};
22+
std::chrono::milliseconds frameDelay{16}; // ~60 FPS
23+
std::chrono::milliseconds setupDelay{1500}; // Increased to 1.5 seconds
24+
std::chrono::milliseconds actionDelay{900}; // Increased to 900ms (3x original)
2825

2926
// Window positioning
3027
int windowX = 50;
@@ -33,26 +30,27 @@ namespace RobotTest {
3330
// Mouse test settings
3431
int dragOffsetX = 100;
3532
int dragOffsetY = 50;
36-
int positionTolerance = 20; // Pixels
33+
int positionTolerance = 20; // Pixels
3734

3835
// Parse command line arguments
39-
static TestConfig fromCommandLine(int argc, char** argv) {
36+
static TestConfig fromCommandLine(int argc, char **argv) {
4037
TestConfig config;
4138

4239
for (int i = 1; i < argc; i++) {
4340
std::string arg = argv[i];
4441

4542
if (arg == "--run-tests") {
4643
config.runTests = true;
47-
}
48-
else if (arg == "--wait-time" && i + 1 < argc) {
44+
} else if (arg == "--wait-time" && i + 1 < argc) {
4945
config.initialWaitTime = std::chrono::milliseconds(std::stoi(argv[i + 1]));
5046
i++;
47+
} else if (arg == "--action-delay" && i + 1 < argc) {
48+
config.actionDelay = std::chrono::milliseconds(std::stoi(argv[i + 1]));
49+
i++;
5150
}
5251
}
5352

5453
return config;
5554
}
5655
};
57-
5856
} // namespace RobotTest

0 commit comments

Comments
 (0)