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