|
1 | 1 | #include <scratchcpp/sound.h> |
| 2 | +#include <scratchcpp/sprite.h> |
2 | 3 | #include <scratch/sound_p.h> |
3 | 4 | #include <audiooutputmock.h> |
4 | 5 | #include <audioplayermock.h> |
| 6 | +#include <enginemock.h> |
5 | 7 |
|
6 | 8 | #include "../common.h" |
7 | 9 |
|
@@ -106,30 +108,78 @@ TEST_F(SoundTest, IsPlaying) |
106 | 108 | SoundPrivate::audioOutput = nullptr; |
107 | 109 | } |
108 | 110 |
|
109 | | -TEST_F(SoundTest, Clone) |
| 111 | +TEST_F(SoundTest, Target) |
110 | 112 | { |
111 | 113 | Sound sound("sound1", "a", "wav"); |
112 | | - sound.setRate(44100); |
113 | | - sound.setSampleCount(10000); |
| 114 | + ASSERT_EQ(sound.target(), nullptr); |
| 115 | + |
| 116 | + Target target; |
| 117 | + sound.setTarget(&target); |
| 118 | + ASSERT_EQ(sound.target(), &target); |
| 119 | +} |
| 120 | + |
| 121 | +TEST_F(SoundTest, Clone) |
| 122 | +{ |
| 123 | + auto sound = std::make_shared<Sound>("sound1", "a", "wav"); |
| 124 | + sound->setRate(44100); |
| 125 | + sound->setSampleCount(10000); |
114 | 126 |
|
115 | 127 | const char *data = "abc"; |
116 | 128 | void *dataPtr = const_cast<void *>(static_cast<const void *>(data)); |
117 | 129 |
|
118 | 130 | EXPECT_CALL(*m_player, isLoaded()).WillOnce(Return(false)); |
119 | 131 | EXPECT_CALL(*m_player, load(3, dataPtr, 44100)).WillOnce(Return(true)); |
120 | | - sound.setData(3, dataPtr); |
| 132 | + sound->setData(3, dataPtr); |
121 | 133 |
|
122 | 134 | auto clonePlayer = std::make_shared<AudioPlayerMock>(); |
123 | 135 | EXPECT_CALL(m_playerFactory, createAudioPlayer()).WillOnce(Return(clonePlayer)); |
124 | 136 | EXPECT_CALL(*clonePlayer, loadCopy(m_player.get())).WillOnce(Return(true)); |
125 | 137 | EXPECT_CALL(*m_player, volume()).WillOnce(Return(0.45)); |
126 | 138 | EXPECT_CALL(*clonePlayer, setVolume(0.45)); |
127 | 139 | EXPECT_CALL(*clonePlayer, isLoaded()).WillOnce(Return(true)); |
128 | | - auto clone = sound.clone(); |
| 140 | + auto clone = sound->clone(); |
129 | 141 | ASSERT_TRUE(clone); |
130 | | - ASSERT_EQ(clone->name(), sound.name()); |
131 | | - ASSERT_EQ(clone->id(), sound.id()); |
132 | | - ASSERT_EQ(clone->dataFormat(), sound.dataFormat()); |
133 | | - ASSERT_EQ(clone->rate(), sound.rate()); |
134 | | - 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(); |
135 | 185 | } |
0 commit comments