|
1 | 1 | #include <scratchcpp/monitor.h> |
2 | 2 | #include <scratchcpp/block.h> |
3 | 3 | #include <scratchcpp/sprite.h> |
| 4 | +#include <scratchcpp/rect.h> |
| 5 | +#include <scratch/monitor_p.h> |
| 6 | +#include <randomgeneratormock.h> |
4 | 7 |
|
5 | 8 | #include "../common.h" |
6 | 9 |
|
7 | 10 | using namespace libscratchcpp; |
8 | 11 |
|
| 12 | +using ::testing::Return; |
| 13 | + |
| 14 | +static const int PADDING = 5; |
| 15 | +static const int SCREEN_WIDTH = 400; |
| 16 | +static const int SCREEN_HEIGHT = 300; |
| 17 | +static const int SCREEN_EDGE_BUFFER = 40; |
| 18 | + |
9 | 19 | TEST(MonitorTest, Constructors) |
10 | 20 | { |
11 | 21 | Monitor monitor("abc", "test"); |
@@ -141,3 +151,37 @@ TEST(MonitorTest, Discrete) |
141 | 151 | monitor.setDiscrete(false); |
142 | 152 | ASSERT_FALSE(monitor.discrete()); |
143 | 153 | } |
| 154 | + |
| 155 | +TEST(MonitorTest, GetInitialPosition) |
| 156 | +{ |
| 157 | + std::vector<std::shared_ptr<Monitor>> monitors; |
| 158 | + const int width = 100; |
| 159 | + const int height = 200; |
| 160 | + |
| 161 | + auto monitor1 = std::make_shared<Monitor>("", ""); |
| 162 | + monitor1->setWidth(PADDING); |
| 163 | + monitor1->setHeight(height); |
| 164 | + monitor1->setX(100); |
| 165 | + monitor1->setY(0); |
| 166 | + monitors.push_back(monitor1); |
| 167 | + |
| 168 | + auto monitor2 = std::make_shared<Monitor>("", ""); |
| 169 | + monitor2->setWidth(width); |
| 170 | + monitor2->setHeight(height + PADDING - 100); |
| 171 | + monitor2->setX(0); |
| 172 | + monitor2->setY(100); |
| 173 | + monitors.push_back(monitor2); |
| 174 | + |
| 175 | + RandomGeneratorMock rng; |
| 176 | + MonitorPrivate::rng = &rng; |
| 177 | + |
| 178 | + EXPECT_CALL(rng, randintDouble(0, SCREEN_WIDTH / 2.0)).WillOnce(Return(SCREEN_WIDTH / 4.5)); |
| 179 | + EXPECT_CALL(rng, randintDouble(0, SCREEN_HEIGHT - SCREEN_EDGE_BUFFER)).WillOnce(Return(SCREEN_HEIGHT - SCREEN_EDGE_BUFFER * 2.3)); |
| 180 | + Rect rect = Monitor::getInitialPosition(monitors, width, height); |
| 181 | + ASSERT_EQ(rect.left(), std::ceil(SCREEN_WIDTH / 4.5)); |
| 182 | + ASSERT_EQ(rect.top(), std::ceil(SCREEN_HEIGHT - SCREEN_EDGE_BUFFER * 2.3)); |
| 183 | + ASSERT_EQ(rect.width(), width); |
| 184 | + ASSERT_EQ(rect.height(), height); |
| 185 | + |
| 186 | + MonitorPrivate::rng = nullptr; |
| 187 | +} |
0 commit comments