@@ -686,6 +686,95 @@ TEST(SpriteTest, BoundingRect)
686686 ScratchConfiguration::removeImageFormat (" test" );
687687}
688688
689+ TEST (SpriteTest, KeepInFence)
690+ {
691+ auto imageFormatFactory = std::make_shared<ImageFormatFactoryMock>();
692+ auto imageFormat = std::make_shared<ImageFormatMock>();
693+
694+ ScratchConfiguration::registerImageFormat (" test" , imageFormatFactory);
695+ EXPECT_CALL (*imageFormatFactory, createInstance ()).WillOnce (Return (imageFormat));
696+ EXPECT_CALL (*imageFormat, width ()).WillOnce (Return (0 ));
697+ EXPECT_CALL (*imageFormat, height ()).WillOnce (Return (0 ));
698+ auto costume = std::make_shared<Costume>(" costume1" , " a" , " test" );
699+
700+ Sprite sprite;
701+ sprite.addCostume (costume);
702+ sprite.setCostumeIndex (0 );
703+
704+ static char data[5 ] = " abcd" ;
705+ EXPECT_CALL (*imageFormat, setData (5 , data));
706+ EXPECT_CALL (*imageFormat, width ()).WillOnce (Return (4 ));
707+ EXPECT_CALL (*imageFormat, height ()).WillOnce (Return (3 ));
708+
709+ EXPECT_CALL (*imageFormat, colorAt (0 , 0 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 0 )));
710+ EXPECT_CALL (*imageFormat, colorAt (1 , 0 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 0 )));
711+ EXPECT_CALL (*imageFormat, colorAt (2 , 0 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 255 )));
712+ EXPECT_CALL (*imageFormat, colorAt (3 , 0 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 0 )));
713+
714+ EXPECT_CALL (*imageFormat, colorAt (0 , 1 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 0 )));
715+ EXPECT_CALL (*imageFormat, colorAt (1 , 1 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 255 )));
716+ EXPECT_CALL (*imageFormat, colorAt (2 , 1 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 0 )));
717+ EXPECT_CALL (*imageFormat, colorAt (3 , 1 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 255 )));
718+
719+ EXPECT_CALL (*imageFormat, colorAt (0 , 2 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 255 )));
720+ EXPECT_CALL (*imageFormat, colorAt (1 , 2 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 0 )));
721+ EXPECT_CALL (*imageFormat, colorAt (2 , 2 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 0 )));
722+ EXPECT_CALL (*imageFormat, colorAt (3 , 2 , 1 )).WillOnce (Return (rgba (0 , 0 , 0 , 0 )));
723+ costume->setData (5 , data);
724+
725+ double fencedX = -1 , fencedY = -1 ;
726+ EngineMock engine;
727+ sprite.setEngine (&engine);
728+
729+ EXPECT_CALL (engine, requestRedraw ());
730+ sprite.setDirection (45 );
731+ EXPECT_CALL (*imageFormat, width ()).WillOnce (Return (4 ));
732+ EXPECT_CALL (*imageFormat, height ()).WillOnce (Return (3 ));
733+ EXPECT_CALL (engine, stageWidth ()).WillOnce (Return (480 ));
734+ EXPECT_CALL (engine, stageHeight ()).WillOnce (Return (360 ));
735+ sprite.keepInFence (0 , 0 , &fencedX, &fencedY);
736+ ASSERT_EQ (fencedX, 0 );
737+ ASSERT_EQ (fencedY, 0 );
738+
739+ EXPECT_CALL (engine, requestRedraw ()).Times (2 );
740+ EXPECT_CALL (engine, spriteFencingEnabled ()).Times (2 ).WillRepeatedly (Return (false ));
741+ sprite.setX (100 );
742+ sprite.setY (60 );
743+ EXPECT_CALL (*imageFormat, width ()).WillOnce (Return (4 ));
744+ EXPECT_CALL (*imageFormat, height ()).WillOnce (Return (3 ));
745+ EXPECT_CALL (engine, stageWidth ()).WillOnce (Return (480 ));
746+ EXPECT_CALL (engine, stageHeight ()).WillOnce (Return (360 ));
747+ sprite.keepInFence (240 , 180 , &fencedX, &fencedY);
748+ ASSERT_EQ (std::round (fencedX * 100 ) / 100 , 238.94 );
749+ ASSERT_EQ (std::round (fencedY * 100 ) / 100 , 179.65 );
750+
751+ EXPECT_CALL (*imageFormat, width ()).WillOnce (Return (4 ));
752+ EXPECT_CALL (*imageFormat, height ()).WillOnce (Return (3 ));
753+ EXPECT_CALL (engine, stageWidth ()).WillOnce (Return (480 ));
754+ EXPECT_CALL (engine, stageHeight ()).WillOnce (Return (360 ));
755+ sprite.keepInFence (240 , -180 , &fencedX, &fencedY);
756+ ASSERT_EQ (std::round (fencedX * 100 ) / 100 , 238.94 );
757+ ASSERT_EQ (std::round (fencedY * 100 ) / 100 , -178.94 );
758+
759+ EXPECT_CALL (*imageFormat, width ()).WillOnce (Return (4 ));
760+ EXPECT_CALL (*imageFormat, height ()).WillOnce (Return (3 ));
761+ EXPECT_CALL (engine, stageWidth ()).WillOnce (Return (480 ));
762+ EXPECT_CALL (engine, stageHeight ()).WillOnce (Return (360 ));
763+ sprite.keepInFence (-240 , -180 , &fencedX, &fencedY);
764+ ASSERT_EQ (std::round (fencedX * 100 ) / 100 , -238.23 );
765+ ASSERT_EQ (std::round (fencedY * 100 ) / 100 , -178.94 );
766+
767+ EXPECT_CALL (*imageFormat, width ()).WillOnce (Return (4 ));
768+ EXPECT_CALL (*imageFormat, height ()).WillOnce (Return (3 ));
769+ EXPECT_CALL (engine, stageWidth ()).WillOnce (Return (480 ));
770+ EXPECT_CALL (engine, stageHeight ()).WillOnce (Return (360 ));
771+ sprite.keepInFence (-240 , 180 , &fencedX, &fencedY);
772+ ASSERT_EQ (std::round (fencedX * 100 ) / 100 , -238.23 );
773+ ASSERT_EQ (std::round (fencedY * 100 ) / 100 , 179.65 );
774+
775+ ScratchConfiguration::removeImageFormat (" test" );
776+ }
777+
689778TEST (SpriteTest, GraphicsEffects)
690779{
691780 auto c1 = std::make_shared<Costume>(" " , " " , " " );
0 commit comments