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/run_loop.h"
10 #include "remoting/base/constants.h"
11 #include "remoting/protocol/fake_session.h"
12 #include "remoting/protocol/protocol_mock_objects.h"
13 #include "testing/gmock/include/gmock/gmock.h"
16 using ::testing::InvokeWithoutArgs
;
17 using ::testing::NotNull
;
18 using ::testing::StrictMock
;
23 class ConnectionToClientTest
: public testing::Test
{
25 ConnectionToClientTest() {
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_
->SetEventHandler(&handler_
);
35 EXPECT_CALL(handler_
, OnConnectionAuthenticated(viewer_
.get()))
37 InvokeWithoutArgs(this, &ConnectionToClientTest::ConnectStubs
));
38 EXPECT_CALL(handler_
, OnConnectionChannelsConnected(viewer_
.get()));
39 session_
->event_handler()->OnSessionStateChange(Session::CONNECTED
);
40 session_
->event_handler()->OnSessionStateChange(Session::AUTHENTICATED
);
41 base::RunLoop().RunUntilIdle();
44 void TearDown() override
{
46 base::RunLoop().RunUntilIdle();
50 viewer_
->set_clipboard_stub(&clipboard_stub_
);
51 viewer_
->set_host_stub(&host_stub_
);
52 viewer_
->set_input_stub(&input_stub_
);
55 base::MessageLoop message_loop_
;
56 MockConnectionToClientEventHandler handler_
;
57 MockClipboardStub clipboard_stub_
;
58 MockHostStub host_stub_
;
59 MockInputStub input_stub_
;
60 scoped_ptr
<ConnectionToClient
> viewer_
;
62 // Owned by |viewer_|.
63 FakeSession
* session_
;
66 DISALLOW_COPY_AND_ASSIGN(ConnectionToClientTest
);
69 TEST_F(ConnectionToClientTest
, SendUpdateStream
) {
70 scoped_ptr
<VideoPacket
> packet(new VideoPacket());
71 viewer_
->video_stub()->ProcessVideoPacket(packet
.Pass(), base::Closure());
73 base::RunLoop().RunUntilIdle();
75 // Verify that something has been written.
76 // TODO(sergeyu): Verify that the correct data has been written.
78 session_
->fake_channel_factory().GetFakeChannel(kVideoChannelName
));
79 EXPECT_FALSE(session_
->fake_channel_factory()
80 .GetFakeChannel(kVideoChannelName
)->written_data().empty());
82 // And then close the connection to ConnectionToClient.
83 viewer_
->Disconnect();
85 base::RunLoop().RunUntilIdle();
88 TEST_F(ConnectionToClientTest
, NoWriteAfterDisconnect
) {
89 scoped_ptr
<VideoPacket
> packet(new VideoPacket());
90 viewer_
->video_stub()->ProcessVideoPacket(packet
.Pass(), base::Closure());
92 // And then close the connection to ConnectionToClient.
93 viewer_
->Disconnect();
95 // The test will crash if data writer tries to write data to the
97 // TODO(sergeyu): Use MockSession to verify that no data is written?
98 base::RunLoop().RunUntilIdle();
101 TEST_F(ConnectionToClientTest
, StateChange
) {
102 EXPECT_CALL(handler_
, OnConnectionClosed(viewer_
.get(), OK
));
103 session_
->event_handler()->OnSessionStateChange(Session::CLOSED
);
104 base::RunLoop().RunUntilIdle();
106 EXPECT_CALL(handler_
, OnConnectionClosed(viewer_
.get(), SESSION_REJECTED
));
107 session_
->set_error(SESSION_REJECTED
);
108 session_
->event_handler()->OnSessionStateChange(Session::FAILED
);
109 base::RunLoop().RunUntilIdle();
112 } // namespace protocol
113 } // namespace remoting