Home | History | Annotate | Download | only in test
      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 #ifndef CHROME_FRAME_TEST_AUTOMATION_CLIENT_MOCK_H_
      6 #define CHROME_FRAME_TEST_AUTOMATION_CLIENT_MOCK_H_
      7 
      8 #include <windows.h>
      9 #include <string>
     10 
     11 #include "base/strings/string_util.h"
     12 #include "chrome_frame/chrome_frame_automation.h"
     13 #include "chrome_frame/test/chrome_frame_test_utils.h"
     14 #include "chrome_frame/test/proxy_factory_mock.h"
     15 #include "chrome_frame/utils.h"
     16 #include "gmock/gmock.h"
     17 
     18 using testing::StrictMock;
     19 
     20 // ChromeFrameAutomationClient [CFAC] tests.
     21 struct MockCFDelegate : public ChromeFrameDelegateImpl {
     22   MOCK_CONST_METHOD0(GetWindow, WindowType());
     23   MOCK_METHOD1(GetBounds, void(RECT* bounds));
     24   MOCK_METHOD0(GetDocumentUrl, std::string());
     25   MOCK_METHOD2(ExecuteScript, bool(const std::string& script,
     26                                    std::string* result));
     27   MOCK_METHOD0(OnAutomationServerReady, void());
     28   MOCK_METHOD2(OnAutomationServerLaunchFailed, void(
     29       AutomationLaunchResult reason, const std::string& server_version));
     30   // This remains in interface since we call it if Navigate()
     31   // returns immediate error.
     32   MOCK_METHOD2(OnLoadFailed, void(int error_code, const std::string& url));
     33 
     34   // Do not mock this method. :) Use it as message demuxer and dispatcher
     35   // to the following methods (which we mock)
     36   // MOCK_METHOD1(OnMessageReceived, void(const IPC::Message&));
     37 
     38   MOCK_METHOD0(OnChannelError, void(void));
     39   MOCK_METHOD1(OnNavigationStateChanged, void(int flags));
     40   MOCK_METHOD1(OnUpdateTargetUrl, void(
     41       const std::wstring& new_target_url));
     42   MOCK_METHOD1(OnAcceleratorPressed, void(const MSG& accel_message));
     43   MOCK_METHOD1(OnTabbedOut, void(bool reverse));
     44   MOCK_METHOD2(OnOpenURL, void(const GURL& url, int open_disposition));
     45   MOCK_METHOD1(OnDidNavigate, void(
     46       const NavigationInfo& navigation_info));
     47   MOCK_METHOD2(OnNavigationFailed, void(int error_code, const GURL& gurl));
     48   MOCK_METHOD1(OnLoad, void(const GURL& url));
     49   MOCK_METHOD3(OnMessageFromChromeFrame, void(
     50       const std::string& message,
     51       const std::string& origin,
     52       const std::string& target));
     53   MOCK_METHOD3(OnHandleContextMenu, void(HANDLE menu_handle,
     54       int align_flags, const MiniContextMenuParams& params));
     55   MOCK_METHOD2(OnRequestStart, void(int request_id,
     56       const AutomationURLRequest& request));
     57   MOCK_METHOD2(OnRequestRead, void(int request_id, int bytes_to_read));
     58   MOCK_METHOD2(OnRequestEnd, void(int request_id,
     59       const net::URLRequestStatus& status));
     60   MOCK_METHOD2(OnSetCookieAsync, void(const GURL& url,
     61       const std::string& cookie));
     62 
     63   // Use for sending network responses
     64   void SetRequestDelegate(PluginUrlRequestDelegate* request_delegate) {
     65     request_delegate_ = request_delegate;
     66   }
     67 
     68   void ReplyStarted(int request_id, const char* headers) {
     69     request_delegate_->OnResponseStarted(request_id, "text/html", headers,
     70       0, base::Time::Now(), EmptyString(), 0, net::HostPortPair(), 0);
     71   }
     72 
     73   void ReplyData(int request_id, const std::string* data) {
     74     request_delegate_->OnReadComplete(request_id, *data);
     75   }
     76 
     77   void Reply(const net::URLRequestStatus& status, int request_id) {
     78     request_delegate_->OnResponseEnd(request_id, status);
     79   }
     80 
     81   void Reply404(int request_id) {
     82     ReplyStarted(request_id, "HTTP/1.1 404\r\n\r\n");
     83     Reply(net::URLRequestStatus(), request_id);
     84   }
     85 
     86   PluginUrlRequestDelegate* request_delegate_;
     87 };
     88 
     89 class MockAutomationProxy : public ChromeFrameAutomationProxy {
     90  public:
     91   MOCK_METHOD1(Send, bool(IPC::Message*));
     92   MOCK_METHOD3(SendAsAsync,
     93                void(IPC::SyncMessage* msg,
     94                     SyncMessageReplyDispatcher::SyncMessageCallContext* context,
     95                     void* key));
     96   MOCK_METHOD1(CancelAsync, void(void* key));
     97   MOCK_METHOD1(CreateTabProxy, scoped_refptr<TabProxy>(int handle));
     98   MOCK_METHOD1(ReleaseTabProxy, void(AutomationHandle handle));
     99   MOCK_METHOD0(server_version, std::string(void));
    100   MOCK_METHOD1(SendProxyConfig, void(const std::string&));
    101 
    102   ~MockAutomationProxy() {}
    103 };
    104 
    105 struct MockAutomationMessageSender : public AutomationMessageSender {
    106   virtual bool Send(IPC::Message* msg) {
    107     return proxy_->Send(msg);
    108   }
    109 
    110   virtual bool Send(IPC::Message* msg, int timeout_ms) {
    111     return proxy_->Send(msg);
    112   }
    113 
    114   void ForwardTo(StrictMock<MockAutomationProxy> *p) {
    115     proxy_ = p;
    116   }
    117 
    118   StrictMock<MockAutomationProxy>* proxy_;
    119 };
    120 
    121 // [CFAC] -- uses a ProxyFactory for creation of ChromeFrameAutomationProxy
    122 // -- uses ChromeFrameAutomationProxy
    123 // -- uses TabProxy obtained from ChromeFrameAutomationProxy
    124 // -- uses ChromeFrameDelegate as outgoing interface
    125 //
    126 // We mock ProxyFactory to return mock object (MockAutomationProxy) implementing
    127 // ChromeFrameAutomationProxy interface.
    128 // Since CFAC uses TabProxy for few calls and TabProxy is not easy mockable,
    129 // we create 'real' TabProxy but with fake AutomationSender (the one responsible
    130 // for sending messages over channel).
    131 // Additionally we have mock implementation ChromeFrameDelagate interface -
    132 // MockCFDelegate.
    133 
    134 // Test fixture, saves typing all of it's members.
    135 class CFACMockTest : public testing::Test {
    136  public:
    137   MockProxyFactory factory_;
    138   MockCFDelegate   cfd_;
    139   chrome_frame_test::TimedMsgLoop loop_;
    140   // Most of the test uses the mocked proxy, but some tests need
    141   // to validate the functionality of the real proxy object.
    142   // So we have a mock that is used as the default for the returned_proxy_
    143   // pointer, but tests can set their own pointer in there as needed.
    144   StrictMock<MockAutomationProxy> mock_proxy_;
    145   ChromeFrameAutomationProxy* returned_proxy_;
    146   scoped_ptr<AutomationHandleTracker> tracker_;
    147   MockAutomationMessageSender dummy_sender_;
    148   scoped_refptr<TabProxy> tab_;
    149   // the victim of all tests
    150   scoped_refptr<ChromeFrameAutomationClient> client_;
    151 
    152   base::FilePath profile_path_;
    153   int timeout_;
    154   void* id_;  // Automation server id we are going to return
    155   int tab_handle_;   // Tab handle. Any non-zero value is Ok.
    156 
    157   inline ChromeFrameAutomationProxy* get_proxy() {
    158     return returned_proxy_;
    159   }
    160 
    161   inline void CreateTab() {
    162     ASSERT_EQ(NULL, tab_.get());
    163     tab_ = new TabProxy(&dummy_sender_, tracker_.get(), tab_handle_);
    164   }
    165 
    166   // Easy methods to set expectations.
    167   void SetAutomationServerOk(int times);
    168   void Set_CFD_LaunchFailed(AutomationLaunchResult result);
    169 
    170  protected:
    171   CFACMockTest()
    172     : timeout_(500),
    173       returned_proxy_(static_cast<ChromeFrameAutomationProxy*>(&mock_proxy_)) {
    174     GetChromeFrameProfilePath(L"Adam.N.Epilinter", &profile_path_);
    175     id_ = reinterpret_cast<void*>(5);
    176     tab_handle_ = 3;
    177   }
    178 
    179   virtual void SetUp() {
    180     dummy_sender_.ForwardTo(&mock_proxy_);
    181     tracker_.reset(new AutomationHandleTracker());
    182 
    183     client_ = new ChromeFrameAutomationClient;
    184     client_->set_proxy_factory(&factory_);
    185   }
    186 };
    187 
    188 #endif  // CHROME_FRAME_TEST_AUTOMATION_CLIENT_MOCK_H_
    189