diff options
author | wez@chromium.org <wez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-26 07:23:12 +0000 |
---|---|---|
committer | wez@chromium.org <wez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-26 07:23:12 +0000 |
commit | 2ce8025b3d7e8ace45a1fae8bdf647362faca9cc (patch) | |
tree | afb1004c0c939d852474e9e71b84829bbc99b8fd /remoting/host/video_scheduler_unittest.cc | |
parent | eb71e02f458da73701713b25e509a9b452b8bb92 (diff) | |
download | chromium_src-2ce8025b3d7e8ace45a1fae8bdf647362faca9cc.zip chromium_src-2ce8025b3d7e8ace45a1fae8bdf647362faca9cc.tar.gz chromium_src-2ce8025b3d7e8ace45a1fae8bdf647362faca9cc.tar.bz2 |
Rename ScreenRecorder to VideoScheduler.
BUG=104543
Review URL: https://chromiumcodereview.appspot.com/11229023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164282 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/host/video_scheduler_unittest.cc')
-rw-r--r-- | remoting/host/video_scheduler_unittest.cc | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/remoting/host/video_scheduler_unittest.cc b/remoting/host/video_scheduler_unittest.cc new file mode 100644 index 0000000..abaea18 --- /dev/null +++ b/remoting/host/video_scheduler_unittest.cc @@ -0,0 +1,201 @@ +// Copyright (c) 2012 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 "remoting/host/video_scheduler.h" + +#include "base/bind.h" +#include "base/message_loop.h" +#include "remoting/base/capture_data.h" +#include "remoting/codec/video_encoder.h" +#include "remoting/host/host_mock_objects.h" +#include "remoting/proto/video.pb.h" +#include "remoting/protocol/protocol_mock_objects.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +using ::remoting::protocol::MockConnectionToClient; +using ::remoting::protocol::MockConnectionToClientEventHandler; +using ::remoting::protocol::MockHostStub; +using ::remoting::protocol::MockSession; +using ::remoting::protocol::MockVideoStub; + +using ::testing::_; +using ::testing::AtLeast; +using ::testing::AnyNumber; +using ::testing::DeleteArg; +using ::testing::DoAll; +using ::testing::Expectation; +using ::testing::InSequence; +using ::testing::InvokeWithoutArgs; +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::SaveArg; + +namespace remoting { + +namespace { + +ACTION_P2(RunCallback, region, data) { + SkRegion& dirty_region = data->mutable_dirty_region(); + dirty_region.op(region, SkRegion::kUnion_Op); + arg0.Run(data); +} + +ACTION(FinishEncode) { + scoped_ptr<VideoPacket> packet(new VideoPacket()); + packet->set_flags(VideoPacket::LAST_PACKET | VideoPacket::LAST_PARTITION); + arg2.Run(packet.Pass()); +} + +ACTION(FinishSend) { + arg1.Run(); +} + +// Helper method to quit the main message loop. +void QuitMessageLoop(MessageLoop* message_loop) { + message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); +} + +ACTION_P2(StopVideoScheduler, scheduler, task) { + scheduler->Stop(task); +} + +} // namespace + +static const int kWidth = 640; +static const int kHeight = 480; +static const media::VideoFrame::Format kFormat = media::VideoFrame::RGB32; +static const VideoPacketFormat::Encoding kEncoding = + VideoPacketFormat::ENCODING_VERBATIM; + +class MockVideoEncoder : public VideoEncoder { + public: + MockVideoEncoder(); + virtual ~MockVideoEncoder(); + + MOCK_METHOD3(Encode, void( + scoped_refptr<CaptureData> capture_data, + bool key_frame, + const DataAvailableCallback& data_available_callback)); + + private: + DISALLOW_COPY_AND_ASSIGN(MockVideoEncoder); +}; + +MockVideoEncoder::MockVideoEncoder() {} + +MockVideoEncoder::~MockVideoEncoder() {} + +class VideoSchedulerTest : public testing::Test { + public: + VideoSchedulerTest() { + } + + virtual void SetUp() OVERRIDE { + // VideoFrameCapturer and VideoEncoder are owned by VideoScheduler. + encoder_ = new MockVideoEncoder(); + + session_ = new MockSession(); + EXPECT_CALL(*session_, SetEventHandler(_)); + EXPECT_CALL(*session_, Close()) + .Times(AnyNumber()); + connection_.reset(new MockConnectionToClient(session_, &host_stub_)); + connection_->SetEventHandler(&handler_); + + scheduler_ = new VideoScheduler( + message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), + message_loop_.message_loop_proxy(), &capturer_, + scoped_ptr<remoting::VideoEncoder>(encoder_)); + } + + virtual void TearDown() OVERRIDE { + connection_.reset(); + // Run message loop before destroying because protocol::Session is + // destroyed asynchronously. + message_loop_.RunAllPending(); + } + + protected: + MessageLoop message_loop_; + scoped_refptr<VideoScheduler> scheduler_; + + MockConnectionToClientEventHandler handler_; + MockHostStub host_stub_; + MockSession* session_; // Owned by |connection_|. + scoped_ptr<MockConnectionToClient> connection_; + + // The following mock objects are owned by VideoScheduler. + MockVideoFrameCapturer capturer_; + MockVideoEncoder* encoder_; + + private: + DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); +}; + +// This test mocks capturer, encoder and network layer to simulate one capture +// cycle. When the first encoded packet is submitted to the network +// VideoScheduler is instructed to come to a complete stop. We expect the stop +// sequence to be executed successfully. +TEST_F(VideoSchedulerTest, StartAndStop) { + SkRegion update_region(SkIRect::MakeXYWH(0, 0, 10, 10)); + DataPlanes planes; + for (int i = 0; i < DataPlanes::kPlaneCount; ++i) { + planes.data[i] = reinterpret_cast<uint8*>(i); + planes.strides[i] = kWidth * 4; + } + + Expectation capturer_start = EXPECT_CALL(capturer_, Start(_)); + + SkISize size(SkISize::Make(kWidth, kHeight)); + scoped_refptr<CaptureData> data(new CaptureData(planes, size, kFormat)); + + EXPECT_CALL(capturer_, size_most_recent()) + .WillRepeatedly(ReturnRef(size)); + + EXPECT_CALL(capturer_, InvalidateRegion(_)); + + // First the capturer is called. + Expectation capturer_capture = EXPECT_CALL(capturer_, CaptureInvalidRegion(_)) + .After(capturer_start) + .WillRepeatedly(RunCallback(update_region, data)); + + // Expect the encoder be called. + EXPECT_CALL(*encoder_, Encode(data, false, _)) + .WillRepeatedly(FinishEncode()); + + MockVideoStub video_stub; + EXPECT_CALL(*connection_, video_stub()) + .WillRepeatedly(Return(&video_stub)); + + // By default delete the arguments when ProcessVideoPacket is received. + EXPECT_CALL(video_stub, ProcessVideoPacketPtr(_, _)) + .WillRepeatedly(FinishSend()); + + // For the first time when ProcessVideoPacket is received we stop the + // VideoScheduler. + EXPECT_CALL(video_stub, ProcessVideoPacketPtr(_, _)) + .WillOnce(DoAll( + FinishSend(), + StopVideoScheduler(scheduler_, + base::Bind(&QuitMessageLoop, &message_loop_)))) + .RetiresOnSaturation(); + + EXPECT_CALL(capturer_, Stop()) + .After(capturer_capture); + + // Add the mock client connection to the session. + scheduler_->AddConnection(connection_.get()); + + // Start capturing. + scheduler_->Start(); + message_loop_.Run(); +} + +TEST_F(VideoSchedulerTest, StopWithoutStart) { + EXPECT_CALL(capturer_, Stop()); + scheduler_->Stop(base::Bind(&QuitMessageLoop, &message_loop_)); + message_loop_.Run(); +} + +} // namespace remoting |