Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / protocol / connection_to_client_unittest.cc
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.
4
5 #include "remoting/protocol/connection_to_client.h"
6
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"
15
16 using ::testing::_;
17 using ::testing::NotNull;
18 using ::testing::StrictMock;
19
20 namespace remoting {
21 namespace protocol {
22
23 class ConnectionToClientTest : public testing::Test {
24  public:
25   ConnectionToClientTest() {
26   }
27
28  protected:
29   void SetUp() override {
30     session_ = new FakeSession();
31
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();
43   }
44
45   void TearDown() override {
46     viewer_.reset();
47     base::RunLoop().RunUntilIdle();
48   }
49
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_;
56
57   // Owned by |viewer_|.
58   FakeSession* session_;
59
60  private:
61   DISALLOW_COPY_AND_ASSIGN(ConnectionToClientTest);
62 };
63
64 TEST_F(ConnectionToClientTest, SendUpdateStream) {
65   scoped_ptr<VideoPacket> packet(new VideoPacket());
66   viewer_->video_stub()->ProcessVideoPacket(packet.Pass(), base::Closure());
67
68   base::RunLoop().RunUntilIdle();
69
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());
76
77   // And then close the connection to ConnectionToClient.
78   viewer_->Disconnect();
79
80   base::RunLoop().RunUntilIdle();
81 }
82
83 TEST_F(ConnectionToClientTest, NoWriteAfterDisconnect) {
84   scoped_ptr<VideoPacket> packet(new VideoPacket());
85   viewer_->video_stub()->ProcessVideoPacket(packet.Pass(), base::Closure());
86
87   // And then close the connection to ConnectionToClient.
88   viewer_->Disconnect();
89
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();
94 }
95
96 TEST_F(ConnectionToClientTest, StateChange) {
97   EXPECT_CALL(handler_, OnConnectionClosed(viewer_.get(), OK));
98   session_->event_handler()->OnSessionStateChange(Session::CLOSED);
99   base::RunLoop().RunUntilIdle();
100
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();
105 }
106
107 }  // namespace protocol
108 }  // namespace remoting