Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(374)

Side by Side Diff: media/audio/audio_output_proxy_unittest.cc

Issue 8818012: Remove the AudioManager singleton. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Made ResourceContext::audio_manager() abide the same rules as other accessors Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/message_loop.h" 5 #include "base/message_loop.h"
6 #include "base/threading/platform_thread.h" 6 #include "base/threading/platform_thread.h"
7 #include "media/audio/audio_output_dispatcher.h" 7 #include "media/audio/audio_output_dispatcher.h"
8 #include "media/audio/audio_output_proxy.h" 8 #include "media/audio/audio_output_proxy.h"
9 #include "media/audio/audio_manager.h" 9 #include "media/audio/audio_manager.h"
10 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 public: 60 public:
61 MOCK_METHOD4(OnMoreData, uint32(AudioOutputStream* stream, 61 MOCK_METHOD4(OnMoreData, uint32(AudioOutputStream* stream,
62 uint8* dest, uint32 max_size, 62 uint8* dest, uint32 max_size,
63 AudioBuffersState buffers_state)); 63 AudioBuffersState buffers_state));
64 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code)); 64 MOCK_METHOD2(OnError, void(AudioOutputStream* stream, int code));
65 }; 65 };
66 66
67 class AudioOutputProxyTest : public testing::Test { 67 class AudioOutputProxyTest : public testing::Test {
68 protected: 68 protected:
69 virtual void SetUp() { 69 virtual void SetUp() {
70 EXPECT_CALL(manager_, GetMessageLoop()) 70 MockAudioManager* manager = new MockAudioManager();
71 EXPECT_CALL(*manager, GetMessageLoop())
71 .WillRepeatedly(Return(&message_loop_)); 72 .WillRepeatedly(Return(&message_loop_));
73 manager_ = manager;
72 InitDispatcher(kTestCloseDelayMs); 74 InitDispatcher(kTestCloseDelayMs);
73 } 75 }
74 76
75 virtual void TearDown() { 77 virtual void TearDown() {
76 // All paused proxies should have been closed at this point. 78 // All paused proxies should have been closed at this point.
77 EXPECT_EQ(0u, dispatcher_->paused_proxies_); 79 EXPECT_EQ(0u, dispatcher_->paused_proxies_);
78 80
79 // This is necessary to free all proxy objects that have been 81 // This is necessary to free all proxy objects that have been
80 // closed by the test. 82 // closed by the test.
81 message_loop_.RunAllPending(); 83 message_loop_.RunAllPending();
82 } 84 }
83 85
84 void InitDispatcher(int close_delay_ms) { 86 void InitDispatcher(int close_delay_ms) {
85 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, 87 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR,
86 CHANNEL_LAYOUT_STEREO, 44100, 16, 1024); 88 CHANNEL_LAYOUT_STEREO, 44100, 16, 1024);
87 dispatcher_ = new AudioOutputDispatcher(&manager_, params, close_delay_ms); 89 dispatcher_ = new AudioOutputDispatcher(&manager(), params, close_delay_ms);
88 90
89 // Necessary to know how long the dispatcher will wait before posting 91 // Necessary to know how long the dispatcher will wait before posting
90 // StopStreamTask. 92 // StopStreamTask.
91 pause_delay_milliseconds_ = dispatcher_->pause_delay_milliseconds_; 93 pause_delay_milliseconds_ = dispatcher_->pause_delay_milliseconds_;
92 } 94 }
93 95
96 MockAudioManager& manager() {
97 return *manager_.get();
scherkus (not reviewing) 2011/12/09 22:47:30 nit: get not required
tommi (sloooow) - chröme 2011/12/10 00:11:14 Done.
98 }
99
94 MessageLoop message_loop_; 100 MessageLoop message_loop_;
95 scoped_refptr<AudioOutputDispatcher> dispatcher_; 101 scoped_refptr<AudioOutputDispatcher> dispatcher_;
96 int64 pause_delay_milliseconds_; 102 int64 pause_delay_milliseconds_;
97 MockAudioManager manager_; 103 scoped_refptr<MockAudioManager> manager_;
98 MockAudioSourceCallback callback_; 104 MockAudioSourceCallback callback_;
99 }; 105 };
100 106
101 TEST_F(AudioOutputProxyTest, CreateAndClose) { 107 TEST_F(AudioOutputProxyTest, CreateAndClose) {
102 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); 108 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_);
103 proxy->Close(); 109 proxy->Close();
104 } 110 }
105 111
106 TEST_F(AudioOutputProxyTest, OpenAndClose) { 112 TEST_F(AudioOutputProxyTest, OpenAndClose) {
107 MockAudioOutputStream stream; 113 MockAudioOutputStream stream;
108 114
109 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 115 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
110 .WillOnce(Return(&stream)); 116 .WillOnce(Return(&stream));
111 EXPECT_CALL(stream, Open()) 117 EXPECT_CALL(stream, Open())
112 .WillOnce(Return(true)); 118 .WillOnce(Return(true));
113 EXPECT_CALL(stream, Close()) 119 EXPECT_CALL(stream, Close())
114 .Times(1); 120 .Times(1);
115 121
116 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); 122 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_);
117 EXPECT_TRUE(proxy->Open()); 123 EXPECT_TRUE(proxy->Open());
118 proxy->Close(); 124 proxy->Close();
119 } 125 }
120 126
121 // Create a stream, and verify that it is closed after kTestCloseDelayMs. 127 // Create a stream, and verify that it is closed after kTestCloseDelayMs.
122 // if it doesn't start playing. 128 // if it doesn't start playing.
123 TEST_F(AudioOutputProxyTest, CreateAndWait) { 129 TEST_F(AudioOutputProxyTest, CreateAndWait) {
124 MockAudioOutputStream stream; 130 MockAudioOutputStream stream;
125 131
126 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 132 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
127 .WillOnce(Return(&stream)); 133 .WillOnce(Return(&stream));
128 EXPECT_CALL(stream, Open()) 134 EXPECT_CALL(stream, Open())
129 .WillOnce(Return(true)); 135 .WillOnce(Return(true));
130 EXPECT_CALL(stream, Close()) 136 EXPECT_CALL(stream, Close())
131 .Times(1); 137 .Times(1);
132 138
133 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); 139 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_);
134 EXPECT_TRUE(proxy->Open()); 140 EXPECT_TRUE(proxy->Open());
135 141
136 // Simulate a delay. 142 // Simulate a delay.
137 base::PlatformThread::Sleep(kTestCloseDelayMs * 2); 143 base::PlatformThread::Sleep(kTestCloseDelayMs * 2);
138 message_loop_.RunAllPending(); 144 message_loop_.RunAllPending();
139 145
140 // Verify expectation before calling Close(). 146 // Verify expectation before calling Close().
141 Mock::VerifyAndClear(&stream); 147 Mock::VerifyAndClear(&stream);
142 148
143 proxy->Close(); 149 proxy->Close();
144 } 150 }
145 151
146 // Create a stream, and then calls Start() and Stop(). 152 // Create a stream, and then calls Start() and Stop().
147 TEST_F(AudioOutputProxyTest, StartAndStop) { 153 TEST_F(AudioOutputProxyTest, StartAndStop) {
148 MockAudioOutputStream stream; 154 MockAudioOutputStream stream;
149 155
150 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 156 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
151 .WillOnce(Return(&stream)); 157 .WillOnce(Return(&stream));
152 EXPECT_CALL(stream, Open()) 158 EXPECT_CALL(stream, Open())
153 .WillOnce(Return(true)); 159 .WillOnce(Return(true));
154 EXPECT_CALL(stream, Start(_)) 160 EXPECT_CALL(stream, Start(_))
155 .Times(1); 161 .Times(1);
156 EXPECT_CALL(stream, SetVolume(_)) 162 EXPECT_CALL(stream, SetVolume(_))
157 .Times(1); 163 .Times(1);
158 EXPECT_CALL(stream, Stop()) 164 EXPECT_CALL(stream, Stop())
159 .Times(1); 165 .Times(1);
160 EXPECT_CALL(stream, Close()) 166 EXPECT_CALL(stream, Close())
161 .Times(1); 167 .Times(1);
162 168
163 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); 169 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_);
164 EXPECT_TRUE(proxy->Open()); 170 EXPECT_TRUE(proxy->Open());
165 171
166 proxy->Start(&callback_); 172 proxy->Start(&callback_);
167 proxy->Stop(); 173 proxy->Stop();
168 174
169 proxy->Close(); 175 proxy->Close();
170 } 176 }
171 177
172 // Verify that the stream is closed after Stop is called. 178 // Verify that the stream is closed after Stop is called.
173 TEST_F(AudioOutputProxyTest, CloseAfterStop) { 179 TEST_F(AudioOutputProxyTest, CloseAfterStop) {
174 MockAudioOutputStream stream; 180 MockAudioOutputStream stream;
175 181
176 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 182 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
177 .WillOnce(Return(&stream)); 183 .WillOnce(Return(&stream));
178 EXPECT_CALL(stream, Open()) 184 EXPECT_CALL(stream, Open())
179 .WillOnce(Return(true)); 185 .WillOnce(Return(true));
180 EXPECT_CALL(stream, Start(_)) 186 EXPECT_CALL(stream, Start(_))
181 .Times(1); 187 .Times(1);
182 EXPECT_CALL(stream, SetVolume(_)) 188 EXPECT_CALL(stream, SetVolume(_))
183 .Times(1); 189 .Times(1);
184 EXPECT_CALL(stream, Stop()) 190 EXPECT_CALL(stream, Stop())
185 .Times(1); 191 .Times(1);
186 EXPECT_CALL(stream, Close()) 192 EXPECT_CALL(stream, Close())
(...skipping 16 matching lines...) Expand all
203 // Verify expectation before calling Close(). 209 // Verify expectation before calling Close().
204 Mock::VerifyAndClear(&stream); 210 Mock::VerifyAndClear(&stream);
205 211
206 proxy->Close(); 212 proxy->Close();
207 } 213 }
208 214
209 // Create two streams, but don't start them. Only one device must be open. 215 // Create two streams, but don't start them. Only one device must be open.
210 TEST_F(AudioOutputProxyTest, TwoStreams) { 216 TEST_F(AudioOutputProxyTest, TwoStreams) {
211 MockAudioOutputStream stream; 217 MockAudioOutputStream stream;
212 218
213 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 219 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
214 .WillOnce(Return(&stream)); 220 .WillOnce(Return(&stream));
215 EXPECT_CALL(stream, Open()) 221 EXPECT_CALL(stream, Open())
216 .WillOnce(Return(true)); 222 .WillOnce(Return(true));
217 EXPECT_CALL(stream, Close()) 223 EXPECT_CALL(stream, Close())
218 .Times(1); 224 .Times(1);
219 225
220 AudioOutputProxy* proxy1 = new AudioOutputProxy(dispatcher_); 226 AudioOutputProxy* proxy1 = new AudioOutputProxy(dispatcher_);
221 AudioOutputProxy* proxy2 = new AudioOutputProxy(dispatcher_); 227 AudioOutputProxy* proxy2 = new AudioOutputProxy(dispatcher_);
222 EXPECT_TRUE(proxy1->Open()); 228 EXPECT_TRUE(proxy1->Open());
223 EXPECT_TRUE(proxy2->Open()); 229 EXPECT_TRUE(proxy2->Open());
224 proxy1->Close(); 230 proxy1->Close();
225 proxy2->Close(); 231 proxy2->Close();
226 } 232 }
227 233
228 // Two streams: verify that second stream is allocated when the first 234 // Two streams: verify that second stream is allocated when the first
229 // starts playing. 235 // starts playing.
230 TEST_F(AudioOutputProxyTest, TwoStreams_OnePlaying) { 236 TEST_F(AudioOutputProxyTest, TwoStreams_OnePlaying) {
231 MockAudioOutputStream stream1; 237 MockAudioOutputStream stream1;
232 MockAudioOutputStream stream2; 238 MockAudioOutputStream stream2;
233 239
234 InitDispatcher(kTestBigCloseDelayMs); 240 InitDispatcher(kTestBigCloseDelayMs);
235 241
236 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 242 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
237 .WillOnce(Return(&stream1)) 243 .WillOnce(Return(&stream1))
238 .WillOnce(Return(&stream2)); 244 .WillOnce(Return(&stream2));
239 245
240 EXPECT_CALL(stream1, Open()) 246 EXPECT_CALL(stream1, Open())
241 .WillOnce(Return(true)); 247 .WillOnce(Return(true));
242 EXPECT_CALL(stream1, Start(_)) 248 EXPECT_CALL(stream1, Start(_))
243 .Times(1); 249 .Times(1);
244 EXPECT_CALL(stream1, SetVolume(_)) 250 EXPECT_CALL(stream1, SetVolume(_))
245 .Times(1); 251 .Times(1);
246 EXPECT_CALL(stream1, Stop()) 252 EXPECT_CALL(stream1, Stop())
(...skipping 19 matching lines...) Expand all
266 proxy2->Close(); 272 proxy2->Close();
267 } 273 }
268 274
269 // Two streams, both are playing. Dispatcher should not open a third stream. 275 // Two streams, both are playing. Dispatcher should not open a third stream.
270 TEST_F(AudioOutputProxyTest, TwoStreams_BothPlaying) { 276 TEST_F(AudioOutputProxyTest, TwoStreams_BothPlaying) {
271 MockAudioOutputStream stream1; 277 MockAudioOutputStream stream1;
272 MockAudioOutputStream stream2; 278 MockAudioOutputStream stream2;
273 279
274 InitDispatcher(kTestBigCloseDelayMs); 280 InitDispatcher(kTestBigCloseDelayMs);
275 281
276 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 282 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
277 .WillOnce(Return(&stream1)) 283 .WillOnce(Return(&stream1))
278 .WillOnce(Return(&stream2)); 284 .WillOnce(Return(&stream2));
279 285
280 EXPECT_CALL(stream1, Open()) 286 EXPECT_CALL(stream1, Open())
281 .WillOnce(Return(true)); 287 .WillOnce(Return(true));
282 EXPECT_CALL(stream1, Start(_)) 288 EXPECT_CALL(stream1, Start(_))
283 .Times(1); 289 .Times(1);
284 EXPECT_CALL(stream1, SetVolume(_)) 290 EXPECT_CALL(stream1, SetVolume(_))
285 .Times(1); 291 .Times(1);
286 EXPECT_CALL(stream1, Stop()) 292 EXPECT_CALL(stream1, Stop())
(...skipping 23 matching lines...) Expand all
310 proxy2->Stop(); 316 proxy2->Stop();
311 317
312 proxy1->Close(); 318 proxy1->Close();
313 proxy2->Close(); 319 proxy2->Close();
314 } 320 }
315 321
316 // Open() method failed. 322 // Open() method failed.
317 TEST_F(AudioOutputProxyTest, OpenFailed) { 323 TEST_F(AudioOutputProxyTest, OpenFailed) {
318 MockAudioOutputStream stream; 324 MockAudioOutputStream stream;
319 325
320 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 326 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
321 .WillOnce(Return(&stream)); 327 .WillOnce(Return(&stream));
322 EXPECT_CALL(stream, Open()) 328 EXPECT_CALL(stream, Open())
323 .WillOnce(Return(false)); 329 .WillOnce(Return(false));
324 EXPECT_CALL(stream, Close()) 330 EXPECT_CALL(stream, Close())
325 .Times(1); 331 .Times(1);
326 332
327 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); 333 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_);
328 EXPECT_FALSE(proxy->Open()); 334 EXPECT_FALSE(proxy->Open());
329 proxy->Close(); 335 proxy->Close();
330 } 336 }
331 337
332 // Start() method failed. 338 // Start() method failed.
333 TEST_F(AudioOutputProxyTest, StartFailed) { 339 TEST_F(AudioOutputProxyTest, StartFailed) {
334 MockAudioOutputStream stream; 340 MockAudioOutputStream stream;
335 341
336 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 342 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
337 .WillOnce(Return(&stream)); 343 .WillOnce(Return(&stream));
338 EXPECT_CALL(stream, Open()) 344 EXPECT_CALL(stream, Open())
339 .WillOnce(Return(true)); 345 .WillOnce(Return(true));
340 EXPECT_CALL(stream, Close()) 346 EXPECT_CALL(stream, Close())
341 .Times(1); 347 .Times(1);
342 348
343 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_); 349 AudioOutputProxy* proxy = new AudioOutputProxy(dispatcher_);
344 EXPECT_TRUE(proxy->Open()); 350 EXPECT_TRUE(proxy->Open());
345 351
346 // Simulate a delay. 352 // Simulate a delay.
347 base::PlatformThread::Sleep(kTestCloseDelayMs * 2); 353 base::PlatformThread::Sleep(kTestCloseDelayMs * 2);
348 message_loop_.RunAllPending(); 354 message_loop_.RunAllPending();
349 355
350 // Verify expectation before calling Close(). 356 // Verify expectation before calling Close().
351 Mock::VerifyAndClear(&stream); 357 Mock::VerifyAndClear(&stream);
352 358
353 // |stream| is closed at this point. Start() should reopen it again. 359 // |stream| is closed at this point. Start() should reopen it again.
354 EXPECT_CALL(manager_, MakeAudioOutputStream(_)) 360 EXPECT_CALL(manager(), MakeAudioOutputStream(_))
355 .WillOnce(Return(reinterpret_cast<AudioOutputStream*>(NULL))); 361 .WillOnce(Return(reinterpret_cast<AudioOutputStream*>(NULL)));
356 362
357 EXPECT_CALL(callback_, OnError(_, _)) 363 EXPECT_CALL(callback_, OnError(_, _))
358 .Times(1); 364 .Times(1);
359 365
360 proxy->Start(&callback_); 366 proxy->Start(&callback_);
361 367
362 Mock::VerifyAndClear(&callback_); 368 Mock::VerifyAndClear(&callback_);
363 369
364 proxy->Close(); 370 proxy->Close();
365 } 371 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698