| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/environment.h" | 5 #include "base/environment.h" |
| 6 #include "base/basictypes.h" | 6 #include "base/basictypes.h" |
| 7 #include "base/waitable_event.h" | 7 #include "base/waitable_event.h" |
| 8 #include "media/audio/audio_output_controller.h" | 8 #include "media/audio/audio_output_controller.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 using ::testing::_; | 12 using ::testing::_; |
| 13 using ::testing::AtLeast; | 13 using ::testing::AtLeast; |
| 14 using ::testing::Exactly; | 14 using ::testing::Exactly; |
| 15 using ::testing::InvokeWithoutArgs; | 15 using ::testing::InvokeWithoutArgs; |
| 16 using ::testing::NotNull; | 16 using ::testing::NotNull; |
| 17 using ::testing::Return; | |
| 18 | 17 |
| 19 static const int kSampleRate = AudioManager::kAudioCDSampleRate; | 18 static const int kSampleRate = AudioManager::kAudioCDSampleRate; |
| 20 static const int kBitsPerSample = 16; | 19 static const int kBitsPerSample = 16; |
| 21 static const int kChannels = 2; | 20 static const int kChannels = 2; |
| 22 static const int kHardwareBufferSize = kSampleRate * kBitsPerSample * | 21 static const int kHardwareBufferSize = kSampleRate * kBitsPerSample * |
| 23 kChannels / 8; | 22 kChannels / 8; |
| 24 static const int kBufferCapacity = 3 * kHardwareBufferSize; | 23 static const int kBufferCapacity = 3 * kHardwareBufferSize; |
| 25 | 24 |
| 26 namespace media { | 25 namespace media { |
| 27 | 26 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 return audio_man->HasAudioOutputDevices(); | 61 return audio_man->HasAudioOutputDevices(); |
| 63 } | 62 } |
| 64 | 63 |
| 65 static bool IsRunningHeadless() { | 64 static bool IsRunningHeadless() { |
| 66 scoped_ptr<base::Environment> env(base::Environment::Create()); | 65 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 67 if (env->HasVar("CHROME_HEADLESS")) | 66 if (env->HasVar("CHROME_HEADLESS")) |
| 68 return true; | 67 return true; |
| 69 return false; | 68 return false; |
| 70 } | 69 } |
| 71 | 70 |
| 72 ACTION_P(SignalEvent, event) { | |
| 73 event->Signal(); | |
| 74 } | |
| 75 | |
| 76 ACTION_P3(SignalEvent, event, count, limit) { | 71 ACTION_P3(SignalEvent, event, count, limit) { |
| 77 if (++*count >= limit) { | 72 if (++*count >= limit) { |
| 78 event->Signal(); | 73 event->Signal(); |
| 79 } | 74 } |
| 80 } | 75 } |
| 81 | 76 |
| 82 TEST(AudioOutputControllerTest, CreateAndClose) { | 77 TEST(AudioOutputControllerTest, CreateAndClose) { |
| 83 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 78 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
| 84 return; | 79 return; |
| 85 | 80 |
| 86 MockAudioOutputControllerEventHandler event_handler; | 81 MockAudioOutputControllerEventHandler event_handler; |
| 87 scoped_refptr<AudioOutputController> controller = | 82 scoped_refptr<AudioOutputController> controller = |
| 88 AudioOutputController::Create(&event_handler, | 83 AudioOutputController::Create(&event_handler, |
| 89 AudioManager::AUDIO_PCM_LINEAR, kChannels, | 84 AudioManager::AUDIO_PCM_LINEAR, kChannels, |
| 90 kSampleRate, kBitsPerSample, | 85 kSampleRate, kBitsPerSample, |
| 91 kHardwareBufferSize, kBufferCapacity); | 86 kHardwareBufferSize, kBufferCapacity); |
| 92 ASSERT_TRUE(controller.get()); | 87 ASSERT_TRUE(controller.get()); |
| 93 | 88 |
| 94 // Close the controller immediately. At this point, chances are that | 89 // Close the controller immediately. |
| 95 // DoCreate() hasn't been called yet. In any case, it should be safe to call | |
| 96 // Close() and it should not try to call |event_handler| later (the test | |
| 97 // would crash otherwise). | |
| 98 controller->Close(); | 90 controller->Close(); |
| 99 } | 91 } |
| 100 | 92 |
| 101 TEST(AudioOutputControllerTest, PlayAndClose) { | 93 TEST(AudioOutputControllerTest, PlayAndClose) { |
| 102 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 94 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
| 103 return; | 95 return; |
| 104 | 96 |
| 105 MockAudioOutputControllerEventHandler event_handler; | 97 MockAudioOutputControllerEventHandler event_handler; |
| 106 base::WaitableEvent event(false, false); | 98 base::WaitableEvent event(false, false); |
| 107 int count = 0; | 99 int count = 0; |
| 108 | 100 |
| 109 // If OnCreated is called then signal the event. | 101 // If OnCreated is called then signal the event. |
| 110 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 102 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| 111 .WillOnce(SignalEvent(&event)); | 103 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal)); |
| 112 | 104 |
| 113 // OnPlaying() will be called only once. | 105 // OnPlaying() will be called only once. |
| 114 EXPECT_CALL(event_handler, OnPlaying(NotNull())) | 106 EXPECT_CALL(event_handler, OnPlaying(NotNull())) |
| 115 .Times(Exactly(1)); | 107 .Times(Exactly(1)); |
| 116 | 108 |
| 117 // If OnMoreData is called enough then signal the event. | 109 // If OnMoreData is called enough then signal the event. |
| 118 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _, 0)) | 110 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _, 0)) |
| 119 .Times(AtLeast(10)) | 111 .Times(AtLeast(10)) |
| 120 .WillRepeatedly(SignalEvent(&event, &count, 10)); | 112 .WillRepeatedly(SignalEvent(&event, &count, 10)); |
| 121 | 113 |
| 122 scoped_refptr<AudioOutputController> controller = | 114 scoped_refptr<AudioOutputController> controller = |
| 123 AudioOutputController::Create(&event_handler, | 115 AudioOutputController::Create(&event_handler, |
| 124 AudioManager::AUDIO_PCM_LINEAR, kChannels, | 116 AudioManager::AUDIO_PCM_LINEAR, kChannels, |
| 125 kSampleRate, kBitsPerSample, | 117 kSampleRate, kBitsPerSample, |
| 126 kHardwareBufferSize, kBufferCapacity); | 118 kHardwareBufferSize, kBufferCapacity); |
| 127 ASSERT_TRUE(controller.get()); | 119 ASSERT_TRUE(controller.get()); |
| 128 | 120 |
| 129 // Wait for OnCreated() to be called. | 121 // Wait for OnCreated() to be called. |
| 130 event.Wait(); | 122 event.Wait(); |
| 123 event.Reset(); |
| 131 | 124 |
| 132 // Play and then wait for the event to be signaled. | 125 // Play and then wait for the event to be signaled. |
| 133 controller->Play(); | 126 controller->Play(); |
| 134 event.Wait(); | 127 event.Wait(); |
| 135 | 128 |
| 136 // Now stop the controller. The object is freed later after DoClose() is | 129 // Now stop the controller. This should shutdown the internal |
| 137 // executed. | 130 // thread and we hold the only reference to it. |
| 138 controller->Close(); | 131 controller->Close(); |
| 139 } | 132 } |
| 140 | 133 |
| 141 TEST(AudioOutputControllerTest, PlayPauseClose) { | 134 TEST(AudioOutputControllerTest, PlayPauseClose) { |
| 142 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 135 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
| 143 return; | 136 return; |
| 144 | 137 |
| 145 MockAudioOutputControllerEventHandler event_handler; | 138 MockAudioOutputControllerEventHandler event_handler; |
| 146 base::WaitableEvent event(false, false); | 139 base::WaitableEvent event(false, false); |
| 147 int count = 0; | 140 int count = 0; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 178 | 171 |
| 179 // Play and then wait for the event to be signaled. | 172 // Play and then wait for the event to be signaled. |
| 180 controller->Play(); | 173 controller->Play(); |
| 181 event.Wait(); | 174 event.Wait(); |
| 182 event.Reset(); | 175 event.Reset(); |
| 183 | 176 |
| 184 // And then wait for pause to complete. | 177 // And then wait for pause to complete. |
| 185 controller->Pause(); | 178 controller->Pause(); |
| 186 event.Wait(); | 179 event.Wait(); |
| 187 | 180 |
| 188 // Now stop the controller. The object is freed later after DoClose() is | 181 // Now stop the controller. This should shutdown the internal |
| 189 // executed. | 182 // thread and we hold the only reference to it. |
| 190 controller->Close(); | 183 controller->Close(); |
| 191 } | 184 } |
| 192 | 185 |
| 193 TEST(AudioOutputControllerTest, PlayPausePlay) { | 186 TEST(AudioOutputControllerTest, PlayPausePlay) { |
| 194 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 187 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
| 195 return; | 188 return; |
| 196 | 189 |
| 197 MockAudioOutputControllerEventHandler event_handler; | 190 MockAudioOutputControllerEventHandler event_handler; |
| 198 base::WaitableEvent event(false, false); | 191 base::WaitableEvent event(false, false); |
| 199 int count = 0; | 192 int count = 0; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 // And then wait for pause to complete. | 235 // And then wait for pause to complete. |
| 243 controller->Pause(); | 236 controller->Pause(); |
| 244 event.Wait(); | 237 event.Wait(); |
| 245 event.Reset(); | 238 event.Reset(); |
| 246 | 239 |
| 247 // Then we play again. | 240 // Then we play again. |
| 248 // Play and then wait for the event to be signaled. | 241 // Play and then wait for the event to be signaled. |
| 249 controller->Play(); | 242 controller->Play(); |
| 250 event.Wait(); | 243 event.Wait(); |
| 251 | 244 |
| 252 // Now stop the controller. The object is freed later after DoClose() is | 245 // Now stop the controller. This should shutdown the internal |
| 253 // executed. | 246 // thread and we hold the only reference to it. |
| 254 controller->Close(); | 247 controller->Close(); |
| 255 } | 248 } |
| 256 | 249 |
| 257 TEST(AudioOutputControllerTest, HardwareBufferTooLarge) { | 250 TEST(AudioOutputControllerTest, HardwareBufferTooLarge) { |
| 258 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 251 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
| 259 return; | 252 return; |
| 260 | 253 |
| 261 // Create an audio device with a very large hardware buffer size. | 254 // Create an audio device with a very large hardware buffer size. |
| 262 MockAudioOutputControllerEventHandler event_handler; | 255 MockAudioOutputControllerEventHandler event_handler; |
| 263 scoped_refptr<AudioOutputController> controller = | 256 scoped_refptr<AudioOutputController> controller = |
| 264 AudioOutputController::Create(&event_handler, | 257 AudioOutputController::Create(&event_handler, |
| 265 AudioManager::AUDIO_PCM_LINEAR, kChannels, | 258 AudioManager::AUDIO_PCM_LINEAR, kChannels, |
| 266 kSampleRate, kBitsPerSample, | 259 kSampleRate, kBitsPerSample, |
| 267 kHardwareBufferSize * 1000, | 260 kHardwareBufferSize * 1000, |
| 268 kBufferCapacity); | 261 kBufferCapacity); |
| 269 | 262 |
| 270 // Use assert because we don't stop the device and assume we can't | 263 // Use assert because we don't stop the device and assume we can't |
| 271 // create one. | 264 // create one. |
| 272 ASSERT_FALSE(controller); | 265 ASSERT_FALSE(controller); |
| 273 } | 266 } |
| 274 | 267 |
| 275 TEST(AudioOutputControllerTest, CloseTwice) { | 268 TEST(AudioOutputControllerTest, CloseTwice) { |
| 276 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 269 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
| 277 return; | 270 return; |
| 278 | 271 |
| 279 MockAudioOutputControllerEventHandler event_handler; | 272 MockAudioOutputControllerEventHandler event_handler; |
| 280 base::WaitableEvent event(false, false); | |
| 281 | |
| 282 // If OnCreated is called then signal the event. | |
| 283 EXPECT_CALL(event_handler, OnCreated(NotNull())) | |
| 284 .WillOnce(SignalEvent(&event)); | |
| 285 | |
| 286 // One OnMoreData() is expected. | |
| 287 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _, 0)) | |
| 288 .Times(AtLeast(1)) | |
| 289 .WillRepeatedly(SignalEvent(&event)); | |
| 290 | |
| 291 scoped_refptr<AudioOutputController> controller = | 273 scoped_refptr<AudioOutputController> controller = |
| 292 AudioOutputController::Create(&event_handler, | 274 AudioOutputController::Create(&event_handler, |
| 293 AudioManager::AUDIO_PCM_LINEAR, kChannels, | 275 AudioManager::AUDIO_PCM_LINEAR, kChannels, |
| 294 kSampleRate, kBitsPerSample, | 276 kSampleRate, kBitsPerSample, |
| 295 kHardwareBufferSize, kBufferCapacity); | 277 kHardwareBufferSize, kBufferCapacity); |
| 296 ASSERT_TRUE(controller.get()); | 278 ASSERT_TRUE(controller.get()); |
| 297 | 279 |
| 298 // Wait for OnCreated() to be called. | 280 // Close the controller immediately. |
| 299 event.Wait(); | |
| 300 | |
| 301 // Wait for OnMoreData() to be called. | |
| 302 event.Wait(); | |
| 303 | |
| 304 controller->Close(); | 281 controller->Close(); |
| 305 controller->Close(); | 282 controller->Close(); |
| 306 } | 283 } |
| 307 | 284 |
| 308 } // namespace media | 285 } // namespace media |
| OLD | NEW |