Home | History | Annotate | Download | only in win
      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::MessageLoop 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     : message_loop_(base::MessageLoop::TYPE_UI) {
    115 }
    116 
    117 RdpClientTest::~RdpClientTest() {
    118 }
    119 
    120 void RdpClientTest::SetUp() {
    121   // Arrange to run |message_loop_| until no components depend on it.
    122   task_runner_ = new AutoThreadTaskRunner(
    123       message_loop_.message_loop_proxy(), run_loop_.QuitClosure());
    124 
    125   module_.reset(new RdpClientModule());
    126 }
    127 
    128 void RdpClientTest::TearDown() {
    129   EXPECT_TRUE(!rdp_client_);
    130 
    131   module_.reset();
    132 }
    133 
    134 void RdpClientTest::OnRdpConnected() {
    135   uint32 session_id = WtsTerminalMonitor::LookupSessionId(terminal_id_);
    136 
    137   std::string id;
    138   EXPECT_TRUE(WtsTerminalMonitor::LookupTerminalId(session_id, &id));
    139   EXPECT_EQ(id, terminal_id_);
    140 
    141   message_loop_.PostTask(FROM_HERE, base::Bind(&RdpClientTest::CloseRdpClient,
    142                                                base::Unretained(this)));
    143 }
    144 
    145 void RdpClientTest::CloseRdpClient() {
    146   EXPECT_TRUE(rdp_client_);
    147 
    148   rdp_client_.reset();
    149 }
    150 
    151 // Creates a loopback RDP connection.
    152 TEST_F(RdpClientTest, Basic) {
    153   terminal_id_ = base::GenerateGUID();
    154 
    155   // An ability to establish a loopback RDP connection depends on many factors
    156   // including OS SKU and having RDP enabled. Accept both successful connection
    157   // and a connection error as a successful outcome.
    158   EXPECT_CALL(event_handler_, OnRdpConnected())
    159       .Times(AtMost(1))
    160       .WillOnce(Invoke(this, &RdpClientTest::OnRdpConnected));
    161   EXPECT_CALL(event_handler_, OnRdpClosed())
    162       .Times(AtMost(1))
    163       .WillOnce(InvokeWithoutArgs(this, &RdpClientTest::CloseRdpClient));
    164 
    165   rdp_client_.reset(new RdpClient(
    166       task_runner_, task_runner_,
    167       webrtc::DesktopSize(kDefaultWidth, kDefaultHeight),
    168       terminal_id_, &event_handler_));
    169   task_runner_ = NULL;
    170 
    171   run_loop_.Run();
    172 }
    173 
    174 }  // namespace remoting
    175