|
3 | 3 | #include <scratchcpp/input.h> |
4 | 4 | #include <scratchcpp/field.h> |
5 | 5 | #include <scratchcpp/target.h> |
| 6 | +#include <scratchcpp/sound.h> |
| 7 | +#include <scratch/sound_p.h> |
6 | 8 | #include <enginemock.h> |
| 9 | +#include <audioplayerfactorymock.h> |
| 10 | +#include <audioplayermock.h> |
7 | 11 |
|
8 | 12 | #include "../common.h" |
9 | 13 | #include "blocks/soundblocks.h" |
@@ -94,16 +98,181 @@ TEST_F(SoundBlocksTest, CategoryVisible) |
94 | 98 | TEST_F(SoundBlocksTest, RegisterBlocks) |
95 | 99 | { |
96 | 100 | // Blocks |
| 101 | + EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_play", &SoundBlocks::compilePlay)); |
97 | 102 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_changevolumeby", &SoundBlocks::compileChangeVolumeBy)); |
98 | 103 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_setvolumeto", &SoundBlocks::compileSetVolumeTo)); |
99 | 104 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sound_volume", &SoundBlocks::compileVolume)); |
100 | 105 |
|
101 | 106 | // Inputs |
| 107 | + EXPECT_CALL(m_engineMock, addInput(m_section.get(), "SOUND_MENU", SoundBlocks::SOUND_MENU)); |
102 | 108 | EXPECT_CALL(m_engineMock, addInput(m_section.get(), "VOLUME", SoundBlocks::VOLUME)); |
103 | 109 |
|
104 | 110 | m_section->registerBlocks(&m_engineMock); |
105 | 111 | } |
106 | 112 |
|
| 113 | +TEST_F(SoundBlocksTest, Play) |
| 114 | +{ |
| 115 | + AudioPlayerFactoryMock factory; |
| 116 | + auto player1 = std::make_shared<AudioPlayerMock>(); |
| 117 | + auto player2 = std::make_shared<AudioPlayerMock>(); |
| 118 | + SoundPrivate::playerFactory = &factory; |
| 119 | + EXPECT_CALL(factory, create()).WillOnce(Return(player1)).WillOnce(Return(player2)); |
| 120 | + EXPECT_CALL(*player1, setVolume); |
| 121 | + EXPECT_CALL(*player2, setVolume); |
| 122 | + Target target; |
| 123 | + target.addSound(std::make_shared<Sound>("test", "", "")); |
| 124 | + target.addSound(std::make_shared<Sound>("some sound", "", "")); |
| 125 | + Compiler compiler(&m_engineMock, &target); |
| 126 | + |
| 127 | + // start sound (some sound) |
| 128 | + auto block1 = std::make_shared<Block>("a", "sound_play"); |
| 129 | + addDropdownInput(block1, "SOUND_MENU", SoundBlocks::SOUND_MENU, "some sound"); |
| 130 | + |
| 131 | + // start sound (1) |
| 132 | + auto block2 = std::make_shared<Block>("b", "sound_play"); |
| 133 | + addDropdownInput(block2, "SOUND_MENU", SoundBlocks::SOUND_MENU, "1"); |
| 134 | + |
| 135 | + // start sound (5) |
| 136 | + auto block3 = std::make_shared<Block>("c", "sound_play"); |
| 137 | + addDropdownInput(block3, "SOUND_MENU", SoundBlocks::SOUND_MENU, "5"); |
| 138 | + |
| 139 | + // start sound (-3) |
| 140 | + auto block4 = std::make_shared<Block>("d", "sound_play"); |
| 141 | + addDropdownInput(block4, "SOUND_MENU", SoundBlocks::SOUND_MENU, "-3"); |
| 142 | + |
| 143 | + // start sound (nonexistent sound) |
| 144 | + auto block5 = std::make_shared<Block>("e", "sound_play"); |
| 145 | + addDropdownInput(block5, "SOUND_MENU", SoundBlocks::SOUND_MENU, "nonexistent sound"); |
| 146 | + |
| 147 | + // start sound (null block) |
| 148 | + auto block6 = std::make_shared<Block>("f", "sound_play"); |
| 149 | + addDropdownInput(block6, "SOUND_MENU", SoundBlocks::SOUND_MENU, "", createNullBlock("g")); |
| 150 | + |
| 151 | + compiler.init(); |
| 152 | + |
| 153 | + EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::playByIndex)).WillOnce(Return(2)); |
| 154 | + compiler.setBlock(block1); |
| 155 | + SoundBlocks::compilePlay(&compiler); |
| 156 | + |
| 157 | + EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::playByIndex)).WillOnce(Return(2)); |
| 158 | + compiler.setBlock(block2); |
| 159 | + SoundBlocks::compilePlay(&compiler); |
| 160 | + |
| 161 | + EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::playByIndex)).WillOnce(Return(2)); |
| 162 | + compiler.setBlock(block3); |
| 163 | + SoundBlocks::compilePlay(&compiler); |
| 164 | + |
| 165 | + EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::playByIndex)).WillOnce(Return(2)); |
| 166 | + compiler.setBlock(block4); |
| 167 | + SoundBlocks::compilePlay(&compiler); |
| 168 | + |
| 169 | + EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::playByIndex)).Times(0); |
| 170 | + EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::play)).Times(0); |
| 171 | + compiler.setBlock(block5); |
| 172 | + SoundBlocks::compilePlay(&compiler); |
| 173 | + |
| 174 | + EXPECT_CALL(m_engineMock, functionIndex(&SoundBlocks::play)).WillOnce(Return(3)); |
| 175 | + compiler.setBlock(block6); |
| 176 | + SoundBlocks::compilePlay(&compiler); |
| 177 | + |
| 178 | + compiler.end(); |
| 179 | + |
| 180 | + ASSERT_EQ( |
| 181 | + compiler.bytecode(), |
| 182 | + std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 2, vm::OP_CONST, 1, vm::OP_EXEC, 2, vm::OP_CONST, 2, vm::OP_EXEC, 2, vm::OP_CONST, 3, vm::OP_EXEC, 2, |
| 183 | + vm::OP_NULL, vm::OP_EXEC, 3, vm::OP_HALT })); |
| 184 | + ASSERT_EQ(compiler.constValues(), std::vector<Value>({ 1, 0, 4, -4 })); |
| 185 | + |
| 186 | + SoundPrivate::playerFactory = nullptr; |
| 187 | +} |
| 188 | + |
| 189 | +TEST_F(SoundBlocksTest, PlayImpl) |
| 190 | +{ |
| 191 | + static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }; |
| 192 | + static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_HALT }; |
| 193 | + static unsigned int bytecode3[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_EXEC, 0, vm::OP_HALT }; |
| 194 | + static unsigned int bytecode4[] = { vm::OP_START, vm::OP_CONST, 3, vm::OP_EXEC, 1, vm::OP_HALT }; |
| 195 | + static unsigned int bytecode5[] = { vm::OP_START, vm::OP_CONST, 4, vm::OP_EXEC, 1, vm::OP_HALT }; |
| 196 | + static unsigned int bytecode6[] = { vm::OP_START, vm::OP_CONST, 5, vm::OP_EXEC, 1, vm::OP_HALT }; |
| 197 | + static unsigned int bytecode7[] = { vm::OP_START, vm::OP_CONST, 6, vm::OP_EXEC, 1, vm::OP_HALT }; |
| 198 | + static unsigned int bytecode8[] = { vm::OP_START, vm::OP_CONST, 7, vm::OP_EXEC, 1, vm::OP_HALT }; |
| 199 | + static BlockFunc functions[] = { &SoundBlocks::playByIndex, &SoundBlocks::play }; |
| 200 | + static Value constValues[] = { 2, 5, -1, "test", "nonexistent", "1", "4", "-3" }; |
| 201 | + |
| 202 | + AudioPlayerFactoryMock factory; |
| 203 | + auto player1 = std::make_shared<AudioPlayerMock>(); |
| 204 | + auto player2 = std::make_shared<AudioPlayerMock>(); |
| 205 | + auto player3 = std::make_shared<AudioPlayerMock>(); |
| 206 | + SoundPrivate::playerFactory = &factory; |
| 207 | + EXPECT_CALL(factory, create()).WillOnce(Return(player1)).WillOnce(Return(player2)).WillOnce(Return(player3)); |
| 208 | + EXPECT_CALL(*player1, setVolume); |
| 209 | + EXPECT_CALL(*player2, setVolume); |
| 210 | + EXPECT_CALL(*player3, setVolume); |
| 211 | + Target target; |
| 212 | + target.addSound(std::make_shared<Sound>("some sound", "", "")); |
| 213 | + target.addSound(std::make_shared<Sound>("test", "", "")); |
| 214 | + target.addSound(std::make_shared<Sound>("another sound", "", "")); |
| 215 | + |
| 216 | + VirtualMachine vm(&target, nullptr, nullptr); |
| 217 | + vm.setFunctions(functions); |
| 218 | + vm.setConstValues(constValues); |
| 219 | + |
| 220 | + EXPECT_CALL(*player3, start()); |
| 221 | + vm.setBytecode(bytecode1); |
| 222 | + vm.run(); |
| 223 | + |
| 224 | + ASSERT_EQ(vm.registerCount(), 0); |
| 225 | + |
| 226 | + EXPECT_CALL(*player3, start()); |
| 227 | + vm.setBytecode(bytecode2); |
| 228 | + vm.run(); |
| 229 | + |
| 230 | + ASSERT_EQ(vm.registerCount(), 0); |
| 231 | + |
| 232 | + EXPECT_CALL(*player3, start()); |
| 233 | + vm.reset(); |
| 234 | + vm.setBytecode(bytecode3); |
| 235 | + vm.run(); |
| 236 | + |
| 237 | + ASSERT_EQ(vm.registerCount(), 0); |
| 238 | + |
| 239 | + EXPECT_CALL(*player2, start()); |
| 240 | + vm.reset(); |
| 241 | + vm.setBytecode(bytecode4); |
| 242 | + vm.run(); |
| 243 | + |
| 244 | + ASSERT_EQ(vm.registerCount(), 0); |
| 245 | + |
| 246 | + vm.reset(); |
| 247 | + vm.setBytecode(bytecode5); |
| 248 | + vm.run(); |
| 249 | + |
| 250 | + ASSERT_EQ(vm.registerCount(), 0); |
| 251 | + |
| 252 | + EXPECT_CALL(*player1, start()); |
| 253 | + vm.reset(); |
| 254 | + vm.setBytecode(bytecode6); |
| 255 | + vm.run(); |
| 256 | + |
| 257 | + ASSERT_EQ(vm.registerCount(), 0); |
| 258 | + |
| 259 | + EXPECT_CALL(*player1, start()); |
| 260 | + vm.reset(); |
| 261 | + vm.setBytecode(bytecode7); |
| 262 | + vm.run(); |
| 263 | + |
| 264 | + ASSERT_EQ(vm.registerCount(), 0); |
| 265 | + |
| 266 | + EXPECT_CALL(*player3, start()); |
| 267 | + vm.reset(); |
| 268 | + vm.setBytecode(bytecode8); |
| 269 | + vm.run(); |
| 270 | + |
| 271 | + ASSERT_EQ(vm.registerCount(), 0); |
| 272 | + |
| 273 | + SoundPrivate::playerFactory = nullptr; |
| 274 | +} |
| 275 | + |
107 | 276 | TEST_F(SoundBlocksTest, ChangeVolumeBy) |
108 | 277 | { |
109 | 278 | Compiler compiler(&m_engineMock); |
|
0 commit comments