44#include < scratchcpp/rect.h>
55#include < scratchcpp/virtualmachine.h>
66#include < scratchcpp/script.h>
7- #include < scratch/monitor_p.h>
87#include < monitorhandlermock.h>
98#include < randomgeneratormock.h>
109
@@ -20,7 +19,6 @@ using ::testing::_;
2019static const int PADDING = 5 ;
2120static const int SCREEN_WIDTH = 400 ;
2221static const int SCREEN_HEIGHT = 300 ;
23- static const int SCREEN_EDGE_BUFFER = 40 ;
2422
2523TEST (MonitorTest, Constructors)
2624{
@@ -272,36 +270,103 @@ TEST(MonitorTest, Discrete)
272270 ASSERT_FALSE (monitor.discrete ());
273271}
274272
275- TEST (MonitorTest, GetInitialPosition )
273+ TEST (MonitorTest, AutoPosition_LineUpTopLeft )
276274{
277- std::vector<std::shared_ptr<Monitor>> monitors;
275+ // https://github.com/scratchfoundation/scratch-gui/blob/875bee35f178411b9149ab766d17b5fb88ddd749/test/unit/reducers/monitor-layout-reducer.test.js#L216-L238
278276 const int width = 100 ;
279277 const int height = 200 ;
280278
281- auto monitor1 = std::make_shared<Monitor>(" " , " " );
282- monitor1->setWidth (PADDING);
283- monitor1->setHeight (height);
284- monitor1->setX (100 );
285- monitor1->setY (0 );
286- monitors.push_back (monitor1);
287-
288- auto monitor2 = std::make_shared<Monitor>(" " , " " );
289- monitor2->setWidth (width);
290- monitor2->setHeight (height + PADDING - 100 );
291- monitor2->setX (0 );
292- monitor2->setY (100 );
293- monitors.push_back (monitor2);
294-
295- RandomGeneratorMock rng;
296- MonitorPrivate::rng = &rng;
297-
298- EXPECT_CALL (rng, randintDouble (0 , SCREEN_WIDTH / 2.0 )).WillOnce (Return (SCREEN_WIDTH / 4.5 ));
299- EXPECT_CALL (rng, randintDouble (0 , SCREEN_HEIGHT - SCREEN_EDGE_BUFFER)).WillOnce (Return (SCREEN_HEIGHT - SCREEN_EDGE_BUFFER * 2.3 ));
300- Rect rect = Monitor::getInitialPosition (monitors, width, height);
301- ASSERT_EQ (rect.left (), std::ceil (SCREEN_WIDTH / 4.5 ));
302- ASSERT_EQ (rect.top (), std::ceil (SCREEN_HEIGHT - SCREEN_EDGE_BUFFER * 2.3 ));
303- ASSERT_EQ (rect.width (), width);
304- ASSERT_EQ (rect.height (), height);
305-
306- MonitorPrivate::rng = nullptr ;
279+ auto monitor = std::make_shared<Monitor>(" " , " " );
280+ monitor->setWidth (width);
281+ monitor->setHeight (height);
282+
283+ // Add monitors to right and bottom, but there is a space in the top left
284+ std::vector<std::shared_ptr<Monitor>> monitors = { std::make_shared<Monitor>(" " , " " ), std::make_shared<Monitor>(" " , " " ), monitor };
285+
286+ monitors[0 ]->setX (width + 2 * PADDING);
287+ monitors[0 ]->setY (0 );
288+ monitors[0 ]->setWidth (100 );
289+ monitors[0 ]->setHeight (height);
290+
291+ monitors[1 ]->setX (0 );
292+ monitors[1 ]->setY (height + 2 * PADDING);
293+ monitors[1 ]->setWidth (width);
294+ monitors[1 ]->setHeight (100 );
295+
296+ // Check that the added monitor appears in the space
297+ monitor->autoPosition (monitors);
298+ ASSERT_EQ (monitor->width (), width);
299+ ASSERT_EQ (monitor->height (), height);
300+ ASSERT_EQ (monitor->x (), PADDING);
301+ ASSERT_EQ (monitor->y (), PADDING);
302+ ASSERT_FALSE (monitor->needsAutoPosition ());
303+ }
304+
305+ TEST (MonitorTest, AutoPosition_LineUpLeft)
306+ {
307+ // https://github.com/scratchfoundation/scratch-gui/blob/875bee35f178411b9149ab766d17b5fb88ddd749/test/unit/reducers/monitor-layout-reducer.test.js#L261-L270
308+ auto monitor = std::make_shared<Monitor>(" " , " " );
309+ monitor->setWidth (20 );
310+ monitor->setHeight (20 );
311+
312+ const int monitor1EndY = 60 ;
313+
314+ // Add a monitor that takes up the upper left corner
315+ std::vector<std::shared_ptr<Monitor>> monitors = { std::make_shared<Monitor>(" " , " " ), monitor };
316+ monitors[0 ]->setX (0 );
317+ monitors[0 ]->setY (0 );
318+ monitors[0 ]->setWidth (100 );
319+ monitors[0 ]->setHeight (monitor1EndY);
320+
321+ // Check that added monitor is under it and lines up left
322+ monitor->autoPosition (monitors);
323+ ASSERT_EQ (monitor->width (), 20 );
324+ ASSERT_EQ (monitor->height (), 20 );
325+ ASSERT_GE (monitor->y (), monitor1EndY + PADDING);
326+ ASSERT_FALSE (monitor->needsAutoPosition ());
327+ }
328+
329+ TEST (MonitorTest, LineUpTop)
330+ {
331+ // https://github.com/scratchfoundation/scratch-gui/blob/875bee35f178411b9149ab766d17b5fb88ddd749/test/unit/reducers/monitor-layout-reducer.test.js#L272-L285
332+ auto monitor = std::make_shared<Monitor>(" " , " " );
333+ monitor->setWidth (20 );
334+ monitor->setHeight (20 );
335+
336+ const int monitor1EndX = 100 ;
337+
338+ // Add a monitor that takes up the whole left side
339+ std::vector<std::shared_ptr<Monitor>> monitors = { std::make_shared<Monitor>(" " , " " ), monitor };
340+ monitors[0 ]->setX (0 );
341+ monitors[0 ]->setY (0 );
342+ monitors[0 ]->setWidth (monitor1EndX);
343+ monitors[0 ]->setHeight (SCREEN_HEIGHT);
344+
345+ // Check that added monitor is to the right of it and lines up top
346+ monitor->autoPosition (monitors);
347+ ASSERT_EQ (monitor->width (), 20 );
348+ ASSERT_EQ (monitor->height (), 20 );
349+ ASSERT_EQ (monitor->y (), PADDING);
350+ ASSERT_GE (monitor->x (), monitor1EndX + PADDING);
351+ }
352+
353+ TEST (MonitorTest, AutoPosition_NoRoom)
354+ {
355+ // https://github.com/scratchfoundation/scratch-gui/blob/875bee35f178411b9149ab766d17b5fb88ddd749/test/unit/reducers/monitor-layout-reducer.test.js#L287-L302
356+ auto monitor = std::make_shared<Monitor>(" " , " " );
357+ monitor->setWidth (7 );
358+ monitor->setHeight (8 );
359+
360+ // Add a monitor that takes up the whole screen
361+ std::vector<std::shared_ptr<Monitor>> monitors = { std::make_shared<Monitor>(" " , " " ), monitor };
362+ monitors[0 ]->setX (0 );
363+ monitors[0 ]->setY (0 );
364+ monitors[0 ]->setWidth (SCREEN_WIDTH);
365+ monitors[0 ]->setHeight (SCREEN_HEIGHT);
366+
367+ // Check that added monitor exists somewhere (we don't care where)
368+ monitor->autoPosition (monitors);
369+ ASSERT_EQ (monitor->width (), 7 );
370+ ASSERT_EQ (monitor->height (), 8 );
371+ ASSERT_FALSE (monitor->needsAutoPosition ());
307372}
0 commit comments