cras_unified_unittest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2013 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. #include <stdint.h>
  5. #include <memory>
  6. #include <string>
  7. #include "base/run_loop.h"
  8. #include "base/synchronization/waitable_event.h"
  9. #include "base/test/test_message_loop.h"
  10. #include "base/test/test_timeouts.h"
  11. #include "base/threading/thread_task_runner_handle.h"
  12. #include "base/time/time.h"
  13. #include "chromeos/ash/components/audio/cras_audio_handler.h"
  14. #include "chromeos/ash/components/dbus/audio/cras_audio_client.h"
  15. #include "media/audio/audio_device_description.h"
  16. #include "media/audio/cras/audio_manager_chromeos.h"
  17. #include "media/audio/fake_audio_log_factory.h"
  18. #include "media/audio/mock_audio_source_callback.h"
  19. #include "media/audio/test_audio_thread.h"
  20. #include "testing/gmock/include/gmock/gmock.h"
  21. #include "testing/gtest/include/gtest/gtest.h"
  22. // cras_util.h defines custom min/max macros which break compilation, so ensure
  23. // it's not included until last. #if avoids presubmit errors.
  24. #if defined(USE_CRAS)
  25. #include "media/audio/cras/cras_unified.h"
  26. #endif
  27. using testing::_;
  28. using testing::DoAll;
  29. using testing::InvokeWithoutArgs;
  30. using testing::Return;
  31. using testing::SetArgPointee;
  32. using testing::StrictMock;
  33. namespace media {
  34. class MockAudioManagerCras : public AudioManagerChromeOS {
  35. public:
  36. MockAudioManagerCras()
  37. : AudioManagerChromeOS(std::make_unique<TestAudioThread>(),
  38. &fake_audio_log_factory_) {}
  39. // We need to override this function in order to skip the checking the number
  40. // of active output streams. It is because the number of active streams
  41. // is managed inside MakeAudioOutputStream, and we don't use
  42. // MakeAudioOutputStream to create the stream in the tests.
  43. void ReleaseOutputStream(AudioOutputStream* stream) override {
  44. DCHECK(stream);
  45. delete stream;
  46. }
  47. private:
  48. FakeAudioLogFactory fake_audio_log_factory_;
  49. };
  50. class CrasUnifiedStreamTest : public testing::Test {
  51. public:
  52. CrasUnifiedStreamTest(const CrasUnifiedStreamTest&) = delete;
  53. CrasUnifiedStreamTest& operator=(const CrasUnifiedStreamTest&) = delete;
  54. protected:
  55. CrasUnifiedStreamTest() {
  56. ash::CrasAudioClient::InitializeFake();
  57. ash::CrasAudioHandler::InitializeForTesting();
  58. mock_manager_.reset(new StrictMock<MockAudioManagerCras>());
  59. base::RunLoop().RunUntilIdle();
  60. }
  61. ~CrasUnifiedStreamTest() override {
  62. mock_manager_->Shutdown();
  63. ash::CrasAudioHandler::Shutdown();
  64. ash::CrasAudioClient::Shutdown();
  65. }
  66. CrasUnifiedStream* CreateStream(ChannelLayout layout) {
  67. return CreateStream(layout, kTestFramesPerPacket);
  68. }
  69. CrasUnifiedStream* CreateStream(ChannelLayout layout,
  70. int32_t samples_per_packet) {
  71. AudioParameters params(kTestFormat, layout, kTestSampleRate,
  72. samples_per_packet);
  73. return new CrasUnifiedStream(params, mock_manager_.get(),
  74. AudioDeviceDescription::kDefaultDeviceId);
  75. }
  76. MockAudioManagerCras& mock_manager() {
  77. return *(mock_manager_.get());
  78. }
  79. static const ChannelLayout kTestChannelLayout;
  80. static const int kTestSampleRate;
  81. static const AudioParameters::Format kTestFormat;
  82. static const uint32_t kTestFramesPerPacket;
  83. base::TestMessageLoop message_loop_;
  84. std::unique_ptr<StrictMock<MockAudioManagerCras>> mock_manager_;
  85. };
  86. const ChannelLayout CrasUnifiedStreamTest::kTestChannelLayout =
  87. CHANNEL_LAYOUT_STEREO;
  88. const int CrasUnifiedStreamTest::kTestSampleRate =
  89. AudioParameters::kAudioCDSampleRate;
  90. const AudioParameters::Format CrasUnifiedStreamTest::kTestFormat =
  91. AudioParameters::AUDIO_PCM_LINEAR;
  92. const uint32_t CrasUnifiedStreamTest::kTestFramesPerPacket = 1000;
  93. TEST_F(CrasUnifiedStreamTest, ConstructedState) {
  94. CrasUnifiedStream* test_stream = CreateStream(kTestChannelLayout);
  95. EXPECT_TRUE(test_stream->Open());
  96. test_stream->Close();
  97. // Should support mono.
  98. test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
  99. EXPECT_TRUE(test_stream->Open());
  100. test_stream->Close();
  101. // Should support multi-channel.
  102. test_stream = CreateStream(CHANNEL_LAYOUT_SURROUND);
  103. EXPECT_TRUE(test_stream->Open());
  104. test_stream->Close();
  105. // Bad sample rate.
  106. AudioParameters bad_rate_params(kTestFormat, kTestChannelLayout, 0,
  107. kTestFramesPerPacket);
  108. test_stream = new CrasUnifiedStream(bad_rate_params, mock_manager_.get(),
  109. AudioDeviceDescription::kDefaultDeviceId);
  110. EXPECT_FALSE(test_stream->Open());
  111. test_stream->Close();
  112. }
  113. TEST_F(CrasUnifiedStreamTest, RenderFrames) {
  114. CrasUnifiedStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
  115. MockAudioSourceCallback mock_callback;
  116. ASSERT_TRUE(test_stream->Open());
  117. base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
  118. base::WaitableEvent::InitialState::NOT_SIGNALED);
  119. EXPECT_CALL(mock_callback, OnMoreData(_, _, 0, _))
  120. .WillRepeatedly(
  121. DoAll(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal),
  122. Return(kTestFramesPerPacket)));
  123. test_stream->Start(&mock_callback);
  124. // Wait for samples to be captured.
  125. EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
  126. test_stream->Stop();
  127. test_stream->Close();
  128. }
  129. } // namespace media