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