1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
media / audio / aecdump_recording_manager_unittest.cc [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/audio/aecdump_recording_manager.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/test/task_environment.h"
#include "base/test/test_file_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::InSequence;
using testing::NiceMock;
using testing::Sequence;
namespace media {
namespace {
class MockAecdumpRecordingSource : public AecdumpRecordingSource {
public:
MOCK_METHOD1(StartAecdump, void(base::File));
MOCK_METHOD0(StopAecdump, void());
};
class AecdumpRecordingManagerTest : public ::testing::Test {
public:
AecdumpRecordingManagerTest()
: manager_(std::make_unique<AecdumpRecordingManager>(
task_environment_.GetMainThreadTaskRunner())),
create_file_callback_(base::BindRepeating(
&AecdumpRecordingManagerTest::AsyncCreateFileCallback,
base::Unretained(this))) {}
void SetUp() override {
temp_dir_path_ = base::CreateUniqueTempDirectoryScopedToTest();
}
void TearDown() override { task_environment_.RunUntilIdle(); }
protected:
// Simulates async file creation by posting the reply to the main thread.
void AsyncCreateFileCallback(
uint32_t /*id*/,
base::OnceCallback<void(base::File)> reply_callback) {
base::FilePath temp_file_path;
base::File file =
base::CreateAndOpenTemporaryFileInDir(temp_dir_path_, &temp_file_path);
ASSERT_TRUE(file.IsValid());
task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(std::move(reply_callback), std::move(file)));
}
base::FilePath temp_dir_path_;
base::test::TaskEnvironment task_environment_;
std::unique_ptr<AecdumpRecordingManager> manager_;
AecdumpRecordingManager::CreateFileCallback create_file_callback_;
};
TEST_F(AecdumpRecordingManagerTest, EnableDisableDoesNotCrash) {
manager_->EnableDebugRecording(create_file_callback_);
manager_->DisableDebugRecording();
}
TEST_F(AecdumpRecordingManagerTest,
EnableDisableWithOneSourceStartsStopsSource) {
MockAecdumpRecordingSource mock_source;
InSequence s;
EXPECT_CALL(mock_source, StartAecdump(_)).Times(1);
EXPECT_CALL(mock_source, StopAecdump()).Times(1);
manager_->RegisterAecdumpSource(&mock_source);
manager_->EnableDebugRecording(create_file_callback_);
base::RunLoop().RunUntilIdle();
manager_->DisableDebugRecording();
manager_->DeregisterAecdumpSource(&mock_source);
}
TEST_F(AecdumpRecordingManagerTest,
EnableDisableWithTwoSourcesStartsStopsSources) {
MockAecdumpRecordingSource mock_source_a;
MockAecdumpRecordingSource mock_source_b;
Sequence s1;
Sequence s2;
EXPECT_CALL(mock_source_a, StartAecdump(_)).Times(1).InSequence(s1);
EXPECT_CALL(mock_source_a, StopAecdump()).Times(1).InSequence(s1);
EXPECT_CALL(mock_source_b, StartAecdump(_)).Times(1).InSequence(s2);
EXPECT_CALL(mock_source_b, StopAecdump()).Times(1).InSequence(s2);
manager_->RegisterAecdumpSource(&mock_source_a);
manager_->RegisterAecdumpSource(&mock_source_b);
manager_->EnableDebugRecording(create_file_callback_);
base::RunLoop().RunUntilIdle();
manager_->DisableDebugRecording();
manager_->DeregisterAecdumpSource(&mock_source_a);
manager_->DeregisterAecdumpSource(&mock_source_b);
}
TEST_F(AecdumpRecordingManagerTest,
RegisterDeregisterSourceBeforeEnableDisableDoesNotStartStopSource) {
MockAecdumpRecordingSource mock_source;
EXPECT_CALL(mock_source, StartAecdump(_)).Times(0);
EXPECT_CALL(mock_source, StopAecdump()).Times(0);
manager_->RegisterAecdumpSource(&mock_source);
manager_->DeregisterAecdumpSource(&mock_source);
// Enabling debug recordings should not trigger any calls to the recording
// source.
manager_->EnableDebugRecording(create_file_callback_);
base::RunLoop().RunUntilIdle();
manager_->DisableDebugRecording();
}
TEST_F(AecdumpRecordingManagerTest,
RegisterDeregisterSourceAfterEnableDisableDoesNotStartStopSource) {
MockAecdumpRecordingSource mock_source;
EXPECT_CALL(mock_source, StartAecdump(_)).Times(0);
EXPECT_CALL(mock_source, StopAecdump()).Times(0);
// Enabling debug recordings should not trigger any calls to the recording
// source.
manager_->EnableDebugRecording(create_file_callback_);
manager_->DisableDebugRecording();
manager_->RegisterAecdumpSource(&mock_source);
base::RunLoop().RunUntilIdle();
manager_->DeregisterAecdumpSource(&mock_source);
}
TEST_F(AecdumpRecordingManagerTest,
RegisterDeregisterSourceDuringRecordingStartsStopsSource) {
InSequence s;
MockAecdumpRecordingSource mock_source;
EXPECT_CALL(mock_source, StartAecdump(_)).Times(1);
EXPECT_CALL(mock_source, StopAecdump()).Times(1);
manager_->EnableDebugRecording(create_file_callback_);
manager_->RegisterAecdumpSource(&mock_source);
base::RunLoop().RunUntilIdle();
manager_->DeregisterAecdumpSource(&mock_source);
::testing::Mock::VerifyAndClearExpectations(&mock_source);
manager_->DisableDebugRecording();
}
TEST_F(AecdumpRecordingManagerTest,
DeregisterSourceBeforeFileCreationCallbackDoesNotStartSource) {
NiceMock<MockAecdumpRecordingSource> mock_source;
EXPECT_CALL(mock_source, StartAecdump(_)).Times(0);
manager_->RegisterAecdumpSource(&mock_source);
// Enabling debug recordings schedules a StartAecdump() call to the recording
// source.
manager_->EnableDebugRecording(create_file_callback_);
// Deregister the source before StartAecdump() can be called.
manager_->DeregisterAecdumpSource(&mock_source);
// Run the file creation callback. Aecdumps should not be started.
base::RunLoop().RunUntilIdle();
manager_->DisableDebugRecording();
}
TEST_F(AecdumpRecordingManagerTest,
StopRecordingBeforeFileCreationCallbackDoesNotStartSource) {
NiceMock<MockAecdumpRecordingSource> mock_source;
EXPECT_CALL(mock_source, StartAecdump(_)).Times(0);
manager_->RegisterAecdumpSource(&mock_source);
// Enabling debug recordings schedules a StartAecdump() call to the recording
// source.
manager_->EnableDebugRecording(create_file_callback_);
// Stop aecdump recording before StartAecdump() can be called.
manager_->DisableDebugRecording();
// Run the file creation callback. Aecdumps should not be started.
base::RunLoop().RunUntilIdle();
// Clean up.
manager_->DeregisterAecdumpSource(&mock_source);
}
TEST_F(AecdumpRecordingManagerTest,
RestartRecordingBeforeFileCreationCallbackDoesNotStartSourceTwice) {
NiceMock<MockAecdumpRecordingSource> mock_source;
EXPECT_CALL(mock_source, StartAecdump(_)).Times(1);
manager_->RegisterAecdumpSource(&mock_source);
// Enabling debug recordings schedules a StartAecdump() call to the recording
// source.
manager_->EnableDebugRecording(create_file_callback_);
// Restart aecdump recording before StartAecdump() can be called.
manager_->DisableDebugRecording();
manager_->EnableDebugRecording(create_file_callback_);
// Run the file creation callback. The first file created should be discarded,
// we expect only one StartAecdump() call.
base::RunLoop().RunUntilIdle();
// Clean up.
manager_->DeregisterAecdumpSource(&mock_source);
manager_->DisableDebugRecording();
}
TEST_F(AecdumpRecordingManagerTest,
DestroyManagerBeforeFileCreationCallbackDoesNotCrash) {
NiceMock<MockAecdumpRecordingSource> mock_source;
manager_->RegisterAecdumpSource(&mock_source);
// Enabling debug recordings schedules a StartAecdump() call to the recording
// source.
manager_->EnableDebugRecording(create_file_callback_);
// Destroy the AecdumpRecordingManager before StartAecdump() can be called.
manager_->DeregisterAecdumpSource(&mock_source);
manager_->DisableDebugRecording();
manager_.reset();
// Run the file creation callback. Aecdumps should not be started.
base::RunLoop().RunUntilIdle();
}
} // namespace
} // namespace media