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 61 // Use for sending network responses 62 void SetRequestDelegate(PluginUrlRequestDelegate* request_delegate) { 63 request_delegate_ = request_delegate; 64 } 65 66 void ReplyStarted(int request_id, const char* headers) { 67 request_delegate_->OnResponseStarted(request_id, "text/html", headers, 68 0, base::Time::Now(), std::string(), 0, net::HostPortPair(), 0); 69 } 70 71 void ReplyData(int request_id, const std::string* data) { 72 request_delegate_->OnReadComplete(request_id, *data); 73 } 74 75 void Reply(const net::URLRequestStatus& status, int request_id) { 76 request_delegate_->OnResponseEnd(request_id, status); 77 } 78 79 void Reply404(int request_id) { 80 ReplyStarted(request_id, "HTTP/1.1 404\r\n\r\n"); 81 Reply(net::URLRequestStatus(), request_id); 82 } 83 84 PluginUrlRequestDelegate* request_delegate_; 85 }; 86 87 class MockAutomationProxy : public ChromeFrameAutomationProxy { 88 public: 89 MOCK_METHOD1(Send, bool(IPC::Message*)); 90 MOCK_METHOD3(SendAsAsync, 91 void(IPC::SyncMessage* msg, 92 SyncMessageReplyDispatcher::SyncMessageCallContext* context, 93 void* key)); 94 MOCK_METHOD1(CancelAsync, void(void* key)); 95 MOCK_METHOD1(CreateTabProxy, scoped_refptr<TabProxy>(int handle)); 96 MOCK_METHOD1(ReleaseTabProxy, void(AutomationHandle handle)); 97 MOCK_METHOD0(server_version, std::string(void)); 98 MOCK_METHOD1(SendProxyConfig, void(const std::string&)); 99 100 ~MockAutomationProxy() {} 101 }; 102 103 struct MockAutomationMessageSender : public AutomationMessageSender { 104 virtual bool Send(IPC::Message* msg) { 105 return proxy_->Send(msg); 106 } 107 108 virtual bool Send(IPC::Message* msg, int timeout_ms) { 109 return proxy_->Send(msg); 110 } 111 112 void ForwardTo(StrictMock<MockAutomationProxy> *p) { 113 proxy_ = p; 114 } 115 116 StrictMock<MockAutomationProxy>* proxy_; 117 }; 118 119 // [CFAC] -- uses a ProxyFactory for creation of ChromeFrameAutomationProxy 120 // -- uses ChromeFrameAutomationProxy 121 // -- uses TabProxy obtained from ChromeFrameAutomationProxy 122 // -- uses ChromeFrameDelegate as outgoing interface 123 // 124 // We mock ProxyFactory to return mock object (MockAutomationProxy) implementing 125 // ChromeFrameAutomationProxy interface. 126 // Since CFAC uses TabProxy for few calls and TabProxy is not easy mockable, 127 // we create 'real' TabProxy but with fake AutomationSender (the one responsible 128 // for sending messages over channel). 129 // Additionally we have mock implementation ChromeFrameDelagate interface - 130 // MockCFDelegate. 131 132 // Test fixture, saves typing all of it's members. 133 class CFACMockTest : public testing::Test { 134 public: 135 MockProxyFactory factory_; 136 MockCFDelegate cfd_; 137 chrome_frame_test::TimedMsgLoop loop_; 138 // Most of the test uses the mocked proxy, but some tests need 139 // to validate the functionality of the real proxy object. 140 // So we have a mock that is used as the default for the returned_proxy_ 141 // pointer, but tests can set their own pointer in there as needed. 142 StrictMock<MockAutomationProxy> mock_proxy_; 143 ChromeFrameAutomationProxy* returned_proxy_; 144 scoped_ptr<AutomationHandleTracker> tracker_; 145 MockAutomationMessageSender dummy_sender_; 146 scoped_refptr<TabProxy> tab_; 147 // the victim of all tests 148 scoped_refptr<ChromeFrameAutomationClient> client_; 149 150 base::FilePath profile_path_; 151 int timeout_; 152 void* id_; // Automation server id we are going to return 153 int tab_handle_; // Tab handle. Any non-zero value is Ok. 154 155 inline ChromeFrameAutomationProxy* get_proxy() { 156 return returned_proxy_; 157 } 158 159 inline void CreateTab() { 160 ASSERT_EQ(NULL, tab_.get()); 161 tab_ = new TabProxy(&dummy_sender_, tracker_.get(), tab_handle_); 162 } 163 164 // Easy methods to set expectations. 165 void SetAutomationServerOk(int times); 166 void Set_CFD_LaunchFailed(AutomationLaunchResult result); 167 168 protected: 169 CFACMockTest() 170 : timeout_(500), 171 returned_proxy_(static_cast<ChromeFrameAutomationProxy*>(&mock_proxy_)) { 172 GetChromeFrameProfilePath(L"Adam.N.Epilinter", &profile_path_); 173 id_ = reinterpret_cast<void*>(5); 174 tab_handle_ = 3; 175 } 176 177 virtual void SetUp() { 178 dummy_sender_.ForwardTo(&mock_proxy_); 179 tracker_.reset(new AutomationHandleTracker()); 180 181 client_ = new ChromeFrameAutomationClient; 182 client_->set_proxy_factory(&factory_); 183 } 184 }; 185 186 #endif // CHROME_FRAME_TEST_AUTOMATION_CLIENT_MOCK_H_ 187