| 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 "content/child/child_process.h" | 6 #include "content/child/child_process.h" |
| 7 #include "content/common/media/video_capture_messages.h" | 7 #include "content/common/media/video_capture_messages.h" |
| 8 #include "content/renderer/media/video_capture_impl.h" | 8 #include "content/renderer/media/video_capture_impl.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 MOCK_METHOD1(OnStarted, void(media::VideoCapture* capture)); | 38 MOCK_METHOD1(OnStarted, void(media::VideoCapture* capture)); |
| 39 MOCK_METHOD1(OnStopped, void(media::VideoCapture* capture)); | 39 MOCK_METHOD1(OnStopped, void(media::VideoCapture* capture)); |
| 40 MOCK_METHOD1(OnPaused, void(media::VideoCapture* capture)); | 40 MOCK_METHOD1(OnPaused, void(media::VideoCapture* capture)); |
| 41 MOCK_METHOD2(OnError, void(media::VideoCapture* capture, int error_code)); | 41 MOCK_METHOD2(OnError, void(media::VideoCapture* capture, int error_code)); |
| 42 MOCK_METHOD1(OnRemoved, void(media::VideoCapture* capture)); | 42 MOCK_METHOD1(OnRemoved, void(media::VideoCapture* capture)); |
| 43 MOCK_METHOD2(OnFrameReady, | 43 MOCK_METHOD2(OnFrameReady, |
| 44 void(media::VideoCapture* capture, | 44 void(media::VideoCapture* capture, |
| 45 const scoped_refptr<media::VideoFrame>& frame)); | 45 const scoped_refptr<media::VideoFrame>& frame)); |
| 46 MOCK_METHOD2(OnDeviceInfoReceived, | 46 MOCK_METHOD2(OnDeviceInfoReceived, |
| 47 void(media::VideoCapture* capture, | 47 void(media::VideoCapture* capture, |
| 48 const media::VideoCaptureParams& device_info)); | 48 const media::VideoCaptureFormat& device_info)); |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureClient); | 51 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureClient); |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 class VideoCaptureImplTest : public ::testing::Test { | 54 class VideoCaptureImplTest : public ::testing::Test { |
| 55 public: | 55 public: |
| 56 class MockVideoCaptureImpl : public VideoCaptureImpl { | 56 class MockVideoCaptureImpl : public VideoCaptureImpl { |
| 57 public: | 57 public: |
| 58 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, | 58 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 75 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, | 75 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, |
| 76 DeviceReceiveEmptyBuffer) | 76 DeviceReceiveEmptyBuffer) |
| 77 IPC_MESSAGE_UNHANDLED(handled = false) | 77 IPC_MESSAGE_UNHANDLED(handled = false) |
| 78 IPC_END_MESSAGE_MAP() | 78 IPC_END_MESSAGE_MAP() |
| 79 EXPECT_TRUE(handled); | 79 EXPECT_TRUE(handled); |
| 80 delete message; | 80 delete message; |
| 81 } | 81 } |
| 82 | 82 |
| 83 void DeviceStartCapture(int device_id, | 83 void DeviceStartCapture(int device_id, |
| 84 const media::VideoCaptureParams& params) { | 84 const media::VideoCaptureParams& params) { |
| 85 media::VideoCaptureParams device_info = params; | |
| 86 OnDeviceInfoReceived(device_info); | |
| 87 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED); | 85 OnStateChanged(VIDEO_CAPTURE_STATE_STARTED); |
| 88 } | 86 } |
| 89 | 87 |
| 90 void DevicePauseCapture(int device_id) {} | 88 void DevicePauseCapture(int device_id) {} |
| 91 | 89 |
| 92 void DeviceStopCapture(int device_id) { | 90 void DeviceStopCapture(int device_id) { |
| 93 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); | 91 OnStateChanged(VIDEO_CAPTURE_STATE_STOPPED); |
| 94 } | 92 } |
| 95 | 93 |
| 96 void DeviceReceiveEmptyBuffer(int device_id, int buffer_id) {} | 94 void DeviceReceiveEmptyBuffer(int device_id, int buffer_id) {} |
| 97 }; | 95 }; |
| 98 | 96 |
| 99 VideoCaptureImplTest() | 97 VideoCaptureImplTest() { |
| 100 : capability_small_(176, | 98 params_small_.width = 176; |
| 101 144, | 99 params_small_.height = 144; |
| 102 30, | 100 params_small_.frame_rate = 30; |
| 103 media::PIXEL_FORMAT_I420, | 101 params_small_.frame_size_type = media::ConstantResolutionVideoCaptureDevice; |
| 104 0, | 102 |
| 105 false, | 103 params_large_.width = 320; |
| 106 media::ConstantResolutionVideoCaptureDevice), | 104 params_large_.height = 240; |
| 107 capability_large_(320, | 105 params_large_.frame_rate = 30; |
| 108 240, | 106 params_large_.frame_size_type = media::ConstantResolutionVideoCaptureDevice; |
| 109 30, | 107 |
| 110 media::PIXEL_FORMAT_I420, | |
| 111 0, | |
| 112 false, | |
| 113 media::ConstantResolutionVideoCaptureDevice) { | |
| 114 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO)); | 108 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_IO)); |
| 115 message_loop_proxy_ = base::MessageLoopProxy::current().get(); | 109 message_loop_proxy_ = base::MessageLoopProxy::current().get(); |
| 116 child_process_.reset(new ChildProcess()); | 110 child_process_.reset(new ChildProcess()); |
| 117 | 111 |
| 118 message_filter_ = new MockVideoCaptureMessageFilter; | 112 message_filter_ = new MockVideoCaptureMessageFilter; |
| 119 session_id_ = 1; | 113 session_id_ = 1; |
| 120 | 114 |
| 121 video_capture_impl_ = new MockVideoCaptureImpl( | 115 video_capture_impl_ = new MockVideoCaptureImpl( |
| 122 session_id_, message_loop_proxy_, message_filter_.get()); | 116 session_id_, message_loop_proxy_, message_filter_.get()); |
| 123 | 117 |
| 124 video_capture_impl_->device_id_ = 2; | 118 video_capture_impl_->device_id_ = 2; |
| 125 } | 119 } |
| 126 | 120 |
| 127 virtual ~VideoCaptureImplTest() { | 121 virtual ~VideoCaptureImplTest() { |
| 128 delete video_capture_impl_; | 122 delete video_capture_impl_; |
| 129 } | 123 } |
| 130 | 124 |
| 131 protected: | 125 protected: |
| 132 scoped_ptr<base::MessageLoop> message_loop_; | 126 scoped_ptr<base::MessageLoop> message_loop_; |
| 133 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 127 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 134 scoped_ptr<ChildProcess> child_process_; | 128 scoped_ptr<ChildProcess> child_process_; |
| 135 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; | 129 scoped_refptr<MockVideoCaptureMessageFilter> message_filter_; |
| 136 media::VideoCaptureSessionId session_id_; | 130 media::VideoCaptureSessionId session_id_; |
| 137 MockVideoCaptureImpl* video_capture_impl_; | 131 MockVideoCaptureImpl* video_capture_impl_; |
| 138 const media::VideoCaptureCapability capability_small_; | 132 media::VideoCaptureParams params_small_; |
| 139 const media::VideoCaptureCapability capability_large_; | 133 media::VideoCaptureParams params_large_; |
| 140 | 134 |
| 141 private: | 135 private: |
| 142 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); | 136 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); |
| 143 }; | 137 }; |
| 144 | 138 |
| 145 TEST_F(VideoCaptureImplTest, Simple) { | 139 TEST_F(VideoCaptureImplTest, Simple) { |
| 146 // Execute SetCapture() and StopCapture() for one client. | 140 // Execute SetCapture() and StopCapture() for one client. |
| 147 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); | 141 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); |
| 148 | 142 |
| 149 EXPECT_CALL(*client, OnStarted(_)) | 143 EXPECT_CALL(*client, OnStarted(_)) |
| 150 .WillOnce(Return()); | 144 .WillOnce(Return()); |
| 151 EXPECT_CALL(*client, OnDeviceInfoReceived(_,_)) | |
| 152 .WillOnce(Return()); | |
| 153 | 145 |
| 154 video_capture_impl_->StartCapture(client.get(), capability_small_); | 146 video_capture_impl_->StartCapture(client.get(), params_small_); |
| 155 message_loop_->RunUntilIdle(); | 147 message_loop_->RunUntilIdle(); |
| 156 | 148 |
| 157 EXPECT_CALL(*client, OnStopped(_)) | 149 EXPECT_CALL(*client, OnStopped(_)) |
| 158 .WillOnce(Return()); | 150 .WillOnce(Return()); |
| 159 EXPECT_CALL(*client, OnRemoved(_)) | 151 EXPECT_CALL(*client, OnRemoved(_)) |
| 160 .WillOnce(Return()); | 152 .WillOnce(Return()); |
| 161 | 153 |
| 162 video_capture_impl_->StopCapture(client.get()); | 154 video_capture_impl_->StopCapture(client.get()); |
| 163 message_loop_->RunUntilIdle(); | 155 message_loop_->RunUntilIdle(); |
| 164 } | 156 } |
| 165 | 157 |
| 166 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { | 158 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { |
| 167 // Execute SetCapture() and StopCapture() for 2 clients in sequence. | 159 // Execute SetCapture() and StopCapture() for 2 clients in sequence. |
| 168 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); | 160 scoped_ptr<MockVideoCaptureClient> client(new MockVideoCaptureClient); |
| 169 | 161 |
| 170 EXPECT_CALL(*client, OnStarted(_)) | 162 EXPECT_CALL(*client, OnStarted(_)) |
| 171 .WillOnce(Return()); | 163 .WillOnce(Return()); |
| 172 EXPECT_CALL(*client, OnDeviceInfoReceived(_,_)) | |
| 173 .WillOnce(Return()); | |
| 174 | 164 |
| 175 video_capture_impl_->StartCapture(client.get(), capability_small_); | 165 video_capture_impl_->StartCapture(client.get(), params_small_); |
| 176 message_loop_->RunUntilIdle(); | 166 message_loop_->RunUntilIdle(); |
| 177 | 167 |
| 178 EXPECT_CALL(*client, OnStopped(_)) | 168 EXPECT_CALL(*client, OnStopped(_)) |
| 179 .WillOnce(Return()); | 169 .WillOnce(Return()); |
| 180 EXPECT_CALL(*client, OnRemoved(_)) | 170 EXPECT_CALL(*client, OnRemoved(_)) |
| 181 .WillOnce(Return()); | 171 .WillOnce(Return()); |
| 182 | 172 |
| 183 video_capture_impl_->StopCapture(client.get()); | 173 video_capture_impl_->StopCapture(client.get()); |
| 184 message_loop_->RunUntilIdle(); | 174 message_loop_->RunUntilIdle(); |
| 185 | 175 |
| 186 EXPECT_CALL(*client, OnStarted(_)) | 176 EXPECT_CALL(*client, OnStarted(_)) |
| 187 .WillOnce(Return()); | 177 .WillOnce(Return()); |
| 188 EXPECT_CALL(*client, OnDeviceInfoReceived(_,_)) | |
| 189 .WillOnce(Return()); | |
| 190 | 178 |
| 191 video_capture_impl_->StartCapture(client.get(), capability_small_); | 179 video_capture_impl_->StartCapture(client.get(), params_small_); |
| 192 message_loop_->RunUntilIdle(); | 180 message_loop_->RunUntilIdle(); |
| 193 | 181 |
| 194 EXPECT_CALL(*client, OnStopped(_)) | 182 EXPECT_CALL(*client, OnStopped(_)) |
| 195 .WillOnce(Return()); | 183 .WillOnce(Return()); |
| 196 EXPECT_CALL(*client, OnRemoved(_)) | 184 EXPECT_CALL(*client, OnRemoved(_)) |
| 197 .WillOnce(Return()); | 185 .WillOnce(Return()); |
| 198 | 186 |
| 199 video_capture_impl_->StopCapture(client.get()); | 187 video_capture_impl_->StopCapture(client.get()); |
| 200 message_loop_->RunUntilIdle(); | 188 message_loop_->RunUntilIdle(); |
| 201 } | 189 } |
| 202 | 190 |
| 203 TEST_F(VideoCaptureImplTest, LargeAndSmall) { | 191 TEST_F(VideoCaptureImplTest, LargeAndSmall) { |
| 204 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. | 192 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. |
| 205 // The large client starts first and stops first. | 193 // The large client starts first and stops first. |
| 206 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); | 194 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); |
| 207 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); | 195 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); |
| 208 | 196 |
| 209 EXPECT_CALL(*client_large, OnStarted(_)) | 197 EXPECT_CALL(*client_large, OnStarted(_)) |
| 210 .WillOnce(Return()); | 198 .WillOnce(Return()); |
| 211 EXPECT_CALL(*client_large, OnDeviceInfoReceived(_,_)) | |
| 212 .WillOnce(Return()); | |
| 213 EXPECT_CALL(*client_small, OnStarted(_)) | 199 EXPECT_CALL(*client_small, OnStarted(_)) |
| 214 .WillOnce(Return()); | 200 .WillOnce(Return()); |
| 215 EXPECT_CALL(*client_small, OnDeviceInfoReceived(_,_)) | |
| 216 .WillOnce(Return()); | |
| 217 | 201 |
| 218 video_capture_impl_->StartCapture(client_large.get(), capability_large_); | 202 video_capture_impl_->StartCapture(client_large.get(), params_large_); |
| 219 video_capture_impl_->StartCapture(client_small.get(), capability_small_); | 203 video_capture_impl_->StartCapture(client_small.get(), params_small_); |
| 220 message_loop_->RunUntilIdle(); | 204 message_loop_->RunUntilIdle(); |
| 221 | 205 |
| 222 EXPECT_CALL(*client_large, OnStopped(_)) | 206 EXPECT_CALL(*client_large, OnStopped(_)) |
| 223 .WillOnce(Return()); | 207 .WillOnce(Return()); |
| 224 EXPECT_CALL(*client_large, OnRemoved(_)) | 208 EXPECT_CALL(*client_large, OnRemoved(_)) |
| 225 .WillOnce(Return()); | 209 .WillOnce(Return()); |
| 226 EXPECT_CALL(*client_small, OnStopped(_)) | 210 EXPECT_CALL(*client_small, OnStopped(_)) |
| 227 .WillOnce(Return()); | 211 .WillOnce(Return()); |
| 228 EXPECT_CALL(*client_small, OnRemoved(_)) | 212 EXPECT_CALL(*client_small, OnRemoved(_)) |
| 229 .WillOnce(Return()); | 213 .WillOnce(Return()); |
| 230 | 214 |
| 231 video_capture_impl_->StopCapture(client_large.get()); | 215 video_capture_impl_->StopCapture(client_large.get()); |
| 232 video_capture_impl_->StopCapture(client_small.get()); | 216 video_capture_impl_->StopCapture(client_small.get()); |
| 233 message_loop_->RunUntilIdle(); | 217 message_loop_->RunUntilIdle(); |
| 234 } | 218 } |
| 235 | 219 |
| 236 TEST_F(VideoCaptureImplTest, SmallAndLarge) { | 220 TEST_F(VideoCaptureImplTest, SmallAndLarge) { |
| 237 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. | 221 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. |
| 238 // The small client starts first and stops first. | 222 // The small client starts first and stops first. |
| 239 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); | 223 scoped_ptr<MockVideoCaptureClient> client_small(new MockVideoCaptureClient); |
| 240 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); | 224 scoped_ptr<MockVideoCaptureClient> client_large(new MockVideoCaptureClient); |
| 241 | 225 |
| 242 EXPECT_CALL(*client_large, OnStarted(_)) | 226 EXPECT_CALL(*client_large, OnStarted(_)) |
| 243 .WillOnce(Return()); | 227 .WillOnce(Return()); |
| 244 EXPECT_CALL(*client_large, OnDeviceInfoReceived(_,_)) | |
| 245 .WillOnce(Return()); | |
| 246 EXPECT_CALL(*client_small, OnStarted(_)) | 228 EXPECT_CALL(*client_small, OnStarted(_)) |
| 247 .WillOnce(Return()); | 229 .WillOnce(Return()); |
| 248 EXPECT_CALL(*client_small, OnDeviceInfoReceived(_,_)) | |
| 249 .Times(AtLeast(1)) | |
| 250 .WillRepeatedly(Return()); | |
| 251 | 230 |
| 252 video_capture_impl_->StartCapture(client_small.get(), capability_small_); | 231 video_capture_impl_->StartCapture(client_small.get(), params_small_); |
| 253 video_capture_impl_->StartCapture(client_large.get(), capability_large_); | 232 video_capture_impl_->StartCapture(client_large.get(), params_large_); |
| 254 message_loop_->RunUntilIdle(); | 233 message_loop_->RunUntilIdle(); |
| 255 | 234 |
| 256 EXPECT_CALL(*client_large, OnStopped(_)) | 235 EXPECT_CALL(*client_large, OnStopped(_)) |
| 257 .WillOnce(Return()); | 236 .WillOnce(Return()); |
| 258 EXPECT_CALL(*client_large, OnRemoved(_)) | 237 EXPECT_CALL(*client_large, OnRemoved(_)) |
| 259 .WillOnce(Return()); | 238 .WillOnce(Return()); |
| 260 EXPECT_CALL(*client_small, OnStopped(_)) | 239 EXPECT_CALL(*client_small, OnStopped(_)) |
| 261 .WillOnce(Return()); | 240 .WillOnce(Return()); |
| 262 EXPECT_CALL(*client_small, OnRemoved(_)) | 241 EXPECT_CALL(*client_small, OnRemoved(_)) |
| 263 .WillOnce(Return()); | 242 .WillOnce(Return()); |
| 264 | 243 |
| 265 video_capture_impl_->StopCapture(client_small.get()); | 244 video_capture_impl_->StopCapture(client_small.get()); |
| 266 video_capture_impl_->StopCapture(client_large.get()); | 245 video_capture_impl_->StopCapture(client_large.get()); |
| 267 message_loop_->RunUntilIdle(); | 246 message_loop_->RunUntilIdle(); |
| 268 } | 247 } |
| 269 | 248 |
| 270 TEST_F(VideoCaptureImplTest, TwoClientsWithSameSize) { | 249 TEST_F(VideoCaptureImplTest, TwoClientsWithSameSize) { |
| 271 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. | 250 // Execute SetCapture() and StopCapture() for 2 clients simultaneously. |
| 272 // The client1 starts first and stops first. | 251 // The client1 starts first and stops first. |
| 273 scoped_ptr<MockVideoCaptureClient> client1(new MockVideoCaptureClient); | 252 scoped_ptr<MockVideoCaptureClient> client1(new MockVideoCaptureClient); |
| 274 scoped_ptr<MockVideoCaptureClient> client2(new MockVideoCaptureClient); | 253 scoped_ptr<MockVideoCaptureClient> client2(new MockVideoCaptureClient); |
| 275 | 254 |
| 276 EXPECT_CALL(*client1, OnStarted(_)) | 255 EXPECT_CALL(*client1, OnStarted(_)) |
| 277 .WillOnce(Return()); | 256 .WillOnce(Return()); |
| 278 EXPECT_CALL(*client1, OnDeviceInfoReceived(_,_)) | |
| 279 .WillOnce(Return()); | |
| 280 EXPECT_CALL(*client2, OnStarted(_)) | 257 EXPECT_CALL(*client2, OnStarted(_)) |
| 281 .WillOnce(Return()); | 258 .WillOnce(Return()); |
| 282 EXPECT_CALL(*client2, OnDeviceInfoReceived(_,_)) | |
| 283 .WillOnce(Return()); | |
| 284 | 259 |
| 285 video_capture_impl_->StartCapture(client1.get(), capability_small_); | 260 video_capture_impl_->StartCapture(client1.get(), params_small_); |
| 286 video_capture_impl_->StartCapture(client2.get(), capability_small_); | 261 video_capture_impl_->StartCapture(client2.get(), params_small_); |
| 287 message_loop_->RunUntilIdle(); | 262 message_loop_->RunUntilIdle(); |
| 288 | 263 |
| 289 EXPECT_CALL(*client1, OnStopped(_)) | 264 EXPECT_CALL(*client1, OnStopped(_)) |
| 290 .WillOnce(Return()); | 265 .WillOnce(Return()); |
| 291 EXPECT_CALL(*client1, OnRemoved(_)) | 266 EXPECT_CALL(*client1, OnRemoved(_)) |
| 292 .WillOnce(Return()); | 267 .WillOnce(Return()); |
| 293 EXPECT_CALL(*client2, OnStopped(_)) | 268 EXPECT_CALL(*client2, OnStopped(_)) |
| 294 .WillOnce(Return()); | 269 .WillOnce(Return()); |
| 295 EXPECT_CALL(*client2, OnRemoved(_)) | 270 EXPECT_CALL(*client2, OnRemoved(_)) |
| 296 .WillOnce(Return()); | 271 .WillOnce(Return()); |
| 297 | 272 |
| 298 video_capture_impl_->StopCapture(client1.get()); | 273 video_capture_impl_->StopCapture(client1.get()); |
| 299 video_capture_impl_->StopCapture(client2.get()); | 274 video_capture_impl_->StopCapture(client2.get()); |
| 300 message_loop_->RunUntilIdle(); | 275 message_loop_->RunUntilIdle(); |
| 301 } | 276 } |
| 302 | 277 |
| 303 } // namespace content | 278 } // namespace content |
| OLD | NEW |