OLD | NEW |
(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 "media/capture/service/mock_stream_client.h" |
| 8 #include "media/capture/service/mojo_video_frame.h" |
| 9 #include "media/capture/service/stream_impl.h" |
| 10 #include "mojo/public/cpp/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 using ::testing::Property; |
| 19 |
| 20 namespace media { |
| 21 |
| 22 namespace { |
| 23 |
| 24 ACTION_P(RunClosure, closure) { |
| 25 closure.Run(); |
| 26 } |
| 27 |
| 28 } // anonymous namespace |
| 29 |
| 30 class StreamImplTest : public testing::Test { |
| 31 public: |
| 32 StreamImplTest() |
| 33 : stream_impl_(new StreamImpl( |
| 34 mojo::GetProxy(&stream_ptr_), |
| 35 base::Bind(&StreamImplTest::StartCallback, base::Unretained(this)), |
| 36 base::Bind(&StreamImplTest::StopCallback, base::Unretained(this)), |
| 37 base::Bind(&StreamImplTest::ErrorCallback, |
| 38 base::Unretained(this)))) {} |
| 39 |
| 40 void SetUp() override {} |
| 41 void TearDown() override {} |
| 42 |
| 43 MOCK_METHOD0(StartCallback, void()); |
| 44 MOCK_METHOD0(StopCallback, void()); |
| 45 MOCK_METHOD0(ErrorCallback, void()); |
| 46 MOCK_METHOD0(OnRelease, void()); |
| 47 |
| 48 void OnFrame(const scoped_refptr<MojoVideoFrame>& frame, |
| 49 const base::TimeTicks& timestamp) { |
| 50 stream_impl_->OnFrame(frame, timestamp); |
| 51 } |
| 52 void OnError(const std::string& reason) { stream_impl_->OnError(reason); } |
| 53 |
| 54 void StartAndExpectStartCallback() { |
| 55 EXPECT_CALL(*this, StartCallback()).Times(1); |
| 56 mojom::StreamClientPtr le_client = stream_client_impl_.CreateProxy(); |
| 57 stream_impl_->Start(std::move(le_client)); |
| 58 } |
| 59 |
| 60 protected: |
| 61 // This is needed for the Binding's Router's HandleWatcher :) |
| 62 const base::MessageLoop loop_; |
| 63 |
| 64 mojom::StreamPtr stream_ptr_; |
| 65 // The class under test. |
| 66 const scoped_ptr<StreamImpl> stream_impl_; |
| 67 |
| 68 // The mock of the (remote) video capture client. |
| 69 MockStreamClient stream_client_impl_; |
| 70 |
| 71 private: |
| 72 DISALLOW_COPY_AND_ASSIGN(StreamImplTest); |
| 73 }; |
| 74 |
| 75 TEST_F(StreamImplTest, CreateAndStartAndDestroy) { |
| 76 StartAndExpectStartCallback(); |
| 77 } |
| 78 |
| 79 // Tests the connection |
| 80 // OnFrame() --> (NotifyFrameAvailable()) --> client's FrameAvailable(). |
| 81 TEST_F(StreamImplTest, OnFramePingsFrameAvailable) { |
| 82 InSequence s; |
| 83 StartAndExpectStartCallback(); |
| 84 |
| 85 const gfx::Size kFrameSize(10, 10); |
| 86 const scoped_refptr<MojoVideoFrame> le_frame = |
| 87 MojoVideoFrame::CreateMojoVideoFrame(kFrameSize, base::TimeDelta()); |
| 88 le_frame->AddDestructionObserver( |
| 89 base::Bind(&StreamImplTest::OnRelease, base::Unretained(this))); |
| 90 const base::TimeTicks now = base::TimeTicks::Now(); |
| 91 |
| 92 base::RunLoop run_loop; |
| 93 base::Closure quit_closure = run_loop.QuitClosure(); |
| 94 EXPECT_CALL( |
| 95 stream_client_impl_, |
| 96 DoFrameAvailable( |
| 97 AllOf(Field(&mojom::FrameInfo::storage_size, le_frame->mapped_size()), |
| 98 Field(&mojom::FrameInfo::pixel_format, |
| 99 static_cast<media::interfaces::VideoFormat>( |
| 100 le_frame->format())), |
| 101 Field(&mojom::FrameInfo::timestamp, now.ToInternalValue())), |
| 102 _)) |
| 103 .Times(1) |
| 104 .WillOnce(RunClosure(quit_closure)); |
| 105 EXPECT_CALL(*this, OnRelease()).Times(1); |
| 106 |
| 107 OnFrame(le_frame, now); |
| 108 run_loop.Run(); |
| 109 } |
| 110 |
| 111 // Tests the connection |
| 112 // OnError() --> (NotifyError()) --> client's and owners' Error(). |
| 113 TEST_F(StreamImplTest, OnErrorPingsError) { |
| 114 InSequence s; |
| 115 StartAndExpectStartCallback(); |
| 116 |
| 117 const std::string kErrorMessage("big error!"); |
| 118 base::RunLoop run_loop; |
| 119 base::Closure quit_closure = run_loop.QuitClosure(); |
| 120 |
| 121 EXPECT_CALL(*this, ErrorCallback()).Times(1); |
| 122 EXPECT_CALL( |
| 123 stream_client_impl_, |
| 124 Error(Property(&mojo::String::To<std::string>, kErrorMessage.c_str()))) |
| 125 .Times(1) |
| 126 .WillOnce(RunClosure(quit_closure)); |
| 127 OnError(kErrorMessage); |
| 128 run_loop.Run(); |
| 129 } |
| 130 |
| 131 // Tests the connection Stop() --> Stop Callback. |
| 132 TEST_F(StreamImplTest, StopPingsStopCallback) { |
| 133 StartAndExpectStartCallback(); |
| 134 |
| 135 EXPECT_CALL(*this, ErrorCallback()).Times(0); |
| 136 EXPECT_CALL(*this, StopCallback()).Times(1); |
| 137 stream_impl_->Stop(); |
| 138 base::RunLoop().RunUntilIdle(); |
| 139 } |
| 140 |
| 141 } // namespace media |
OLD | NEW |