OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/audio/audio_system_impl.h" |
| 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/task_runner_util.h" |
| 10 #include "base/threading/thread.h" |
| 11 #include "base/threading/thread_checker.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "media/audio/audio_device_description.h" |
| 14 #include "media/audio/mock_audio_manager.h" |
| 15 #include "media/base/test_helpers.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace media { |
| 20 |
| 21 class AudioSystemImplTest : public testing::TestWithParam<bool> { |
| 22 public: |
| 23 AudioSystemImplTest() |
| 24 : use_audio_thread_(GetParam()), audio_thread_("AudioSystemThread") { |
| 25 if (use_audio_thread_) { |
| 26 audio_thread_.StartAndWaitForTesting(); |
| 27 audio_manager_.reset( |
| 28 new media::MockAudioManager(audio_thread_.task_runner())); |
| 29 } else { |
| 30 audio_manager_.reset(new media::MockAudioManager( |
| 31 base::ThreadTaskRunnerHandle::Get().get())); |
| 32 } |
| 33 audio_manager_->SetInputStreamParameters( |
| 34 media::AudioParameters::UnavailableDeviceParams()); |
| 35 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); |
| 36 } |
| 37 |
| 38 ~AudioSystemImplTest() override { |
| 39 // Deleting |audio_manager_| on its thread. |
| 40 audio_manager_.reset(); |
| 41 audio_thread_.Stop(); |
| 42 } |
| 43 |
| 44 void OnAudioParams(const AudioParameters& expected, |
| 45 const AudioParameters& received) { |
| 46 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
| 47 EXPECT_EQ(expected.AsHumanReadableString(), |
| 48 received.AsHumanReadableString()); |
| 49 AudioParametersReceived(); |
| 50 } |
| 51 |
| 52 void WaitForCallback() { |
| 53 if (!use_audio_thread_) { |
| 54 base::RunLoop().RunUntilIdle(); |
| 55 return; |
| 56 } |
| 57 media::WaitableMessageLoopEvent event; |
| 58 audio_thread_.task_runner()->PostTaskAndReply( |
| 59 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); |
| 60 // Runs the loop and waits for the |audio_thread_| to call event's closure, |
| 61 // which means AudioSystem reply containing device parameters is already |
| 62 // queued on the main thread. |
| 63 event.RunAndWait(); |
| 64 base::RunLoop().RunUntilIdle(); |
| 65 } |
| 66 |
| 67 MOCK_METHOD0(AudioParametersReceived, void(void)); |
| 68 |
| 69 protected: |
| 70 base::MessageLoop message_loop_; |
| 71 base::ThreadChecker thread_checker_; |
| 72 bool use_audio_thread_; |
| 73 base::Thread audio_thread_; |
| 74 MockAudioManager::UniquePtr audio_manager_; |
| 75 std::unique_ptr<media::AudioSystem> audio_system_; |
| 76 }; |
| 77 |
| 78 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { |
| 79 EXPECT_CALL(*this, AudioParametersReceived()); |
| 80 audio_system_->GetInputStreamParameters( |
| 81 media::AudioDeviceDescription::kDefaultDeviceId, |
| 82 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
| 83 media::AudioParameters::UnavailableDeviceParams())); |
| 84 WaitForCallback(); |
| 85 } |
| 86 |
| 87 TEST_P(AudioSystemImplTest, GetInputStreamParametersNoDevice) { |
| 88 audio_manager_->SetHasInputDevices(false); |
| 89 EXPECT_CALL(*this, AudioParametersReceived()); |
| 90 audio_system_->GetInputStreamParameters( |
| 91 media::AudioDeviceDescription::kDefaultDeviceId, |
| 92 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
| 93 media::AudioParameters())); |
| 94 WaitForCallback(); |
| 95 } |
| 96 |
| 97 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); |
| 98 |
| 99 } // namespace media |
OLD | NEW |