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::NotNull
;
18 using ::testing::StrictMock
;
23 class ConnectionToClientTest
: public testing::Test
{
25 ConnectionToClientTest() {
29 virtual 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 virtual void TearDown() OVERRIDE
{
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_
;
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(session_
->GetStreamChannel(kVideoChannelName
));
73 EXPECT_GT(session_
->GetStreamChannel(kVideoChannelName
)->
74 written_data().size(), 0u);
76 // And then close the connection to ConnectionToClient.
77 viewer_
->Disconnect();
79 base::RunLoop().RunUntilIdle();
82 TEST_F(ConnectionToClientTest
, NoWriteAfterDisconnect
) {
83 scoped_ptr
<VideoPacket
> packet(new VideoPacket());
84 viewer_
->video_stub()->ProcessVideoPacket(packet
.Pass(), base::Closure());
86 // And then close the connection to ConnectionToClient.
87 viewer_
->Disconnect();
89 // The test will crash if data writer tries to write data to the
91 // TODO(sergeyu): Use MockSession to verify that no data is written?
92 base::RunLoop().RunUntilIdle();
95 TEST_F(ConnectionToClientTest
, StateChange
) {
96 EXPECT_CALL(handler_
, OnConnectionClosed(viewer_
.get(), OK
));
97 session_
->event_handler()->OnSessionStateChange(Session::CLOSED
);
98 base::RunLoop().RunUntilIdle();
100 EXPECT_CALL(handler_
, OnConnectionClosed(viewer_
.get(), SESSION_REJECTED
));
101 session_
->set_error(SESSION_REJECTED
);
102 session_
->event_handler()->OnSessionStateChange(Session::FAILED
);
103 base::RunLoop().RunUntilIdle();
106 } // namespace protocol
107 } // namespace remoting