Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: media/audio/audio_debug_recording_manager_unittest.cc

Issue 2582703003: Audio output debug recording. (Closed)
Patch Set: Code review (pfeldman@/dalecurtis@) and a fix. Rebase. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // This is a dummy passed as a callback that should never be called. (It would
53 // be called in AudioDebugRecordingHelper, but not in the mock for that class
54 // used in this test.)
55 std::unique_ptr<AudioFileWriter> CreateAudioFileWriterDummy(
56 const AudioParameters& params) {
57 NOTREACHED();
58 return nullptr;
59 }
60
61 } // namespace
62
63 // Mock class to verify enable and disable calls.
64 class MockAudioDebugRecordingHelper : public AudioDebugRecordingHelper {
65 public:
66 MockAudioDebugRecordingHelper(
67 const AudioParameters& params,
68 const AudioFileWriter::CreateCallback& create_audio_file_writer_callback,
69 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
70 base::OnceClosure on_destruction_closure)
71 : AudioDebugRecordingHelper(params,
72 create_audio_file_writer_callback,
73 std::move(task_runner),
74 base::OnceClosure()),
75 on_destruction_closure_in_mock_(std::move(on_destruction_closure)) {
76 if (g_expect_enable_after_create_helper)
77 EXPECT_CALL(*this, EnableDebugRecording(_));
78 }
79
80 ~MockAudioDebugRecordingHelper() override {
81 if (on_destruction_closure_in_mock_)
82 std::move(on_destruction_closure_in_mock_).Run();
83 }
84
85 MOCK_METHOD1(EnableDebugRecording, void(const base::FilePath&));
86 MOCK_METHOD0(DisableDebugRecording, void());
87
88 private:
89 // We let the mock run the destruction closure to not rely on the real
90 // implementation.
91 base::OnceClosure on_destruction_closure_in_mock_;
92
93 DISALLOW_COPY_AND_ASSIGN(MockAudioDebugRecordingHelper);
94 };
95
96 // Sub-class of the manager that overrides the CreateAudioDebugRecordingHelper
97 // function to create the above mock instead.
98 class AudioDebugRecordingManagerUnderTest : public AudioDebugRecordingManager {
99 public:
100 AudioDebugRecordingManagerUnderTest(
101 AudioFileWriter::CreateCallback create_audio_file_writer_callback,
102 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
103 : AudioDebugRecordingManager(std::move(create_audio_file_writer_callback),
104 std::move(task_runner)) {}
105 ~AudioDebugRecordingManagerUnderTest() override {}
106
107 private:
108 std::unique_ptr<AudioDebugRecordingHelper> CreateAudioDebugRecordingHelper(
109 const AudioParameters& params,
110 const AudioFileWriter::CreateCallback& create_audio_file_writer_callback,
111 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
112 base::OnceClosure on_destruction_closure) override {
113 return base::MakeUnique<MockAudioDebugRecordingHelper>(
114 params, create_audio_file_writer_callback, task_runner,
115 std::move(on_destruction_closure));
116 }
117
118 DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingManagerUnderTest);
119 };
120
121 // The test fixture.
122 class AudioDebugRecordingManagerTest : public ::testing::Test {
123 public:
124 AudioDebugRecordingManagerTest()
125 : manager_(base::BindRepeating(&CreateAudioFileWriterDummy),
126 message_loop_.task_runner()),
127 base_file_path_(base::FilePath::FromUTF8Unsafe("base_path")) {}
128
129 ~AudioDebugRecordingManagerTest() override {}
130
131 // Registers a source and increases counter for the expected next source id.
132 std::unique_ptr<AudioDebugRecorder> RegisterDebugRecordingSource(
133 const AudioParameters& params) {
134 ++expected_next_source_id_;
135 return manager_.RegisterDebugRecordingSource(kFileNameExtension, params);
136 }
137
138 private:
139 // Must come before |manager_|, so that it's inialized before it.
140 base::TestMessageLoop message_loop_;
141
142 protected:
143 AudioDebugRecordingManagerUnderTest manager_;
144 base::FilePath base_file_path_;
145
146 // The expected next source id the manager will assign. It's static since the
147 // manager uses a global running id, thus doesn't restart at each
148 // instantiation.
149 static int expected_next_source_id_;
150
151 private:
152 DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingManagerTest);
153 };
154
155 int AudioDebugRecordingManagerTest::expected_next_source_id_ = 1;
156
157 // Shouldn't do anything but store the path, i.e. no calls to recorders.
158 TEST_F(AudioDebugRecordingManagerTest, EnableDisable) {
159 manager_.EnableDebugRecording(base_file_path_);
160 manager_.DisableDebugRecording();
161 }
162
163 // Tests registration and automatic unregistration on destruction of a recorder.
164 // The unregistration relies on that the MockAudioDebugRecordingHelper runs the
165 // |on_destruction_closure| given to it.
166 TEST_F(AudioDebugRecordingManagerTest, RegisterAutomaticUnregisterAtDelete) {
167 const AudioParameters params;
168 std::vector<std::unique_ptr<AudioDebugRecorder>> recorders;
169 recorders.push_back(RegisterDebugRecordingSource(params));
170 recorders.push_back(RegisterDebugRecordingSource(params));
171 recorders.push_back(RegisterDebugRecordingSource(params));
172 EXPECT_EQ(3ul, recorders.size());
173 EXPECT_EQ(recorders.size(), manager_.debug_recording_helpers_.size());
174
175 while (!recorders.empty()) {
176 recorders.pop_back();
177 EXPECT_EQ(recorders.size(), manager_.debug_recording_helpers_.size());
178 }
179 EXPECT_EQ(0ul, recorders.size());
180 }
181
182 TEST_F(AudioDebugRecordingManagerTest, RegisterEnableDisable) {
183 // Store away the extected id for the next source to use after registering all
184 // sources.
185 int expected_id = expected_next_source_id_;
186
187 const AudioParameters params;
188 std::vector<std::unique_ptr<AudioDebugRecorder>> recorders;
189 recorders.push_back(RegisterDebugRecordingSource(params));
190 recorders.push_back(RegisterDebugRecordingSource(params));
191 recorders.push_back(RegisterDebugRecordingSource(params));
192 EXPECT_EQ(3ul, recorders.size());
193 EXPECT_EQ(recorders.size(), manager_.debug_recording_helpers_.size());
194
195 for (const auto& recorder : recorders) {
196 MockAudioDebugRecordingHelper* mock_recording_helper =
197 static_cast<MockAudioDebugRecordingHelper*>(recorder.get());
198 base::FilePath expected_file_path =
199 base_file_path_.AddExtension(kFileNameExtension)
200 .AddExtension(IntToStringType(expected_id++));
201 EXPECT_CALL(*mock_recording_helper,
202 EnableDebugRecording(expected_file_path));
203 EXPECT_CALL(*mock_recording_helper, DisableDebugRecording());
204 }
205
206 manager_.EnableDebugRecording(base_file_path_);
207 manager_.DisableDebugRecording();
208 }
209
210 // Test enabling first, then registering. This should call enable on the
211 // recoders, but we can't set expectation for that since the mock object is
212 // created and called enable upon in RegisterDebugRecordingSource(), then
213 // returned. Instead expectation is set in the ctor of the mock by setting
214 // |g_expect_enable_after_create_helper| to true here (by using the scoped
215 // variable).
216 TEST_F(AudioDebugRecordingManagerTest, EnableRegisterDisable) {
217 ScopedExpectEnableAfterCreateHelper scoped_enable_after_create_helper;
218
219 manager_.EnableDebugRecording(base_file_path_);
220
221 const AudioParameters params;
222 std::vector<std::unique_ptr<AudioDebugRecorder>> recorders;
223 recorders.push_back(RegisterDebugRecordingSource(params));
224 recorders.push_back(RegisterDebugRecordingSource(params));
225 recorders.push_back(RegisterDebugRecordingSource(params));
226 EXPECT_EQ(3ul, recorders.size());
227 EXPECT_EQ(recorders.size(), manager_.debug_recording_helpers_.size());
228
229 for (const auto& recorder : recorders) {
230 MockAudioDebugRecordingHelper* mock_recording_helper =
231 static_cast<MockAudioDebugRecordingHelper*>(recorder.get());
232 EXPECT_CALL(*mock_recording_helper, DisableDebugRecording());
233 }
234
235 manager_.DisableDebugRecording();
236 }
237
238 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698