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

Side by Side Diff: content/renderer/media/video_capture_impl_manager_unittest.cc

Issue 242013002: Refactor video capturing code in the render process (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merged again :( Created 6 years, 8 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/callback.h" 6 #include "base/callback.h"
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "content/child/child_process.h" 10 #include "content/child/child_process.h"
11 #include "content/renderer/media/video_capture_impl.h" 11 #include "content/renderer/media/video_capture_impl.h"
12 #include "content/renderer/media/video_capture_impl_manager.h" 12 #include "content/renderer/media/video_capture_impl_manager.h"
13 #include "content/renderer/media/video_capture_message_filter.h" 13 #include "content/renderer/media/video_capture_message_filter.h"
14 #include "media/base/bind_to_current_loop.h" 14 #include "media/base/bind_to_current_loop.h"
15 #include "media/video/capture/mock_video_capture_event_handler.h"
16 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
18 17
19 using ::testing::_; 18 using ::testing::_;
20 using ::testing::DoAll; 19 using ::testing::DoAll;
21 using ::testing::SaveArg; 20 using ::testing::SaveArg;
22 using media::BindToCurrentLoop; 21 using media::BindToCurrentLoop;
23 using media::MockVideoCaptureEventHandler;
24 22
25 namespace content { 23 namespace content {
26 24
27 ACTION_P(RunClosure, closure) { 25 ACTION_P(RunClosure, closure) {
28 closure.Run(); 26 closure.Run();
29 } 27 }
30 28
31 class MockVideoCaptureImpl : public VideoCaptureImpl { 29 class MockVideoCaptureImpl : public VideoCaptureImpl {
32 public: 30 public:
33 MockVideoCaptureImpl(media::VideoCaptureSessionId session_id, 31 MockVideoCaptureImpl(media::VideoCaptureSessionId session_id,
(...skipping 12 matching lines...) Expand all
46 44
47 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl); 45 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl);
48 }; 46 };
49 47
50 class MockVideoCaptureImplManager : public VideoCaptureImplManager { 48 class MockVideoCaptureImplManager : public VideoCaptureImplManager {
51 public: 49 public:
52 explicit MockVideoCaptureImplManager( 50 explicit MockVideoCaptureImplManager(
53 base::Closure destruct_video_capture_callback) 51 base::Closure destruct_video_capture_callback)
54 : destruct_video_capture_callback_( 52 : destruct_video_capture_callback_(
55 destruct_video_capture_callback) {} 53 destruct_video_capture_callback) {}
54 virtual ~MockVideoCaptureImplManager() {}
56 55
57 protected: 56 protected:
58 virtual VideoCaptureImpl* CreateVideoCaptureImpl( 57 virtual VideoCaptureImpl* CreateVideoCaptureImplForTesting(
59 media::VideoCaptureSessionId id, 58 media::VideoCaptureSessionId id,
60 VideoCaptureMessageFilter* filter) const OVERRIDE { 59 VideoCaptureMessageFilter* filter) const OVERRIDE {
61 return new MockVideoCaptureImpl(id, 60 return new MockVideoCaptureImpl(id,
62 filter, 61 filter,
63 destruct_video_capture_callback_); 62 destruct_video_capture_callback_);
64 } 63 }
65 64
66 private: 65 private:
67 base::Closure destruct_video_capture_callback_; 66 base::Closure destruct_video_capture_callback_;
68 67
69 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager); 68 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager);
70 }; 69 };
71 70
72 class VideoCaptureImplManagerTest : public ::testing::Test { 71 class VideoCaptureImplManagerTest : public ::testing::Test {
73 public: 72 public:
74 VideoCaptureImplManagerTest() 73 VideoCaptureImplManagerTest()
75 : manager_(BindToCurrentLoop(cleanup_run_loop_.QuitClosure())) { 74 : manager_(new MockVideoCaptureImplManager(
75 BindToCurrentLoop(cleanup_run_loop_.QuitClosure()))) {
76 params_.requested_format = media::VideoCaptureFormat( 76 params_.requested_format = media::VideoCaptureFormat(
77 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420); 77 gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420);
78 child_process_.reset(new ChildProcess()); 78 child_process_.reset(new ChildProcess());
79 } 79 }
80 80
81 void FakeChannelSetup() { 81 void FakeChannelSetup() {
82 scoped_refptr<base::MessageLoopProxy> loop = 82 scoped_refptr<base::MessageLoopProxy> loop =
83 child_process_->io_message_loop_proxy(); 83 child_process_->io_message_loop_proxy();
84 if (!loop->BelongsToCurrentThread()) { 84 if (!loop->BelongsToCurrentThread()) {
85 loop->PostTask( 85 loop->PostTask(
86 FROM_HERE, 86 FROM_HERE,
87 base::Bind( 87 base::Bind(
88 &VideoCaptureImplManagerTest::FakeChannelSetup, 88 &VideoCaptureImplManagerTest::FakeChannelSetup,
89 base::Unretained(this))); 89 base::Unretained(this)));
90 return; 90 return;
91 } 91 }
92 manager_.video_capture_message_filter()->OnFilterAdded(NULL); 92 manager_->video_capture_message_filter()->OnFilterAdded(NULL);
93 } 93 }
94 94
95 protected: 95 protected:
96 MOCK_METHOD3(OnFrameReady,
97 void(const scoped_refptr<media::VideoFrame>&,
98 const media::VideoCaptureFormat&,
99 const base::TimeTicks&));
100 MOCK_METHOD0(OnStarted, void());
101 MOCK_METHOD0(OnStopped, void());
102
103 void OnStateUpdate(VideoCaptureState state) {
104 switch (state) {
105 case VIDEO_CAPTURE_STATE_STARTED:
106 OnStarted();
107 break;
108 case VIDEO_CAPTURE_STATE_STOPPED:
109 OnStopped();
110 break;
111 default:
112 NOTREACHED();
113 }
114 }
115
116 base::Closure StartCapture(const media::VideoCaptureParams& params) {
117 return manager_->StartCapture(
118 0, params,
119 base::Bind(&VideoCaptureImplManagerTest::OnStateUpdate,
120 base::Unretained(this)),
121 base::Bind(&VideoCaptureImplManagerTest::OnFrameReady,
122 base::Unretained(this)));
123 }
124
96 base::MessageLoop message_loop_; 125 base::MessageLoop message_loop_;
97 scoped_ptr<ChildProcess> child_process_; 126 scoped_ptr<ChildProcess> child_process_;
98 media::VideoCaptureParams params_; 127 media::VideoCaptureParams params_;
99 base::RunLoop cleanup_run_loop_; 128 base::RunLoop cleanup_run_loop_;
100 MockVideoCaptureImplManager manager_; 129 scoped_ptr<MockVideoCaptureImplManager> manager_;
101 130
102 private: 131 private:
103 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest); 132 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest);
104 }; 133 };
105 134
106 // Multiple clients with the same session id. There is only one 135 // Multiple clients with the same session id. There is only one
107 // media::VideoCapture object. 136 // media::VideoCapture object.
108 TEST_F(VideoCaptureImplManagerTest, MultipleClients) { 137 TEST_F(VideoCaptureImplManagerTest, MultipleClients) {
109 scoped_ptr<MockVideoCaptureEventHandler> client1( 138 base::Closure release_cb1 = manager_->UseDevice(0);
110 new MockVideoCaptureEventHandler); 139 base::Closure release_cb2 = manager_->UseDevice(0);
111 scoped_ptr<MockVideoCaptureEventHandler> client2( 140 base::Closure stop_cb1, stop_cb2;
112 new MockVideoCaptureEventHandler);
113
114 media::VideoCapture* device1 = NULL;
115 media::VideoCapture* device2 = NULL;
116
117 scoped_ptr<VideoCaptureHandle> handle1;
118 scoped_ptr<VideoCaptureHandle> handle2;
119 { 141 {
120 base::RunLoop run_loop; 142 base::RunLoop run_loop;
121 base::Closure quit_closure = BindToCurrentLoop( 143 base::Closure quit_closure = BindToCurrentLoop(
122 run_loop.QuitClosure()); 144 run_loop.QuitClosure());
123 145 EXPECT_CALL(*this, OnStarted()).WillOnce(
124 EXPECT_CALL(*client1, OnStarted(_)).WillOnce(SaveArg<0>(&device1)); 146 RunClosure(quit_closure));
125 EXPECT_CALL(*client2, OnStarted(_)).WillOnce( 147 EXPECT_CALL(*this, OnStarted()).RetiresOnSaturation();
126 DoAll( 148 stop_cb1 = StartCapture(params_);
127 SaveArg<0>(&device2), 149 stop_cb2 = StartCapture(params_);
128 RunClosure(quit_closure)));
129 handle1 = manager_.UseDevice(1);
130 handle2 = manager_.UseDevice(1);
131 handle1->StartCapture(client1.get(), params_);
132 handle2->StartCapture(client2.get(), params_);
133 FakeChannelSetup(); 150 FakeChannelSetup();
134 run_loop.Run(); 151 run_loop.Run();
135 } 152 }
136 153
137 { 154 {
138 base::RunLoop run_loop; 155 base::RunLoop run_loop;
139 base::Closure quit_closure = BindToCurrentLoop( 156 base::Closure quit_closure = BindToCurrentLoop(
140 run_loop.QuitClosure()); 157 run_loop.QuitClosure());
141 158 EXPECT_CALL(*this, OnStopped()).WillOnce(
142 EXPECT_CALL(*client1, OnStopped(_));
143 EXPECT_CALL(*client1, OnRemoved(_));
144 EXPECT_CALL(*client2, OnStopped(_));
145 EXPECT_CALL(*client2, OnRemoved(_)).WillOnce(
146 RunClosure(quit_closure)); 159 RunClosure(quit_closure));
147 handle1->StopCapture(client1.get()); 160 EXPECT_CALL(*this, OnStopped()).RetiresOnSaturation();
148 handle2->StopCapture(client2.get()); 161 stop_cb1.Run();
162 stop_cb2.Run();
149 run_loop.Run(); 163 run_loop.Run();
150 } 164 }
151 EXPECT_TRUE(device1 == device2);
152 165
153 handle1.reset(); 166 release_cb1.Run();
154 handle2.reset(); 167 release_cb2.Run();
155 cleanup_run_loop_.Run(); 168 cleanup_run_loop_.Run();
156 } 169 }
157 170
171 TEST_F(VideoCaptureImplManagerTest, NoLeak) {
172 manager_->UseDevice(0).Reset();
173 manager_.reset();
174 cleanup_run_loop_.Run();
175 }
176
158 } // namespace content 177 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698