OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_system_impl.h" | 5 #include "media/audio/audio_system_impl.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
9 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "media/audio/audio_device_description.h" | 13 #include "media/audio/audio_device_description.h" |
14 #include "media/audio/mock_audio_manager.h" | 14 #include "media/audio/mock_audio_manager.h" |
15 #include "media/base/test_helpers.h" | 15 #include "media/base/test_helpers.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
| 19 namespace { |
| 20 const char* kNonDefaultDeviceId = "non-default-device-id"; |
| 21 } |
19 namespace media { | 22 namespace media { |
20 | 23 |
21 class AudioSystemImplTest : public testing::TestWithParam<bool> { | 24 class AudioSystemImplTest : public testing::TestWithParam<bool> { |
22 public: | 25 public: |
23 AudioSystemImplTest() | 26 AudioSystemImplTest() |
24 : use_audio_thread_(GetParam()), audio_thread_("AudioSystemThread") { | 27 : use_audio_thread_(GetParam()), |
| 28 audio_thread_("AudioSystemThread"), |
| 29 input_params_(AudioParameters::AUDIO_PCM_LINEAR, |
| 30 CHANNEL_LAYOUT_MONO, |
| 31 AudioParameters::kTelephoneSampleRate, |
| 32 16, |
| 33 AudioParameters::kTelephoneSampleRate / 10), |
| 34 output_params_(AudioParameters::AUDIO_PCM_LINEAR, |
| 35 CHANNEL_LAYOUT_MONO, |
| 36 AudioParameters::kTelephoneSampleRate, |
| 37 16, |
| 38 AudioParameters::kTelephoneSampleRate / 20), |
| 39 default_output_params_(AudioParameters::AUDIO_PCM_LINEAR, |
| 40 CHANNEL_LAYOUT_MONO, |
| 41 AudioParameters::kTelephoneSampleRate, |
| 42 16, |
| 43 AudioParameters::kTelephoneSampleRate / 30) { |
25 if (use_audio_thread_) { | 44 if (use_audio_thread_) { |
26 audio_thread_.StartAndWaitForTesting(); | 45 audio_thread_.StartAndWaitForTesting(); |
27 audio_manager_.reset( | 46 audio_manager_.reset( |
28 new media::MockAudioManager(audio_thread_.task_runner())); | 47 new media::MockAudioManager(audio_thread_.task_runner())); |
29 } else { | 48 } else { |
30 audio_manager_.reset(new media::MockAudioManager( | 49 audio_manager_.reset(new media::MockAudioManager( |
31 base::ThreadTaskRunnerHandle::Get().get())); | 50 base::ThreadTaskRunnerHandle::Get().get())); |
32 } | 51 } |
33 audio_manager_->SetInputStreamParameters( | 52 |
34 media::AudioParameters::UnavailableDeviceParams()); | 53 audio_manager_->SetInputStreamParameters(input_params_); |
| 54 audio_manager_->SetOutputStreamParameters(output_params_); |
| 55 audio_manager_->SetDefaultOutputStreamParameters(default_output_params_); |
| 56 |
35 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); | 57 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); |
36 EXPECT_EQ(AudioSystem::Get(), audio_system_.get()); | 58 EXPECT_EQ(AudioSystem::Get(), audio_system_.get()); |
37 } | 59 } |
38 | 60 |
39 ~AudioSystemImplTest() override { | 61 ~AudioSystemImplTest() override { |
40 // Deleting |audio_manager_| on its thread. | 62 // Deleting |audio_manager_| on its thread. |
41 audio_system_.reset(); | 63 audio_system_.reset(); |
42 EXPECT_EQ(AudioSystem::Get(), nullptr); | 64 EXPECT_EQ(AudioSystem::Get(), nullptr); |
43 audio_manager_.reset(); | 65 audio_manager_.reset(); |
44 audio_thread_.Stop(); | 66 audio_thread_.Stop(); |
45 } | 67 } |
46 | 68 |
47 void OnAudioParams(const AudioParameters& expected, | 69 void OnAudioParams(const AudioParameters& expected, |
48 const AudioParameters& received) { | 70 const AudioParameters& received) { |
49 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | 71 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
50 EXPECT_EQ(expected.AsHumanReadableString(), | 72 EXPECT_EQ(expected.AsHumanReadableString(), |
51 received.AsHumanReadableString()); | 73 received.AsHumanReadableString()); |
52 AudioParametersReceived(); | 74 AudioParametersReceived(); |
53 } | 75 } |
54 | 76 |
| 77 void OnHasInputDevices(bool result) { |
| 78 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
| 79 HasInputDevicesCallback(result); |
| 80 } |
| 81 |
55 void WaitForCallback() { | 82 void WaitForCallback() { |
56 if (!use_audio_thread_) { | 83 if (!use_audio_thread_) { |
57 base::RunLoop().RunUntilIdle(); | 84 base::RunLoop().RunUntilIdle(); |
58 return; | 85 return; |
59 } | 86 } |
60 media::WaitableMessageLoopEvent event; | 87 WaitableMessageLoopEvent event; |
61 audio_thread_.task_runner()->PostTaskAndReply( | 88 audio_thread_.task_runner()->PostTaskAndReply( |
62 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); | 89 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); |
63 // Runs the loop and waits for the |audio_thread_| to call event's closure, | 90 // Runs the loop and waits for the |audio_thread_| to call event's closure, |
64 // which means AudioSystem reply containing device parameters is already | 91 // which means AudioSystem reply containing device parameters is already |
65 // queued on the main thread. | 92 // queued on the main thread. |
66 event.RunAndWait(); | 93 event.RunAndWait(); |
67 base::RunLoop().RunUntilIdle(); | 94 base::RunLoop().RunUntilIdle(); |
68 } | 95 } |
69 | 96 |
70 MOCK_METHOD0(AudioParametersReceived, void(void)); | 97 MOCK_METHOD0(AudioParametersReceived, void(void)); |
71 MOCK_METHOD1(HasInputDevicesCallback, void(bool)); | 98 MOCK_METHOD1(HasInputDevicesCallback, void(bool)); |
72 | 99 |
73 protected: | 100 protected: |
74 base::MessageLoop message_loop_; | 101 base::MessageLoop message_loop_; |
75 base::ThreadChecker thread_checker_; | 102 base::ThreadChecker thread_checker_; |
76 bool use_audio_thread_; | 103 bool use_audio_thread_; |
77 base::Thread audio_thread_; | 104 base::Thread audio_thread_; |
78 MockAudioManager::UniquePtr audio_manager_; | 105 MockAudioManager::UniquePtr audio_manager_; |
79 std::unique_ptr<media::AudioSystem> audio_system_; | 106 std::unique_ptr<media::AudioSystem> audio_system_; |
| 107 AudioParameters input_params_; |
| 108 AudioParameters output_params_; |
| 109 AudioParameters default_output_params_; |
80 }; | 110 }; |
81 | 111 |
82 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { | 112 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { |
83 EXPECT_CALL(*this, AudioParametersReceived()); | 113 EXPECT_CALL(*this, AudioParametersReceived()); |
84 audio_system_->GetInputStreamParameters( | 114 audio_system_->GetInputStreamParameters( |
85 media::AudioDeviceDescription::kDefaultDeviceId, | 115 media::AudioDeviceDescription::kDefaultDeviceId, |
86 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), | 116 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
87 media::AudioParameters::UnavailableDeviceParams())); | 117 input_params_)); |
88 WaitForCallback(); | 118 WaitForCallback(); |
89 } | 119 } |
90 | 120 |
91 TEST_P(AudioSystemImplTest, GetInputStreamParametersNoDevice) { | 121 TEST_P(AudioSystemImplTest, GetInputStreamParametersNoDevice) { |
92 audio_manager_->SetHasInputDevices(false); | 122 audio_manager_->SetHasInputDevices(false); |
93 EXPECT_CALL(*this, AudioParametersReceived()); | 123 EXPECT_CALL(*this, AudioParametersReceived()); |
94 audio_system_->GetInputStreamParameters( | 124 audio_system_->GetInputStreamParameters( |
95 media::AudioDeviceDescription::kDefaultDeviceId, | 125 media::AudioDeviceDescription::kDefaultDeviceId, |
96 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), | 126 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
97 media::AudioParameters())); | 127 media::AudioParameters())); |
98 WaitForCallback(); | 128 WaitForCallback(); |
99 } | 129 } |
100 | 130 |
| 131 TEST_P(AudioSystemImplTest, GetStreamParameters) { |
| 132 EXPECT_CALL(*this, AudioParametersReceived()); |
| 133 audio_system_->GetOutputStreamParameters( |
| 134 kNonDefaultDeviceId, base::Bind(&AudioSystemImplTest::OnAudioParams, |
| 135 base::Unretained(this), output_params_)); |
| 136 WaitForCallback(); |
| 137 } |
| 138 |
| 139 TEST_P(AudioSystemImplTest, GetDefaultOutputStreamParameters) { |
| 140 EXPECT_CALL(*this, AudioParametersReceived()); |
| 141 audio_system_->GetOutputStreamParameters( |
| 142 media::AudioDeviceDescription::kDefaultDeviceId, |
| 143 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
| 144 default_output_params_)); |
| 145 WaitForCallback(); |
| 146 } |
| 147 |
| 148 TEST_P(AudioSystemImplTest, GetOutputStreamParametersNoDevice) { |
| 149 audio_manager_->SetHasOutputDevices(false); |
| 150 EXPECT_CALL(*this, AudioParametersReceived()).Times(2); |
| 151 |
| 152 audio_system_->GetOutputStreamParameters( |
| 153 media::AudioDeviceDescription::kDefaultDeviceId, |
| 154 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
| 155 media::AudioParameters())); |
| 156 WaitForCallback(); |
| 157 |
| 158 audio_system_->GetOutputStreamParameters( |
| 159 kNonDefaultDeviceId, |
| 160 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
| 161 media::AudioParameters())); |
| 162 WaitForCallback(); |
| 163 } |
| 164 |
101 TEST_P(AudioSystemImplTest, HasInputDevices) { | 165 TEST_P(AudioSystemImplTest, HasInputDevices) { |
102 EXPECT_CALL(*this, HasInputDevicesCallback(true)); | 166 EXPECT_CALL(*this, HasInputDevicesCallback(true)); |
103 audio_system_->HasInputDevices(base::Bind( | 167 audio_system_->HasInputDevices(base::Bind( |
104 &AudioSystemImplTest::HasInputDevicesCallback, base::Unretained(this))); | 168 &AudioSystemImplTest::OnHasInputDevices, base::Unretained(this))); |
105 WaitForCallback(); | 169 WaitForCallback(); |
106 } | 170 } |
107 | 171 |
108 TEST_P(AudioSystemImplTest, HasNoInputDevices) { | 172 TEST_P(AudioSystemImplTest, HasNoInputDevices) { |
109 audio_manager_->SetHasInputDevices(false); | 173 audio_manager_->SetHasInputDevices(false); |
110 EXPECT_CALL(*this, HasInputDevicesCallback(false)); | 174 EXPECT_CALL(*this, HasInputDevicesCallback(false)); |
111 audio_system_->HasInputDevices(base::Bind( | 175 audio_system_->HasInputDevices(base::Bind( |
112 &AudioSystemImplTest::HasInputDevicesCallback, base::Unretained(this))); | 176 &AudioSystemImplTest::OnHasInputDevices, base::Unretained(this))); |
113 WaitForCallback(); | 177 WaitForCallback(); |
114 } | 178 } |
115 | 179 |
116 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); | 180 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); |
117 | 181 |
118 } // namespace media | 182 } // namespace media |
OLD | NEW |