| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Unit test for VideoCaptureController. |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/process_util.h" |
| 14 #include "content/browser/browser_thread_impl.h" |
| 15 #include "content/browser/renderer_host/media/media_stream_provider.h" |
| 16 #include "content/browser/renderer_host/media/video_capture_controller.h" |
| 17 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 18 #include "content/common/media/media_stream_options.h" |
| 19 #include "media/video/capture/fake_video_capture_device.h" |
| 20 #include "media/video/capture/video_capture_device.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 |
| 24 using ::testing::_; |
| 25 using ::testing::AnyNumber; |
| 26 using ::testing::AtLeast; |
| 27 using ::testing::InSequence; |
| 28 using ::testing::Return; |
| 29 using content::BrowserThread; |
| 30 using content::BrowserThreadImpl; |
| 31 |
| 32 enum { kDeviceId = 1 }; |
| 33 |
| 34 ACTION_P5(StopCapture, controller, controller_id, controller_handler, flag, |
| 35 message_loop) { |
| 36 message_loop->PostTask(FROM_HERE, |
| 37 base::Bind(&VideoCaptureController::StopCapture, |
| 38 controller, controller_id, controller_handler, flag)); |
| 39 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 40 } |
| 41 |
| 42 ACTION_P3(StopSession, controller, session_id, message_loop) { |
| 43 message_loop->PostTask(FROM_HERE, |
| 44 base::Bind(&VideoCaptureController::StopSession, |
| 45 controller, session_id)); |
| 46 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 47 } |
| 48 |
| 49 class MockVideoCaptureControllerEventHandler |
| 50 : public VideoCaptureControllerEventHandler { |
| 51 public: |
| 52 MockVideoCaptureControllerEventHandler(VideoCaptureController* controller, |
| 53 MessageLoop* message_loop) |
| 54 : controller_(controller), |
| 55 message_loop_(message_loop), |
| 56 controller_id_(kDeviceId), |
| 57 process_handle_(base::kNullProcessHandle) { |
| 58 } |
| 59 virtual ~MockVideoCaptureControllerEventHandler() {} |
| 60 |
| 61 MOCK_METHOD1(DoBufferCreated, void(const VideoCaptureControllerID&)); |
| 62 MOCK_METHOD1(DoBufferReady, void(const VideoCaptureControllerID&)); |
| 63 MOCK_METHOD1(DoFrameInfo, void(const VideoCaptureControllerID&)); |
| 64 MOCK_METHOD1(DoPaused, void(const VideoCaptureControllerID&)); |
| 65 |
| 66 virtual void OnError(const VideoCaptureControllerID& id) OVERRIDE {} |
| 67 virtual void OnBufferCreated(const VideoCaptureControllerID& id, |
| 68 base::SharedMemoryHandle handle, |
| 69 int length, int buffer_id) OVERRIDE { |
| 70 EXPECT_EQ(id, controller_id_); |
| 71 DoBufferCreated(id); |
| 72 } |
| 73 virtual void OnBufferReady(const VideoCaptureControllerID& id, |
| 74 int buffer_id, |
| 75 base::Time timestamp) OVERRIDE { |
| 76 EXPECT_EQ(id, controller_id_); |
| 77 DoBufferReady(id); |
| 78 message_loop_->PostTask(FROM_HERE, |
| 79 base::Bind(&VideoCaptureController::ReturnBuffer, |
| 80 controller_, controller_id_, this, buffer_id)); |
| 81 } |
| 82 virtual void OnFrameInfo(const VideoCaptureControllerID& id, |
| 83 int width, |
| 84 int height, |
| 85 int frame_per_second) OVERRIDE { |
| 86 EXPECT_EQ(id, controller_id_); |
| 87 DoFrameInfo(id); |
| 88 } |
| 89 virtual void OnPaused(const VideoCaptureControllerID& id) OVERRIDE { |
| 90 EXPECT_EQ(id, controller_id_); |
| 91 DoPaused(id); |
| 92 } |
| 93 virtual void OnReadyToDelete(const VideoCaptureControllerID& id) OVERRIDE {} |
| 94 |
| 95 scoped_refptr<VideoCaptureController> controller_; |
| 96 MessageLoop* message_loop_; |
| 97 VideoCaptureControllerID controller_id_; |
| 98 base::ProcessHandle process_handle_; |
| 99 }; |
| 100 |
| 101 class MockVideoCaptureManager |
| 102 : public media_stream::VideoCaptureManager { |
| 103 public: |
| 104 MockVideoCaptureManager() |
| 105 : video_session_id_(kStartOpenSessionId) {} |
| 106 |
| 107 void Init() { |
| 108 device_name_.unique_id = "/dev/video0"; |
| 109 device_name_.device_name = "fake_device_0"; |
| 110 |
| 111 video_capture_device_.reset( |
| 112 media::FakeVideoCaptureDevice::Create(device_name_)); |
| 113 ASSERT_TRUE(video_capture_device_.get() != NULL); |
| 114 } |
| 115 |
| 116 MOCK_METHOD3(StartCapture, void(int, int, |
| 117 media::VideoCaptureDevice::EventHandler*)); |
| 118 MOCK_METHOD1(StopCapture, void(const media::VideoCaptureSessionId&)); |
| 119 |
| 120 void Start(const media::VideoCaptureParams& capture_params, |
| 121 media::VideoCaptureDevice::EventHandler* vc_receiver) OVERRIDE { |
| 122 StartCapture(capture_params.width, capture_params.height, vc_receiver); |
| 123 video_capture_device_->Allocate(capture_params.width, capture_params.height, |
| 124 capture_params.frame_per_second, |
| 125 vc_receiver); |
| 126 video_capture_device_->Start(); |
| 127 } |
| 128 |
| 129 void Stop(const media::VideoCaptureSessionId& capture_session_id, |
| 130 base::Closure stopped_cb) OVERRIDE { |
| 131 StopCapture(capture_session_id); |
| 132 video_capture_device_->Stop(); |
| 133 video_capture_device_->DeAllocate(); |
| 134 } |
| 135 |
| 136 void Error(const media::VideoCaptureSessionId& capture_session_id) OVERRIDE { |
| 137 } |
| 138 |
| 139 int video_session_id_; |
| 140 media::VideoCaptureDevice::Name device_name_; |
| 141 scoped_ptr<media::VideoCaptureDevice> video_capture_device_; |
| 142 |
| 143 private: |
| 144 virtual ~MockVideoCaptureManager() {} |
| 145 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureManager); |
| 146 }; |
| 147 |
| 148 // Test class. |
| 149 class VideoCaptureControllerTest : public testing::Test { |
| 150 public: |
| 151 VideoCaptureControllerTest() {} |
| 152 virtual ~VideoCaptureControllerTest() {} |
| 153 |
| 154 protected: |
| 155 virtual void SetUp() { |
| 156 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| 157 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, |
| 158 message_loop_.get())); |
| 159 vcm_ = new MockVideoCaptureManager(); |
| 160 vcm_->Init(); |
| 161 controller_ = new VideoCaptureController(vcm_); |
| 162 controller_handler_.reset( |
| 163 new MockVideoCaptureControllerEventHandler(controller_.get(), |
| 164 message_loop_.get())); |
| 165 } |
| 166 |
| 167 virtual void TearDown() { |
| 168 io_thread_.reset(); |
| 169 } |
| 170 |
| 171 scoped_ptr<MessageLoop> message_loop_; |
| 172 scoped_ptr<BrowserThreadImpl> io_thread_; |
| 173 scoped_refptr<MockVideoCaptureManager> vcm_; |
| 174 scoped_ptr<MockVideoCaptureControllerEventHandler> controller_handler_; |
| 175 scoped_refptr<VideoCaptureController> controller_; |
| 176 |
| 177 private: |
| 178 DISALLOW_COPY_AND_ASSIGN(VideoCaptureControllerTest); |
| 179 }; |
| 180 |
| 181 // Try to start and stop capture. |
| 182 TEST_F(VideoCaptureControllerTest, StartAndStop) { |
| 183 media::VideoCaptureParams capture_params; |
| 184 capture_params.session_id = vcm_->video_session_id_; |
| 185 capture_params.width = 320; |
| 186 capture_params.height = 240; |
| 187 capture_params.frame_per_second = 30; |
| 188 |
| 189 InSequence s; |
| 190 EXPECT_CALL(*vcm_, |
| 191 StartCapture(capture_params.width, |
| 192 capture_params.height, |
| 193 controller_.get())) |
| 194 .Times(1); |
| 195 EXPECT_CALL(*controller_handler_, |
| 196 DoFrameInfo(controller_handler_->controller_id_)) |
| 197 .Times(AtLeast(1)); |
| 198 EXPECT_CALL(*controller_handler_, |
| 199 DoBufferCreated(controller_handler_->controller_id_)) |
| 200 .Times(AtLeast(1)); |
| 201 EXPECT_CALL(*controller_handler_, |
| 202 DoBufferReady(controller_handler_->controller_id_)) |
| 203 .Times(AtLeast(1)) |
| 204 .WillOnce(StopCapture(controller_.get(), |
| 205 controller_handler_->controller_id_, |
| 206 controller_handler_.get(), true, |
| 207 message_loop_.get())); |
| 208 EXPECT_CALL(*vcm_, |
| 209 StopCapture(vcm_->video_session_id_)) |
| 210 .Times(1); |
| 211 |
| 212 controller_->StartCapture(controller_handler_->controller_id_, |
| 213 controller_handler_.get(), |
| 214 controller_handler_->process_handle_, |
| 215 capture_params); |
| 216 message_loop_->Run(); |
| 217 } |
| 218 |
| 219 // Try to stop session before stopping capture. |
| 220 TEST_F(VideoCaptureControllerTest, StopSession) { |
| 221 media::VideoCaptureParams capture_params; |
| 222 capture_params.session_id = vcm_->video_session_id_; |
| 223 capture_params.width = 320; |
| 224 capture_params.height = 240; |
| 225 capture_params.frame_per_second = 30; |
| 226 |
| 227 InSequence s; |
| 228 EXPECT_CALL(*vcm_, |
| 229 StartCapture(capture_params.width, |
| 230 capture_params.height, |
| 231 controller_.get())) |
| 232 .Times(1); |
| 233 EXPECT_CALL(*controller_handler_, |
| 234 DoFrameInfo(controller_handler_->controller_id_)) |
| 235 .Times(AtLeast(1)); |
| 236 EXPECT_CALL(*controller_handler_, |
| 237 DoBufferCreated(controller_handler_->controller_id_)) |
| 238 .Times(AtLeast(1)); |
| 239 EXPECT_CALL(*controller_handler_, |
| 240 DoBufferReady(controller_handler_->controller_id_)) |
| 241 .Times(AtLeast(1)) |
| 242 .WillOnce(StopSession(controller_.get(), |
| 243 vcm_->video_session_id_, |
| 244 message_loop_.get())); |
| 245 EXPECT_CALL(*controller_handler_, |
| 246 DoPaused(controller_handler_->controller_id_)) |
| 247 .Times(1); |
| 248 |
| 249 controller_->StartCapture(controller_handler_->controller_id_, |
| 250 controller_handler_.get(), |
| 251 controller_handler_->process_handle_, |
| 252 capture_params); |
| 253 message_loop_->Run(); |
| 254 |
| 255 // The session is stopped now. There should be no buffer coming from |
| 256 // controller. |
| 257 EXPECT_CALL(*controller_handler_, |
| 258 DoBufferReady(controller_handler_->controller_id_)) |
| 259 .Times(0); |
| 260 message_loop_->PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 1000); |
| 261 message_loop_->Run(); |
| 262 |
| 263 EXPECT_CALL(*vcm_, |
| 264 StopCapture(vcm_->video_session_id_)) |
| 265 .Times(1); |
| 266 controller_->StopCapture(controller_handler_->controller_id_, |
| 267 controller_handler_.get(), true); |
| 268 } |
| OLD | NEW |