Index: media/capture/service/stream_impl_unittest.cc |
diff --git a/media/capture/service/stream_impl_unittest.cc b/media/capture/service/stream_impl_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f439d49c4335747a700a6595539fc30d6b100a3c |
--- /dev/null |
+++ b/media/capture/service/stream_impl_unittest.cc |
@@ -0,0 +1,142 @@ |
+// 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 "media/capture/service/mock_stream_client.h" |
+#include "media/capture/service/mojo_video_frame.h" |
+#include "media/capture/service/stream_impl.h" |
+#include "mojo/public/cpp/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 media { |
+ |
+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::VideoCaptureStreamClientPtr 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::VideoCaptureStreamPtr 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() --> (NotifyOnFrameAvailable()) --> client's FrameAvailable(). |
+TEST_F(StreamImplTest, OnFramePingsOnFrameAvailable) { |
+ 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_, |
+ DoOnFrameAvailable( |
+ AllOf(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_, |
+ OnError(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) { |
+ StartAndExpectStartCallback(); |
+ |
+ EXPECT_CALL(*this, ErrorCallback()).Times(0); |
+ EXPECT_CALL(*this, StopCallback()).Times(1); |
+ stream_impl_->Stop(); |
+ base::RunLoop().RunUntilIdle(); |
+} |
+ |
+} // namespace media |