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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
media / mojo / services / mojo_audio_input_stream_unittest.cc [blame]
// Copyright 2017 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/mojo/services/mojo_audio_input_stream.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/run_loop.h"
#include "base/sync_socket.h"
#include "base/test/task_environment.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
namespace {
const double kNewVolume = 0.618;
// Not actually used, but sent from the AudioInputDelegate.
const int kStreamId = 0;
const int kShmemSize = 100;
// const bool kInitiallyMuted = true;
const bool kInitiallyNotMuted = true;
using testing::_;
using testing::Mock;
using testing::NotNull;
using testing::Return;
using testing::SaveArg;
using testing::StrictMock;
using testing::Test;
using AudioInputStream = mojom::AudioInputStream;
class TestCancelableSyncSocket : public base::CancelableSyncSocket {
public:
TestCancelableSyncSocket() = default;
void ExpectOwnershipTransfer() { expect_ownership_transfer_ = true; }
TestCancelableSyncSocket(const TestCancelableSyncSocket&) = delete;
TestCancelableSyncSocket& operator=(const TestCancelableSyncSocket&) = delete;
~TestCancelableSyncSocket() override {
// When the handle is sent over mojo, mojo takes ownership over it and
// closes it. We have to make sure we do not also retain the handle in the
// sync socket, as the sync socket closes the handle on destruction.
if (expect_ownership_transfer_)
EXPECT_FALSE(IsValid());
}
private:
bool expect_ownership_transfer_ = false;
};
class MockDelegate : public AudioInputDelegate {
public:
MockDelegate() = default;
~MockDelegate() override = default;
MOCK_METHOD0(GetStreamId, int());
MOCK_METHOD0(OnRecordStream, void());
MOCK_METHOD1(OnSetVolume, void(double));
MOCK_METHOD1(OnSetOutputDeviceForAec, void(const std::string&));
};
class MockDelegateFactory {
public:
void PrepareDelegateForCreation(
std::unique_ptr<AudioInputDelegate> delegate) {
ASSERT_EQ(nullptr, delegate_);
delegate_.swap(delegate);
}
std::unique_ptr<AudioInputDelegate> CreateDelegate(
AudioInputDelegate::EventHandler* handler) {
MockCreateDelegate(handler);
EXPECT_NE(nullptr, delegate_);
return std::move(delegate_);
}
MOCK_METHOD1(MockCreateDelegate, void(AudioInputDelegate::EventHandler*));
private:
std::unique_ptr<AudioInputDelegate> delegate_;
};
class MockDeleter {
public:
MOCK_METHOD0(Finished, void());
};
class MockClient : public mojom::AudioInputStreamClient {
public:
MockClient() = default;
MockClient(const MockClient&) = delete;
MockClient& operator=(const MockClient&) = delete;
void Initialized(mojom::ReadWriteAudioDataPipePtr data_pipe,
bool initially_muted) {
ASSERT_TRUE(data_pipe->shared_memory.IsValid());
ASSERT_TRUE(data_pipe->socket.is_valid());
socket_ = std::make_unique<base::CancelableSyncSocket>(
data_pipe->socket.TakePlatformFile());
EXPECT_TRUE(socket_->IsValid());
region_ = std::move(data_pipe->shared_memory);
EXPECT_TRUE(region_.IsValid());
GotNotification(initially_muted);
}
MOCK_METHOD1(GotNotification, void(bool initially_muted));
MOCK_METHOD1(OnMutedStateChanged, void(bool));
MOCK_METHOD1(OnError, void(mojom::InputStreamErrorCode));
private:
base::UnsafeSharedMemoryRegion region_;
std::unique_ptr<base::CancelableSyncSocket> socket_;
};
std::unique_ptr<AudioInputDelegate> CreateNoDelegate(
AudioInputDelegate::EventHandler* event_handler) {
return nullptr;
}
void NotCalled(mojom::ReadWriteAudioDataPipePtr data_pipe,
bool initially_muted) {
EXPECT_TRUE(false) << "The StreamCreated callback was called despite the "
"test expecting it not to.";
}
} // namespace
class MojoAudioInputStreamTest : public Test {
public:
MojoAudioInputStreamTest()
: foreign_socket_(std::make_unique<TestCancelableSyncSocket>()),
client_receiver_(&client_) {}
MojoAudioInputStreamTest(const MojoAudioInputStreamTest&) = delete;
MojoAudioInputStreamTest& operator=(const MojoAudioInputStreamTest&) = delete;
mojo::Remote<mojom::AudioInputStream> CreateAudioInput() {
mojo::Remote<mojom::AudioInputStream> stream;
ExpectDelegateCreation();
impl_ = std::make_unique<MojoAudioInputStream>(
stream.BindNewPipeAndPassReceiver(),
client_receiver_.BindNewPipeAndPassRemote(),
base::BindOnce(&MockDelegateFactory::CreateDelegate,
base::Unretained(&mock_delegate_factory_)),
base::BindOnce(&MockClient::Initialized, base::Unretained(&client_)),
base::BindOnce(&MockDeleter::Finished, base::Unretained(&deleter_)));
EXPECT_TRUE(stream.is_bound());
return stream;
}
protected:
void ExpectDelegateCreation() {
delegate_ = new StrictMock<MockDelegate>();
mock_delegate_factory_.PrepareDelegateForCreation(
base::WrapUnique(delegate_.get()));
EXPECT_TRUE(
base::CancelableSyncSocket::CreatePair(&local_, foreign_socket_.get()));
mem_ = base::UnsafeSharedMemoryRegion::Create(kShmemSize);
EXPECT_TRUE(mem_.IsValid());
EXPECT_CALL(mock_delegate_factory_, MockCreateDelegate(NotNull()))
.WillOnce(SaveArg<0>(&delegate_event_handler_));
}
base::test::SingleThreadTaskEnvironment task_environment_;
base::CancelableSyncSocket local_;
std::unique_ptr<TestCancelableSyncSocket> foreign_socket_;
base::UnsafeSharedMemoryRegion mem_;
StrictMock<MockDelegateFactory> mock_delegate_factory_;
StrictMock<MockDeleter> deleter_;
StrictMock<MockClient> client_;
mojo::Receiver<media::mojom::AudioInputStreamClient> client_receiver_;
std::unique_ptr<MojoAudioInputStream> impl_;
raw_ptr<StrictMock<MockDelegate>> delegate_ = nullptr;
raw_ptr<AudioInputDelegate::EventHandler> delegate_event_handler_ = nullptr;
};
TEST_F(MojoAudioInputStreamTest, NoDelegate_SignalsError) {
bool deleter_called = false;
EXPECT_CALL(client_, OnError(mojom::InputStreamErrorCode::kUnknown));
mojo::Remote<mojom::AudioInputStream> stream_remote;
MojoAudioInputStream stream(
stream_remote.BindNewPipeAndPassReceiver(),
client_receiver_.BindNewPipeAndPassRemote(),
base::BindOnce(&CreateNoDelegate), base::BindOnce(&NotCalled),
base::BindOnce([](bool* p) { *p = true; }, &deleter_called));
EXPECT_FALSE(deleter_called)
<< "Stream shouldn't call the deleter from its constructor.";
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(deleter_called);
}
TEST_F(MojoAudioInputStreamTest, Record_Records) {
auto audio_input = CreateAudioInput();
EXPECT_CALL(*delegate_, OnRecordStream());
audio_input->Record();
base::RunLoop().RunUntilIdle();
}
TEST_F(MojoAudioInputStreamTest, SetVolume_SetsVolume) {
auto audio_input = CreateAudioInput();
EXPECT_CALL(*delegate_, OnSetVolume(kNewVolume));
audio_input->SetVolume(kNewVolume);
base::RunLoop().RunUntilIdle();
}
TEST_F(MojoAudioInputStreamTest, DestructWithCallPending_Safe) {
auto audio_input = CreateAudioInput();
EXPECT_CALL(client_, GotNotification(kInitiallyNotMuted));
base::RunLoop().RunUntilIdle();
ASSERT_NE(nullptr, delegate_event_handler_);
foreign_socket_->ExpectOwnershipTransfer();
delegate_event_handler_->OnStreamCreated(kStreamId, std::move(mem_),
std::move(foreign_socket_),
kInitiallyNotMuted);
audio_input->Record();
// Depends on `impl_` so must be cleared before it is destroyed.
delegate_event_handler_ = nullptr;
delegate_ = nullptr;
impl_.reset();
base::RunLoop().RunUntilIdle();
}
TEST_F(MojoAudioInputStreamTest, Created_NotifiesClient) {
auto audio_input = CreateAudioInput();
base::RunLoop().RunUntilIdle();
EXPECT_CALL(client_, GotNotification(kInitiallyNotMuted));
ASSERT_NE(nullptr, delegate_event_handler_);
foreign_socket_->ExpectOwnershipTransfer();
delegate_event_handler_->OnStreamCreated(kStreamId, std::move(mem_),
std::move(foreign_socket_),
kInitiallyNotMuted);
base::RunLoop().RunUntilIdle();
}
TEST_F(MojoAudioInputStreamTest, SetVolumeTooLarge_Error) {
auto audio_input = CreateAudioInput();
EXPECT_CALL(deleter_, Finished());
EXPECT_CALL(client_, OnError(mojom::InputStreamErrorCode::kUnknown));
audio_input->SetVolume(15);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClear(&deleter_);
}
TEST_F(MojoAudioInputStreamTest, SetVolumeNegative_Error) {
auto audio_input = CreateAudioInput();
EXPECT_CALL(deleter_, Finished());
EXPECT_CALL(client_, OnError(mojom::InputStreamErrorCode::kUnknown));
audio_input->SetVolume(-0.5);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClear(&deleter_);
}
TEST_F(MojoAudioInputStreamTest, DelegateErrorBeforeCreated_PropagatesError) {
auto audio_input = CreateAudioInput();
EXPECT_CALL(deleter_, Finished());
EXPECT_CALL(client_, OnError(mojom::InputStreamErrorCode::kUnknown));
ASSERT_NE(nullptr, delegate_event_handler_);
delegate_event_handler_->OnStreamError(kStreamId);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClear(&deleter_);
}
TEST_F(MojoAudioInputStreamTest, DelegateErrorAfterCreated_PropagatesError) {
auto audio_input = CreateAudioInput();
EXPECT_CALL(client_, GotNotification(kInitiallyNotMuted));
EXPECT_CALL(deleter_, Finished());
EXPECT_CALL(client_, OnError(mojom::InputStreamErrorCode::kUnknown));
base::RunLoop().RunUntilIdle();
ASSERT_NE(nullptr, delegate_event_handler_);
foreign_socket_->ExpectOwnershipTransfer();
delegate_event_handler_->OnStreamCreated(kStreamId, std::move(mem_),
std::move(foreign_socket_),
kInitiallyNotMuted);
delegate_event_handler_->OnStreamError(kStreamId);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClear(&deleter_);
}
TEST_F(MojoAudioInputStreamTest, RemoteEndGone_Error) {
auto audio_input = CreateAudioInput();
EXPECT_CALL(deleter_, Finished());
audio_input.reset();
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClear(&deleter_);
}
} // namespace media