|
1 | 1 | #include <scratchcpp/sound.h> |
2 | | -#include <scratchcpp/target.h> |
| 2 | +#include <scratchcpp/sprite.h> |
3 | 3 | #include <scratch/sound_p.h> |
4 | 4 | #include <audiooutputmock.h> |
5 | 5 | #include <audioplayermock.h> |
| 6 | +#include <enginemock.h> |
6 | 7 |
|
7 | 8 | #include "../common.h" |
8 | 9 |
|
@@ -119,28 +120,66 @@ TEST_F(SoundTest, Target) |
119 | 120 |
|
120 | 121 | TEST_F(SoundTest, Clone) |
121 | 122 | { |
122 | | - Sound sound("sound1", "a", "wav"); |
123 | | - sound.setRate(44100); |
124 | | - sound.setSampleCount(10000); |
| 123 | + auto sound = std::make_shared<Sound>("sound1", "a", "wav"); |
| 124 | + sound->setRate(44100); |
| 125 | + sound->setSampleCount(10000); |
125 | 126 |
|
126 | 127 | const char *data = "abc"; |
127 | 128 | void *dataPtr = const_cast<void *>(static_cast<const void *>(data)); |
128 | 129 |
|
129 | 130 | EXPECT_CALL(*m_player, isLoaded()).WillOnce(Return(false)); |
130 | 131 | EXPECT_CALL(*m_player, load(3, dataPtr, 44100)).WillOnce(Return(true)); |
131 | | - sound.setData(3, dataPtr); |
| 132 | + sound->setData(3, dataPtr); |
132 | 133 |
|
133 | 134 | auto clonePlayer = std::make_shared<AudioPlayerMock>(); |
134 | 135 | EXPECT_CALL(m_playerFactory, createAudioPlayer()).WillOnce(Return(clonePlayer)); |
135 | 136 | EXPECT_CALL(*clonePlayer, loadCopy(m_player.get())).WillOnce(Return(true)); |
136 | 137 | EXPECT_CALL(*m_player, volume()).WillOnce(Return(0.45)); |
137 | 138 | EXPECT_CALL(*clonePlayer, setVolume(0.45)); |
138 | 139 | EXPECT_CALL(*clonePlayer, isLoaded()).WillOnce(Return(true)); |
139 | | - auto clone = sound.clone(); |
| 140 | + auto clone = sound->clone(); |
140 | 141 | ASSERT_TRUE(clone); |
141 | | - ASSERT_EQ(clone->name(), sound.name()); |
142 | | - ASSERT_EQ(clone->id(), sound.id()); |
143 | | - ASSERT_EQ(clone->dataFormat(), sound.dataFormat()); |
144 | | - ASSERT_EQ(clone->rate(), sound.rate()); |
145 | | - ASSERT_EQ(clone->sampleCount(), sound.sampleCount()); |
| 142 | + ASSERT_EQ(clone->name(), sound->name()); |
| 143 | + ASSERT_EQ(clone->id(), sound->id()); |
| 144 | + ASSERT_EQ(clone->dataFormat(), sound->dataFormat()); |
| 145 | + ASSERT_EQ(clone->rate(), sound->rate()); |
| 146 | + ASSERT_EQ(clone->sampleCount(), sound->sampleCount()); |
| 147 | + |
| 148 | + // Stopping/starting the sound should stop its clones |
| 149 | + auto anotherPlayer = std::make_shared<AudioPlayerMock>(); |
| 150 | + EXPECT_CALL(m_playerFactory, createAudioPlayer()).WillOnce(Return(anotherPlayer)); |
| 151 | + auto another = std::make_shared<Sound>("another", "c", "mp3"); |
| 152 | + Sprite sprite; |
| 153 | + EngineMock engine; |
| 154 | + sprite.setEngine(&engine); |
| 155 | + |
| 156 | + EXPECT_CALL(engine, cloneLimit()).WillOnce(Return(-1)); |
| 157 | + EXPECT_CALL(engine, initClone); |
| 158 | + EXPECT_CALL(engine, requestRedraw); |
| 159 | + EXPECT_CALL(engine, moveSpriteBehindOther); |
| 160 | + auto spriteClone = sprite.clone(); |
| 161 | + |
| 162 | + EXPECT_CALL(*anotherPlayer, setVolume).Times(2); |
| 163 | + EXPECT_CALL(*m_player, setVolume); |
| 164 | + EXPECT_CALL(*clonePlayer, setVolume); |
| 165 | + sprite.addSound(another); |
| 166 | + sprite.addSound(sound); |
| 167 | + spriteClone->addSound(another); |
| 168 | + spriteClone->addSound(clone); |
| 169 | + |
| 170 | + EXPECT_CALL(*m_player, stop()); |
| 171 | + EXPECT_CALL(*clonePlayer, stop()); |
| 172 | + sound->stop(); |
| 173 | + |
| 174 | + EXPECT_CALL(*m_player, stop()); |
| 175 | + EXPECT_CALL(*clonePlayer, stop()); |
| 176 | + clone->stop(); |
| 177 | + |
| 178 | + EXPECT_CALL(*m_player, start()); |
| 179 | + EXPECT_CALL(*clonePlayer, stop()); |
| 180 | + sound->start(); |
| 181 | + |
| 182 | + EXPECT_CALL(*m_player, stop()); |
| 183 | + EXPECT_CALL(*clonePlayer, start()); |
| 184 | + clone->start(); |
146 | 185 | } |
0 commit comments