debug_recording_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2018 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 "services/audio/debug_recording.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/files/file_path.h"
  8. #include "base/test/task_environment.h"
  9. #include "media/audio/audio_debug_recording_test.h"
  10. #include "media/audio/mock_audio_debug_recording_manager.h"
  11. #include "media/audio/mock_audio_manager.h"
  12. #include "mojo/public/cpp/bindings/pending_receiver.h"
  13. #include "mojo/public/cpp/bindings/receiver.h"
  14. #include "mojo/public/cpp/bindings/remote.h"
  15. #include "services/audio/aecdump_recording_manager.h"
  16. #include "services/audio/public/cpp/debug_recording_session.h"
  17. #include "services/audio/public/mojom/debug_recording.mojom.h"
  18. #include "testing/gmock/include/gmock/gmock.h"
  19. using testing::_;
  20. using testing::Exactly;
  21. using testing::Sequence;
  22. namespace audio {
  23. namespace {
  24. const base::FilePath::CharType kBaseFileName[] =
  25. FILE_PATH_LITERAL("base_file_name");
  26. // Empty function bound and passed to DebugRecording::CreateWavFile and
  27. // DebugRecording::CreateAecdumpFile.
  28. void FileCreated(base::File file) {}
  29. } // namespace
  30. class MockFileProvider : public mojom::DebugRecordingFileProvider {
  31. public:
  32. MockFileProvider(
  33. mojo::PendingReceiver<mojom::DebugRecordingFileProvider> receiver,
  34. const base::FilePath& file_name_base)
  35. : receiver_(this, std::move(receiver)) {}
  36. MockFileProvider(const MockFileProvider&) = delete;
  37. MockFileProvider& operator=(const MockFileProvider&) = delete;
  38. MOCK_METHOD2(DoCreateWavFile,
  39. void(media::AudioDebugRecordingStreamType stream_type,
  40. uint32_t id));
  41. MOCK_METHOD1(DoCreateAecdumpFile, void(uint32_t id));
  42. void CreateWavFile(media::AudioDebugRecordingStreamType stream_type,
  43. uint32_t id,
  44. CreateWavFileCallback reply_callback) override {
  45. DoCreateWavFile(stream_type, id);
  46. std::move(reply_callback).Run(base::File());
  47. }
  48. void CreateAecdumpFile(uint32_t id,
  49. CreateAecdumpFileCallback reply_callback) override {
  50. DoCreateAecdumpFile(id);
  51. std::move(reply_callback).Run(base::File());
  52. }
  53. private:
  54. mojo::Receiver<mojom::DebugRecordingFileProvider> receiver_;
  55. };
  56. class MockAecdumpRecordingManager : public AecdumpRecordingManager {
  57. public:
  58. explicit MockAecdumpRecordingManager(
  59. scoped_refptr<base::SingleThreadTaskRunner> task_runner)
  60. : AecdumpRecordingManager(task_runner) {}
  61. MOCK_METHOD1(EnableDebugRecording, void(CreateFileCallback));
  62. MOCK_METHOD0(DisableDebugRecording, void());
  63. };
  64. class DebugRecordingTest : public media::AudioDebugRecordingTest {
  65. public:
  66. DebugRecordingTest() = default;
  67. DebugRecordingTest(const DebugRecordingTest&) = delete;
  68. DebugRecordingTest& operator=(const DebugRecordingTest&) = delete;
  69. ~DebugRecordingTest() override = default;
  70. void SetUp() override {
  71. CreateAudioManager();
  72. InitializeAudioDebugRecordingManager();
  73. mock_aecdump_recording_manager_ =
  74. std::make_unique<MockAecdumpRecordingManager>(
  75. mock_audio_manager_->GetTaskRunner());
  76. }
  77. void TearDown() override { ShutdownAudioManager(); }
  78. protected:
  79. void CreateDebugRecording() {
  80. if (remote_debug_recording_)
  81. remote_debug_recording_.reset();
  82. debug_recording_ = std::make_unique<DebugRecording>(
  83. remote_debug_recording_.BindNewPipeAndPassReceiver(),
  84. static_cast<media::AudioManager*>(mock_audio_manager_.get()),
  85. mock_aecdump_recording_manager_.get());
  86. }
  87. void EnableDebugRecording() {
  88. mojo::PendingRemote<mojom::DebugRecordingFileProvider> remote_file_provider;
  89. DebugRecordingSession::DebugRecordingFileProvider file_provider(
  90. remote_file_provider.InitWithNewPipeAndPassReceiver(),
  91. base::FilePath(kBaseFileName));
  92. remote_debug_recording_->Enable(std::move(remote_file_provider));
  93. }
  94. void DestroyDebugRecording() {
  95. remote_debug_recording_.reset();
  96. task_environment_.RunUntilIdle();
  97. }
  98. std::unique_ptr<MockAecdumpRecordingManager> mock_aecdump_recording_manager_;
  99. std::unique_ptr<DebugRecording> debug_recording_;
  100. mojo::Remote<mojom::DebugRecording> remote_debug_recording_;
  101. };
  102. TEST_F(DebugRecordingTest, EnableResetEnablesDisablesDebugRecording) {
  103. Sequence s1;
  104. EXPECT_CALL(*mock_debug_recording_manager_, EnableDebugRecording(_))
  105. .InSequence(s1);
  106. EXPECT_CALL(*mock_debug_recording_manager_, DisableDebugRecording())
  107. .InSequence(s1);
  108. Sequence s2;
  109. EXPECT_CALL(*mock_aecdump_recording_manager_, EnableDebugRecording(_))
  110. .InSequence(s2);
  111. EXPECT_CALL(*mock_aecdump_recording_manager_, DisableDebugRecording())
  112. .InSequence(s2);
  113. CreateDebugRecording();
  114. EnableDebugRecording();
  115. DestroyDebugRecording();
  116. }
  117. TEST_F(DebugRecordingTest, ResetWithoutEnableDoesNotDisableDebugRecording) {
  118. EXPECT_CALL(*mock_debug_recording_manager_, DisableDebugRecording()).Times(0);
  119. EXPECT_CALL(*mock_aecdump_recording_manager_, DisableDebugRecording())
  120. .Times(0);
  121. CreateDebugRecording();
  122. DestroyDebugRecording();
  123. }
  124. TEST_F(DebugRecordingTest, CreateFileCallsFileProviderCreateFile) {
  125. Sequence s1;
  126. EXPECT_CALL(*mock_debug_recording_manager_, EnableDebugRecording(_))
  127. .InSequence(s1);
  128. EXPECT_CALL(*mock_debug_recording_manager_, DisableDebugRecording())
  129. .InSequence(s1);
  130. Sequence s2;
  131. EXPECT_CALL(*mock_aecdump_recording_manager_, EnableDebugRecording(_))
  132. .InSequence(s2);
  133. EXPECT_CALL(*mock_aecdump_recording_manager_, DisableDebugRecording())
  134. .InSequence(s2);
  135. CreateDebugRecording();
  136. mojo::PendingRemote<mojom::DebugRecordingFileProvider> remote_file_provider;
  137. MockFileProvider mock_file_provider(
  138. remote_file_provider.InitWithNewPipeAndPassReceiver(),
  139. base::FilePath(kBaseFileName));
  140. remote_debug_recording_->Enable(std::move(remote_file_provider));
  141. task_environment_.RunUntilIdle();
  142. const int id = 1;
  143. EXPECT_CALL(
  144. mock_file_provider,
  145. DoCreateWavFile(media::AudioDebugRecordingStreamType::kInput, id));
  146. EXPECT_CALL(mock_file_provider, DoCreateAecdumpFile(id));
  147. debug_recording_->CreateWavFile(media::AudioDebugRecordingStreamType::kInput,
  148. id, base::BindOnce(&FileCreated));
  149. debug_recording_->CreateAecdumpFile(id, base::BindOnce(&FileCreated));
  150. task_environment_.RunUntilIdle();
  151. DestroyDebugRecording();
  152. }
  153. TEST_F(DebugRecordingTest, SequencialCreate) {
  154. CreateDebugRecording();
  155. DestroyDebugRecording();
  156. CreateDebugRecording();
  157. DestroyDebugRecording();
  158. }
  159. TEST_F(DebugRecordingTest, ConcurrentCreate) {
  160. CreateDebugRecording();
  161. CreateDebugRecording();
  162. DestroyDebugRecording();
  163. }
  164. } // namespace audio