Chromium Code Reviews| Index: components/video_capture/stream_impl_unittest.cc |
| diff --git a/components/video_capture/stream_impl_unittest.cc b/components/video_capture/stream_impl_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6d3045884895950c2e3d7b0369825f4622fbeb34 |
| --- /dev/null |
| +++ b/components/video_capture/stream_impl_unittest.cc |
| @@ -0,0 +1,143 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/run_loop.h" |
| +#include "components/video_capture/mock_stream_client.h" |
| +#include "components/video_capture/mojo_video_frame.h" |
| +#include "components/video_capture/stream_impl.h" |
| +#include "mojo/public/c/system/buffer.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using ::testing::_; |
| +using ::testing::AllOf; |
| +using ::testing::InSequence; |
| +using ::testing::Field; |
| +using ::testing::Property; |
| + |
| +namespace video_capture { |
| + |
| +namespace { |
| + |
| +ACTION_P(RunClosure, closure) { |
| + closure.Run(); |
| +} |
| + |
| +} // anonymous namespace |
| + |
| +class StreamImplTest : public testing::Test { |
| + public: |
| + StreamImplTest() |
| + : stream_impl_(new StreamImpl( |
| + mojo::GetProxy(&stream_ptr_), |
| + base::Bind(&StreamImplTest::StartCallback, base::Unretained(this)), |
| + base::Bind(&StreamImplTest::StopCallback, base::Unretained(this)), |
| + base::Bind(&StreamImplTest::ErrorCallback, |
| + base::Unretained(this)))) {} |
| + |
| + void SetUp() override {} |
| + void TearDown() override {} |
| + |
| + MOCK_METHOD0(StartCallback, void()); |
| + MOCK_METHOD0(StopCallback, void()); |
| + MOCK_METHOD0(ErrorCallback, void()); |
| + MOCK_METHOD0(OnRelease, void()); |
| + |
| + void OnFrame(const scoped_refptr<MojoVideoFrame>& frame, |
| + const base::TimeTicks& timestamp) { |
| + stream_impl_->OnFrame(frame, timestamp); |
| + } |
| + void OnError(const std::string& reason) { stream_impl_->OnError(reason); } |
| + |
| + void StartAndExpectStartCallback() { |
| + EXPECT_CALL(*this, StartCallback()).Times(1); |
| + mojom::StreamClientPtr le_client = stream_client_impl_.CreateProxy(); |
| + stream_impl_->Start(std::move(le_client)); |
| + } |
| + |
| + protected: |
| + // This is needed for the Binding's Router's HandleWatcher :) |
| + const base::MessageLoop loop_; |
| + |
| + mojom::StreamPtr stream_ptr_; |
| + // The class under test. |
| + const scoped_ptr<StreamImpl> stream_impl_; |
| + |
| + // The mock of the (remote) video capture client. |
| + MockStreamClient stream_client_impl_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(StreamImplTest); |
| +}; |
| + |
| +TEST_F(StreamImplTest, CreateAndStartAndDestroy) { |
| + StartAndExpectStartCallback(); |
| +} |
| + |
| +// Tests the connection |
| +// OnFrame() --> (NotifyFrameAvailable()) --> client's FrameAvailable(). |
| +TEST_F(StreamImplTest, OnFramePingsFrameAvailable) { |
| + InSequence s; |
| + StartAndExpectStartCallback(); |
| + |
| + const gfx::Size kFrameSize(10, 10); |
| + const scoped_refptr<MojoVideoFrame> le_frame = |
| + MojoVideoFrame::CreateMojoVideoFrame(kFrameSize, base::TimeDelta()); |
| + le_frame->AddDestructionObserver( |
| + base::Bind(&StreamImplTest::OnRelease, base::Unretained(this))); |
| + const base::TimeTicks now = base::TimeTicks::Now(); |
| + |
| + base::RunLoop run_loop; |
| + base::Closure quit_closure = run_loop.QuitClosure(); |
| + EXPECT_CALL( |
| + stream_client_impl_, |
| + DoFrameAvailable( |
| + AllOf(Field(&mojom::FrameInfo::buffer_id, 0), |
| + Field(&mojom::FrameInfo::storage_size, le_frame->mapped_size()), |
| + Field(&mojom::FrameInfo::pixel_format, |
| + static_cast<media::interfaces::VideoFormat>( |
| + le_frame->format())), |
| + Field(&mojom::FrameInfo::timestamp, now.ToInternalValue())), |
| + _)) |
| + .Times(1) |
| + .WillOnce(RunClosure(quit_closure)); |
| + EXPECT_CALL(*this, OnRelease()).Times(1); |
| + |
| + OnFrame(le_frame, now); |
| + run_loop.Run(); |
| +} |
| + |
| +// Tests the connection |
| +// OnError() --> (NotifyError()) --> client's and owners' Error(). |
| +TEST_F(StreamImplTest, OnErrorPingsError) { |
| + InSequence s; |
| + StartAndExpectStartCallback(); |
| + |
| + const std::string kErrorMessage("big error!"); |
| + base::RunLoop run_loop; |
| + base::Closure quit_closure = run_loop.QuitClosure(); |
| + |
| + EXPECT_CALL(*this, ErrorCallback()).Times(1); |
| + EXPECT_CALL( |
| + stream_client_impl_, |
| + DoError(Property(&mojo::String::To<std::string>, kErrorMessage.c_str()))) |
| + .Times(1) |
| + .WillOnce(RunClosure(quit_closure)); |
| + OnError(kErrorMessage); |
| + run_loop.Run(); |
| +} |
| + |
| +// Tests the connection Stop() --> Stop Callback. |
| +TEST_F(StreamImplTest, StopPingsStopCallback) { |
|
Ken Rockot(use gerrit already)
2016/02/05 04:51:26
In your message to me you said you set the client
|
| + stream_impl_->Pause(); |
| + stream_impl_->Resume(); |
| + |
| + EXPECT_CALL(*this, ErrorCallback()).Times(0); |
| + EXPECT_CALL(*this, StopCallback()).Times(1); |
| + stream_impl_->Stop(); |
|
mcasas
2016/02/05 17:35:18
Yeah, so the problem is:
StartAndExpectStartCallb
|
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +} // namespace video_capture |