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"
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"
17 using ::testing::InvokeWithoutArgs
;
18 using ::testing::NotNull
;
19 using ::testing::StrictMock
;
24 class ConnectionToClientTest
: public testing::Test
{
26 ConnectionToClientTest() {
30 void SetUp() override
{
31 session_
= new FakeSession();
33 // Allocate a ClientConnection object with the mock objects.
34 viewer_
.reset(new ConnectionToClient(session_
));
35 viewer_
->SetEventHandler(&handler_
);
36 EXPECT_CALL(handler_
, OnConnectionAuthenticated(viewer_
.get()))
38 InvokeWithoutArgs(this, &ConnectionToClientTest::ConnectStubs
));
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
{
47 base::RunLoop().RunUntilIdle();
51 viewer_
->set_clipboard_stub(&clipboard_stub_
);
52 viewer_
->set_host_stub(&host_stub_
);
53 viewer_
->set_input_stub(&input_stub_
);
56 base::MessageLoop message_loop_
;
57 MockConnectionToClientEventHandler handler_
;
58 MockClipboardStub clipboard_stub_
;
59 MockHostStub host_stub_
;
60 MockInputStub input_stub_
;
61 scoped_ptr
<ConnectionToClient
> viewer_
;
63 // Owned by |viewer_|.
64 FakeSession
* session_
;
67 DISALLOW_COPY_AND_ASSIGN(ConnectionToClientTest
);
70 TEST_F(ConnectionToClientTest
, SendUpdateStream
) {
71 scoped_ptr
<VideoPacket
> packet(new VideoPacket());
72 viewer_
->video_stub()->ProcessVideoPacket(packet
.Pass(), base::Closure());
74 base::RunLoop().RunUntilIdle();
76 // Verify that something has been written.
77 // TODO(sergeyu): Verify that the correct data has been written.
79 session_
->fake_channel_factory().GetFakeChannel(kVideoChannelName
));
80 EXPECT_FALSE(session_
->fake_channel_factory()
81 .GetFakeChannel(kVideoChannelName
)->written_data().empty());
83 // And then close the connection to ConnectionToClient.
84 viewer_
->Disconnect();
86 base::RunLoop().RunUntilIdle();
89 TEST_F(ConnectionToClientTest
, NoWriteAfterDisconnect
) {
90 scoped_ptr
<VideoPacket
> packet(new VideoPacket());
91 viewer_
->video_stub()->ProcessVideoPacket(packet
.Pass(), base::Closure());
93 // And then close the connection to ConnectionToClient.
94 viewer_
->Disconnect();
96 // The test will crash if data writer tries to write data to the
98 // TODO(sergeyu): Use MockSession to verify that no data is written?
99 base::RunLoop().RunUntilIdle();
102 TEST_F(ConnectionToClientTest
, StateChange
) {
103 EXPECT_CALL(handler_
, OnConnectionClosed(viewer_
.get(), OK
));
104 session_
->event_handler()->OnSessionStateChange(Session::CLOSED
);
105 base::RunLoop().RunUntilIdle();
107 EXPECT_CALL(handler_
, OnConnectionClosed(viewer_
.get(), SESSION_REJECTED
));
108 session_
->set_error(SESSION_REJECTED
);
109 session_
->event_handler()->OnSessionStateChange(Session::FAILED
);
110 base::RunLoop().RunUntilIdle();
113 } // namespace protocol
114 } // namespace remoting