OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 #include "base/memory/scoped_ptr.h" | |
6 #include "base/threading/thread.h" | |
7 #include "base/synchronization/waitable_event.h" | |
8 | |
9 | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
remove extra empty line.
Per K
2011/06/27 11:47:50
Done.
| |
10 #include "media/video/capture/fake_video_capture_device.h" | |
11 #include "media/video/capture/video_capture_device.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using ::testing::_; | |
16 using ::testing::AnyNumber; | |
17 using ::testing::Return; | |
18 using ::testing::AtLeast; | |
19 | |
20 namespace media { | |
21 | |
22 class MockFrameObserver: public media::VideoCaptureDevice::EventHandler { | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
space before :
Can you run gcl lint on the cl and
Per K
2011/06/27 11:47:50
Done.
| |
23 public: | |
24 MOCK_METHOD0(OnErr, void()); | |
25 MOCK_METHOD3(OnFrameInfo, void(int width, int height, int frame_rate)); | |
26 | |
27 explicit MockFrameObserver(base::WaitableEvent* wait_event) | |
28 : frame_received_(false), | |
29 wait_event_(wait_event) {} | |
30 | |
31 virtual void OnError() { | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
for virtual methods Chromium has started to use th
Per K
2011/06/27 11:47:50
Done.
| |
32 OnErr(); | |
33 } | |
34 void OnFrameInfo(const VideoCaptureDevice::Capability& info) { | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
should this be virtual?
also add an empty line bet
Per K
2011/06/27 11:47:50
Done.
| |
35 OnFrameInfo(info.width, info.height, info.frame_rate); | |
36 } | |
37 virtual void OnIncomingCapturedFrame(const uint8* data, int length, | |
38 base::Time timestamp) { | |
39 wait_event_->Signal(); | |
40 } | |
41 | |
42 bool frame_received_; | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
private/protected?
Per K
2011/06/27 11:47:50
Done.
| |
43 base::WaitableEvent* wait_event_; | |
44 }; | |
45 | |
46 class VideoCaptureDeviceTest : public testing::Test { | |
47 public: | |
48 VideoCaptureDeviceTest(): wait_event_(false, false) { } | |
49 | |
50 protected: | |
51 virtual void SetUp() { | |
52 frame_observer_.reset(new MockFrameObserver(&wait_event_)); | |
53 | |
54 VideoCaptureDevice::GetDeviceNames(&names_); | |
55 // Make sure there are more than 0 cameras. | |
56 ASSERT_GT(static_cast<int>(names_.size()), 0); | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
instead of using static_cast<int>(), just do:
ASSE
Per K
2011/06/27 11:47:50
Done - kind of.
On 2011/06/23 14:05:56, tommi wro
| |
57 } | |
58 | |
59 virtual void TearDown() { | |
60 } | |
61 | |
62 base::WaitableEvent wait_event_; | |
63 scoped_ptr<MockFrameObserver> frame_observer_; | |
64 VideoCaptureDevice::Names names_; | |
65 }; | |
66 | |
67 TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) { | |
68 VideoCaptureDevice::Name device_name; | |
69 device_name.device_name = "jibberish"; | |
70 device_name.unique_id = "jibberish"; | |
71 VideoCaptureDevice* device = VideoCaptureDevice::Create(device_name); | |
72 EXPECT_TRUE(device == NULL); | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
fyi - I'm sure lint will tell you to use EXPECT_EQ
Per K
2011/06/27 11:47:50
Lint don't complain about this.
On 2011/06/23 14:
| |
73 } | |
74 | |
75 TEST_F(VideoCaptureDeviceTest, CaptureVGA) { | |
76 scoped_ptr<VideoCaptureDevice> device( | |
77 VideoCaptureDevice::Create(names_.front())); | |
78 ASSERT_FALSE(device.get() == NULL); | |
79 | |
80 // Get info about the new resolution. | |
81 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)) | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
is there a chance that allocate succeeds but the f
Per K
2011/06/27 11:47:50
Almost all cameras can do VGA 30fps. But whenever
| |
82 .Times(1); | |
83 | |
84 EXPECT_CALL(*frame_observer_, OnErr()) | |
85 .Times(0); | |
86 | |
87 device->Allocate(640, 480, 30, frame_observer_.get()); | |
88 device->Start(); | |
89 // Wait for 3s or for captured frame. | |
90 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(3000))); | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
add a constant at the top of the file for the wait
Per K
2011/06/27 11:47:50
Done.
| |
91 device->Stop(); | |
92 device->DeAllocate(); | |
93 } | |
94 | |
95 TEST_F(VideoCaptureDeviceTest, Capture720p) { | |
96 scoped_ptr<VideoCaptureDevice> device( | |
97 VideoCaptureDevice::Create(names_.front())); | |
98 ASSERT_FALSE(device.get() == NULL); | |
99 | |
100 // Get info about the new resolution. | |
101 // We don't care about the resulting resolution or frame rate as it might | |
102 // be different from one machine to the next. | |
103 EXPECT_CALL(*frame_observer_, OnFrameInfo(_, _, _)) | |
104 .Times(1); | |
105 | |
106 EXPECT_CALL(*frame_observer_, OnErr()) | |
107 .Times(0); | |
108 | |
109 device->Allocate(1280, 720, 30, frame_observer_.get()); | |
110 device->Start(); | |
111 // Get captured video frames. | |
112 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(3000))); | |
113 device->Stop(); | |
114 device->DeAllocate(); | |
115 } | |
116 | |
117 TEST_F(VideoCaptureDeviceTest, AllocateSameCameraTwice) { | |
118 scoped_ptr<VideoCaptureDevice> device1( | |
119 VideoCaptureDevice::Create(names_.front())); | |
120 ASSERT_FALSE(device1.get() == NULL); | |
tommi (sloooow) - chröme
2011/06/23 14:05:56
nit: All the ASSERT_FALSE(ptr == NULL) checks read
Per K
2011/06/27 11:47:50
Done.
| |
121 | |
122 scoped_ptr<VideoCaptureDevice> device2( | |
123 VideoCaptureDevice::Create(names_.front())); | |
124 ASSERT_FALSE(device2.get() == NULL); | |
125 | |
126 // 1. Get info about the new resolution on the first allocated camera | |
127 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); | |
128 | |
129 device1->Allocate(640, 480, 30, frame_observer_.get()); | |
130 | |
131 // 2. Error when trying to allocate the same camera again. | |
132 EXPECT_CALL(*frame_observer_, OnErr()); | |
133 device2->Allocate(640, 480, 30, frame_observer_.get()); | |
134 | |
135 device1->DeAllocate(); | |
136 device2->DeAllocate(); | |
137 } | |
138 | |
139 TEST_F(VideoCaptureDeviceTest, AllocateBadSize) { | |
140 scoped_ptr<VideoCaptureDevice> device( | |
141 VideoCaptureDevice::Create(names_.front())); | |
142 ASSERT_FALSE(device.get() == NULL); | |
143 | |
144 EXPECT_CALL(*frame_observer_, OnErr()) | |
145 .Times(0); | |
146 | |
147 // get info about the new resolution | |
148 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480 , _)) | |
149 .Times(AtLeast(1)); | |
150 | |
151 device->Allocate(637, 472, 35, frame_observer_.get()); | |
152 device->DeAllocate(); | |
153 } | |
154 | |
155 TEST_F(VideoCaptureDeviceTest, ReAllocateCamera) { | |
156 scoped_ptr<VideoCaptureDevice> device( | |
157 VideoCaptureDevice::Create(names_.front())); | |
158 ASSERT_FALSE(device.get() == NULL); | |
159 EXPECT_CALL(*frame_observer_, OnErr()) | |
160 .Times(0); | |
161 // get info about the new resolution | |
162 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, _)); | |
163 | |
164 EXPECT_CALL(*frame_observer_, OnFrameInfo(320, 240, _)); | |
165 | |
166 device->Allocate(640, 480, 30, frame_observer_.get()); | |
167 device->Start(); | |
168 // Nothing shall happen. | |
169 device->Allocate(1280, 1024, 30, frame_observer_.get()); | |
170 device->DeAllocate(); | |
171 // Allocate new size 320, 240 | |
172 device->Allocate(320, 240, 30, frame_observer_.get()); | |
173 | |
174 device->Start(); | |
175 // Get captured video frames. | |
176 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(1000))); | |
177 device->Stop(); | |
178 device->DeAllocate(); | |
179 } | |
180 | |
181 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { | |
182 scoped_ptr<VideoCaptureDevice> device( | |
183 VideoCaptureDevice::Create(names_.front())); | |
184 ASSERT_FALSE(device.get() == NULL); | |
185 | |
186 // 1. Get info about the new resolution. | |
187 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); | |
188 | |
189 device->Allocate(640, 480, 30, frame_observer_.get()); | |
190 | |
191 device->Start(); | |
192 // Get captured video frames. | |
193 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(1000))); | |
194 device->DeAllocate(); | |
195 } | |
196 | |
197 TEST_F(VideoCaptureDeviceTest, TestFakeCapture) { | |
198 VideoCaptureDevice::Names names; | |
199 | |
200 FakeVideoCaptureDevice::GetDeviceNames(&names); | |
201 | |
202 ASSERT_GT(static_cast<int>(names.size()), 0); | |
203 | |
204 scoped_ptr<VideoCaptureDevice> device( | |
205 FakeVideoCaptureDevice::Create(names.front())); | |
206 ASSERT_FALSE(device.get() == NULL); | |
207 | |
208 // Get info about the new resolution. | |
209 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)) | |
210 .Times(1); | |
211 | |
212 EXPECT_CALL(*frame_observer_, OnErr()) | |
213 .Times(0); | |
214 | |
215 device->Allocate(640, 480, 30, frame_observer_.get()); | |
216 | |
217 device->Start(); | |
218 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds(1000))); | |
219 device->Stop(); | |
220 device->DeAllocate(); | |
221 } | |
222 | |
223 }; // namespace media | |
OLD | NEW |