Revert of Revert of Roll android_tools f6e2370:2a860d8 (patchset #1 id:1 of https...
[chromium-blink-merge.git] / remoting / protocol / connection_to_client_unittest.cc
blobe123cfaf0db922a7a6492d43de476d2977d6ac2f
1 // Copyright (c) 2012 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/connection_to_client.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/run_loop.h"
11 #include "remoting/base/constants.h"
12 #include "remoting/protocol/fake_session.h"
13 #include "remoting/protocol/protocol_mock_objects.h"
14 #include "testing/gmock/include/gmock/gmock.h"
16 using ::testing::_;
17 using ::testing::NotNull;
18 using ::testing::StrictMock;
20 namespace remoting {
21 namespace protocol {
23 class ConnectionToClientTest : public testing::Test {
24 public:
25 ConnectionToClientTest() {
28 protected:
29 void SetUp() override {
30 session_ = new FakeSession();
32 // Allocate a ClientConnection object with the mock objects.
33 viewer_.reset(new ConnectionToClient(session_));
34 viewer_->set_clipboard_stub(&clipboard_stub_);
35 viewer_->set_host_stub(&host_stub_);
36 viewer_->set_input_stub(&input_stub_);
37 viewer_->SetEventHandler(&handler_);
38 EXPECT_CALL(handler_, OnConnectionAuthenticated(viewer_.get()));
39 EXPECT_CALL(handler_, OnConnectionChannelsConnected(viewer_.get()));
40 session_->event_handler()->OnSessionStateChange(Session::CONNECTED);
41 session_->event_handler()->OnSessionStateChange(Session::AUTHENTICATED);
42 base::RunLoop().RunUntilIdle();
45 void TearDown() override {
46 viewer_.reset();
47 base::RunLoop().RunUntilIdle();
50 base::MessageLoop message_loop_;
51 MockConnectionToClientEventHandler handler_;
52 MockClipboardStub clipboard_stub_;
53 MockHostStub host_stub_;
54 MockInputStub input_stub_;
55 scoped_ptr<ConnectionToClient> viewer_;
57 // Owned by |viewer_|.
58 FakeSession* session_;
60 private:
61 DISALLOW_COPY_AND_ASSIGN(ConnectionToClientTest);
64 TEST_F(ConnectionToClientTest, SendUpdateStream) {
65 scoped_ptr<VideoPacket> packet(new VideoPacket());
66 viewer_->video_stub()->ProcessVideoPacket(packet.Pass(), base::Closure());
68 base::RunLoop().RunUntilIdle();
70 // Verify that something has been written.
71 // TODO(sergeyu): Verify that the correct data has been written.
72 ASSERT_TRUE(
73 session_->fake_channel_factory().GetFakeChannel(kVideoChannelName));
74 EXPECT_FALSE(session_->fake_channel_factory()
75 .GetFakeChannel(kVideoChannelName)->written_data().empty());
77 // And then close the connection to ConnectionToClient.
78 viewer_->Disconnect();
80 base::RunLoop().RunUntilIdle();
83 TEST_F(ConnectionToClientTest, NoWriteAfterDisconnect) {
84 scoped_ptr<VideoPacket> packet(new VideoPacket());
85 viewer_->video_stub()->ProcessVideoPacket(packet.Pass(), base::Closure());
87 // And then close the connection to ConnectionToClient.
88 viewer_->Disconnect();
90 // The test will crash if data writer tries to write data to the
91 // channel socket.
92 // TODO(sergeyu): Use MockSession to verify that no data is written?
93 base::RunLoop().RunUntilIdle();
96 TEST_F(ConnectionToClientTest, StateChange) {
97 EXPECT_CALL(handler_, OnConnectionClosed(viewer_.get(), OK));
98 session_->event_handler()->OnSessionStateChange(Session::CLOSED);
99 base::RunLoop().RunUntilIdle();
101 EXPECT_CALL(handler_, OnConnectionClosed(viewer_.get(), SESSION_REJECTED));
102 session_->set_error(SESSION_REJECTED);
103 session_->event_handler()->OnSessionStateChange(Session::FAILED);
104 base::RunLoop().RunUntilIdle();
107 } // namespace protocol
108 } // namespace remoting