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