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

Side by Side Diff: components/video_capture/stream_impl_unittest.cc

Issue 1637793004: [WIP] Mojo Video Capture thingy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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
OLDNEW
(Empty)
1 // Copyright 2016 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/run_loop.h"
7 #include "components/video_capture/public/cpp/stream_client_impl.h"
8 #include "components/video_capture/stream_impl.h"
9 #include "media/base/video_frame.h"
10 #include "mojo/public/c/system/buffer.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using ::testing::_;
15 using ::testing::AllOf;
16 using ::testing::InSequence;
17 using ::testing::Field;
18
19 namespace video_capture {
20
21 namespace {
22
23 class MockStreamClient : public mojom::StreamClient {
24 public:
25 MockStreamClient() {}
26
27 // mojom::StreamClient. Use forwarders for move only types, see
28 // https://github.com/google/googletest/issues/395
29 MOCK_METHOD1(DoBufferCreated, void(mojom::FrameBufferInfo* info));
30 void BufferCreated(mojom::FrameBufferInfoPtr info) override {
31 DoBufferCreated(info.get());
32 }
33 MOCK_METHOD1(BufferDestroyed, void(int32_t buffer_id));
34 MOCK_METHOD2(DoFrameAvailable,
35 void(mojom::FrameInfo* info,
36 const FrameAvailableCallback& callback));
37 void FrameAvailable(mojom::FrameInfoPtr info,
38 const FrameAvailableCallback& callback) override {
39 DCHECK(false);
40 DoFrameAvailable(info.get(), callback);
41 callback.Run();
42 }
43
44 private:
45 DISALLOW_COPY_AND_ASSIGN(MockStreamClient);
46 };
47
48 } // anonymous namespace
49
50 class StreamImplTest : public testing::Test {
51 public:
52 StreamImplTest()
53 : stream_impl_(new StreamImpl(
54 mojo::GetProxy(&stream_ptr_),
55 base::Bind(&StreamImplTest::OnStart, base::Unretained(this)),
56 base::Bind(&StreamImplTest::OnStop, base::Unretained(this)))) {}
57
58 void SetUp() override {}
59 void TearDown() override {}
60
61 MOCK_METHOD0(OnStart, void());
62 MOCK_METHOD0(OnStop, void());
63 MOCK_METHOD0(OnRelease, void());
64
65 void OnFrame(const scoped_refptr<media::VideoFrame>& frame,
66 const base::TimeTicks& timestamp,
67 const base::Closure& release_callback) {
68 stream_impl_->OnFrame(frame, timestamp, release_callback);
69 }
70 void NotifyBufferCreated(int32_t id,
71 mojo::ScopedSharedBufferHandle handle,
72 size_t size) {
73 stream_impl_->NotifyBufferCreated(id, std::move(handle), size);
74 }
75 void NotifyBufferDestroyed(int32_t id) {
76 stream_impl_->NotifyBufferDestroyed(id);
77 }
78 void NotifyFrameAvailable(int32_t buffer_id,
79 const scoped_refptr<media::VideoFrame>& frame,
80 const base::TimeTicks& timestamp,
81 const base::Closure& release_callback) {
82 stream_impl_->NotifyFrameAvailable(buffer_id, frame, timestamp,
83 release_callback);
84 }
85 void StartAndExpectOnStart() {
86 EXPECT_CALL(*this, OnStart()).Times(1);
87 scoped_ptr<mojo::Binding<mojom::StreamClient>> binding(
Ken Rockot(use gerrit already) 2016/01/28 03:20:36 Binding takes ownership of its end of the message
88 new mojo::Binding<mojom::StreamClient>(&stream_client_impl_));
89 stream_impl_->Start(binding->CreateInterfacePtrAndBind());
90 }
91
92 protected:
93 // This is needed for the Binding's Router's HandleWatcher :)
94 const base::MessageLoop loop_;
95
96 mojom::StreamPtr stream_ptr_;
97 // The class under test.
98 scoped_ptr<StreamImpl> stream_impl_;
99
100 // The mock of the (remote) video capture client.
101 MockStreamClient stream_client_impl_;
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(StreamImplTest);
105 };
106
107 TEST_F(StreamImplTest, CreateAndDestroy) {
108 }
109
110 TEST_F(StreamImplTest, StartAndCreateAndDestroyBuffer) {
111 InSequence s;
112
113 StartAndExpectOnStart();
114
115 const int kBufferId = 123;
116 const int kBufferSize = 456;
117
118 MojoCreateSharedBufferOptions options;
119 options.struct_size = sizeof(options);
120 options.flags = MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE;
121 mojo::SharedBufferHandle buffer1;
122 EXPECT_EQ(MOJO_RESULT_OK,
123 MojoCreateSharedBuffer(&options, kBufferSize,
124 buffer1.mutable_value()));
125
126 // TODO(mcasas): SaveArg and compare EXPECT_CALLs argument.
127 EXPECT_CALL(stream_client_impl_, DoBufferCreated(_)).Times(1);
128 NotifyBufferCreated(kBufferId, mojo::ScopedSharedBufferHandle(buffer1),
129 kBufferSize);
130
131 EXPECT_CALL(stream_client_impl_, BufferDestroyed(kBufferId)).Times(1);
132 NotifyBufferDestroyed(kBufferId);
133 }
Ken Rockot(use gerrit already) 2016/01/28 03:20:36 You do need to wait. Note that there's a separate
134
135 // Tests the connection NotifyFrameAvailable() --> client's FrameAvailable().
136 TEST_F(StreamImplTest, NotifyFrameAvailableSendsFrame) {
137 InSequence s;
138
139 StartAndExpectOnStart();
140
141 const int kBufferId = 123;
142 const scoped_refptr<media::VideoFrame> black_frame =
143 media::VideoFrame::CreateBlackFrame(gfx::Size(10, 10));
144 const base::TimeTicks now = base::TimeTicks::Now();
145
146 EXPECT_CALL(
147 stream_client_impl_,
148 DoFrameAvailable(
149 AllOf(Field(&mojom::FrameInfo::buffer_id, kBufferId),
150 Field(&mojom::FrameInfo::pixel_format,
151 static_cast<media::interfaces::VideoFormat>(
152 black_frame->format())),
153 Field(&mojom::FrameInfo::timestamp, now.ToInternalValue())),
154 _))
155 .Times(1);
156 EXPECT_CALL(*this, OnRelease()).Times(1);
157
158 NotifyFrameAvailable(
159 kBufferId, black_frame, now,
160 base::Bind(&StreamImplTest::OnRelease, base::Unretained(this)));
161
162 base::MessageLoop::current()->RunUntilIdle();
163 }
164
165 // Tests the connection OnFrame() --> (NotifyFrameAvailable()) --> client's
166 // FrameAvailable(). This is a superset of the paths touched by the
167 // NotifyFrameAvailableSendsFrame test.
168 TEST_F(StreamImplTest, OnFrameSendsFrame) {
169 InSequence s;
170
171 StartAndExpectOnStart();
172
173 const scoped_refptr<media::VideoFrame> black_frame =
174 media::VideoFrame::CreateBlackFrame(gfx::Size(10, 10));
175 const base::TimeTicks now = base::TimeTicks::Now();
176
177 // TODO(mcasas): SaveArg and compare EXPECT_CALLs argument.
178 EXPECT_CALL(
179 stream_client_impl_,
180 DoFrameAvailable(
181 AllOf(Field(&mojom::FrameInfo::pixel_format,
182 static_cast<media::interfaces::VideoFormat>(
183 black_frame->format())),
184 Field(&mojom::FrameInfo::timestamp, now.ToInternalValue())),
185 _))
186 .Times(1);
187 EXPECT_CALL(*this, OnRelease()).Times(1);
188
189 OnFrame(black_frame, now,
190 base::Bind(&StreamImplTest::OnRelease, base::Unretained(this)));
191 base::MessageLoop::current()->RunUntilIdle();
192
193 }
194
195 } // namespace video_capture
OLDNEW
« no previous file with comments | « components/video_capture/stream_impl.cc ('k') | content/browser/renderer_host/media/video_capture_device_client_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698