- add sources.
[platform/framework/web/crosswalk.git] / src / remoting / host / win / rdp_client_unittest.cc
1 // Copyright (c) 2013 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 // ATL headers have to go first.
6 #include <atlbase.h>
7 #include <atlhost.h>
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/guid.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "base/win/scoped_com_initializer.h"
17 #include "remoting/base/auto_thread_task_runner.h"
18 #include "remoting/host/win/rdp_client.h"
19 #include "remoting/host/win/wts_terminal_monitor.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gmock_mutant.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 using testing::_;
25 using testing::AtMost;
26 using testing::InvokeWithoutArgs;
27
28 namespace remoting {
29
30 namespace {
31
32 // Default width and hight of the RDP client window.
33 const long kDefaultWidth = 1024;
34 const long kDefaultHeight = 768;
35
36 class MockRdpClientEventHandler : public RdpClient::EventHandler {
37  public:
38   MockRdpClientEventHandler() {}
39   virtual ~MockRdpClientEventHandler() {}
40
41   MOCK_METHOD0(OnRdpConnected, void());
42   MOCK_METHOD0(OnRdpClosed, void());
43
44  private:
45   DISALLOW_COPY_AND_ASSIGN(MockRdpClientEventHandler);
46 };
47
48 // a14498c6-7f3b-4e42-9605-6c4a20d53c87
49 static GUID RdpClientModuleLibid = {
50   0xa14498c6,
51   0x7f3b,
52   0x4e42,
53   { 0x96, 0x05, 0x6c, 0x4a, 0x20, 0xd5, 0x3c, 0x87 }
54 };
55
56 class RdpClientModule : public ATL::CAtlModuleT<RdpClientModule> {
57  public:
58   RdpClientModule();
59   virtual ~RdpClientModule();
60
61   DECLARE_LIBID(RdpClientModuleLibid)
62
63  private:
64   base::win::ScopedCOMInitializer com_initializer_;
65 };
66
67 RdpClientModule::RdpClientModule() {
68   AtlAxWinInit();
69 }
70
71 RdpClientModule::~RdpClientModule() {
72   AtlAxWinTerm();
73   ATL::_pAtlModule = NULL;
74 }
75
76 }  // namespace
77
78 class RdpClientTest : public testing::Test {
79  public:
80   RdpClientTest();
81   virtual ~RdpClientTest();
82
83   virtual void SetUp() OVERRIDE;
84   virtual void TearDown() OVERRIDE;
85
86   // Caaled when an RDP connection is established.
87   void OnRdpConnected();
88
89   // Tears down |rdp_client_|.
90   void CloseRdpClient();
91
92  protected:
93   // The ATL module instance required by the ATL code.
94   scoped_ptr<RdpClientModule> module_;
95
96   // The UI message loop used by RdpClient. The loop is stopped once there is no
97   // more references to |task_runner_|.
98   base::MessageLoop message_loop_;
99   base::RunLoop run_loop_;
100   scoped_refptr<AutoThreadTaskRunner> task_runner_;
101
102   // Mocks RdpClient::EventHandler for testing.
103   MockRdpClientEventHandler event_handler_;
104
105   // Points to the object being tested.
106   scoped_ptr<RdpClient> rdp_client_;
107
108   // Unique terminal identifier passed to RdpClient.
109   std::string terminal_id_;
110 };
111
112 RdpClientTest::RdpClientTest()
113     : message_loop_(base::MessageLoop::TYPE_UI) {
114 }
115
116 RdpClientTest::~RdpClientTest() {
117 }
118
119 void RdpClientTest::SetUp() {
120   // Arrange to run |message_loop_| until no components depend on it.
121   task_runner_ = new AutoThreadTaskRunner(
122       message_loop_.message_loop_proxy(), run_loop_.QuitClosure());
123
124   module_.reset(new RdpClientModule());
125 }
126
127 void RdpClientTest::TearDown() {
128   EXPECT_TRUE(!rdp_client_);
129
130   module_.reset();
131 }
132
133 void RdpClientTest::OnRdpConnected() {
134   uint32 session_id = WtsTerminalMonitor::LookupSessionId(terminal_id_);
135
136   std::string id;
137   EXPECT_TRUE(WtsTerminalMonitor::LookupTerminalId(session_id, &id));
138   EXPECT_EQ(id, terminal_id_);
139
140   message_loop_.PostTask(FROM_HERE, base::Bind(&RdpClientTest::CloseRdpClient,
141                                                base::Unretained(this)));
142 }
143
144 void RdpClientTest::CloseRdpClient() {
145   EXPECT_TRUE(rdp_client_);
146
147   rdp_client_.reset();
148 }
149
150 // Creates a loopback RDP connection.
151 TEST_F(RdpClientTest, Basic) {
152   terminal_id_ = base::GenerateGUID();
153
154   // An ability to establish a loopback RDP connection depends on many factors
155   // including OS SKU and having RDP enabled. Accept both successful connection
156   // and a connection error as a successful outcome.
157   EXPECT_CALL(event_handler_, OnRdpConnected())
158       .Times(AtMost(1))
159       .WillOnce(Invoke(this, &RdpClientTest::OnRdpConnected));
160   EXPECT_CALL(event_handler_, OnRdpClosed())
161       .Times(AtMost(1))
162       .WillOnce(InvokeWithoutArgs(this, &RdpClientTest::CloseRdpClient));
163
164   rdp_client_.reset(new RdpClient(task_runner_, task_runner_,
165                                   SkISize::Make(kDefaultWidth, kDefaultHeight),
166                                   terminal_id_, &event_handler_));
167   task_runner_ = NULL;
168
169   run_loop_.Run();
170 }
171
172 }  // namespace remoting