Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / media / web_contents_video_capture_device_unittest.cc
blob4c7e4d95f826d80e3874c6a7066016383faeeabf
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.
5 #include "content/browser/renderer_host/media/web_contents_video_capture_device.h"
7 #include "base/bind_helpers.h"
8 #include "base/debug/debugger.h"
9 #include "base/run_loop.h"
10 #include "base/test/test_timeouts.h"
11 #include "base/time.h"
12 #include "base/timer.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h"
15 #include "content/browser/renderer_host/media/video_capture_oracle.h"
16 #include "content/browser/renderer_host/media/web_contents_capture_util.h"
17 #include "content/browser/renderer_host/render_view_host_factory.h"
18 #include "content/browser/renderer_host/render_widget_host_impl.h"
19 #include "content/browser/renderer_host/test_render_view_host.h"
20 #include "content/port/browser/render_widget_host_view_frame_subscriber.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_types.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "content/public/test/test_utils.h"
27 #include "content/test/test_web_contents.h"
28 #include "media/base/video_util.h"
29 #include "media/base/yuv_convert.h"
30 #include "media/video/capture/video_capture_types.h"
31 #include "skia/ext/platform_canvas.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "third_party/skia/include/core/SkColor.h"
35 namespace content {
36 namespace {
38 const int kTestWidth = 320;
39 const int kTestHeight = 240;
40 const int kTestFramesPerSecond = 20;
41 const SkColor kNothingYet = 0xdeadbeef;
42 const SkColor kNotInterested = ~kNothingYet;
44 void DeadlineExceeded(base::Closure quit_closure) {
45 if (!base::debug::BeingDebugged()) {
46 quit_closure.Run();
47 FAIL() << "Deadline exceeded while waiting, quitting";
48 } else {
49 LOG(WARNING) << "Deadline exceeded; test would fail if debugger weren't "
50 << "attached.";
54 void RunCurrentLoopWithDeadline() {
55 base::Timer deadline(false, false);
56 deadline.Start(FROM_HERE, TestTimeouts::action_max_timeout(), base::Bind(
57 &DeadlineExceeded, base::MessageLoop::current()->QuitClosure()));
58 base::MessageLoop::current()->Run();
59 deadline.Stop();
62 SkColor ConvertRgbToYuv(SkColor rgb) {
63 uint8 yuv[3];
64 media::ConvertRGB32ToYUV(reinterpret_cast<uint8*>(&rgb),
65 yuv, yuv + 1, yuv + 2, 1, 1, 1, 1, 1);
66 return SkColorSetRGB(yuv[0], yuv[1], yuv[2]);
69 // Thread-safe class that controls the source pattern to be captured by the
70 // system under test. The lifetime of this class is greater than the lifetime
71 // of all objects that reference it, so it does not need to be reference
72 // counted.
73 class CaptureTestSourceController {
74 public:
76 CaptureTestSourceController()
77 : color_(SK_ColorMAGENTA),
78 copy_result_size_(kTestWidth, kTestHeight),
79 can_copy_to_video_frame_(false),
80 use_frame_subscriber_(false) {}
82 void SetSolidColor(SkColor color) {
83 base::AutoLock guard(lock_);
84 color_ = color;
87 SkColor GetSolidColor() {
88 base::AutoLock guard(lock_);
89 return color_;
92 void SetCopyResultSize(int width, int height) {
93 base::AutoLock guard(lock_);
94 copy_result_size_ = gfx::Size(width, height);
97 gfx::Size GetCopyResultSize() {
98 base::AutoLock guard(lock_);
99 return copy_result_size_;
102 void SignalCopy() {
103 // TODO(nick): This actually should always be happening on the UI thread.
104 base::AutoLock guard(lock_);
105 if (!copy_done_.is_null()) {
106 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, copy_done_);
107 copy_done_.Reset();
111 void SetCanCopyToVideoFrame(bool value) {
112 base::AutoLock guard(lock_);
113 can_copy_to_video_frame_ = value;
116 bool CanCopyToVideoFrame() {
117 base::AutoLock guard(lock_);
118 return can_copy_to_video_frame_;
121 void SetUseFrameSubscriber(bool value) {
122 base::AutoLock guard(lock_);
123 use_frame_subscriber_ = value;
126 bool CanUseFrameSubscriber() {
127 base::AutoLock guard(lock_);
128 return use_frame_subscriber_;
131 void WaitForNextCopy() {
133 base::AutoLock guard(lock_);
134 copy_done_ = base::MessageLoop::current()->QuitClosure();
137 RunCurrentLoopWithDeadline();
140 private:
141 base::Lock lock_; // Guards changes to all members.
142 SkColor color_;
143 gfx::Size copy_result_size_;
144 bool can_copy_to_video_frame_;
145 bool use_frame_subscriber_;
146 base::Closure copy_done_;
148 DISALLOW_COPY_AND_ASSIGN(CaptureTestSourceController);
151 // A stub implementation which returns solid-color bitmaps in calls to
152 // CopyFromCompositingSurfaceToVideoFrame(), and which allows the video-frame
153 // readback path to be switched on and off. The behavior is controlled by a
154 // CaptureTestSourceController.
155 class CaptureTestView : public TestRenderWidgetHostView {
156 public:
157 explicit CaptureTestView(RenderWidgetHostImpl* rwh,
158 CaptureTestSourceController* controller)
159 : TestRenderWidgetHostView(rwh),
160 controller_(controller) {}
162 virtual ~CaptureTestView() {}
164 // TestRenderWidgetHostView overrides.
165 virtual gfx::Rect GetViewBounds() const OVERRIDE {
166 return gfx::Rect(100, 100, 100 + kTestWidth, 100 + kTestHeight);
169 virtual bool CanCopyToVideoFrame() const OVERRIDE {
170 return controller_->CanCopyToVideoFrame();
173 virtual void CopyFromCompositingSurfaceToVideoFrame(
174 const gfx::Rect& src_subrect,
175 const scoped_refptr<media::VideoFrame>& target,
176 const base::Callback<void(bool)>& callback) OVERRIDE {
177 SkColor c = ConvertRgbToYuv(controller_->GetSolidColor());
178 media::FillYUV(
179 target.get(), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
180 callback.Run(true);
181 controller_->SignalCopy();
184 virtual void BeginFrameSubscription(
185 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) OVERRIDE {
186 subscriber_.reset(subscriber.release());
189 virtual void EndFrameSubscription() OVERRIDE {
190 subscriber_.reset();
193 // Simulate a compositor paint event for our subscriber.
194 void SimulateUpdate() {
195 const base::Time present_time = base::Time::Now();
196 RenderWidgetHostViewFrameSubscriber::DeliverFrameCallback callback;
197 scoped_refptr<media::VideoFrame> target;
198 if (subscriber_ && subscriber_->ShouldCaptureFrame(present_time,
199 &target, &callback)) {
200 SkColor c = ConvertRgbToYuv(controller_->GetSolidColor());
201 media::FillYUV(
202 target.get(), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
203 BrowserThread::PostTask(BrowserThread::UI,
204 FROM_HERE,
205 base::Bind(callback, present_time, true));
206 controller_->SignalCopy();
210 private:
211 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber_;
212 CaptureTestSourceController* const controller_;
214 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestView);
217 #if defined(COMPILER_MSVC)
218 // MSVC warns on diamond inheritance. See comment for same warning on
219 // RenderViewHostImpl.
220 #pragma warning(push)
221 #pragma warning(disable: 4250)
222 #endif
224 // A stub implementation which returns solid-color bitmaps in calls to
225 // CopyFromBackingStore(). The behavior is controlled by a
226 // CaptureTestSourceController.
227 class CaptureTestRenderViewHost : public TestRenderViewHost {
228 public:
229 CaptureTestRenderViewHost(SiteInstance* instance,
230 RenderViewHostDelegate* delegate,
231 RenderWidgetHostDelegate* widget_delegate,
232 int routing_id,
233 int main_frame_routing_id,
234 bool swapped_out,
235 CaptureTestSourceController* controller)
236 : TestRenderViewHost(instance, delegate, widget_delegate, routing_id,
237 main_frame_routing_id, swapped_out),
238 controller_(controller) {
239 // Override the default view installed by TestRenderViewHost; we need
240 // our special subclass which has mocked-out tab capture support.
241 RenderWidgetHostView* old_view = GetView();
242 SetView(new CaptureTestView(this, controller));
243 delete old_view;
246 // TestRenderViewHost overrides.
247 virtual void CopyFromBackingStore(
248 const gfx::Rect& src_rect,
249 const gfx::Size& accelerated_dst_size,
250 const base::Callback<void(bool, const SkBitmap&)>& callback) OVERRIDE {
251 gfx::Size size = controller_->GetCopyResultSize();
252 SkColor color = controller_->GetSolidColor();
254 // Although it's not necessary, use a PlatformBitmap here (instead of a
255 // regular SkBitmap) to exercise possible threading issues.
256 skia::PlatformBitmap output;
257 EXPECT_TRUE(output.Allocate(size.width(), size.height(), false));
259 SkAutoLockPixels locker(output.GetBitmap());
260 output.GetBitmap().eraseColor(color);
262 callback.Run(true, output.GetBitmap());
263 controller_->SignalCopy();
266 private:
267 CaptureTestSourceController* controller_;
269 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHost);
272 #if defined(COMPILER_MSVC)
273 // Re-enable warning 4250
274 #pragma warning(pop)
275 #endif
277 class CaptureTestRenderViewHostFactory : public RenderViewHostFactory {
278 public:
279 explicit CaptureTestRenderViewHostFactory(
280 CaptureTestSourceController* controller) : controller_(controller) {
281 RegisterFactory(this);
284 virtual ~CaptureTestRenderViewHostFactory() {
285 UnregisterFactory();
288 // RenderViewHostFactory implementation.
289 virtual RenderViewHost* CreateRenderViewHost(
290 SiteInstance* instance,
291 RenderViewHostDelegate* delegate,
292 RenderWidgetHostDelegate* widget_delegate,
293 int routing_id,
294 int main_frame_routing_id,
295 bool swapped_out,
296 SessionStorageNamespace* session_storage_namespace) OVERRIDE {
297 return new CaptureTestRenderViewHost(instance, delegate, widget_delegate,
298 routing_id, main_frame_routing_id,
299 swapped_out, controller_);
301 private:
302 CaptureTestSourceController* controller_;
304 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHostFactory);
307 // A stub consumer of captured video frames, which checks the output of
308 // WebContentsVideoCaptureDevice.
309 class StubConsumer : public media::VideoCaptureDevice::EventHandler {
310 public:
311 StubConsumer()
312 : error_encountered_(false),
313 wait_color_yuv_(0xcafe1950) {
314 buffer_pool_ = new VideoCaptureBufferPool(
315 gfx::Size(kTestWidth, kTestHeight), 2);
316 EXPECT_TRUE(buffer_pool_->Allocate());
318 virtual ~StubConsumer() {}
320 void QuitIfConditionMet(SkColor color) {
321 base::AutoLock guard(lock_);
323 if (wait_color_yuv_ == color || error_encountered_)
324 base::MessageLoop::current()->Quit();
327 void WaitForNextColor(SkColor expected_color) {
329 base::AutoLock guard(lock_);
330 wait_color_yuv_ = ConvertRgbToYuv(expected_color);
331 error_encountered_ = false;
333 RunCurrentLoopWithDeadline();
335 base::AutoLock guard(lock_);
336 ASSERT_FALSE(error_encountered_);
340 void WaitForError() {
342 base::AutoLock guard(lock_);
343 wait_color_yuv_ = kNotInterested;
344 error_encountered_ = false;
346 RunCurrentLoopWithDeadline();
348 base::AutoLock guard(lock_);
349 ASSERT_TRUE(error_encountered_);
353 bool HasError() {
354 base::AutoLock guard(lock_);
355 return error_encountered_;
358 virtual scoped_refptr<media::VideoFrame> ReserveOutputBuffer() OVERRIDE {
359 return buffer_pool_->ReserveForProducer(0);
362 virtual void OnIncomingCapturedFrame(
363 const uint8* data,
364 int length,
365 base::Time timestamp,
366 int rotation,
367 bool flip_vert,
368 bool flip_horiz) OVERRIDE {
369 FAIL();
372 virtual void OnIncomingCapturedVideoFrame(
373 const scoped_refptr<media::VideoFrame>& frame,
374 base::Time timestamp) OVERRIDE {
375 EXPECT_EQ(gfx::Size(kTestWidth, kTestHeight), frame->coded_size());
376 EXPECT_EQ(media::VideoFrame::YV12, frame->format());
377 EXPECT_NE(0, buffer_pool_->RecognizeReservedBuffer(frame));
378 uint8 yuv[3];
379 for (int plane = 0; plane < 3; ++plane) {
380 yuv[plane] = frame->data(plane)[0];
382 // TODO(nick): We just look at the first pixel presently, because if
383 // the analysis is too slow, the backlog of frames will grow without bound
384 // and trouble erupts. http://crbug.com/174519
385 PostColorOrError(SkColorSetRGB(yuv[0], yuv[1], yuv[2]));
388 void PostColorOrError(SkColor new_color) {
389 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
390 &StubConsumer::QuitIfConditionMet, base::Unretained(this), new_color));
393 virtual void OnError() OVERRIDE {
395 base::AutoLock guard(lock_);
396 error_encountered_ = true;
398 PostColorOrError(kNothingYet);
401 virtual void OnFrameInfo(const media::VideoCaptureCapability& info) OVERRIDE {
402 EXPECT_EQ(kTestWidth, info.width);
403 EXPECT_EQ(kTestHeight, info.height);
404 EXPECT_EQ(kTestFramesPerSecond, info.frame_rate);
405 EXPECT_EQ(media::VideoCaptureCapability::kI420, info.color);
408 private:
409 base::Lock lock_;
410 bool error_encountered_;
411 SkColor wait_color_yuv_;
412 scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
414 DISALLOW_COPY_AND_ASSIGN(StubConsumer);
417 // Test harness that sets up a minimal environment with necessary stubs.
418 class WebContentsVideoCaptureDeviceTest : public testing::Test {
419 public:
420 // This is public because C++ method pointer scoping rules are silly and make
421 // this hard to use with Bind().
422 void ResetWebContents() {
423 web_contents_.reset();
426 protected:
427 virtual void SetUp() {
428 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be
429 // eliminated here, if only we could use RenderViewHostTestHarness. The
430 // catch is that we need our TestRenderViewHost to support a
431 // CopyFromBackingStore operation that we control. To accomplish that,
432 // either RenderViewHostTestHarness would have to support installing a
433 // custom RenderViewHostFactory, or else we implant some kind of delegated
434 // CopyFromBackingStore functionality into TestRenderViewHost itself.
436 render_process_host_factory_.reset(new MockRenderProcessHostFactory());
437 // Create our (self-registering) RVH factory, so that when we create a
438 // WebContents, it in turn creates CaptureTestRenderViewHosts.
439 render_view_host_factory_.reset(
440 new CaptureTestRenderViewHostFactory(&controller_));
442 browser_context_.reset(new TestBrowserContext());
444 scoped_refptr<SiteInstance> site_instance =
445 SiteInstance::Create(browser_context_.get());
446 SiteInstanceImpl::set_render_process_host_factory(
447 render_process_host_factory_.get());
448 web_contents_.reset(
449 TestWebContents::Create(browser_context_.get(), site_instance.get()));
451 // This is actually a CaptureTestRenderViewHost.
452 RenderWidgetHostImpl* rwh =
453 RenderWidgetHostImpl::From(web_contents_->GetRenderViewHost());
455 std::string device_id =
456 WebContentsCaptureUtil::AppendWebContentsDeviceScheme(
457 base::StringPrintf("%d:%d", rwh->GetProcess()->GetID(),
458 rwh->GetRoutingID()));
460 device_.reset(WebContentsVideoCaptureDevice::Create(device_id));
462 base::RunLoop().RunUntilIdle();
465 virtual void TearDown() {
466 // Tear down in opposite order of set-up.
468 // The device is destroyed asynchronously, and will notify the
469 // CaptureTestSourceController when it finishes destruction.
470 // Trigger this, and wait.
471 if (device_) {
472 device_->DeAllocate();
473 device_.reset();
476 base::RunLoop().RunUntilIdle();
478 // Destroy the browser objects.
479 web_contents_.reset();
480 browser_context_.reset();
482 base::RunLoop().RunUntilIdle();
484 SiteInstanceImpl::set_render_process_host_factory(NULL);
485 render_view_host_factory_.reset();
486 render_process_host_factory_.reset();
489 // Accessors.
490 CaptureTestSourceController* source() { return &controller_; }
491 media::VideoCaptureDevice* device() { return device_.get(); }
492 StubConsumer* consumer() { return &consumer_; }
494 void SimulateDrawEvent() {
495 if (source()->CanUseFrameSubscriber()) {
496 // Print
497 CaptureTestView* test_view = static_cast<CaptureTestView*>(
498 web_contents_->GetRenderViewHost()->GetView());
499 test_view->SimulateUpdate();
500 } else {
501 // Simulate a non-accelerated paint.
502 NotificationService::current()->Notify(
503 NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE,
504 Source<RenderWidgetHost>(web_contents_->GetRenderViewHost()),
505 NotificationService::NoDetails());
509 void DestroyVideoCaptureDevice() { device_.reset(); }
511 private:
512 // The consumer is the ultimate recipient of captured pixel data.
513 StubConsumer consumer_;
515 // The controller controls which pixel patterns to produce.
516 CaptureTestSourceController controller_;
518 // Self-registering RenderProcessHostFactory.
519 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory_;
521 // Creates capture-capable RenderViewHosts whose pixel content production is
522 // under the control of |controller_|.
523 scoped_ptr<CaptureTestRenderViewHostFactory> render_view_host_factory_;
525 // A mocked-out browser and tab.
526 scoped_ptr<TestBrowserContext> browser_context_;
527 scoped_ptr<WebContents> web_contents_;
529 // Finally, the WebContentsVideoCaptureDevice under test.
530 scoped_ptr<media::VideoCaptureDevice> device_;
532 TestBrowserThreadBundle thread_bundle_;
535 TEST_F(WebContentsVideoCaptureDeviceTest, InvalidInitialWebContentsError) {
536 // Before the installs itself on the UI thread up to start capturing, we'll
537 // delete the web contents. This should trigger an error which can happen in
538 // practice; we should be able to recover gracefully.
539 ResetWebContents();
541 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
542 device()->Start();
543 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForError());
544 device()->DeAllocate();
547 TEST_F(WebContentsVideoCaptureDeviceTest, WebContentsDestroyed) {
548 // We'll simulate the tab being closed after the capture pipeline is up and
549 // running.
550 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
551 device()->Start();
553 // Do one capture to prove
554 source()->SetSolidColor(SK_ColorRED);
555 SimulateDrawEvent();
556 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED));
558 base::RunLoop().RunUntilIdle();
560 // Post a task to close the tab. We should see an error reported to the
561 // consumer.
562 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
563 base::Bind(&WebContentsVideoCaptureDeviceTest::ResetWebContents,
564 base::Unretained(this)));
565 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForError());
566 device()->DeAllocate();
569 TEST_F(WebContentsVideoCaptureDeviceTest,
570 StopDeviceBeforeCaptureMachineCreation) {
571 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
572 device()->Start();
573 // Make a point of not running the UI messageloop here.
574 device()->Stop();
575 device()->DeAllocate();
576 DestroyVideoCaptureDevice();
578 // Currently, there should be CreateCaptureMachineOnUIThread() and
579 // DestroyCaptureMachineOnUIThread() tasks pending on the current (UI) message
580 // loop. These should both succeed without crashing, and the machine should
581 // wind up in the idle state.
582 base::RunLoop().RunUntilIdle();
585 TEST_F(WebContentsVideoCaptureDeviceTest, StopWithRendererWorkToDo) {
586 // Set up the test to use RGB copies and an normal
587 source()->SetCanCopyToVideoFrame(false);
588 source()->SetUseFrameSubscriber(false);
589 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond,
590 consumer());
591 device()->Start();
592 // Make a point of not running the UI messageloop here.
593 // TODO(ajwong): Why do we care?
594 base::RunLoop().RunUntilIdle();
596 for (int i = 0; i < 10; ++i)
597 SimulateDrawEvent();
599 device()->Stop();
600 device()->DeAllocate();
601 // Currently, there should be CreateCaptureMachineOnUIThread() and
602 // DestroyCaptureMachineOnUIThread() tasks pending on the current message
603 // loop. These should both succeed without crashing, and the machine should
604 // wind up in the idle state.
605 ASSERT_FALSE(consumer()->HasError());
606 base::RunLoop().RunUntilIdle();
607 ASSERT_FALSE(consumer()->HasError());
610 TEST_F(WebContentsVideoCaptureDeviceTest, DeviceRestart) {
611 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
612 device()->Start();
613 base::RunLoop().RunUntilIdle();
614 source()->SetSolidColor(SK_ColorRED);
615 SimulateDrawEvent();
616 SimulateDrawEvent();
617 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED));
618 SimulateDrawEvent();
619 SimulateDrawEvent();
620 source()->SetSolidColor(SK_ColorGREEN);
621 SimulateDrawEvent();
622 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorGREEN));
623 device()->Stop();
625 // Device is stopped, but content can still be animating.
626 SimulateDrawEvent();
627 SimulateDrawEvent();
628 base::RunLoop().RunUntilIdle();
630 device()->Start();
631 source()->SetSolidColor(SK_ColorBLUE);
632 SimulateDrawEvent();
633 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorBLUE));
634 source()->SetSolidColor(SK_ColorYELLOW);
635 SimulateDrawEvent();
636 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorYELLOW));
637 device()->DeAllocate();
640 // The "happy case" test. No scaling is needed, so we should be able to change
641 // the picture emitted from the source and expect to see each delivered to the
642 // consumer. The test will alternate between the three capture paths, simulating
643 // falling in and out of accelerated compositing.
644 TEST_F(WebContentsVideoCaptureDeviceTest, GoesThroughAllTheMotions) {
645 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
647 device()->Start();
649 for (int i = 0; i < 6; i++) {
650 const char* name = NULL;
651 switch (i % 3) {
652 case 0:
653 source()->SetCanCopyToVideoFrame(true);
654 source()->SetUseFrameSubscriber(false);
655 name = "VideoFrame";
656 break;
657 case 1:
658 source()->SetCanCopyToVideoFrame(false);
659 source()->SetUseFrameSubscriber(true);
660 name = "Subscriber";
661 break;
662 case 2:
663 source()->SetCanCopyToVideoFrame(false);
664 source()->SetUseFrameSubscriber(false);
665 name = "SkBitmap";
666 break;
667 default:
668 FAIL();
671 SCOPED_TRACE(base::StringPrintf("Using %s path, iteration #%d", name, i));
673 source()->SetSolidColor(SK_ColorRED);
674 SimulateDrawEvent();
675 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED));
677 source()->SetSolidColor(SK_ColorGREEN);
678 SimulateDrawEvent();
679 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorGREEN));
681 source()->SetSolidColor(SK_ColorBLUE);
682 SimulateDrawEvent();
683 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorBLUE));
685 source()->SetSolidColor(SK_ColorBLACK);
686 SimulateDrawEvent();
687 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorBLACK));
689 device()->DeAllocate();
692 TEST_F(WebContentsVideoCaptureDeviceTest, RejectsInvalidAllocateParams) {
693 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
694 base::Bind(&media::VideoCaptureDevice::Allocate,
695 base::Unretained(device()), 1280, 720, -2, consumer()));
696 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForError());
699 TEST_F(WebContentsVideoCaptureDeviceTest, BadFramesGoodFrames) {
700 device()->Allocate(kTestWidth, kTestHeight, kTestFramesPerSecond, consumer());
702 // 1x1 is too small to process; we intend for this to result in an error.
703 source()->SetCopyResultSize(1, 1);
704 source()->SetSolidColor(SK_ColorRED);
705 device()->Start();
707 // These frames ought to be dropped during the Render stage. Let
708 // several captures to happen.
709 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
710 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
711 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
712 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
713 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
715 // Now push some good frames through; they should be processed normally.
716 source()->SetCopyResultSize(kTestWidth, kTestHeight);
717 source()->SetSolidColor(SK_ColorGREEN);
718 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorGREEN));
719 source()->SetSolidColor(SK_ColorRED);
720 ASSERT_NO_FATAL_FAILURE(consumer()->WaitForNextColor(SK_ColorRED));
722 device()->Stop();
723 device()->DeAllocate();
726 } // namespace
727 } // namespace content