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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "content/child/child_process.h"
#include "content/common/media/video_capture_messages.h"
#include "content/renderer/media/video_capture_impl.h"
#include "media/base/bind_to_current_loop.h"
#include "media/video/capture/mock_video_capture_event_handler.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::AtLeast;
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
using ::testing::SaveArg;
using media::MockVideoCaptureEventHandler;
namespace content {
class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter {
public:
MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {}
// Filter implementation.
MOCK_METHOD1(Send, bool(IPC::Message* message));
protected:
virtual ~MockVideoCaptureMessageFilter() {}
private:
DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter);
};
class VideoCaptureImplTest : public ::testing::Test {
public:
class MockVideoCaptureImpl : public VideoCaptureImpl {
public:
MockVideoCaptureImpl(const media::VideoCaptureSessionId id,
VideoCaptureMessageFilter* filter)
: VideoCaptureImpl(id, filter) {
}
virtual ~MockVideoCaptureImpl() {}
// Override Send() to mimic device to send events.
virtual void Send(IPC::Message* message) OVERRIDE {
CHECK(message);
// In this method, messages are sent to the according handlers as if
// we are the device.
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message)
IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, DeviceStartCapture)
IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, DevicePauseCapture)
IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Stop, DeviceStopCapture)
IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady,
DeviceReceiveEmptyBuffer)
IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceSupportedFormats,
DeviceGetSupportedFormats)
IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_GetDeviceFormatsInUse,
DeviceGetFormatsInUse)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
EXPECT_TRUE(handled);
delete message;
}
void DeviceStartCapture(int device_id,
media::VideoCaptureSessionId session_id,
const media::VideoCaptureParams& params) {
OnStateChanged(VIDEO_CAPTURE_STATE_STARTED);
}
void DevicePauseCapture(int device_id) {}
void DeviceStopCapture(int device_id) {
OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED);
}
void DeviceReceiveEmptyBuffer(int device_id, int buffer_id) {}
void DeviceGetSupportedFormats(int device_id,
media::VideoCaptureSessionId session_id) {
// When the mock message filter receives a request for the device
// supported formats, replies immediately with an empty format list.
OnDeviceSupportedFormatsEnumerated(
media::VideoCaptureFormats());
}
void DeviceGetFormatsInUse(int device_id,
media::VideoCaptureSessionId session_id) {
OnDeviceFormatsInUseReceived(media::VideoCaptureFormats());
}
};
VideoCaptureImplTest() {
params_small_.requested_format = media::VideoCaptureFormat(
gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420);
params_large_.requested_format = media::VideoCaptureFormat(
gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420);
child_process_.reset(new ChildProcess());
message_filter_ = new MockVideoCaptureMessageFilter;
session_id_ = 1;
video_capture_impl_ = new MockVideoCaptureImpl(
session_id_, message_filter_.get());
video_capture_impl_->device_id_ = 2;
}
virtual ~VideoCaptureImplTest() {
delete video_capture_impl_;
}
protected:
base::MessageLoop message_loop_;
base::RunLoop run_loop_;
scoped_ptr<ChildProcess> child_process_;
scoped_refptr<MockVideoCaptureMessageFilter> message_filter_;
media::VideoCaptureSessionId session_id_;
MockVideoCaptureImpl* video_capture_impl_;
media::VideoCaptureParams params_small_;
media::VideoCaptureParams params_large_;
private:
DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest);
};
TEST_F(VideoCaptureImplTest, Simple) {
// Execute SetCapture() and StopCapture() for one client.
scoped_ptr<MockVideoCaptureEventHandler> client(
new MockVideoCaptureEventHandler);
EXPECT_CALL(*client, OnStarted(_));
EXPECT_CALL(*client, OnStopped(_));
EXPECT_CALL(*client, OnRemoved(_));
video_capture_impl_->StartCapture(client.get(), params_small_);
video_capture_impl_->StopCapture(client.get());
video_capture_impl_->DeInit(
media::BindToCurrentLoop(run_loop_.QuitClosure()));
run_loop_.Run();
}
TEST_F(VideoCaptureImplTest, TwoClientsInSequence) {
// Execute SetCapture() and StopCapture() for 2 clients in sequence.
scoped_ptr<MockVideoCaptureEventHandler> client1(
new MockVideoCaptureEventHandler);
scoped_ptr<MockVideoCaptureEventHandler> client2(
new MockVideoCaptureEventHandler);
EXPECT_CALL(*client1, OnStarted(_));
EXPECT_CALL(*client1, OnStopped(_));
EXPECT_CALL(*client1, OnRemoved(_));
EXPECT_CALL(*client2, OnStarted(_));
EXPECT_CALL(*client2, OnStopped(_));
EXPECT_CALL(*client2, OnRemoved(_));
video_capture_impl_->StartCapture(client1.get(), params_small_);
video_capture_impl_->StopCapture(client1.get());
video_capture_impl_->StartCapture(client2.get(), params_small_);
video_capture_impl_->StopCapture(client2.get());
video_capture_impl_->DeInit(
media::BindToCurrentLoop(run_loop_.QuitClosure()));
run_loop_.Run();
}
TEST_F(VideoCaptureImplTest, LargeAndSmall) {
// Execute SetCapture() and StopCapture() for 2 clients simultaneously.
// The large client starts first and stops first.
scoped_ptr<MockVideoCaptureEventHandler> client_small(
new MockVideoCaptureEventHandler);
scoped_ptr<MockVideoCaptureEventHandler> client_large(
new MockVideoCaptureEventHandler);
EXPECT_CALL(*client_large, OnStarted(_));
EXPECT_CALL(*client_small, OnStarted(_));
EXPECT_CALL(*client_large, OnStopped(_));
EXPECT_CALL(*client_large, OnRemoved(_));
EXPECT_CALL(*client_small, OnStopped(_));
EXPECT_CALL(*client_small, OnRemoved(_));
video_capture_impl_->StartCapture(client_large.get(), params_large_);
video_capture_impl_->StartCapture(client_small.get(), params_small_);
video_capture_impl_->StopCapture(client_large.get());
video_capture_impl_->StopCapture(client_small.get());
video_capture_impl_->DeInit(
media::BindToCurrentLoop(run_loop_.QuitClosure()));
run_loop_.Run();
}
TEST_F(VideoCaptureImplTest, SmallAndLarge) {
// Execute SetCapture() and StopCapture() for 2 clients simultaneously.
// The small client starts first and stops first.
scoped_ptr<MockVideoCaptureEventHandler> client_small(
new MockVideoCaptureEventHandler);
scoped_ptr<MockVideoCaptureEventHandler> client_large(
new MockVideoCaptureEventHandler);
EXPECT_CALL(*client_small, OnStarted(_));
EXPECT_CALL(*client_large, OnStarted(_));
EXPECT_CALL(*client_small, OnStopped(_));
EXPECT_CALL(*client_small, OnRemoved(_));
EXPECT_CALL(*client_large, OnStopped(_));
EXPECT_CALL(*client_large, OnRemoved(_));
video_capture_impl_->StartCapture(client_small.get(), params_small_);
video_capture_impl_->StartCapture(client_large.get(), params_large_);
video_capture_impl_->StopCapture(client_small.get());
video_capture_impl_->StopCapture(client_large.get());
video_capture_impl_->DeInit(
media::BindToCurrentLoop(run_loop_.QuitClosure()));
run_loop_.Run();
}
// Check that a request to GetDeviceSupportedFormats() ends up eventually in the
// provided callback.
TEST_F(VideoCaptureImplTest, GetDeviceFormats) {
scoped_ptr<MockVideoCaptureEventHandler> client(
new MockVideoCaptureEventHandler);
EXPECT_CALL(*client, OnDeviceSupportedFormatsEnumerated(_));
const base::Callback<void(const media::VideoCaptureFormats&)>
callback = base::Bind(
&MockVideoCaptureEventHandler::OnDeviceSupportedFormatsEnumerated,
base::Unretained(client.get()));
video_capture_impl_->GetDeviceSupportedFormats(callback);
video_capture_impl_->DeInit(
media::BindToCurrentLoop(run_loop_.QuitClosure()));
run_loop_.Run();
}
// Check that two requests to GetDeviceSupportedFormats() end up eventually
// calling the provided callbacks.
TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) {
scoped_ptr<MockVideoCaptureEventHandler> client1(
new MockVideoCaptureEventHandler);
scoped_ptr<MockVideoCaptureEventHandler> client2(
new MockVideoCaptureEventHandler);
EXPECT_CALL(*client1, OnDeviceSupportedFormatsEnumerated(_));
EXPECT_CALL(*client2, OnDeviceSupportedFormatsEnumerated(_));
const base::Callback<void(const media::VideoCaptureFormats&)>
callback1 = base::Bind(
&MockVideoCaptureEventHandler::OnDeviceSupportedFormatsEnumerated,
base::Unretained(client1.get()));
const base::Callback<void(const media::VideoCaptureFormats&)>
callback2 = base::Bind(
&MockVideoCaptureEventHandler::OnDeviceSupportedFormatsEnumerated,
base::Unretained(client2.get()));
video_capture_impl_->GetDeviceSupportedFormats(callback1);
video_capture_impl_->GetDeviceSupportedFormats(callback2);
video_capture_impl_->DeInit(
media::BindToCurrentLoop(run_loop_.QuitClosure()));
run_loop_.Run();
}
// Check that a request to GetDeviceFormatsInUse() ends up eventually in the
// provided callback.
TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) {
scoped_ptr<MockVideoCaptureEventHandler> client(
new MockVideoCaptureEventHandler);
media::VideoCaptureFormats formats_in_use;
EXPECT_CALL(*client, OnDeviceFormatsInUseReceived(_))
.WillOnce(SaveArg<0>(&formats_in_use));
const base::Callback<void(const media::VideoCaptureFormats&)> callback =
base::Bind(&MockVideoCaptureEventHandler::OnDeviceFormatsInUseReceived,
base::Unretained(client.get()));
video_capture_impl_->GetDeviceFormatsInUse(callback);
video_capture_impl_->DeInit(
media::BindToCurrentLoop(run_loop_.QuitClosure()));
run_loop_.Run();
EXPECT_TRUE(formats_in_use.empty());
}
} // namespace content
|