OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "base/run_loop.h" | |
7 #include "content/child/child_process.h" | 6 #include "content/child/child_process.h" |
8 #include "content/common/media/video_capture_messages.h" | 7 #include "content/common/media/video_capture_messages.h" |
9 #include "content/renderer/media/video_capture_impl.h" | 8 #include "content/renderer/media/video_capture_impl.h" |
10 #include "media/video/capture/mock_video_capture_event_handler.h" | |
11 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
13 | 11 |
14 using ::testing::_; | 12 using ::testing::_; |
15 using ::testing::AtLeast; | 13 using ::testing::AtLeast; |
16 using ::testing::InvokeWithoutArgs; | 14 using ::testing::Return; |
17 using media::MockVideoCaptureEventHandler; | |
18 | 15 |
19 namespace content { | 16 namespace content { |
20 | 17 |
21 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { | 18 class MockVideoCaptureMessageFilter : public VideoCaptureMessageFilter { |
22 public: | 19 public: |
23 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {} | 20 MockVideoCaptureMessageFilter() : VideoCaptureMessageFilter() {} |
24 | 21 |
25 // Filter implementation. | 22 // Filter implementation. |
26 MOCK_METHOD1(Send, bool(IPC::Message* message)); | 23 MOCK_METHOD1(Send, bool(IPC::Message* message)); |
27 | 24 |
28 protected: | 25 protected: |
29 virtual ~MockVideoCaptureMessageFilter() {} | 26 virtual ~MockVideoCaptureMessageFilter() {} |
30 | 27 |
31 private: | 28 private: |
32 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); | 29 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); |
33 }; | 30 }; |
34 | 31 |
| 32 class MockVideoCaptureClient : public media::VideoCapture::EventHandler { |
| 33 public: |
| 34 MockVideoCaptureClient() {} |
| 35 virtual ~MockVideoCaptureClient() {} |
| 36 |
| 37 // EventHandler implementation. |
| 38 MOCK_METHOD1(OnStarted, void(media::VideoCapture* capture)); |
| 39 MOCK_METHOD1(OnStopped, void(media::VideoCapture* capture)); |
| 40 MOCK_METHOD1(OnPaused, void(media::VideoCapture* capture)); |
| 41 MOCK_METHOD2(OnError, void(media::VideoCapture* capture, int error_code)); |
| 42 MOCK_METHOD1(OnRemoved, void(media::VideoCapture* capture)); |
| 43 MOCK_METHOD2(OnFrameReady, |
| 44 void(media::VideoCapture* capture, |
| 45 const scoped_refptr<media::VideoFrame>& frame)); |
| 46 MOCK_METHOD2(OnDeviceInfoReceived, |
| 47 void(media::VideoCapture* capture, |
| 48 const media::VideoCaptureFormat& device_info)); |
| 49 |
| 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureClient); |
| 52 }; |
| 53 |
35 class VideoCaptureImplTest : public ::testing::Test { | 54 class VideoCaptureImplTest : public ::testing::Test { |
36 public: | 55 public: |
37 class MockVideoCaptureImpl : public VideoCaptureImpl { | 56 class MockVideoCaptureImpl : public VideoCaptureImpl { |
38 public: | 57 public: |
39 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, | 58 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, |
| 59 scoped_refptr<base::MessageLoopProxy> ml_proxy, |
40 VideoCaptureMessageFilter* filter) | 60 VideoCaptureMessageFilter* filter) |
41 : VideoCaptureImpl(id, filter) {} | 61 : VideoCaptureImpl(id, ml_proxy.get(), filter) {} |
42 virtual ~MockVideoCaptureImpl() {} | 62 virtual ~MockVideoCaptureImpl() {} |
43 | 63 |
44 // Override Send() to mimic device to send events. | 64 // Override Send() to mimic device to send events. |
45 virtual void Send(IPC::Message* message) OVERRIDE { | 65 virtual void Send(IPC::Message* message) OVERRIDE { |
46 CHECK(message); | 66 CHECK(message); |
47 | 67 |
48 // In this method, messages are sent to the according handlers as if | 68 // In this method, messages are sent to the according handlers as if |
49 // we are the device. | 69 // we are the device. |
50 bool handled = true; | 70 bool handled = true; |
51 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message) | 71 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message) |
(...skipping 23 matching lines...) Expand all Loading... |
75 void DeviceReceiveEmptyBuffer(int device_id, int buffer_id) {} | 95 void DeviceReceiveEmptyBuffer(int device_id, int buffer_id) {} |
76 }; | 96 }; |
77 | 97 |
78 VideoCaptureImplTest() { | 98 VideoCaptureImplTest() { |
79 params_small_.requested_format = media::VideoCaptureFormat( | 99 params_small_.requested_format = media::VideoCaptureFormat( |
80 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); | 100 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); |
81 | 101 |
82 params_large_.requested_format = media::VideoCaptureFormat( | 102 params_large_.requested_format = media::VideoCaptureFormat( |
83 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); | 103 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); |
84 | 104 |
| 105 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO)); |
| 106 message_loop_proxy_ = base::MessageLoopProxy::current().get(); |
85 child_process_.reset(new ChildProcess()); | 107 child_process_.reset(new ChildProcess()); |
86 | 108 |
87 message_filter_ = new MockVideoCaptureMessageFilter; | 109 message_filter_ = new MockVideoCaptureMessageFilter; |
88 session_id_ = 1; | 110 session_id_ = 1; |
89 | 111 |
90 video_capture_impl_ = new MockVideoCaptureImpl( | 112 video_capture_impl_ = new MockVideoCaptureImpl( |
91 session_id_, message_filter_.get()); | 113 session_id_, message_loop_proxy_, message_filter_.get()); |
92 | 114 |
93 video_capture_impl_->device_id_ = 2; | 115 video_capture_impl_->device_id_ = 2; |
94 } | 116 } |
95 | 117 |
96 virtual ~VideoCaptureImplTest() { | 118 virtual ~VideoCaptureImplTest() { |
97 delete video_capture_impl_; | 119 delete video_capture_impl_; |
98 } | 120 } |
99 | 121 |
100 void Quit() { | |
101 message_loop_.PostTask(FROM_HERE, run_loop_.QuitClosure()); | |
102 } | |
103 | |
104 protected: | 122 protected: |
105 base::MessageLoop message_loop_; | 123 scoped_ptr<base::MessageLoop> message_loop_; |
106 base::RunLoop run_loop_; | 124 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
107 scoped_ptr<ChildProcess> child_process_; | 125 scoped_ptr<ChildProcess> child_process_; |
108 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; | 126 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; |
109 media::VideoCaptureSessionId session_id_; | 127 media::VideoCaptureSessionId session_id_; |
110 MockVideoCaptureImpl* video_capture_impl_; | 128 MockVideoCaptureImpl* video_capture_impl_; |
111 media::VideoCaptureParams params_small_; | 129 media::VideoCaptureParams params_small_; |
112 media::VideoCaptureParams params_large_; | 130 media::VideoCaptureParams params_large_; |
113 | 131 |
114 private: | 132 private: |
115 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); | 133 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); |
116 }; | 134 }; |
117 | 135 |
118 TEST_F(VideoCaptureImplTest, Simple) { | 136 TEST_F(VideoCaptureImplTest, Simple) { |
119 // Execute SetCapture() and StopCapture() for one client. | 137 // Execute SetCapture() and StopCapture() for one client. |
120 scoped_ptr<MockVideoCaptureEventHandler> client( | 138 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); |
121 new MockVideoCaptureEventHandler); | |
122 | 139 |
123 EXPECT_CALL(*client, OnStarted(_)); | 140 EXPECT_CALL(*client, OnStarted(_)) |
124 EXPECT_CALL(*client, OnStopped(_)); | 141 .WillOnce(Return()); |
125 EXPECT_CALL(*client, OnRemoved(_)) | |
126 .WillOnce(InvokeWithoutArgs(this, &VideoCaptureImplTest::Quit)); | |
127 | 142 |
128 video_capture_impl_->StartCapture(client.get(), params_small_); | 143 video_capture_impl_->StartCapture(client.get(), params_small_); |
| 144 message_loop_->RunUntilIdle(); |
| 145 |
| 146 EXPECT_CALL(*client, OnStopped(_)) |
| 147 .WillOnce(Return()); |
| 148 EXPECT_CALL(*client, OnRemoved(_)) |
| 149 .WillOnce(Return()); |
| 150 |
129 video_capture_impl_->StopCapture(client.get()); | 151 video_capture_impl_->StopCapture(client.get()); |
130 run_loop_.Run(); | 152 message_loop_->RunUntilIdle(); |
131 } | 153 } |
132 | 154 |
133 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { | 155 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { |
134 // Execute SetCapture() and StopCapture() for 2 clients in sequence. | 156 // Execute SetCapture() and StopCapture() for 2 clients in sequence. |
135 scoped_ptr<MockVideoCaptureEventHandler> client1( | 157 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); |
136 new MockVideoCaptureEventHandler); | |
137 scoped_ptr<MockVideoCaptureEventHandler> client2( | |
138 new MockVideoCaptureEventHandler); | |
139 | 158 |
140 EXPECT_CALL(*client1, OnStarted(_)); | 159 EXPECT_CALL(*client, OnStarted(_)) |
141 EXPECT_CALL(*client1, OnStopped(_)); | 160 .WillOnce(Return()); |
142 EXPECT_CALL(*client1, OnRemoved(_)); | |
143 EXPECT_CALL(*client2, OnStarted(_)); | |
144 EXPECT_CALL(*client2, OnStopped(_)); | |
145 EXPECT_CALL(*client2, OnRemoved(_)) | |
146 .WillOnce(InvokeWithoutArgs(this, &VideoCaptureImplTest::Quit)); | |
147 | 161 |
148 video_capture_impl_->StartCapture(client1.get(), params_small_); | 162 video_capture_impl_->StartCapture(client.get(), params_small_); |
149 video_capture_impl_->StopCapture(client1.get()); | 163 message_loop_->RunUntilIdle(); |
150 video_capture_impl_->StartCapture(client2.get(), params_small_); | 164 |
151 video_capture_impl_->StopCapture(client2.get()); | 165 EXPECT_CALL(*client, OnStopped(_)) |
152 run_loop_.Run(); | 166 .WillOnce(Return()); |
| 167 EXPECT_CALL(*client, OnRemoved(_)) |
| 168 .WillOnce(Return()); |
| 169 |
| 170 video_capture_impl_->StopCapture(client.get()); |
| 171 message_loop_->RunUntilIdle(); |
| 172 |
| 173 EXPECT_CALL(*client, OnStarted(_)) |
| 174 .WillOnce(Return()); |
| 175 |
| 176 video_capture_impl_->StartCapture(client.get(), params_small_); |
| 177 message_loop_->RunUntilIdle(); |
| 178 |
| 179 EXPECT_CALL(*client, OnStopped(_)) |
| 180 .WillOnce(Return()); |
| 181 EXPECT_CALL(*client, OnRemoved(_)) |
| 182 .WillOnce(Return()); |
| 183 |
| 184 video_capture_impl_->StopCapture(client.get()); |
| 185 message_loop_->RunUntilIdle(); |
153 } | 186 } |
154 | 187 |
155 TEST_F(VideoCaptureImplTest, LargeAndSmall) { | 188 TEST_F(VideoCaptureImplTest, LargeAndSmall) { |
156 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. | 189 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. |
157 // The large client starts first and stops first. | 190 // The large client starts first and stops first. |
158 scoped_ptr<MockVideoCaptureEventHandler> client_small( | 191 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); |
159 new MockVideoCaptureEventHandler); | 192 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); |
160 scoped_ptr<MockVideoCaptureEventHandler> client_large( | |
161 new MockVideoCaptureEventHandler); | |
162 | 193 |
163 EXPECT_CALL(*client_large, OnStarted(_)); | 194 EXPECT_CALL(*client_large, OnStarted(_)) |
164 EXPECT_CALL(*client_small, OnStarted(_)); | 195 .WillOnce(Return()); |
165 EXPECT_CALL(*client_large, OnStopped(_)); | 196 EXPECT_CALL(*client_small, OnStarted(_)) |
166 EXPECT_CALL(*client_large, OnRemoved(_)); | 197 .WillOnce(Return()); |
167 EXPECT_CALL(*client_small, OnStopped(_)); | |
168 EXPECT_CALL(*client_small, OnRemoved(_)) | |
169 .WillOnce(InvokeWithoutArgs(this, &VideoCaptureImplTest::Quit)); | |
170 | 198 |
171 video_capture_impl_->StartCapture(client_large.get(), params_large_); | 199 video_capture_impl_->StartCapture(client_large.get(), params_large_); |
172 video_capture_impl_->StartCapture(client_small.get(), params_small_); | 200 video_capture_impl_->StartCapture(client_small.get(), params_small_); |
| 201 message_loop_->RunUntilIdle(); |
| 202 |
| 203 EXPECT_CALL(*client_large, OnStopped(_)) |
| 204 .WillOnce(Return()); |
| 205 EXPECT_CALL(*client_large, OnRemoved(_)) |
| 206 .WillOnce(Return()); |
| 207 EXPECT_CALL(*client_small, OnStopped(_)) |
| 208 .WillOnce(Return()); |
| 209 EXPECT_CALL(*client_small, OnRemoved(_)) |
| 210 .WillOnce(Return()); |
| 211 |
173 video_capture_impl_->StopCapture(client_large.get()); | 212 video_capture_impl_->StopCapture(client_large.get()); |
174 video_capture_impl_->StopCapture(client_small.get()); | 213 video_capture_impl_->StopCapture(client_small.get()); |
175 run_loop_.Run(); | 214 message_loop_->RunUntilIdle(); |
176 } | 215 } |
177 | 216 |
178 TEST_F(VideoCaptureImplTest, SmallAndLarge) { | 217 TEST_F(VideoCaptureImplTest, SmallAndLarge) { |
179 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. | 218 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. |
180 // The small client starts first and stops first. | 219 // The small client starts first and stops first. |
181 scoped_ptr<MockVideoCaptureEventHandler> client_small( | 220 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); |
182 new MockVideoCaptureEventHandler); | 221 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); |
183 scoped_ptr<MockVideoCaptureEventHandler> client_large( | |
184 new MockVideoCaptureEventHandler); | |
185 | 222 |
186 EXPECT_CALL(*client_small, OnStarted(_)); | 223 EXPECT_CALL(*client_large, OnStarted(_)) |
187 EXPECT_CALL(*client_large, OnStarted(_)); | 224 .WillOnce(Return()); |
188 EXPECT_CALL(*client_small, OnStopped(_)); | 225 EXPECT_CALL(*client_small, OnStarted(_)) |
189 EXPECT_CALL(*client_small, OnRemoved(_)); | 226 .WillOnce(Return()); |
190 EXPECT_CALL(*client_large, OnStopped(_)); | |
191 EXPECT_CALL(*client_large, OnRemoved(_)) | |
192 .WillOnce(InvokeWithoutArgs(this, &VideoCaptureImplTest::Quit)); | |
193 | 227 |
194 video_capture_impl_->StartCapture(client_small.get(), params_small_); | 228 video_capture_impl_->StartCapture(client_small.get(), params_small_); |
195 video_capture_impl_->StartCapture(client_large.get(), params_large_); | 229 video_capture_impl_->StartCapture(client_large.get(), params_large_); |
| 230 message_loop_->RunUntilIdle(); |
| 231 |
| 232 EXPECT_CALL(*client_large, OnStopped(_)) |
| 233 .WillOnce(Return()); |
| 234 EXPECT_CALL(*client_large, OnRemoved(_)) |
| 235 .WillOnce(Return()); |
| 236 EXPECT_CALL(*client_small, OnStopped(_)) |
| 237 .WillOnce(Return()); |
| 238 EXPECT_CALL(*client_small, OnRemoved(_)) |
| 239 .WillOnce(Return()); |
| 240 |
196 video_capture_impl_->StopCapture(client_small.get()); | 241 video_capture_impl_->StopCapture(client_small.get()); |
197 video_capture_impl_->StopCapture(client_large.get()); | 242 video_capture_impl_->StopCapture(client_large.get()); |
198 run_loop_.Run(); | 243 message_loop_->RunUntilIdle(); |
| 244 } |
| 245 |
| 246 TEST_F(VideoCaptureImplTest, TwoClientsWithSameSize) { |
| 247 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. |
| 248 // The client1 starts first and stops first. |
| 249 scoped_ptr<MockVideoCaptureClient> client1(new MockVideoCaptureClient); |
| 250 scoped_ptr<MockVideoCaptureClient> client2(new MockVideoCaptureClient); |
| 251 |
| 252 EXPECT_CALL(*client1, OnStarted(_)) |
| 253 .WillOnce(Return()); |
| 254 EXPECT_CALL(*client2, OnStarted(_)) |
| 255 .WillOnce(Return()); |
| 256 |
| 257 video_capture_impl_->StartCapture(client1.get(), params_small_); |
| 258 video_capture_impl_->StartCapture(client2.get(), params_small_); |
| 259 message_loop_->RunUntilIdle(); |
| 260 |
| 261 EXPECT_CALL(*client1, OnStopped(_)) |
| 262 .WillOnce(Return()); |
| 263 EXPECT_CALL(*client1, OnRemoved(_)) |
| 264 .WillOnce(Return()); |
| 265 EXPECT_CALL(*client2, OnStopped(_)) |
| 266 .WillOnce(Return()); |
| 267 EXPECT_CALL(*client2, OnRemoved(_)) |
| 268 .WillOnce(Return()); |
| 269 |
| 270 video_capture_impl_->StopCapture(client1.get()); |
| 271 video_capture_impl_->StopCapture(client2.get()); |
| 272 message_loop_->RunUntilIdle(); |
199 } | 273 } |
200 | 274 |
201 } // namespace content | 275 } // namespace content |
OLD | NEW |