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 "content/browser/renderer_host/media/web_contents_video_capture_device.
h" | 5 #include "content/browser/renderer_host/media/web_contents_video_capture_device.
h" |
6 | 6 |
7 #include "base/synchronization/condition_variable.h" | 7 #include "base/synchronization/condition_variable.h" |
8 #include "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
9 #include "content/browser/renderer_host/render_widget_host_delegate.h" | 9 #include "content/browser/renderer_host/render_widget_host_delegate.h" |
10 #include "content/browser/renderer_host/render_widget_host_impl.h" | 10 #include "content/browser/renderer_host/render_widget_host_impl.h" |
11 #include "content/public/test/mock_render_process_host.h" | 11 #include "content/public/test/mock_render_process_host.h" |
12 #include "content/public/test/test_browser_context.h" | 12 #include "content/public/test/test_browser_context.h" |
13 #include "media/video/capture/video_capture_types.h" | 13 #include "media/video/capture/video_capture_types.h" |
14 #include "skia/ext/platform_canvas.h" | 14 #include "skia/ext/platform_canvas.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/skia/include/core/SkColor.h" | 16 #include "third_party/skia/include/core/SkColor.h" |
17 | 17 |
| 18 namespace content { |
18 namespace { | 19 namespace { |
19 const int kTestWidth = 1280; | 20 const int kTestWidth = 1280; |
20 const int kTestHeight = 720; | 21 const int kTestHeight = 720; |
21 const int kBytesPerPixel = 4; | 22 const int kBytesPerPixel = 4; |
22 const int kTestFramesPerSecond = 8; | 23 const int kTestFramesPerSecond = 8; |
23 const SkColor kNothingYet = 0xdeadbeef; | 24 const SkColor kNothingYet = 0xdeadbeef; |
24 const SkColor kNotInterested = ~kNothingYet; | 25 const SkColor kNotInterested = ~kNothingYet; |
25 } | 26 } |
26 | 27 |
27 // A stub implementation which returns solid-color bitmaps in calls to | 28 // A stub implementation which returns solid-color bitmaps in calls to |
28 // CopyFromBackingStore(). The unit tests can change the color for successive | 29 // CopyFromBackingStore(). The unit tests can change the color for successive |
29 // captures. | 30 // captures. |
30 class StubRenderWidgetHost : public content::RenderWidgetHostImpl { | 31 class StubRenderWidgetHost : public RenderWidgetHostImpl { |
31 public: | 32 public: |
32 StubRenderWidgetHost(content::RenderProcessHost* process, | 33 StubRenderWidgetHost(RenderProcessHost* process, int routing_id) |
33 int routing_id) | |
34 : RenderWidgetHostImpl(&delegate_, process, routing_id), | 34 : RenderWidgetHostImpl(&delegate_, process, routing_id), |
35 color_(kNothingYet) {} | 35 color_(kNothingYet) {} |
36 | 36 |
37 void SetSolidColor(SkColor color) { | 37 void SetSolidColor(SkColor color) { |
38 color_ = color; | 38 color_ = color; |
39 } | 39 } |
40 | 40 |
41 virtual void CopyFromBackingStore( | 41 virtual void CopyFromBackingStore( |
42 const gfx::Rect& src_rect, | 42 const gfx::Rect& src_rect, |
43 const gfx::Size& accelerated_dst_size, | 43 const gfx::Size& accelerated_dst_size, |
44 const base::Callback<void(bool)>& callback, | 44 const base::Callback<void(bool)>& callback, |
45 skia::PlatformBitmap* output) OVERRIDE { | 45 skia::PlatformBitmap* output) OVERRIDE { |
46 DCHECK(output); | 46 DCHECK(output); |
47 EXPECT_TRUE(output->Allocate(kTestWidth, kTestHeight, true)); | 47 EXPECT_TRUE(output->Allocate(kTestWidth, kTestHeight, true)); |
48 SkBitmap bitmap = output->GetBitmap(); | 48 SkBitmap bitmap = output->GetBitmap(); |
49 { | 49 { |
50 SkAutoLockPixels locker(bitmap); | 50 SkAutoLockPixels locker(bitmap); |
51 bitmap.eraseColor(color_); | 51 bitmap.eraseColor(color_); |
52 } | 52 } |
53 | 53 |
54 callback.Run(true); | 54 callback.Run(true); |
55 } | 55 } |
56 | 56 |
57 private: | 57 private: |
58 class StubRenderWidgetHostDelegate | 58 class StubRenderWidgetHostDelegate : public RenderWidgetHostDelegate { |
59 : public content::RenderWidgetHostDelegate { | |
60 public: | 59 public: |
61 StubRenderWidgetHostDelegate() {} | 60 StubRenderWidgetHostDelegate() {} |
62 virtual ~StubRenderWidgetHostDelegate() {} | 61 virtual ~StubRenderWidgetHostDelegate() {} |
63 | 62 |
64 private: | 63 private: |
65 DISALLOW_COPY_AND_ASSIGN(StubRenderWidgetHostDelegate); | 64 DISALLOW_COPY_AND_ASSIGN(StubRenderWidgetHostDelegate); |
66 }; | 65 }; |
67 | 66 |
68 StubRenderWidgetHostDelegate delegate_; | 67 StubRenderWidgetHostDelegate delegate_; |
69 SkColor color_; | 68 SkColor color_; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 public: | 146 public: |
148 WebContentsVideoCaptureDeviceTest() {} | 147 WebContentsVideoCaptureDeviceTest() {} |
149 | 148 |
150 protected: | 149 protected: |
151 virtual void SetUp() { | 150 virtual void SetUp() { |
152 // This is a MessageLoop for the current thread. The MockRenderProcessHost | 151 // This is a MessageLoop for the current thread. The MockRenderProcessHost |
153 // will schedule its destruction in this MessageLoop during TearDown(). | 152 // will schedule its destruction in this MessageLoop during TearDown(). |
154 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 153 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
155 | 154 |
156 // The CopyFromBackingStore and WebContents tracking occur on the UI thread. | 155 // The CopyFromBackingStore and WebContents tracking occur on the UI thread. |
157 ui_thread_.reset( | 156 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); |
158 new content::BrowserThreadImpl(content::BrowserThread::UI)); | |
159 ui_thread_->Start(); | 157 ui_thread_->Start(); |
160 | 158 |
161 // And the rest... | 159 // And the rest... |
162 browser_context_.reset(new content::TestBrowserContext()); | 160 browser_context_.reset(new TestBrowserContext()); |
163 source_.reset(new StubRenderWidgetHost( | 161 source_.reset(new StubRenderWidgetHost( |
164 new content::MockRenderProcessHost(browser_context_.get()), | 162 new MockRenderProcessHost(browser_context_.get()), MSG_ROUTING_NONE)); |
165 MSG_ROUTING_NONE)); | 163 device_.reset(WebContentsVideoCaptureDevice::CreateForTesting( |
166 device_.reset(content::WebContentsVideoCaptureDevice::CreateForTesting( | |
167 source_.get())); | 164 source_.get())); |
168 consumer_.reset(new StubConsumer); | 165 consumer_.reset(new StubConsumer); |
169 } | 166 } |
170 | 167 |
171 virtual void TearDown() { | 168 virtual void TearDown() { |
172 // Tear down in opposite order of set-up. | 169 // Tear down in opposite order of set-up. |
173 consumer_.reset(); | 170 consumer_.reset(); |
174 device_.reset(); | 171 device_.reset(); |
175 source_.reset(); | 172 source_.reset(); |
176 browser_context_.reset(); | 173 browser_context_.reset(); |
177 ui_thread_->Stop(); | 174 ui_thread_->Stop(); |
178 ui_thread_.reset(); | 175 ui_thread_.reset(); |
179 message_loop_->RunAllPending(); // Deletes MockRenderProcessHost. | 176 message_loop_->RunAllPending(); // Deletes MockRenderProcessHost. |
180 message_loop_.reset(); | 177 message_loop_.reset(); |
181 } | 178 } |
182 | 179 |
183 // Accessors. | 180 // Accessors. |
184 StubRenderWidgetHost* source() const { return source_.get(); } | 181 StubRenderWidgetHost* source() const { return source_.get(); } |
185 media::VideoCaptureDevice* device() const { return device_.get(); } | 182 media::VideoCaptureDevice* device() const { return device_.get(); } |
186 StubConsumer* consumer() const { return consumer_.get(); } | 183 StubConsumer* consumer() const { return consumer_.get(); } |
187 | 184 |
188 private: | 185 private: |
189 scoped_ptr<MessageLoop> message_loop_; | 186 scoped_ptr<MessageLoop> message_loop_; |
190 scoped_ptr<content::BrowserThreadImpl> ui_thread_; | 187 scoped_ptr<BrowserThreadImpl> ui_thread_; |
191 scoped_ptr<content::TestBrowserContext> browser_context_; | 188 scoped_ptr<TestBrowserContext> browser_context_; |
192 scoped_ptr<StubRenderWidgetHost> source_; | 189 scoped_ptr<StubRenderWidgetHost> source_; |
193 scoped_ptr<media::VideoCaptureDevice> device_; | 190 scoped_ptr<media::VideoCaptureDevice> device_; |
194 scoped_ptr<StubConsumer> consumer_; | 191 scoped_ptr<StubConsumer> consumer_; |
195 | 192 |
196 DISALLOW_COPY_AND_ASSIGN(WebContentsVideoCaptureDeviceTest); | 193 DISALLOW_COPY_AND_ASSIGN(WebContentsVideoCaptureDeviceTest); |
197 }; | 194 }; |
198 | 195 |
199 // The "happy case" test. No scaling is needed, so we should be able to change | 196 // The "happy case" test. No scaling is needed, so we should be able to change |
200 // the picture emitted from the source and expect to see each delivered to the | 197 // the picture emitted from the source and expect to see each delivered to the |
201 // consumer. | 198 // consumer. |
(...skipping 11 matching lines...) Expand all Loading... |
213 source()->SetSolidColor(SK_ColorBLACK); | 210 source()->SetSolidColor(SK_ColorBLACK); |
214 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLACK)); | 211 EXPECT_TRUE(consumer()->WaitForNextColorOrError(SK_ColorBLACK)); |
215 | 212 |
216 device()->DeAllocate(); | 213 device()->DeAllocate(); |
217 } | 214 } |
218 | 215 |
219 TEST_F(WebContentsVideoCaptureDeviceTest, RejectsInvalidAllocateParams) { | 216 TEST_F(WebContentsVideoCaptureDeviceTest, RejectsInvalidAllocateParams) { |
220 device()->Allocate(1280, 720, -2, consumer()); | 217 device()->Allocate(1280, 720, -2, consumer()); |
221 EXPECT_FALSE(consumer()->WaitForNextColorOrError(kNotInterested)); | 218 EXPECT_FALSE(consumer()->WaitForNextColorOrError(kNotInterested)); |
222 } | 219 } |
| 220 |
| 221 } // namespace content |
OLD | NEW |