Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: media/video/capture/fake_video_capture_device_unittest.cc

Issue 294893006: VideoCaptureDeviceFactory: Change device enumeration to callback + QTKit enumerates in UI thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: tommi@s comments. ConsolidateDevicesInfoOnDeviceThread() uses a const&. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/bind.h" 5 #include "base/bind.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/test/test_timeouts.h" 8 #include "base/test/test_timeouts.h"
9 #include "base/threading/thread.h" 9 #include "base/threading/thread.h"
10 #include "media/video/capture/fake_video_capture_device.h" 10 #include "media/video/capture/fake_video_capture_device.h"
11 #include "media/video/capture/fake_video_capture_device_factory.h" 11 #include "media/video/capture/fake_video_capture_device_factory.h"
12 #include "media/video/capture/video_capture_device.h" 12 #include "media/video/capture/video_capture_device.h"
13 #include "media/video/capture/video_capture_types.h" 13 #include "media/video/capture/video_capture_types.h"
14 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 using ::testing::_; 17 using ::testing::_;
18 using ::testing::SaveArg;
18 19
19 namespace media { 20 namespace media {
20 21
21 class MockClient : public media::VideoCaptureDevice::Client { 22 class MockClient : public media::VideoCaptureDevice::Client {
22 public: 23 public:
23 MOCK_METHOD2(ReserveOutputBuffer, 24 MOCK_METHOD2(ReserveOutputBuffer,
24 scoped_refptr<Buffer>(media::VideoFrame::Format format, 25 scoped_refptr<Buffer>(media::VideoFrame::Format format,
25 const gfx::Size& dimensions)); 26 const gfx::Size& dimensions));
26 MOCK_METHOD0(OnErr, void()); 27 MOCK_METHOD0(OnErr, void());
27 28
(...skipping 18 matching lines...) Expand all
46 const scoped_refptr<media::VideoFrame>& frame, 47 const scoped_refptr<media::VideoFrame>& frame,
47 base::TimeTicks timestamp) OVERRIDE { 48 base::TimeTicks timestamp) OVERRIDE {
48 NOTREACHED(); 49 NOTREACHED();
49 } 50 }
50 51
51 private: 52 private:
52 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; 53 scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
53 base::Callback<void(const VideoCaptureFormat&)> frame_cb_; 54 base::Callback<void(const VideoCaptureFormat&)> frame_cb_;
54 }; 55 };
55 56
57 class DeviceEnumerationListener :
58 public base::RefCounted<DeviceEnumerationListener> {
59 public:
60 MOCK_METHOD1(OnEnumeratedDevicesCallback,
61 void(const media::VideoCaptureDevice::Names& names));
62
63 private:
64 friend class base::RefCounted<DeviceEnumerationListener>;
65 virtual ~DeviceEnumerationListener() {}
66 };
67
56 class FakeVideoCaptureDeviceTest : public testing::Test { 68 class FakeVideoCaptureDeviceTest : public testing::Test {
57 protected: 69 protected:
58 typedef media::VideoCaptureDevice::Client Client; 70 typedef media::VideoCaptureDevice::Client Client;
59 71
60 FakeVideoCaptureDeviceTest() 72 FakeVideoCaptureDeviceTest()
61 : loop_(new base::MessageLoop()), 73 : loop_(new base::MessageLoop()),
62 client_(new MockClient( 74 client_(new MockClient(
63 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured, 75 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured,
64 base::Unretained(this)))), 76 base::Unretained(this)))),
65 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {} 77 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {
78 device_enumeration_listener_ = new DeviceEnumerationListener();
79 }
66 80
67 virtual void SetUp() { 81 virtual void SetUp() {
68 } 82 }
69 83
70 void OnFrameCaptured(const VideoCaptureFormat& format) { 84 void OnFrameCaptured(const VideoCaptureFormat& format) {
71 last_format_ = format; 85 last_format_ = format;
72 run_loop_->QuitClosure().Run(); 86 run_loop_->QuitClosure().Run();
73 } 87 }
74 88
75 void WaitForCapturedFrame() { 89 void WaitForCapturedFrame() {
76 run_loop_.reset(new base::RunLoop()); 90 run_loop_.reset(new base::RunLoop());
77 run_loop_->Run(); 91 run_loop_->Run();
78 } 92 }
79 93
94 void EnumerateDevices(VideoCaptureDevice::Names* device_names) {
95 EXPECT_CALL(*device_enumeration_listener_, OnEnumeratedDevicesCallback(_))
96 .WillOnce(SaveArg<0>(device_names));
97
98 video_capture_device_factory_->EnumerateDeviceNames(
99 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback,
100 device_enumeration_listener_));
101 base::MessageLoop::current()->RunUntilIdle();
102 }
103
80 const VideoCaptureFormat& last_format() const { return last_format_; } 104 const VideoCaptureFormat& last_format() const { return last_format_; }
81 105
82 VideoCaptureDevice::Names names_; 106 VideoCaptureDevice::Names names_;
83 scoped_ptr<base::MessageLoop> loop_; 107 scoped_ptr<base::MessageLoop> loop_;
84 scoped_ptr<base::RunLoop> run_loop_; 108 scoped_ptr<base::RunLoop> run_loop_;
85 scoped_ptr<MockClient> client_; 109 scoped_ptr<MockClient> client_;
110 scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_;
86 VideoCaptureFormat last_format_; 111 VideoCaptureFormat last_format_;
87 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_; 112 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_;
88 }; 113 };
89 114
90 TEST_F(FakeVideoCaptureDeviceTest, Capture) { 115 TEST_F(FakeVideoCaptureDeviceTest, Capture) {
91 VideoCaptureDevice::Names names; 116 VideoCaptureDevice::Names names;
92 117
93 video_capture_device_factory_->GetDeviceNames(&names); 118 EnumerateDevices(&names);
94 119
95 ASSERT_GT(static_cast<int>(names.size()), 0); 120 ASSERT_GT(static_cast<int>(names.size()), 0);
96 121
97 scoped_ptr<VideoCaptureDevice> device( 122 scoped_ptr<VideoCaptureDevice> device(
98 video_capture_device_factory_->Create( 123 video_capture_device_factory_->Create(names.front()));
99 base::MessageLoopProxy::current(), names.front()));
100 ASSERT_TRUE(device); 124 ASSERT_TRUE(device);
101 125
102 EXPECT_CALL(*client_, OnErr()).Times(0); 126 EXPECT_CALL(*client_, OnErr()).Times(0);
103 127
104 VideoCaptureParams capture_params; 128 VideoCaptureParams capture_params;
105 capture_params.requested_format.frame_size.SetSize(640, 480); 129 capture_params.requested_format.frame_size.SetSize(640, 480);
106 capture_params.requested_format.frame_rate = 30; 130 capture_params.requested_format.frame_rate = 30;
107 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; 131 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420;
108 capture_params.allow_resolution_change = false; 132 capture_params.allow_resolution_change = false;
109 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); 133 device->AllocateAndStart(capture_params, client_.PassAs<Client>());
110 WaitForCapturedFrame(); 134 WaitForCapturedFrame();
111 EXPECT_EQ(last_format().frame_size.width(), 640); 135 EXPECT_EQ(last_format().frame_size.width(), 640);
112 EXPECT_EQ(last_format().frame_size.height(), 480); 136 EXPECT_EQ(last_format().frame_size.height(), 480);
113 EXPECT_EQ(last_format().frame_rate, 30); 137 EXPECT_EQ(last_format().frame_rate, 30);
114 device->StopAndDeAllocate(); 138 device->StopAndDeAllocate();
115 } 139 }
116 140
117 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { 141 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) {
118 VideoCaptureDevice::Names names; 142 VideoCaptureDevice::Names names;
119 video_capture_device_factory_->GetDeviceNames(&names); 143 EnumerateDevices(&names);
120 144
121 VideoCaptureFormats supported_formats; 145 VideoCaptureFormats supported_formats;
122 VideoCaptureDevice::Names::iterator names_iterator; 146 VideoCaptureDevice::Names::iterator names_iterator;
123 147
124 for (names_iterator = names.begin(); names_iterator != names.end(); 148 for (names_iterator = names.begin(); names_iterator != names.end();
125 ++names_iterator) { 149 ++names_iterator) {
126 video_capture_device_factory_->GetDeviceSupportedFormats( 150 video_capture_device_factory_->GetDeviceSupportedFormats(
127 *names_iterator, &supported_formats); 151 *names_iterator, &supported_formats);
128 EXPECT_EQ(supported_formats.size(), 3u); 152 EXPECT_EQ(supported_formats.size(), 3u);
129 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); 153 EXPECT_EQ(supported_formats[0].frame_size.width(), 320);
130 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); 154 EXPECT_EQ(supported_formats[0].frame_size.height(), 240);
131 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420); 155 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420);
132 EXPECT_GE(supported_formats[0].frame_rate, 20); 156 EXPECT_GE(supported_formats[0].frame_rate, 20);
133 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); 157 EXPECT_EQ(supported_formats[1].frame_size.width(), 640);
134 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); 158 EXPECT_EQ(supported_formats[1].frame_size.height(), 480);
135 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); 159 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420);
136 EXPECT_GE(supported_formats[1].frame_rate, 20); 160 EXPECT_GE(supported_formats[1].frame_rate, 20);
137 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); 161 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280);
138 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); 162 EXPECT_EQ(supported_formats[2].frame_size.height(), 720);
139 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); 163 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420);
140 EXPECT_GE(supported_formats[2].frame_rate, 20); 164 EXPECT_GE(supported_formats[2].frame_rate, 20);
141 } 165 }
142 } 166 }
143 167
144 TEST_F(FakeVideoCaptureDeviceTest, CaptureVariableResolution) { 168 TEST_F(FakeVideoCaptureDeviceTest, CaptureVariableResolution) {
145 VideoCaptureDevice::Names names; 169 VideoCaptureDevice::Names names;
146 170
147 video_capture_device_factory_->GetDeviceNames(&names); 171 EnumerateDevices(&names);
148 VideoCaptureParams capture_params; 172 VideoCaptureParams capture_params;
149 capture_params.requested_format.frame_size.SetSize(640, 480); 173 capture_params.requested_format.frame_size.SetSize(640, 480);
150 capture_params.requested_format.frame_rate = 30; 174 capture_params.requested_format.frame_rate = 30;
151 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; 175 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420;
152 capture_params.allow_resolution_change = true; 176 capture_params.allow_resolution_change = true;
153 177
154 ASSERT_GT(static_cast<int>(names.size()), 0); 178 ASSERT_GT(static_cast<int>(names.size()), 0);
155 179
156 scoped_ptr<VideoCaptureDevice> device( 180 scoped_ptr<VideoCaptureDevice> device(
157 video_capture_device_factory_->Create( 181 video_capture_device_factory_->Create(names.front()));
158 base::MessageLoopProxy::current(), names.front()));
159 ASSERT_TRUE(device); 182 ASSERT_TRUE(device);
160 183
161 // Configure the FakeVideoCaptureDevice to use all its formats as roster. 184 // Configure the FakeVideoCaptureDevice to use all its formats as roster.
162 VideoCaptureFormats formats; 185 VideoCaptureFormats formats;
163 video_capture_device_factory_->GetDeviceSupportedFormats(names.front(), 186 video_capture_device_factory_->GetDeviceSupportedFormats(names.front(),
164 &formats); 187 &formats);
165 static_cast<FakeVideoCaptureDevice*>(device.get())-> 188 static_cast<FakeVideoCaptureDevice*>(device.get())->
166 PopulateVariableFormatsRoster(formats); 189 PopulateVariableFormatsRoster(formats);
167 190
168 EXPECT_CALL(*client_, OnErr()) 191 EXPECT_CALL(*client_, OnErr())
169 .Times(0); 192 .Times(0);
170 int action_count = 200; 193 int action_count = 200;
171 194
172 device->AllocateAndStart(capture_params, client_.PassAs<Client>()); 195 device->AllocateAndStart(capture_params, client_.PassAs<Client>());
173 196
174 // We set TimeWait to 200 action timeouts and this should be enough for at 197 // We set TimeWait to 200 action timeouts and this should be enough for at
175 // least action_count/kFakeCaptureCapabilityChangePeriod calls. 198 // least action_count/kFakeCaptureCapabilityChangePeriod calls.
176 for (int i = 0; i < action_count; ++i) { 199 for (int i = 0; i < action_count; ++i) {
177 WaitForCapturedFrame(); 200 WaitForCapturedFrame();
178 } 201 }
179 device->StopAndDeAllocate(); 202 device->StopAndDeAllocate();
180 } 203 }
181 204
182 }; // namespace media 205 }; // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698