|
| 1 | +#include <QtTest/QSignalSpy> |
| 2 | +#include <renderedtarget.h> |
| 3 | +#include <stagemodel.h> |
| 4 | +#include <spritemodel.h> |
| 5 | +#include <scratchcpp/stage.h> |
| 6 | +#include <scratchcpp/sprite.h> |
| 7 | +#include <scratchcpp/costume.h> |
| 8 | +#include <enginemock.h> |
| 9 | + |
| 10 | +#include "../common.h" |
| 11 | + |
| 12 | +using namespace scratchcppgui; |
| 13 | +using namespace libscratchcpp; |
| 14 | + |
| 15 | +using ::testing::Return; |
| 16 | + |
| 17 | +TEST(RenderedTargetTest, Constructors) |
| 18 | +{ |
| 19 | + RenderedTarget target1; |
| 20 | + RenderedTarget target2(&target1); |
| 21 | + ASSERT_EQ(target2.parent(), &target1); |
| 22 | + ASSERT_EQ(target2.parentItem(), &target1); |
| 23 | +} |
| 24 | + |
| 25 | +TEST(RenderedTargetTest, LoadAndUpdateProperties) |
| 26 | +{ |
| 27 | + RenderedTarget target; |
| 28 | + QSignalSpy mirrorHorizontallySpy(&target, &RenderedTarget::mirrorHorizontallyChanged); |
| 29 | + |
| 30 | + // Stage |
| 31 | + Stage stage; |
| 32 | + StageModel stageModel; |
| 33 | + stage.setInterface(&stageModel); |
| 34 | + target.setStageModel(&stageModel); |
| 35 | + Costume costume("", "", ""); |
| 36 | + costume.setRotationCenterX(-23); |
| 37 | + costume.setRotationCenterY(72); |
| 38 | + EngineMock engine; |
| 39 | + target.loadCostume(&costume); |
| 40 | + target.setEngine(&engine); |
| 41 | + target.setCostumeWidth(102.3); |
| 42 | + target.setCostumeHeight(80.7); |
| 43 | + |
| 44 | + target.setWidth(14.3); |
| 45 | + target.setHeight(5.8); |
| 46 | + target.setX(64.5); |
| 47 | + target.setY(-43.7); |
| 48 | + target.setZ(2.5); |
| 49 | + target.setRotation(-78.05); |
| 50 | + target.setTransformOriginPoint(QPointF(3.4, 9.7)); |
| 51 | + |
| 52 | + EXPECT_CALL(engine, stageWidth()).WillOnce(Return(544)); |
| 53 | + EXPECT_CALL(engine, stageHeight()).WillOnce(Return(249)); |
| 54 | + target.loadProperties(); |
| 55 | + ASSERT_EQ(target.width(), 14.3); |
| 56 | + ASSERT_EQ(target.height(), 5.8); |
| 57 | + ASSERT_EQ(target.x(), 64.5); |
| 58 | + ASSERT_EQ(target.y(), -43.7); |
| 59 | + ASSERT_EQ(target.z(), 2.5); |
| 60 | + ASSERT_EQ(target.rotation(), -78.05); |
| 61 | + ASSERT_EQ(target.transformOriginPoint(), QPointF(3.4, 9.7)); |
| 62 | + |
| 63 | + target.updateProperties(); |
| 64 | + ASSERT_EQ(target.width(), 102.3); |
| 65 | + ASSERT_EQ(target.height(), 80.7); |
| 66 | + ASSERT_EQ(target.x(), 283.5); |
| 67 | + ASSERT_EQ(target.y(), 88.5); |
| 68 | + ASSERT_EQ(target.z(), 0); |
| 69 | + ASSERT_EQ(target.rotation(), 0); |
| 70 | + ASSERT_EQ(target.transformOriginPoint(), QPointF(-11.5, 36)); |
| 71 | + |
| 72 | + target.setStageModel(nullptr); |
| 73 | + ASSERT_TRUE(mirrorHorizontallySpy.empty()); |
| 74 | + |
| 75 | + // Sprite |
| 76 | + Sprite sprite; |
| 77 | + sprite.setVisible(true); |
| 78 | + sprite.setRotationStyle(Sprite::RotationStyle::AllAround); |
| 79 | + sprite.setDirection(-67.16); |
| 80 | + sprite.setSize(143.98); |
| 81 | + sprite.setX(-67.94); |
| 82 | + sprite.setY(121.76); |
| 83 | + sprite.setLayerOrder(3); |
| 84 | + SpriteModel spriteModel; |
| 85 | + sprite.setInterface(&spriteModel); |
| 86 | + target.setSpriteModel(&spriteModel); |
| 87 | + |
| 88 | + target.setWidth(14.3); |
| 89 | + target.setHeight(5.8); |
| 90 | + target.setX(64.5); |
| 91 | + target.setY(-43.7); |
| 92 | + target.setZ(2.5); |
| 93 | + target.setRotation(-78.05); |
| 94 | + target.setTransformOriginPoint(QPointF(3.4, 9.7)); |
| 95 | + |
| 96 | + EXPECT_CALL(engine, stageWidth()).WillOnce(Return(544)); |
| 97 | + EXPECT_CALL(engine, stageHeight()).WillOnce(Return(249)); |
| 98 | + target.loadProperties(); |
| 99 | + ASSERT_EQ(target.width(), 14.3); |
| 100 | + ASSERT_EQ(target.height(), 5.8); |
| 101 | + ASSERT_EQ(target.x(), 64.5); |
| 102 | + ASSERT_EQ(target.y(), -43.7); |
| 103 | + ASSERT_EQ(target.z(), 2.5); |
| 104 | + ASSERT_EQ(target.rotation(), -78.05); |
| 105 | + ASSERT_EQ(target.transformOriginPoint(), QPointF(3.4, 9.7)); |
| 106 | + |
| 107 | + target.updateProperties(); |
| 108 | + ASSERT_EQ(target.width(), 102.3); |
| 109 | + ASSERT_EQ(target.height(), 80.7); |
| 110 | + ASSERT_EQ(std::round(target.x() * 100) / 100, 220.62); |
| 111 | + ASSERT_EQ(std::round(target.y() * 100) / 100, -49.09); |
| 112 | + ASSERT_EQ(target.z(), 3); |
| 113 | + ASSERT_EQ(target.rotation(), -157.16); |
| 114 | + ASSERT_EQ(target.transformOriginPoint(), QPointF(-16.5577, 51.8328)); |
| 115 | + ASSERT_TRUE(mirrorHorizontallySpy.empty()); |
| 116 | + |
| 117 | + EXPECT_CALL(engine, stageWidth()).WillOnce(Return(544)); |
| 118 | + EXPECT_CALL(engine, stageHeight()).WillOnce(Return(249)); |
| 119 | + sprite.setRotationStyle(Sprite::RotationStyle::LeftRight); |
| 120 | + target.loadProperties(); |
| 121 | + ASSERT_EQ(target.mirrorHorizontally(), false); |
| 122 | + ASSERT_EQ(target.rotation(), -157.16); |
| 123 | + ASSERT_TRUE(mirrorHorizontallySpy.empty()); |
| 124 | + |
| 125 | + target.updateProperties(); |
| 126 | + ASSERT_EQ(target.mirrorHorizontally(), true); |
| 127 | + ASSERT_EQ(target.rotation(), 0); |
| 128 | + ASSERT_EQ(mirrorHorizontallySpy.count(), 1); |
| 129 | + |
| 130 | + EXPECT_CALL(engine, stageWidth()).WillOnce(Return(544)); |
| 131 | + EXPECT_CALL(engine, stageHeight()).WillOnce(Return(249)); |
| 132 | + sprite.setDirection(134.89); |
| 133 | + target.loadProperties(); |
| 134 | + ASSERT_EQ(target.mirrorHorizontally(), true); |
| 135 | + ASSERT_EQ(target.rotation(), 0); |
| 136 | + ASSERT_EQ(mirrorHorizontallySpy.count(), 1); |
| 137 | + |
| 138 | + target.updateProperties(); |
| 139 | + ASSERT_EQ(target.mirrorHorizontally(), false); |
| 140 | + ASSERT_EQ(target.rotation(), 0); |
| 141 | + ASSERT_EQ(mirrorHorizontallySpy.count(), 2); |
| 142 | + |
| 143 | + EXPECT_CALL(engine, stageWidth()).WillOnce(Return(544)); |
| 144 | + EXPECT_CALL(engine, stageHeight()).WillOnce(Return(249)); |
| 145 | + sprite.setRotationStyle(Sprite::RotationStyle::DoNotRotate); |
| 146 | + target.loadProperties(); |
| 147 | + ASSERT_EQ(target.mirrorHorizontally(), false); |
| 148 | + ASSERT_EQ(target.rotation(), 0); |
| 149 | + ASSERT_EQ(mirrorHorizontallySpy.count(), 2); |
| 150 | + |
| 151 | + target.updateProperties(); |
| 152 | + ASSERT_EQ(target.mirrorHorizontally(), false); |
| 153 | + ASSERT_EQ(target.rotation(), 0); |
| 154 | + ASSERT_EQ(mirrorHorizontallySpy.count(), 2); |
| 155 | +} |
| 156 | + |
| 157 | +TEST(RenderedTargetTest, LoadJpegCostume) |
| 158 | +{ |
| 159 | + std::string str = readFileStr("image.jpg"); |
| 160 | + Costume costume("", "", ""); |
| 161 | + costume.setData(str.size(), static_cast<void *>(const_cast<char *>(str.c_str()))); |
| 162 | + costume.setBitmapResolution(3); |
| 163 | + costume.setId("abc"); |
| 164 | + |
| 165 | + RenderedTarget target; |
| 166 | + ASSERT_EQ(target.costumeWidth(), 0); |
| 167 | + ASSERT_EQ(target.costumeHeight(), 0); |
| 168 | + |
| 169 | + target.loadCostume(&costume); |
| 170 | + ASSERT_EQ(target.costumeWidth(), 4 / 3.0); |
| 171 | + ASSERT_EQ(target.costumeHeight(), 2); |
| 172 | + ASSERT_FALSE(target.bitmapBuffer()->isOpen()); |
| 173 | + target.bitmapBuffer()->open(QBuffer::ReadOnly); |
| 174 | + ASSERT_EQ(target.bitmapBuffer()->readAll().toStdString(), str); |
| 175 | + ASSERT_EQ(target.bitmapUniqueKey().toStdString(), costume.id()); |
| 176 | + ASSERT_EQ(target.svgBitmap(), nullptr); |
| 177 | +} |
| 178 | + |
| 179 | +TEST(RenderedTargetTest, LoadPngCostume) |
| 180 | +{ |
| 181 | + std::string str = readFileStr("image.png"); |
| 182 | + Costume costume("", "", ""); |
| 183 | + costume.setData(str.size(), static_cast<void *>(const_cast<char *>(str.c_str()))); |
| 184 | + costume.setBitmapResolution(3); |
| 185 | + costume.setId("abc"); |
| 186 | + |
| 187 | + RenderedTarget target; |
| 188 | + ASSERT_EQ(target.costumeWidth(), 0); |
| 189 | + ASSERT_EQ(target.costumeHeight(), 0); |
| 190 | + |
| 191 | + target.loadCostume(&costume); |
| 192 | + ASSERT_EQ(target.costumeWidth(), 4 / 3.0); |
| 193 | + ASSERT_EQ(target.costumeHeight(), 2); |
| 194 | + ASSERT_FALSE(target.bitmapBuffer()->isOpen()); |
| 195 | + target.bitmapBuffer()->open(QBuffer::ReadOnly); |
| 196 | + ASSERT_EQ(target.bitmapBuffer()->readAll().toStdString(), str); |
| 197 | + ASSERT_EQ(target.bitmapUniqueKey().toStdString(), costume.id()); |
| 198 | + ASSERT_EQ(target.svgBitmap(), nullptr); |
| 199 | +} |
| 200 | + |
| 201 | +TEST(RenderedTargetTest, Engine) |
| 202 | +{ |
| 203 | + RenderedTarget target; |
| 204 | + ASSERT_EQ(target.engine(), nullptr); |
| 205 | + |
| 206 | + EngineMock engine; |
| 207 | + target.setEngine(&engine); |
| 208 | + ASSERT_EQ(target.engine(), &engine); |
| 209 | +} |
| 210 | + |
| 211 | +TEST(RenderedTargetTest, StageModel) |
| 212 | +{ |
| 213 | + RenderedTarget target; |
| 214 | + ASSERT_EQ(target.stageModel(), nullptr); |
| 215 | + |
| 216 | + StageModel model; |
| 217 | + target.setStageModel(&model); |
| 218 | + ASSERT_EQ(target.stageModel(), &model); |
| 219 | +} |
| 220 | + |
| 221 | +TEST(RenderedTargetTest, SpriteModel) |
| 222 | +{ |
| 223 | + RenderedTarget target; |
| 224 | + ASSERT_EQ(target.spriteModel(), nullptr); |
| 225 | + |
| 226 | + SpriteModel model; |
| 227 | + target.setSpriteModel(&model); |
| 228 | + ASSERT_EQ(target.spriteModel(), &model); |
| 229 | +} |
| 230 | + |
| 231 | +TEST(RenderedTargetTest, ScratchTarget) |
| 232 | +{ |
| 233 | + RenderedTarget target; |
| 234 | + ASSERT_EQ(target.scratchTarget(), nullptr); |
| 235 | + |
| 236 | + StageModel stageModel; |
| 237 | + Stage stage; |
| 238 | + stageModel.init(&stage); |
| 239 | + target.setStageModel(&stageModel); |
| 240 | + ASSERT_EQ(target.scratchTarget(), &stage); |
| 241 | + |
| 242 | + target.setStageModel(nullptr); |
| 243 | + SpriteModel spriteModel; |
| 244 | + Sprite sprite; |
| 245 | + spriteModel.init(&sprite); |
| 246 | + target.setSpriteModel(&spriteModel); |
| 247 | + ASSERT_EQ(target.scratchTarget(), &sprite); |
| 248 | +} |
| 249 | + |
| 250 | +TEST(RenderedTargetTest, CostumeWidth) |
| 251 | +{ |
| 252 | + RenderedTarget target; |
| 253 | + ASSERT_EQ(target.costumeWidth(), 0); |
| 254 | + |
| 255 | + target.setCostumeWidth(64.15); |
| 256 | + ASSERT_EQ(target.costumeWidth(), 64.15); |
| 257 | +} |
| 258 | + |
| 259 | +TEST(RenderedTargetTest, CostumeHeight) |
| 260 | +{ |
| 261 | + RenderedTarget target; |
| 262 | + ASSERT_EQ(target.costumeHeight(), 0); |
| 263 | + |
| 264 | + target.setCostumeHeight(46.48); |
| 265 | + ASSERT_EQ(target.costumeHeight(), 46.48); |
| 266 | +} |
0 commit comments