Re-land: C++ readability review
[chromium-blink-merge.git] / remoting / protocol / client_video_dispatcher_unittest.cc
blob92087b3357a595b5afe026f00110515ce5ef2476
1 // Copyright 2015 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.
5 #include "remoting/protocol/client_video_dispatcher.h"
7 #include "base/bind.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "remoting/base/constants.h"
12 #include "remoting/proto/video.pb.h"
13 #include "remoting/protocol/fake_session.h"
14 #include "remoting/protocol/fake_stream_socket.h"
15 #include "remoting/protocol/message_serialization.h"
16 #include "remoting/protocol/video_stub.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace remoting {
20 namespace protocol {
22 class ClientVideoDispatcherTest : public testing::Test,
23 public VideoStub,
24 public ChannelDispatcherBase::EventHandler {
25 public:
26 ClientVideoDispatcherTest();
28 // VideoStub interface.
29 void ProcessVideoPacket(scoped_ptr<VideoPacket> video_packet,
30 const base::Closure& done) override;
32 // ChannelDispatcherBase::EventHandler interface.
33 void OnChannelInitialized(ChannelDispatcherBase* channel_dispatcher) override;
34 void OnChannelError(ChannelDispatcherBase* channel_dispatcher,
35 ErrorCode error) override;
37 protected:
38 void OnVideoAck(scoped_ptr<VideoAck> ack, const base::Closure& done);
40 base::MessageLoop message_loop_;
42 // Set to true in OnChannelInitialized().
43 bool initialized_;
45 // Client side.
46 ClientVideoDispatcher dispatcher_;
47 FakeSession session_;
49 // Host side.
50 FakeStreamSocket host_socket_;
51 MessageReader reader_;
52 ProtobufMessageParser<VideoAck> parser_;
53 BufferedSocketWriter writer_;
55 ScopedVector<VideoPacket> video_packets_;
56 std::vector<base::Closure> packet_done_callbacks_;
58 ScopedVector<VideoAck> ack_messages_;
61 ClientVideoDispatcherTest::ClientVideoDispatcherTest()
62 : initialized_(false),
63 dispatcher_(this),
64 parser_(base::Bind(&ClientVideoDispatcherTest::OnVideoAck,
65 base::Unretained(this)),
66 &reader_) {
67 dispatcher_.Init(&session_, ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
68 kDefaultStreamVersion,
69 ChannelConfig::CODEC_UNDEFINED),
70 this);
71 base::RunLoop().RunUntilIdle();
72 DCHECK(initialized_);
73 host_socket_.PairWith(
74 session_.fake_channel_factory().GetFakeChannel(kVideoChannelName));
75 reader_.StartReading(&host_socket_);
76 writer_.Init(&host_socket_, BufferedSocketWriter::WriteFailedCallback());
79 void ClientVideoDispatcherTest::ProcessVideoPacket(
80 scoped_ptr<VideoPacket> video_packet,
81 const base::Closure& done) {
82 video_packets_.push_back(video_packet.release());
83 packet_done_callbacks_.push_back(done);
86 void ClientVideoDispatcherTest::OnChannelInitialized(
87 ChannelDispatcherBase* channel_dispatcher) {
88 initialized_ = true;
91 void ClientVideoDispatcherTest::OnChannelError(
92 ChannelDispatcherBase* channel_dispatcher,
93 ErrorCode error) {
94 // Don't expect channel creation to fail.
95 FAIL();
98 void ClientVideoDispatcherTest::OnVideoAck(scoped_ptr<VideoAck> ack,
99 const base::Closure& done) {
100 ack_messages_.push_back(ack.release());
101 done.Run();
104 // Verify that the client can receive video packets and acks are not sent for
105 // VideoPackets that don't have frame_id field set.
106 TEST_F(ClientVideoDispatcherTest, WithoutAcks) {
107 VideoPacket packet;
108 packet.set_data(std::string());
110 // Send a VideoPacket and verify that the client receives it.
111 writer_.Write(SerializeAndFrameMessage(packet), base::Closure());
112 base::RunLoop().RunUntilIdle();
113 EXPECT_EQ(1U, video_packets_.size());
115 packet_done_callbacks_.front().Run();
116 base::RunLoop().RunUntilIdle();
118 // Ack should never be sent for the packet without frame_id.
119 EXPECT_TRUE(ack_messages_.empty());
122 // Verifies that the dispatcher sends Ack message with correct rendering delay.
123 TEST_F(ClientVideoDispatcherTest, WithAcks) {
124 int kTestFrameId = 3;
126 VideoPacket packet;
127 packet.set_data(std::string());
128 packet.set_frame_id(kTestFrameId);
130 // Send a VideoPacket and verify that the client receives it.
131 writer_.Write(SerializeAndFrameMessage(packet), base::Closure());
132 base::RunLoop().RunUntilIdle();
133 EXPECT_EQ(1U, video_packets_.size());
135 // Ack should only be sent after the packet is processed.
136 EXPECT_TRUE(ack_messages_.empty());
137 base::RunLoop().RunUntilIdle();
139 // Fake completion of video packet decoding, to trigger the Ack.
140 packet_done_callbacks_.front().Run();
141 base::RunLoop().RunUntilIdle();
143 // Verify that the Ack message has been received.
144 ASSERT_EQ(1U, ack_messages_.size());
145 EXPECT_EQ(kTestFrameId, ack_messages_[0]->frame_id());
148 // Verify that Ack messages are sent in correct order.
149 TEST_F(ClientVideoDispatcherTest, AcksOrder) {
150 int kTestFrameId = 3;
152 VideoPacket packet;
153 packet.set_data(std::string());
154 packet.set_frame_id(kTestFrameId);
156 // Send two VideoPackets.
157 writer_.Write(SerializeAndFrameMessage(packet), base::Closure());
158 base::RunLoop().RunUntilIdle();
160 packet.set_frame_id(kTestFrameId + 1);
161 writer_.Write(SerializeAndFrameMessage(packet), base::Closure());
162 base::RunLoop().RunUntilIdle();
164 EXPECT_EQ(2U, video_packets_.size());
165 EXPECT_TRUE(ack_messages_.empty());
167 // Call completion callbacks in revers order.
168 packet_done_callbacks_[1].Run();
169 packet_done_callbacks_[0].Run();
171 base::RunLoop().RunUntilIdle();
173 // Verify order of Ack messages.
174 ASSERT_EQ(2U, ack_messages_.size());
175 EXPECT_EQ(kTestFrameId, ack_messages_[0]->frame_id());
176 EXPECT_EQ(kTestFrameId + 1, ack_messages_[1]->frame_id());
179 } // namespace protocol
180 } // namespace remoting