OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/message_loop.h" | |
6 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
7 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
8 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
9 #include "media/video/capture/fake_video_capture_device.h" | 10 #include "media/video/capture/fake_video_capture_device.h" |
10 #include "media/video/capture/video_capture_device.h" | 11 #include "media/video/capture/video_capture_device.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
15 #if defined(OS_MACOSX) | |
16 // The camera is 'locked' by the application once started on Mac OS X, not when | |
17 // allocated as for Windows and Linux, and this test case will fail. | |
18 #define MAYBE_AllocateSameCameraTwice DISABLED_AllocateSameCameraTwice | |
19 #else | |
20 #define MAYBE_AllocateSameCameraTwice AllocateSameCameraTwice | |
21 #endif | |
22 | |
23 #if defined(OS_MACOSX) | |
24 // Mac/QTKit will always give you the size you ask for and this case will fail. | |
25 #define MAYBE_AllocateBadSize DISABLED_AllocateBadSize | |
26 #else | |
27 #define MAYBE_AllocateBadSize AllocateBadSize | |
28 #endif | |
29 | |
14 using ::testing::_; | 30 using ::testing::_; |
15 using ::testing::AnyNumber; | 31 using ::testing::AnyNumber; |
16 using ::testing::Return; | 32 using ::testing::Return; |
17 using ::testing::AtLeast; | 33 using ::testing::AtLeast; |
18 | 34 |
19 namespace media { | 35 namespace media { |
20 | 36 |
21 class MockFrameObserver: public media::VideoCaptureDevice::EventHandler { | 37 class MockFrameObserver : public media::VideoCaptureDevice::EventHandler { |
22 public: | 38 public: |
23 MOCK_METHOD0(OnErr, void()); | 39 MOCK_METHOD0(OnErr, void()); |
24 MOCK_METHOD3(OnFrameInfo, void(int width, int height, int frame_rate)); | 40 MOCK_METHOD3(OnFrameInfo, void(int width, int height, int frame_rate)); |
25 | 41 |
26 explicit MockFrameObserver(base::WaitableEvent* wait_event) | 42 explicit MockFrameObserver(base::WaitableEvent* wait_event) |
27 : wait_event_(wait_event) {} | 43 : wait_event_(wait_event) {} |
28 | 44 |
29 virtual void OnError() OVERRIDE { | 45 virtual void OnError() OVERRIDE { |
30 OnErr(); | 46 OnErr(); |
31 } | 47 } |
32 | 48 |
33 virtual void OnFrameInfo( | 49 virtual void OnFrameInfo( |
34 const VideoCaptureDevice::Capability& info) OVERRIDE { | 50 const VideoCaptureDevice::Capability& info) OVERRIDE { |
35 OnFrameInfo(info.width, info.height, info.frame_rate); | 51 OnFrameInfo(info.width, info.height, info.frame_rate); |
36 } | 52 } |
37 | 53 |
38 virtual void OnIncomingCapturedFrame(const uint8* data, int length, | 54 virtual void OnIncomingCapturedFrame(const uint8* data, int length, |
39 base::Time timestamp) OVERRIDE { | 55 base::Time timestamp) OVERRIDE { |
40 wait_event_->Signal(); | 56 wait_event_->Signal(); |
41 } | 57 } |
42 | 58 |
43 private: | 59 private: |
44 base::WaitableEvent* wait_event_; | 60 base::WaitableEvent* wait_event_; |
45 }; | 61 }; |
46 | 62 |
47 class VideoCaptureDeviceTest : public testing::Test { | 63 class VideoCaptureDeviceTest : public testing::Test { |
48 public: | 64 public: |
49 VideoCaptureDeviceTest(): wait_event_(false, false) { } | 65 VideoCaptureDeviceTest(): wait_event_(false, false) { } |
50 | 66 |
67 void PostQuitTask() { | |
68 // Give the camera 5s to start. | |
69 const int quit_task_wait_time_ms = 5000; | |
dmac
2011/10/17 17:02:29
don't use a fixed constant here. Use one of the va
mflodman_chromium_OOO
2011/10/17 18:40:38
Ok, changed to your original proposal. It will tak
| |
70 loop_->PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, | |
71 quit_task_wait_time_ms); | |
72 loop_->Run(); | |
73 } | |
74 | |
51 protected: | 75 protected: |
52 virtual void SetUp() { | 76 virtual void SetUp() { |
53 frame_observer_.reset(new MockFrameObserver(&wait_event_)); | 77 frame_observer_.reset(new MockFrameObserver(&wait_event_)); |
78 loop_.reset(new MessageLoopForUI()); | |
54 } | 79 } |
55 | 80 |
56 virtual void TearDown() { | 81 virtual void TearDown() { |
57 } | 82 } |
58 | 83 |
59 base::WaitableEvent wait_event_; | 84 base::WaitableEvent wait_event_; |
60 scoped_ptr<MockFrameObserver> frame_observer_; | 85 scoped_ptr<MockFrameObserver> frame_observer_; |
61 VideoCaptureDevice::Names names_; | 86 VideoCaptureDevice::Names names_; |
87 scoped_ptr<MessageLoop> loop_; | |
62 }; | 88 }; |
63 | 89 |
64 TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) { | 90 TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) { |
65 VideoCaptureDevice::Name device_name; | 91 VideoCaptureDevice::Name device_name; |
66 device_name.device_name = "jibberish"; | 92 device_name.device_name = "jibberish"; |
67 device_name.unique_id = "jibberish"; | 93 device_name.unique_id = "jibberish"; |
68 VideoCaptureDevice* device = VideoCaptureDevice::Create(device_name); | 94 VideoCaptureDevice* device = VideoCaptureDevice::Create(device_name); |
69 EXPECT_TRUE(device == NULL); | 95 EXPECT_TRUE(device == NULL); |
70 } | 96 } |
71 | 97 |
(...skipping 10 matching lines...) Expand all Loading... | |
82 | 108 |
83 // Get info about the new resolution. | 109 // Get info about the new resolution. |
84 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)) | 110 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)) |
85 .Times(1); | 111 .Times(1); |
86 | 112 |
87 EXPECT_CALL(*frame_observer_, OnErr()) | 113 EXPECT_CALL(*frame_observer_, OnErr()) |
88 .Times(0); | 114 .Times(0); |
89 | 115 |
90 device->Allocate(640, 480, 30, frame_observer_.get()); | 116 device->Allocate(640, 480, 30, frame_observer_.get()); |
91 device->Start(); | 117 device->Start(); |
92 // Wait for 3s or for captured frame. | 118 // Get captured video frames. |
119 PostQuitTask(); | |
93 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( | 120 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( |
94 TestTimeouts::action_max_timeout_ms()))); | 121 TestTimeouts::action_max_timeout_ms()))); |
95 device->Stop(); | 122 device->Stop(); |
96 device->DeAllocate(); | 123 device->DeAllocate(); |
97 } | 124 } |
98 | 125 |
99 TEST_F(VideoCaptureDeviceTest, Capture720p) { | 126 TEST_F(VideoCaptureDeviceTest, Capture720p) { |
100 VideoCaptureDevice::GetDeviceNames(&names_); | 127 VideoCaptureDevice::GetDeviceNames(&names_); |
101 if (!names_.size()) { | 128 if (!names_.size()) { |
102 LOG(WARNING) << "No camera available. Exiting test."; | 129 LOG(WARNING) << "No camera available. Exiting test."; |
103 return; | 130 return; |
104 } | 131 } |
105 | 132 |
106 scoped_ptr<VideoCaptureDevice> device( | 133 scoped_ptr<VideoCaptureDevice> device( |
107 VideoCaptureDevice::Create(names_.front())); | 134 VideoCaptureDevice::Create(names_.front())); |
108 ASSERT_FALSE(device.get() == NULL); | 135 ASSERT_FALSE(device.get() == NULL); |
109 | 136 |
110 // Get info about the new resolution. | 137 // Get info about the new resolution. |
111 // We don't care about the resulting resolution or frame rate as it might | 138 // We don't care about the resulting resolution or frame rate as it might |
112 // be different from one machine to the next. | 139 // be different from one machine to the next. |
113 EXPECT_CALL(*frame_observer_, OnFrameInfo(_, _, _)) | 140 EXPECT_CALL(*frame_observer_, OnFrameInfo(_, _, _)) |
114 .Times(1); | 141 .Times(1); |
115 | 142 |
116 EXPECT_CALL(*frame_observer_, OnErr()) | 143 EXPECT_CALL(*frame_observer_, OnErr()) |
117 .Times(0); | 144 .Times(0); |
118 | 145 |
119 device->Allocate(1280, 720, 30, frame_observer_.get()); | 146 device->Allocate(1280, 720, 30, frame_observer_.get()); |
120 device->Start(); | 147 device->Start(); |
121 // Get captured video frames. | 148 // Get captured video frames. |
149 PostQuitTask(); | |
122 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( | 150 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( |
123 TestTimeouts::action_max_timeout_ms()))); | 151 TestTimeouts::action_max_timeout_ms()))); |
124 device->Stop(); | 152 device->Stop(); |
125 device->DeAllocate(); | 153 device->DeAllocate(); |
126 } | 154 } |
127 | 155 |
128 TEST_F(VideoCaptureDeviceTest, AllocateSameCameraTwice) { | 156 TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateSameCameraTwice) { |
129 VideoCaptureDevice::GetDeviceNames(&names_); | 157 VideoCaptureDevice::GetDeviceNames(&names_); |
130 if (!names_.size()) { | 158 if (!names_.size()) { |
131 LOG(WARNING) << "No camera available. Exiting test."; | 159 LOG(WARNING) << "No camera available. Exiting test."; |
132 return; | 160 return; |
133 } | 161 } |
134 scoped_ptr<VideoCaptureDevice> device1( | 162 scoped_ptr<VideoCaptureDevice> device1( |
135 VideoCaptureDevice::Create(names_.front())); | 163 VideoCaptureDevice::Create(names_.front())); |
136 ASSERT_TRUE(device1.get() != NULL); | 164 ASSERT_TRUE(device1.get() != NULL); |
137 | 165 |
138 scoped_ptr<VideoCaptureDevice> device2( | 166 scoped_ptr<VideoCaptureDevice> device2( |
139 VideoCaptureDevice::Create(names_.front())); | 167 VideoCaptureDevice::Create(names_.front())); |
140 ASSERT_TRUE(device2.get() != NULL); | 168 ASSERT_TRUE(device2.get() != NULL); |
141 | 169 |
142 // 1. Get info about the new resolution on the first allocated camera | 170 // 1. Get info about the new resolution on the first allocated camera |
143 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); | 171 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); |
144 | 172 |
145 device1->Allocate(640, 480, 30, frame_observer_.get()); | 173 device1->Allocate(640, 480, 30, frame_observer_.get()); |
146 | 174 |
147 // 2. Error when trying to allocate the same camera again. | 175 // 2. Error when trying to allocate the same camera again. |
148 EXPECT_CALL(*frame_observer_, OnErr()); | 176 EXPECT_CALL(*frame_observer_, OnErr()); |
149 device2->Allocate(640, 480, 30, frame_observer_.get()); | 177 device2->Allocate(640, 480, 30, frame_observer_.get()); |
150 | 178 |
151 device1->DeAllocate(); | 179 device1->DeAllocate(); |
152 device2->DeAllocate(); | 180 device2->DeAllocate(); |
153 } | 181 } |
154 | 182 |
155 TEST_F(VideoCaptureDeviceTest, AllocateBadSize) { | 183 TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) { |
156 VideoCaptureDevice::GetDeviceNames(&names_); | 184 VideoCaptureDevice::GetDeviceNames(&names_); |
157 if (!names_.size()) { | 185 if (!names_.size()) { |
158 LOG(WARNING) << "No camera available. Exiting test."; | 186 LOG(WARNING) << "No camera available. Exiting test."; |
159 return; | 187 return; |
160 } | 188 } |
161 scoped_ptr<VideoCaptureDevice> device( | 189 scoped_ptr<VideoCaptureDevice> device( |
162 VideoCaptureDevice::Create(names_.front())); | 190 VideoCaptureDevice::Create(names_.front())); |
163 ASSERT_TRUE(device.get() != NULL); | 191 ASSERT_TRUE(device.get() != NULL); |
164 | 192 |
165 EXPECT_CALL(*frame_observer_, OnErr()) | 193 EXPECT_CALL(*frame_observer_, OnErr()) |
(...skipping 26 matching lines...) Expand all Loading... | |
192 device->Allocate(640, 480, 30, frame_observer_.get()); | 220 device->Allocate(640, 480, 30, frame_observer_.get()); |
193 device->Start(); | 221 device->Start(); |
194 // Nothing shall happen. | 222 // Nothing shall happen. |
195 device->Allocate(1280, 1024, 30, frame_observer_.get()); | 223 device->Allocate(1280, 1024, 30, frame_observer_.get()); |
196 device->DeAllocate(); | 224 device->DeAllocate(); |
197 // Allocate new size 320, 240 | 225 // Allocate new size 320, 240 |
198 device->Allocate(320, 240, 30, frame_observer_.get()); | 226 device->Allocate(320, 240, 30, frame_observer_.get()); |
199 | 227 |
200 device->Start(); | 228 device->Start(); |
201 // Get captured video frames. | 229 // Get captured video frames. |
230 PostQuitTask(); | |
202 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( | 231 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( |
dmac
2011/10/17 17:02:29
do you need the TimedWait and the PostQuitTask?
mflodman_chromium_OOO
2011/10/17 18:40:38
PostQuitTask is needed since it takes care of the
| |
203 TestTimeouts::action_max_timeout_ms()))); | 232 TestTimeouts::action_max_timeout_ms()))); |
204 device->Stop(); | 233 device->Stop(); |
205 device->DeAllocate(); | 234 device->DeAllocate(); |
206 } | 235 } |
207 | 236 |
208 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { | 237 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { |
209 VideoCaptureDevice::GetDeviceNames(&names_); | 238 VideoCaptureDevice::GetDeviceNames(&names_); |
210 if (!names_.size()) { | 239 if (!names_.size()) { |
211 LOG(WARNING) << "No camera available. Exiting test."; | 240 LOG(WARNING) << "No camera available. Exiting test."; |
212 return; | 241 return; |
213 } | 242 } |
214 scoped_ptr<VideoCaptureDevice> device( | 243 scoped_ptr<VideoCaptureDevice> device( |
215 VideoCaptureDevice::Create(names_.front())); | 244 VideoCaptureDevice::Create(names_.front())); |
216 ASSERT_TRUE(device.get() != NULL); | 245 ASSERT_TRUE(device.get() != NULL); |
217 | 246 |
218 EXPECT_CALL(*frame_observer_, OnErr()) | 247 EXPECT_CALL(*frame_observer_, OnErr()) |
219 .Times(0); | 248 .Times(0); |
220 // Get info about the new resolution. | 249 // Get info about the new resolution. |
221 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); | 250 EXPECT_CALL(*frame_observer_, OnFrameInfo(640, 480, 30)); |
222 | 251 |
223 device->Allocate(640, 480, 30, frame_observer_.get()); | 252 device->Allocate(640, 480, 30, frame_observer_.get()); |
224 | 253 |
225 device->Start(); | 254 device->Start(); |
226 // Get captured video frames. | 255 // Get captured video frames. |
256 PostQuitTask(); | |
227 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( | 257 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( |
228 TestTimeouts::action_max_timeout_ms()))); | 258 TestTimeouts::action_max_timeout_ms()))); |
229 device->DeAllocate(); | 259 device->DeAllocate(); |
230 } | 260 } |
231 | 261 |
232 TEST_F(VideoCaptureDeviceTest, TestFakeCapture) { | 262 TEST_F(VideoCaptureDeviceTest, TestFakeCapture) { |
233 VideoCaptureDevice::Names names; | 263 VideoCaptureDevice::Names names; |
234 | 264 |
235 FakeVideoCaptureDevice::GetDeviceNames(&names); | 265 FakeVideoCaptureDevice::GetDeviceNames(&names); |
236 | 266 |
(...skipping 13 matching lines...) Expand all Loading... | |
250 device->Allocate(640, 480, 30, frame_observer_.get()); | 280 device->Allocate(640, 480, 30, frame_observer_.get()); |
251 | 281 |
252 device->Start(); | 282 device->Start(); |
253 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( | 283 EXPECT_TRUE(wait_event_.TimedWait(base::TimeDelta::FromMilliseconds( |
254 TestTimeouts::action_max_timeout_ms()))); | 284 TestTimeouts::action_max_timeout_ms()))); |
255 device->Stop(); | 285 device->Stop(); |
256 device->DeAllocate(); | 286 device->DeAllocate(); |
257 } | 287 } |
258 | 288 |
259 }; // namespace media | 289 }; // namespace media |
OLD | NEW |