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_debug_recording_manager.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/test/test_message_loop.h" |
| 15 #include "media/audio/audio_debug_recording_helper.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using testing::_; |
| 20 |
| 21 #if defined(OS_WIN) |
| 22 #define IntToStringType base::IntToString16 |
| 23 #else |
| 24 #define IntToStringType base::IntToString |
| 25 #endif |
| 26 |
| 27 namespace media { |
| 28 |
| 29 namespace { |
| 30 |
| 31 // The filename extension expected to be added. |
| 32 const base::FilePath::CharType kFileNameExtension[] = |
| 33 FILE_PATH_LITERAL("extension"); |
| 34 |
| 35 // Used to be able to set call expectations in the MockAudioDebugRecordingHelper |
| 36 // ctor. See also comment on the test EnableRegisterDisable. |
| 37 bool g_expect_enable_after_create_helper = false; |
| 38 |
| 39 // A helper struct to be able to set and unset |
| 40 // |g_expect_enable_after_create_helper| scoped. |
| 41 struct ScopedExpectEnableAfterCreateHelper { |
| 42 ScopedExpectEnableAfterCreateHelper() { |
| 43 CHECK(!g_expect_enable_after_create_helper); |
| 44 g_expect_enable_after_create_helper = true; |
| 45 } |
| 46 ~ScopedExpectEnableAfterCreateHelper() { |
| 47 CHECK(g_expect_enable_after_create_helper); |
| 48 g_expect_enable_after_create_helper = false; |
| 49 } |
| 50 }; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 // Mock class to verify enable and disable calls. |
| 55 class MockAudioDebugRecordingHelper : public AudioDebugRecordingHelper { |
| 56 public: |
| 57 MockAudioDebugRecordingHelper( |
| 58 const AudioParameters& params, |
| 59 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 60 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 61 base::OnceClosure on_destruction_closure) |
| 62 : AudioDebugRecordingHelper(params, |
| 63 std::move(task_runner), |
| 64 std::move(file_task_runner), |
| 65 base::OnceClosure()), |
| 66 on_destruction_closure_in_mock_(std::move(on_destruction_closure)) { |
| 67 if (g_expect_enable_after_create_helper) |
| 68 EXPECT_CALL(*this, EnableDebugRecording(_)); |
| 69 } |
| 70 |
| 71 ~MockAudioDebugRecordingHelper() override { |
| 72 if (on_destruction_closure_in_mock_) |
| 73 std::move(on_destruction_closure_in_mock_).Run(); |
| 74 } |
| 75 |
| 76 MOCK_METHOD1(EnableDebugRecording, void(const base::FilePath&)); |
| 77 MOCK_METHOD0(DisableDebugRecording, void()); |
| 78 |
| 79 private: |
| 80 // We let the mock run the destruction closure to not rely on the real |
| 81 // implementation. |
| 82 base::OnceClosure on_destruction_closure_in_mock_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(MockAudioDebugRecordingHelper); |
| 85 }; |
| 86 |
| 87 // Sub-class of the manager that overrides the CreateAudioDebugRecordingHelper |
| 88 // function to create the above mock instead. |
| 89 class AudioDebugRecordingManagerUnderTest : public AudioDebugRecordingManager { |
| 90 public: |
| 91 AudioDebugRecordingManagerUnderTest( |
| 92 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 93 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
| 94 : AudioDebugRecordingManager(std::move(task_runner), |
| 95 std::move(file_task_runner)) {} |
| 96 ~AudioDebugRecordingManagerUnderTest() override {} |
| 97 |
| 98 private: |
| 99 std::unique_ptr<AudioDebugRecordingHelper> CreateAudioDebugRecordingHelper( |
| 100 const AudioParameters& params, |
| 101 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 102 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 103 base::OnceClosure on_destruction_closure) override { |
| 104 return base::MakeUnique<MockAudioDebugRecordingHelper>( |
| 105 params, std::move(task_runner), std::move(file_task_runner), |
| 106 std::move(on_destruction_closure)); |
| 107 } |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingManagerUnderTest); |
| 110 }; |
| 111 |
| 112 // The test fixture. |
| 113 class AudioDebugRecordingManagerTest : public ::testing::Test { |
| 114 public: |
| 115 AudioDebugRecordingManagerTest() |
| 116 : manager_(message_loop_.task_runner(), message_loop_.task_runner()), |
| 117 base_file_path_(base::FilePath::FromUTF8Unsafe("base_path")) {} |
| 118 |
| 119 ~AudioDebugRecordingManagerTest() override {} |
| 120 |
| 121 // Registers a source and increases counter for the expected next source id. |
| 122 std::unique_ptr<AudioDebugRecorder> RegisterDebugRecordingSource( |
| 123 const AudioParameters& params) { |
| 124 ++expected_next_source_id_; |
| 125 return manager_.RegisterDebugRecordingSource(kFileNameExtension, params); |
| 126 } |
| 127 |
| 128 private: |
| 129 // Must come before |manager_|, so that it's inialized before it. |
| 130 base::TestMessageLoop message_loop_; |
| 131 |
| 132 protected: |
| 133 AudioDebugRecordingManagerUnderTest manager_; |
| 134 base::FilePath base_file_path_; |
| 135 |
| 136 // The expected next source id the manager will assign. It's static since the |
| 137 // manager uses a global running id, thus doesn't restart at each |
| 138 // instantiation. |
| 139 static int expected_next_source_id_; |
| 140 |
| 141 private: |
| 142 DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingManagerTest); |
| 143 }; |
| 144 |
| 145 int AudioDebugRecordingManagerTest::expected_next_source_id_ = 1; |
| 146 |
| 147 // Shouldn't do anything but store the path, i.e. no calls to recorders. |
| 148 TEST_F(AudioDebugRecordingManagerTest, EnableDisable) { |
| 149 manager_.EnableDebugRecording(base_file_path_); |
| 150 manager_.DisableDebugRecording(); |
| 151 } |
| 152 |
| 153 // Tests registration and automatic unregistration on destruction of a recorder. |
| 154 // The unregistration relies on that the MockAudioDebugRecordingHelper runs the |
| 155 // |on_destruction_closure| given to it. |
| 156 TEST_F(AudioDebugRecordingManagerTest, RegisterAutomaticUnregisterAtDelete) { |
| 157 const AudioParameters params; |
| 158 std::vector<std::unique_ptr<AudioDebugRecorder>> recorders; |
| 159 recorders.push_back(RegisterDebugRecordingSource(params)); |
| 160 recorders.push_back(RegisterDebugRecordingSource(params)); |
| 161 recorders.push_back(RegisterDebugRecordingSource(params)); |
| 162 EXPECT_EQ(3ul, recorders.size()); |
| 163 EXPECT_EQ(recorders.size(), manager_.debug_recording_helpers_.size()); |
| 164 |
| 165 while (!recorders.empty()) { |
| 166 recorders.pop_back(); |
| 167 EXPECT_EQ(recorders.size(), manager_.debug_recording_helpers_.size()); |
| 168 } |
| 169 EXPECT_EQ(0ul, recorders.size()); |
| 170 } |
| 171 |
| 172 TEST_F(AudioDebugRecordingManagerTest, RegisterEnableDisable) { |
| 173 // Store away the extected id for the next source to use after registering all |
| 174 // sources. |
| 175 int expected_id = expected_next_source_id_; |
| 176 |
| 177 const AudioParameters params; |
| 178 std::vector<std::unique_ptr<AudioDebugRecorder>> recorders; |
| 179 recorders.push_back(RegisterDebugRecordingSource(params)); |
| 180 recorders.push_back(RegisterDebugRecordingSource(params)); |
| 181 recorders.push_back(RegisterDebugRecordingSource(params)); |
| 182 EXPECT_EQ(3ul, recorders.size()); |
| 183 EXPECT_EQ(recorders.size(), manager_.debug_recording_helpers_.size()); |
| 184 |
| 185 for (const auto& recorder : recorders) { |
| 186 MockAudioDebugRecordingHelper* mock_recording_helper = |
| 187 static_cast<MockAudioDebugRecordingHelper*>(recorder.get()); |
| 188 base::FilePath expected_file_path = |
| 189 base_file_path_.AddExtension(kFileNameExtension) |
| 190 .AddExtension(IntToStringType(expected_id++)); |
| 191 EXPECT_CALL(*mock_recording_helper, |
| 192 EnableDebugRecording(expected_file_path)); |
| 193 EXPECT_CALL(*mock_recording_helper, DisableDebugRecording()); |
| 194 } |
| 195 |
| 196 manager_.EnableDebugRecording(base_file_path_); |
| 197 manager_.DisableDebugRecording(); |
| 198 } |
| 199 |
| 200 // Test enabling first, then registering. This should call enable on the |
| 201 // recoders, but we can't set expectation for that since the mock object is |
| 202 // created and called enable upon in RegisterDebugRecordingSource(), then |
| 203 // returned. Instead expectation is set in the ctor of the mock by setting |
| 204 // |g_expect_enable_after_create_helper| to true here (by using the scoped |
| 205 // variable). |
| 206 TEST_F(AudioDebugRecordingManagerTest, EnableRegisterDisable) { |
| 207 ScopedExpectEnableAfterCreateHelper scoped_enable_after_create_helper; |
| 208 |
| 209 manager_.EnableDebugRecording(base_file_path_); |
| 210 |
| 211 const AudioParameters params; |
| 212 std::vector<std::unique_ptr<AudioDebugRecorder>> recorders; |
| 213 recorders.push_back(RegisterDebugRecordingSource(params)); |
| 214 recorders.push_back(RegisterDebugRecordingSource(params)); |
| 215 recorders.push_back(RegisterDebugRecordingSource(params)); |
| 216 EXPECT_EQ(3ul, recorders.size()); |
| 217 EXPECT_EQ(recorders.size(), manager_.debug_recording_helpers_.size()); |
| 218 |
| 219 for (const auto& recorder : recorders) { |
| 220 MockAudioDebugRecordingHelper* mock_recording_helper = |
| 221 static_cast<MockAudioDebugRecordingHelper*>(recorder.get()); |
| 222 EXPECT_CALL(*mock_recording_helper, DisableDebugRecording()); |
| 223 } |
| 224 |
| 225 manager_.DisableDebugRecording(); |
| 226 } |
| 227 |
| 228 } // namespace media |
OLD | NEW |