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

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

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

Powered by Google App Engine
This is Rietveld 408576698