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.
5 // ATL headers have to go first.
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"
25 using testing::AtMost;
26 using testing::InvokeWithoutArgs;
32 // Default width and hight of the RDP client window.
33 const long kDefaultWidth = 1024;
34 const long kDefaultHeight = 768;
36 class MockRdpClientEventHandler : public RdpClient::EventHandler {
38 MockRdpClientEventHandler() {}
39 virtual ~MockRdpClientEventHandler() {}
41 MOCK_METHOD0(OnRdpConnected, void());
42 MOCK_METHOD0(OnRdpClosed, void());
45 DISALLOW_COPY_AND_ASSIGN(MockRdpClientEventHandler);
48 // a14498c6-7f3b-4e42-9605-6c4a20d53c87
49 static GUID RdpClientModuleLibid = {
53 { 0x96, 0x05, 0x6c, 0x4a, 0x20, 0xd5, 0x3c, 0x87 }
56 class RdpClientModule : public ATL::CAtlModuleT<RdpClientModule> {
59 virtual ~RdpClientModule();
61 DECLARE_LIBID(RdpClientModuleLibid)
64 base::win::ScopedCOMInitializer com_initializer_;
67 RdpClientModule::RdpClientModule() {
71 RdpClientModule::~RdpClientModule() {
73 ATL::_pAtlModule = NULL;
78 class RdpClientTest : public testing::Test {
81 virtual ~RdpClientTest();
83 virtual void SetUp() OVERRIDE;
84 virtual void TearDown() OVERRIDE;
86 // Caaled when an RDP connection is established.
87 void OnRdpConnected();
89 // Tears down |rdp_client_|.
90 void CloseRdpClient();
93 // The ATL module instance required by the ATL code.
94 scoped_ptr<RdpClientModule> module_;
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_;
102 // Mocks RdpClient::EventHandler for testing.
103 MockRdpClientEventHandler event_handler_;
105 // Points to the object being tested.
106 scoped_ptr<RdpClient> rdp_client_;
108 // Unique terminal identifier passed to RdpClient.
109 std::string terminal_id_;
112 RdpClientTest::RdpClientTest()
113 : message_loop_(base::MessageLoop::TYPE_UI) {
116 RdpClientTest::~RdpClientTest() {
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());
124 module_.reset(new RdpClientModule());
127 void RdpClientTest::TearDown() {
128 EXPECT_TRUE(!rdp_client_);
133 void RdpClientTest::OnRdpConnected() {
134 uint32 session_id = WtsTerminalMonitor::LookupSessionId(terminal_id_);
137 EXPECT_TRUE(WtsTerminalMonitor::LookupTerminalId(session_id, &id));
138 EXPECT_EQ(id, terminal_id_);
140 message_loop_.PostTask(FROM_HERE, base::Bind(&RdpClientTest::CloseRdpClient,
141 base::Unretained(this)));
144 void RdpClientTest::CloseRdpClient() {
145 EXPECT_TRUE(rdp_client_);
150 // Creates a loopback RDP connection.
151 TEST_F(RdpClientTest, Basic) {
152 terminal_id_ = base::GenerateGUID();
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())
159 .WillOnce(Invoke(this, &RdpClientTest::OnRdpConnected));
160 EXPECT_CALL(event_handler_, OnRdpClosed())
162 .WillOnce(InvokeWithoutArgs(this, &RdpClientTest::CloseRdpClient));
164 rdp_client_.reset(new RdpClient(task_runner_, task_runner_,
165 SkISize::Make(kDefaultWidth, kDefaultHeight),
166 terminal_id_, &event_handler_));
172 } // namespace remoting