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

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

Issue 1897953003: Unmute Tab Audio For Desktop Share (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Unittest Created 4 years, 7 months 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "media/audio/audio_output_controller.h" 5 #include "media/audio/audio_output_controller.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/environment.h" 12 #include "base/environment.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/test/test_message_loop.h" 17 #include "base/test/test_message_loop.h"
18 #include "base/thread_task_runner_handle.h" 18 #include "base/thread_task_runner_handle.h"
19 #include "media/audio/audio_device_description.h" 19 #include "media/audio/audio_device_description.h"
20 #include "media/audio/audio_source_diverter.h"
20 #include "media/base/audio_bus.h" 21 #include "media/base/audio_bus.h"
21 #include "media/base/audio_parameters.h" 22 #include "media/base/audio_parameters.h"
22 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 25
25 using ::testing::_; 26 using ::testing::_;
26 using ::testing::AtLeast; 27 using ::testing::AtLeast;
27 using ::testing::DoAll; 28 using ::testing::DoAll;
28 using ::testing::Invoke; 29 using ::testing::Invoke;
29 using ::testing::NotNull; 30 using ::testing::NotNull;
30 using ::testing::Return; 31 using ::testing::Return;
31 32
32 namespace media { 33 namespace media {
33 34
34 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; 35 static const int kSampleRate = AudioParameters::kAudioCDSampleRate;
35 static const int kBitsPerSample = 16; 36 static const int kBitsPerSample = 16;
36 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; 37 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
37 static const int kSamplesPerPacket = kSampleRate / 100; 38 static const int kSamplesPerPacket = kSampleRate / 100;
38 static const double kTestVolume = 0.25; 39 static const double kTestVolume = 0.25;
40 static const float kBufferNonZeroData = 1.0f;
39 41
40 class MockAudioOutputControllerEventHandler 42 class MockAudioOutputControllerEventHandler
41 : public AudioOutputController::EventHandler { 43 : public AudioOutputController::EventHandler {
42 public: 44 public:
43 MockAudioOutputControllerEventHandler() {} 45 MockAudioOutputControllerEventHandler() {}
44 46
45 MOCK_METHOD0(OnCreated, void()); 47 MOCK_METHOD0(OnCreated, void());
46 MOCK_METHOD0(OnPlaying, void()); 48 MOCK_METHOD0(OnPlaying, void());
47 MOCK_METHOD0(OnPaused, void()); 49 MOCK_METHOD0(OnPaused, void());
48 MOCK_METHOD0(OnError, void()); 50 MOCK_METHOD0(OnError, void());
(...skipping 26 matching lines...) Expand all
75 MOCK_METHOD0(Close, void()); 77 MOCK_METHOD0(Close, void());
76 78
77 // Set/get the callback passed to Start(). 79 // Set/get the callback passed to Start().
78 AudioSourceCallback* callback() const { return callback_; } 80 AudioSourceCallback* callback() const { return callback_; }
79 void SetCallback(AudioSourceCallback* asc) { callback_ = asc; } 81 void SetCallback(AudioSourceCallback* asc) { callback_ = asc; }
80 82
81 private: 83 private:
82 AudioSourceCallback* callback_; 84 AudioSourceCallback* callback_;
83 }; 85 };
84 86
85 static const float kBufferNonZeroData = 1.0f; 87 class MockAudioPushSink : public AudioPushSink {
88 public:
89 MOCK_METHOD0(Close, void());
90 MOCK_METHOD1(OnDataCheck, void(float));
91
92 void OnData(const AudioBus& source, base::TimeTicks reference_time) override {
93 OnDataCheck(source.channel(0)[0]);
94 }
95 };
96
86 ACTION(PopulateBuffer) { 97 ACTION(PopulateBuffer) {
87 arg0->Zero(); 98 arg0->Zero();
88 // Note: To confirm the buffer will be populated in these tests, it's 99 // Note: To confirm the buffer will be populated in these tests, it's
89 // sufficient that only the first float in channel 0 is set to the value. 100 // sufficient that only the first float in channel 0 is set to the value.
90 arg0->channel(0)[0] = kBufferNonZeroData; 101 arg0->channel(0)[0] = kBufferNonZeroData;
91 } 102 }
92 103
93 class AudioOutputControllerTest : public testing::Test { 104 class AudioOutputControllerTest : public testing::Test {
94 public: 105 public:
95 AudioOutputControllerTest() 106 AudioOutputControllerTest()
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 .WillRepeatedly( 181 .WillRepeatedly(
171 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback)); 182 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback));
172 EXPECT_CALL(mock_stream_, Stop()) 183 EXPECT_CALL(mock_stream_, Stop())
173 .Times(num_times_to_be_started); 184 .Times(num_times_to_be_started);
174 } 185 }
175 186
176 controller_->StartDiverting(&mock_stream_); 187 controller_->StartDiverting(&mock_stream_);
177 base::RunLoop().RunUntilIdle(); 188 base::RunLoop().RunUntilIdle();
178 } 189 }
179 190
191 void Duplicate(MockAudioPushSink* sink) {
192 controller_->StartDuplicating(sink);
193 base::RunLoop().RunUntilIdle();
194 }
195
180 void ReadDivertedAudioData() { 196 void ReadDivertedAudioData() {
181 std::unique_ptr<AudioBus> dest = AudioBus::Create(params_); 197 std::unique_ptr<AudioBus> dest = AudioBus::Create(params_);
182 ASSERT_TRUE(mock_stream_.callback()); 198 ASSERT_TRUE(mock_stream_.callback());
183 const int frames_read = 199 const int frames_read =
184 mock_stream_.callback()->OnMoreData(dest.get(), 0, 0); 200 mock_stream_.callback()->OnMoreData(dest.get(), 0, 0);
185 EXPECT_LT(0, frames_read); 201 EXPECT_LT(0, frames_read);
186 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]); 202 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]);
187 } 203 }
188 204
205 void ReadDuplicatedAudioData(std::vector<MockAudioPushSink*> sinks) {
miu 2016/05/06 22:29:50 The argument type should be: const std::vector<...
qiangchen 2016/05/10 22:36:53 Done.
206 for (size_t i = 0; i < sinks.size(); i++) {
207 EXPECT_CALL(*sinks[i], OnDataCheck(kBufferNonZeroData));
208 }
209
210 std::unique_ptr<AudioBus> dest = AudioBus::Create(params_);
211
212 // It is this OnMoreData() call that triggers |sink|'s OnData().
213 const int frames_read = controller_->OnMoreData(dest.get(), 0, 0);
214
215 EXPECT_LT(0, frames_read);
216 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]);
217 }
218
189 void Revert(bool was_playing) { 219 void Revert(bool was_playing) {
190 if (was_playing) { 220 if (was_playing) {
191 // Expect the handler to receive one OnPlaying() call as a result of the 221 // Expect the handler to receive one OnPlaying() call as a result of the
192 // stream switching back. 222 // stream switching back.
193 EXPECT_CALL(mock_event_handler_, OnPlaying()); 223 EXPECT_CALL(mock_event_handler_, OnPlaying());
194 } 224 }
195 225
196 EXPECT_CALL(mock_stream_, Close()); 226 EXPECT_CALL(mock_stream_, Close());
197 227
198 controller_->StopDiverting(); 228 controller_->StopDiverting();
199 base::RunLoop().RunUntilIdle(); 229 base::RunLoop().RunUntilIdle();
200 } 230 }
201 231
232 void StopDuplicate(MockAudioPushSink* sink) {
miu 2016/05/06 22:29:50 naming nit: StopDuplicating
qiangchen 2016/05/10 22:36:53 Done.
233 EXPECT_CALL(*sink, Close());
234 controller_->StopDuplicating(sink);
235 base::RunLoop().RunUntilIdle();
236 }
237
202 void SwitchDevice(bool diverting) { 238 void SwitchDevice(bool diverting) {
203 if (!diverting) { 239 if (!diverting) {
204 // Expect the current stream to close and a new stream to start 240 // Expect the current stream to close and a new stream to start
205 // playing if not diverting. When diverting, nothing happens 241 // playing if not diverting. When diverting, nothing happens
206 // until diverting is stopped. 242 // until diverting is stopped.
207 EXPECT_CALL(mock_event_handler_, OnPlaying()); 243 EXPECT_CALL(mock_event_handler_, OnPlaying());
208 } 244 }
209 245
210 controller_->SwitchOutputDevice( 246 controller_->SwitchOutputDevice(
211 AudioDeviceDescription::GetDefaultDeviceName(), 247 AudioDeviceDescription::GetDefaultDeviceName(),
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 Close(); 365 Close();
330 } 366 }
331 367
332 TEST_F(AudioOutputControllerTest, DivertRevertClose) { 368 TEST_F(AudioOutputControllerTest, DivertRevertClose) {
333 Create(kSamplesPerPacket); 369 Create(kSamplesPerPacket);
334 DivertNeverPlaying(); 370 DivertNeverPlaying();
335 RevertWasNotPlaying(); 371 RevertWasNotPlaying();
336 Close(); 372 Close();
337 } 373 }
338 374
375 TEST_F(AudioOutputControllerTest, PlayDuplicateStopClose) {
miu 2016/05/06 22:29:50 Nice tests here too! :)
qiangchen 2016/05/10 22:36:53 Acknowledged.
376 Create(kSamplesPerPacket);
377 MockAudioPushSink mock_sink;
378 Play();
379 Duplicate(&mock_sink);
380 ReadDuplicatedAudioData({&mock_sink});
381 StopDuplicate(&mock_sink);
382 Close();
383 }
384
385 TEST_F(AudioOutputControllerTest, TwoDuplicates) {
386 Create(kSamplesPerPacket);
387 MockAudioPushSink mock_sink_1;
388 MockAudioPushSink mock_sink_2;
389 Play();
390 Duplicate(&mock_sink_1);
391 Duplicate(&mock_sink_2);
392 ReadDuplicatedAudioData({&mock_sink_1, &mock_sink_2});
393 StopDuplicate(&mock_sink_1);
394 StopDuplicate(&mock_sink_2);
395 Close();
396 }
397
398 TEST_F(AudioOutputControllerTest, DuplicateDivertInteract) {
399 Create(kSamplesPerPacket);
400 MockAudioPushSink mock_sink;
401 Play();
402 Duplicate(&mock_sink);
403 DivertWhilePlaying();
404
405 // When diverted stream pulls data, it would trigger a push to sink.
406 EXPECT_CALL(mock_sink, OnDataCheck(kBufferNonZeroData));
407 ReadDivertedAudioData();
408
409 StopDuplicate(&mock_sink);
410 RevertWhilePlaying();
411 Close();
412 }
413
414 TEST_F(AudioOutputControllerTest, DuplicateSwitchDeviceInteract) {
415 Create(kSamplesPerPacket);
416 MockAudioPushSink mock_sink;
417 Play();
418 Duplicate(&mock_sink);
419 ReadDuplicatedAudioData({&mock_sink});
420
421 // Switching device would trigger a read, and in turn it would trigger a push
422 // to sink.
423 EXPECT_CALL(mock_sink, OnDataCheck(kBufferNonZeroData));
424 SwitchDevice(false);
425
426 StopDuplicate(&mock_sink);
427 Close();
428 }
429
339 } // namespace media 430 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698