OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/message_loop.h" |
| 6 #include "base/platform_thread.h" |
| 7 #include "media/audio/audio_output_dispatcher.h" |
| 8 #include "media/audio/audio_output_proxy.h" |
| 9 #include "media/audio/audio_manager.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 using ::testing::_; |
| 14 using ::testing::Mock; |
| 15 using ::testing::Return; |
| 16 |
| 17 namespace { |
| 18 const int kTestCloseDelayMs = 100; |
| 19 |
| 20 // Used in the test where we don't want a stream to be closed unexpectedly. |
| 21 const int kTestBigCloseDelayMs = 1000 * 1000; |
| 22 } // namespace |
| 23 |
| 24 class MockAudioOutputStream : public AudioOutputStream { |
| 25 public: |
| 26 MockAudioOutputStream() {} |
| 27 |
| 28 MOCK_METHOD0(Open, bool()); |
| 29 MOCK_METHOD1(Start, void(AudioSourceCallback* callback)); |
| 30 MOCK_METHOD0(Stop, void()); |
| 31 MOCK_METHOD1(SetVolume, void(double volume)); |
| 32 MOCK_METHOD1(GetVolume, void(double* volume)); |
| 33 MOCK_METHOD0(Close, void()); |
| 34 }; |
| 35 |
| 36 class MockAudioManager : public AudioManager { |
| 37 public: |
| 38 MockAudioManager() { }; |
| 39 |
| 40 MOCK_METHOD0(Init, void()); |
| 41 MOCK_METHOD0(HasAudioOutputDevices, bool()); |
| 42 MOCK_METHOD0(HasAudioInputDevices, bool()); |
| 43 MOCK_METHOD0(GetAudioInputDeviceModel, string16()); |
| 44 MOCK_METHOD1(MakeAudioOutputStream, AudioOutputStream*( |
| 45 AudioParameters params)); |
| 46 MOCK_METHOD1(MakeAudioOutputStreamProxy, AudioOutputStream*( |
| 47 const AudioParameters& params)); |
| 48 MOCK_METHOD1(MakeAudioInputStream, AudioInputStream*( |
| 49 AudioParameters params)); |
| 50 MOCK_METHOD0(MuteAll, void()); |
| 51 MOCK_METHOD0(UnMuteAll, void()); |
| 52 MOCK_METHOD0(GetMessageLoop, MessageLoop*()); |
| 53 }; |
| 54 |
| 55 class MockAudioSourceCallback : public AudioOutputStream::AudioSourceCallback { |
| 56 public: |
| 57 MOCK_METHOD4(OnMoreData, uint32(AudioOutputStream* stream, |
| 58 uint8* dest, uint32 max_size, |
| 59 AudioBuffersState buffers_state)); |
| 60 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code)); |
| 61 }; |
| 62 |
| 63 class AudioOutputProxyTest : public testing::Test { |
| 64 protected: |
| 65 virtual void SetUp() { |
| 66 EXPECT_CALL(manager_, GetMessageLoop()) |
| 67 .WillRepeatedly(Return(&message_loop_)); |
| 68 InitDispatcher(kTestCloseDelayMs); |
| 69 } |
| 70 |
| 71 virtual void TearDown() { |
| 72 // All paused proxies should have been closed at this point. |
| 73 EXPECT_EQ(0u, dispatcher_->paused_proxies_); |
| 74 |
| 75 // This is necessary to free all proxy objects that have been |
| 76 // closed by the test. |
| 77 message_loop_.RunAllPending(); |
| 78 } |
| 79 |
| 80 void InitDispatcher(int close_delay_ms) { |
| 81 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, 2, 44100, |
| 82 16, 1024); |
| 83 dispatcher_ = new AudioOutputDispatcher(&manager_, params, close_delay_ms); |
| 84 } |
| 85 |
| 86 MessageLoop message_loop_; |
| 87 scoped_refptr<AudioOutputDispatcher> dispatcher_; |
| 88 MockAudioManager manager_; |
| 89 MockAudioSourceCallback callback_; |
| 90 }; |
| 91 |
| 92 TEST_F(AudioOutputProxyTest, CreateAndClose) { |
| 93 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); |
| 94 proxy->Close(); |
| 95 } |
| 96 |
| 97 TEST_F(AudioOutputProxyTest, OpenAndClose) { |
| 98 MockAudioOutputStream stream; |
| 99 |
| 100 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 101 .WillOnce(Return(&stream)); |
| 102 EXPECT_CALL(stream, Open()) |
| 103 .WillOnce(Return(true)); |
| 104 EXPECT_CALL(stream, Close()) |
| 105 .Times(1); |
| 106 |
| 107 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); |
| 108 EXPECT_TRUE(proxy->Open()); |
| 109 proxy->Close(); |
| 110 } |
| 111 |
| 112 // Create a stream, and verify that it is closed after kTestCloseDelayMs. |
| 113 // if it doesn't start playing. |
| 114 TEST_F(AudioOutputProxyTest, CreateAndWait) { |
| 115 MockAudioOutputStream stream; |
| 116 |
| 117 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 118 .WillOnce(Return(&stream)); |
| 119 EXPECT_CALL(stream, Open()) |
| 120 .WillOnce(Return(true)); |
| 121 EXPECT_CALL(stream, Close()) |
| 122 .Times(1); |
| 123 |
| 124 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); |
| 125 EXPECT_TRUE(proxy->Open()); |
| 126 |
| 127 // Simulate a delay. |
| 128 PlatformThread::Sleep(kTestCloseDelayMs * 2); |
| 129 message_loop_.RunAllPending(); |
| 130 |
| 131 // Verify expectation before calling Close(). |
| 132 Mock::VerifyAndClear(&stream); |
| 133 |
| 134 proxy->Close(); |
| 135 } |
| 136 |
| 137 // Create a stream, and then calls Start() and Stop(). |
| 138 TEST_F(AudioOutputProxyTest, StartAndStop) { |
| 139 MockAudioOutputStream stream; |
| 140 |
| 141 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 142 .WillOnce(Return(&stream)); |
| 143 EXPECT_CALL(stream, Open()) |
| 144 .WillOnce(Return(true)); |
| 145 EXPECT_CALL(stream, Start(_)) |
| 146 .Times(1); |
| 147 EXPECT_CALL(stream, SetVolume(_)) |
| 148 .Times(1); |
| 149 EXPECT_CALL(stream, Stop()) |
| 150 .Times(1); |
| 151 EXPECT_CALL(stream, Close()) |
| 152 .Times(1); |
| 153 |
| 154 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); |
| 155 EXPECT_TRUE(proxy->Open()); |
| 156 |
| 157 proxy->Start(&callback_); |
| 158 proxy->Stop(); |
| 159 |
| 160 proxy->Close(); |
| 161 } |
| 162 |
| 163 // Verify that the stream is closed after Stop is called. |
| 164 TEST_F(AudioOutputProxyTest, CloseAfterStop) { |
| 165 MockAudioOutputStream stream; |
| 166 |
| 167 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 168 .WillOnce(Return(&stream)); |
| 169 EXPECT_CALL(stream, Open()) |
| 170 .WillOnce(Return(true)); |
| 171 EXPECT_CALL(stream, Start(_)) |
| 172 .Times(1); |
| 173 EXPECT_CALL(stream, SetVolume(_)) |
| 174 .Times(1); |
| 175 EXPECT_CALL(stream, Stop()) |
| 176 .Times(1); |
| 177 EXPECT_CALL(stream, Close()) |
| 178 .Times(1); |
| 179 |
| 180 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); |
| 181 EXPECT_TRUE(proxy->Open()); |
| 182 |
| 183 proxy->Start(&callback_); |
| 184 proxy->Stop(); |
| 185 |
| 186 // Simulate a delay. |
| 187 message_loop_.RunAllPending(); |
| 188 PlatformThread::Sleep(kTestCloseDelayMs * 10); |
| 189 message_loop_.RunAllPending(); |
| 190 |
| 191 // Verify expectation before calling Close(). |
| 192 Mock::VerifyAndClear(&stream); |
| 193 |
| 194 proxy->Close(); |
| 195 } |
| 196 |
| 197 // Create two streams, but don't start them. Only one device must be open. |
| 198 TEST_F(AudioOutputProxyTest, TwoStreams) { |
| 199 MockAudioOutputStream stream; |
| 200 |
| 201 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 202 .WillOnce(Return(&stream)); |
| 203 EXPECT_CALL(stream, Open()) |
| 204 .WillOnce(Return(true)); |
| 205 EXPECT_CALL(stream, Close()) |
| 206 .Times(1); |
| 207 |
| 208 AudioOutputProxy* proxy1 = new AudioOutputProxy(dispatcher_); |
| 209 AudioOutputProxy* proxy2 = new AudioOutputProxy(dispatcher_); |
| 210 EXPECT_TRUE(proxy1->Open()); |
| 211 EXPECT_TRUE(proxy2->Open()); |
| 212 proxy1->Close(); |
| 213 proxy2->Close(); |
| 214 } |
| 215 |
| 216 // Two streams: verify that second stream is allocated when the first |
| 217 // starts playing. |
| 218 TEST_F(AudioOutputProxyTest, TwoStreams_OnePlaying) { |
| 219 MockAudioOutputStream stream1; |
| 220 MockAudioOutputStream stream2; |
| 221 |
| 222 InitDispatcher(kTestBigCloseDelayMs); |
| 223 |
| 224 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 225 .WillOnce(Return(&stream1)) |
| 226 .WillOnce(Return(&stream2)); |
| 227 |
| 228 EXPECT_CALL(stream1, Open()) |
| 229 .WillOnce(Return(true)); |
| 230 EXPECT_CALL(stream1, Start(_)) |
| 231 .Times(1); |
| 232 EXPECT_CALL(stream1, SetVolume(_)) |
| 233 .Times(1); |
| 234 EXPECT_CALL(stream1, Stop()) |
| 235 .Times(1); |
| 236 EXPECT_CALL(stream1, Close()) |
| 237 .Times(1); |
| 238 |
| 239 EXPECT_CALL(stream2, Open()) |
| 240 .WillOnce(Return(true)); |
| 241 EXPECT_CALL(stream2, Close()) |
| 242 .Times(1); |
| 243 |
| 244 AudioOutputProxy* proxy1 = new AudioOutputProxy(dispatcher_); |
| 245 AudioOutputProxy* proxy2 = new AudioOutputProxy(dispatcher_); |
| 246 EXPECT_TRUE(proxy1->Open()); |
| 247 EXPECT_TRUE(proxy2->Open()); |
| 248 |
| 249 proxy1->Start(&callback_); |
| 250 message_loop_.RunAllPending(); |
| 251 proxy1->Stop(); |
| 252 |
| 253 proxy1->Close(); |
| 254 proxy2->Close(); |
| 255 } |
| 256 |
| 257 // Two streams, both are playing. Dispatcher should not open a third stream. |
| 258 TEST_F(AudioOutputProxyTest, TwoStreams_BothPlaying) { |
| 259 MockAudioOutputStream stream1; |
| 260 MockAudioOutputStream stream2; |
| 261 |
| 262 InitDispatcher(kTestBigCloseDelayMs); |
| 263 |
| 264 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 265 .WillOnce(Return(&stream1)) |
| 266 .WillOnce(Return(&stream2)); |
| 267 |
| 268 EXPECT_CALL(stream1, Open()) |
| 269 .WillOnce(Return(true)); |
| 270 EXPECT_CALL(stream1, Start(_)) |
| 271 .Times(1); |
| 272 EXPECT_CALL(stream1, SetVolume(_)) |
| 273 .Times(1); |
| 274 EXPECT_CALL(stream1, Stop()) |
| 275 .Times(1); |
| 276 EXPECT_CALL(stream1, Close()) |
| 277 .Times(1); |
| 278 |
| 279 EXPECT_CALL(stream2, Open()) |
| 280 .WillOnce(Return(true)); |
| 281 EXPECT_CALL(stream2, Start(_)) |
| 282 .Times(1); |
| 283 EXPECT_CALL(stream2, SetVolume(_)) |
| 284 .Times(1); |
| 285 EXPECT_CALL(stream2, Stop()) |
| 286 .Times(1); |
| 287 EXPECT_CALL(stream2, Close()) |
| 288 .Times(1); |
| 289 |
| 290 AudioOutputProxy* proxy1 = new AudioOutputProxy(dispatcher_); |
| 291 AudioOutputProxy* proxy2 = new AudioOutputProxy(dispatcher_); |
| 292 EXPECT_TRUE(proxy1->Open()); |
| 293 EXPECT_TRUE(proxy2->Open()); |
| 294 |
| 295 proxy1->Start(&callback_); |
| 296 proxy2->Start(&callback_); |
| 297 proxy1->Stop(); |
| 298 proxy2->Stop(); |
| 299 |
| 300 proxy1->Close(); |
| 301 proxy2->Close(); |
| 302 } |
| 303 |
| 304 // Open() method failed. |
| 305 TEST_F(AudioOutputProxyTest, OpenFailed) { |
| 306 MockAudioOutputStream stream; |
| 307 |
| 308 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 309 .WillOnce(Return(&stream)); |
| 310 EXPECT_CALL(stream, Open()) |
| 311 .WillOnce(Return(false)); |
| 312 EXPECT_CALL(stream, Close()) |
| 313 .Times(1); |
| 314 |
| 315 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); |
| 316 EXPECT_FALSE(proxy->Open()); |
| 317 proxy->Close(); |
| 318 } |
| 319 |
| 320 // Start() method failed. |
| 321 TEST_F(AudioOutputProxyTest, StartFailed) { |
| 322 MockAudioOutputStream stream; |
| 323 |
| 324 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 325 .WillOnce(Return(&stream)); |
| 326 EXPECT_CALL(stream, Open()) |
| 327 .WillOnce(Return(true)); |
| 328 EXPECT_CALL(stream, Close()) |
| 329 .Times(1); |
| 330 |
| 331 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); |
| 332 EXPECT_TRUE(proxy->Open()); |
| 333 |
| 334 // Simulate a delay. |
| 335 PlatformThread::Sleep(kTestCloseDelayMs); |
| 336 message_loop_.RunAllPending(); |
| 337 |
| 338 // Verify expectation before calling Close(). |
| 339 Mock::VerifyAndClear(&stream); |
| 340 |
| 341 // |stream| is closed at this point. Start() should reopen it again. |
| 342 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) |
| 343 .WillOnce(Return(reinterpret_cast<AudioOutputStream*>(NULL))); |
| 344 |
| 345 EXPECT_CALL(callback_, OnError(_, _)) |
| 346 .Times(1); |
| 347 |
| 348 proxy->Start(&callback_); |
| 349 |
| 350 Mock::VerifyAndClear(&callback_); |
| 351 |
| 352 proxy->Close(); |
| 353 } |
OLD | NEW |