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 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" 24 25 using testing::_; 26 using testing::AtMost; 27 using testing::InvokeWithoutArgs; 28 29 namespace remoting { 30 31 namespace { 32 33 // Default width and hight of the RDP client window. 34 const long kDefaultWidth = 1024; 35 const long kDefaultHeight = 768; 36 37 class MockRdpClientEventHandler : public RdpClient::EventHandler { 38 public: 39 MockRdpClientEventHandler() {} 40 virtual ~MockRdpClientEventHandler() {} 41 42 MOCK_METHOD0(OnRdpConnected, void()); 43 MOCK_METHOD0(OnRdpClosed, void()); 44 45 private: 46 DISALLOW_COPY_AND_ASSIGN(MockRdpClientEventHandler); 47 }; 48 49 // a14498c6-7f3b-4e42-9605-6c4a20d53c87 50 static GUID RdpClientModuleLibid = { 51 0xa14498c6, 52 0x7f3b, 53 0x4e42, 54 { 0x96, 0x05, 0x6c, 0x4a, 0x20, 0xd5, 0x3c, 0x87 } 55 }; 56 57 class RdpClientModule : public ATL::CAtlModuleT<RdpClientModule> { 58 public: 59 RdpClientModule(); 60 virtual ~RdpClientModule(); 61 62 DECLARE_LIBID(RdpClientModuleLibid) 63 64 private: 65 base::win::ScopedCOMInitializer com_initializer_; 66 }; 67 68 RdpClientModule::RdpClientModule() { 69 AtlAxWinInit(); 70 } 71 72 RdpClientModule::~RdpClientModule() { 73 AtlAxWinTerm(); 74 ATL::_pAtlModule = NULL; 75 } 76 77 } // namespace 78 79 class RdpClientTest : public testing::Test { 80 public: 81 RdpClientTest(); 82 virtual ~RdpClientTest(); 83 84 virtual void SetUp() OVERRIDE; 85 virtual void TearDown() OVERRIDE; 86 87 // Caaled when an RDP connection is established. 88 void OnRdpConnected(); 89 90 // Tears down |rdp_client_|. 91 void CloseRdpClient(); 92 93 protected: 94 // The ATL module instance required by the ATL code. 95 scoped_ptr<RdpClientModule> module_; 96 97 // The UI message loop used by RdpClient. The loop is stopped once there is no 98 // more references to |task_runner_|. 99 base::MessageLoopForUI message_loop_; 100 base::RunLoop run_loop_; 101 scoped_refptr<AutoThreadTaskRunner> task_runner_; 102 103 // Mocks RdpClient::EventHandler for testing. 104 MockRdpClientEventHandler event_handler_; 105 106 // Points to the object being tested. 107 scoped_ptr<RdpClient> rdp_client_; 108 109 // Unique terminal identifier passed to RdpClient. 110 std::string terminal_id_; 111 }; 112 113 RdpClientTest::RdpClientTest() { 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( 165 task_runner_, task_runner_, 166 webrtc::DesktopSize(kDefaultWidth, kDefaultHeight), 167 terminal_id_, &event_handler_)); 168 task_runner_ = NULL; 169 170 run_loop_.Run(); 171 } 172 173 } // namespace remoting 174