Roll leveldb to r76.
[chromium-blink-merge.git] / chrome_frame / test / chrome_frame_automation_mock.h
blobe8e2a39286aa94cc8639a5dbc732da25e442b751
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.
5 #ifndef CHROME_FRAME_TEST_CHROME_FRAME_AUTOMATION_MOCK_H_
6 #define CHROME_FRAME_TEST_CHROME_FRAME_AUTOMATION_MOCK_H_
8 #include <string>
10 #include "base/files/file_path.h"
11 #include "base/path_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/test/test_process_killer_win.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome_frame/chrome_frame_automation.h"
16 #include "chrome_frame/chrome_frame_plugin.h"
17 #include "chrome_frame/navigation_constraints.h"
18 #include "chrome_frame/test/test_scrubber.h"
19 #include "chrome_frame/test/test_with_web_server.h"
20 #include "chrome_frame/utils.h"
22 template <typename T>
23 class AutomationMockDelegate
24 : public CWindowImpl<T>,
25 public ChromeFramePlugin<T> {
26 public:
27 AutomationMockDelegate(base::MessageLoop* caller_message_loop,
28 int launch_timeout, bool perform_version_check,
29 const std::wstring& profile_name,
30 const std::wstring& language,
31 bool incognito, bool is_widget_mode)
32 : caller_message_loop_(caller_message_loop), is_connected_(false),
33 navigation_result_(false),
34 mock_server_(1337, L"127.0.0.1",
35 chrome_frame_test::GetTestDataFolder()) {
37 // Endeavour to only kill off Chrome Frame derived Chrome processes.
38 base::KillAllNamedProcessesWithArgument(
39 UTF8ToWide(chrome_frame_test::kChromeImageName),
40 UTF8ToWide(switches::kChromeFrame));
42 mock_server_.ExpectAndServeAnyRequests(CFInvocation(CFInvocation::NONE));
44 base::FilePath profile_path;
45 GetChromeFrameProfilePath(profile_name, &profile_path);
46 chrome_frame_test::OverrideDataDirectoryForThisTest(profile_path.value());
48 automation_client_ = new ChromeFrameAutomationClient;
49 GURL empty;
50 scoped_refptr<ChromeFrameLaunchParams> clp(
51 new ChromeFrameLaunchParams(empty, empty, profile_path, profile_name,
52 language, incognito, is_widget_mode, false));
53 clp->set_launch_timeout(launch_timeout);
54 clp->set_version_check(perform_version_check);
55 automation_client_->Initialize(this, clp);
57 ~AutomationMockDelegate() {
58 if (automation_client_.get()) {
59 automation_client_->Uninitialize();
60 automation_client_ = NULL;
62 if (IsWindow())
63 DestroyWindow();
66 // Navigate external tab to the specified url through automation
67 bool Navigate(const std::string& url) {
68 NavigationConstraintsImpl navigation_constraints;
69 url_ = GURL(url);
70 bool result = automation_client_->InitiateNavigation(
71 url, std::string(), &navigation_constraints);
72 if (!result)
73 OnLoadFailed(0, url);
74 return result;
77 // Navigate the external to a 'file://' url for unit test files
78 bool NavigateRelativeFile(const std::wstring& file) {
79 base::FilePath cf_source_path;
80 PathService::Get(base::DIR_SOURCE_ROOT, &cf_source_path);
81 std::wstring file_url(L"file://");
82 file_url.append(cf_source_path.Append(
83 FILE_PATH_LITERAL("chrome_frame")).Append(
84 FILE_PATH_LITERAL("test")).Append(
85 FILE_PATH_LITERAL("data")).Append(file).value());
86 return Navigate(WideToUTF8(file_url));
89 bool NavigateRelative(const std::wstring& relative_url) {
90 return Navigate(WideToUTF8(mock_server_.Resolve(relative_url.c_str())));
93 virtual void OnAutomationServerReady() {
94 if (automation_client_.get()) {
95 Create(NULL, 0, NULL, WS_OVERLAPPEDWINDOW);
96 DCHECK(IsWindow());
97 is_connected_ = true;
98 } else {
99 NOTREACHED();
103 virtual void OnAutomationServerLaunchFailed() {
104 QuitMessageLoop();
107 virtual void OnLoad(const GURL& url) {
108 if (url_ == url) {
109 navigation_result_ = true;
110 } else {
111 QuitMessageLoop();
115 virtual void OnLoadFailed(int error_code, const std::string& url) {
116 navigation_result_ = false;
117 QuitMessageLoop();
120 ChromeFrameAutomationClient* automation() {
121 return automation_client_.get();
123 const GURL& url() const {
124 return url_;
126 bool is_connected() const {
127 return is_connected_;
129 bool navigation_result() const {
130 return navigation_result_;
133 BEGIN_MSG_MAP(AutomationMockDelegate)
134 END_MSG_MAP()
136 protected:
137 void QuitMessageLoop() {
138 // Quit on the caller message loop has to be called on the caller
139 // thread.
140 caller_message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
143 private:
144 testing::StrictMock<MockWebServer> mock_server_;
145 base::MessageLoop* caller_message_loop_;
146 GURL url_;
147 bool is_connected_;
148 bool navigation_result_;
151 class AutomationMockLaunch
152 : public AutomationMockDelegate<AutomationMockLaunch> {
153 public:
154 typedef AutomationMockDelegate<AutomationMockLaunch> Base;
155 AutomationMockLaunch(base::MessageLoop* caller_message_loop,
156 int launch_timeout)
157 : Base(caller_message_loop, launch_timeout, true, L"", L"", false,
158 false) {
160 virtual void OnAutomationServerReady() {
161 Base::OnAutomationServerReady();
162 QuitMessageLoop();
164 bool launch_result() const {
165 return is_connected();
169 class AutomationMockNavigate
170 : public AutomationMockDelegate<AutomationMockNavigate> {
171 public:
172 typedef AutomationMockDelegate<AutomationMockNavigate> Base;
173 AutomationMockNavigate(base::MessageLoop* caller_message_loop,
174 int launch_timeout)
175 : Base(caller_message_loop, launch_timeout, true, L"", L"", false,
176 false) {
178 virtual void OnLoad(const GURL& url) {
179 Base::OnLoad(url);
180 QuitMessageLoop();
184 class AutomationMockPostMessage
185 : public AutomationMockDelegate<AutomationMockPostMessage> {
186 public:
187 typedef AutomationMockDelegate<AutomationMockPostMessage> Base;
188 AutomationMockPostMessage(base::MessageLoop* caller_message_loop,
189 int launch_timeout)
190 : Base(caller_message_loop, launch_timeout, true, L"", L"", false,
191 false),
192 postmessage_result_(false) {}
193 bool postmessage_result() const {
194 return postmessage_result_;
196 virtual void OnLoad(const GURL& url) {
197 Base::OnLoad(url);
198 if (navigation_result()) {
199 automation()->ForwardMessageFromExternalHost("Test", "null", "*");
202 virtual void OnMessageFromChromeFrame(const std::string& message,
203 const std::string& origin,
204 const std::string& target) {
205 postmessage_result_ = true;
206 QuitMessageLoop();
208 private:
209 bool postmessage_result_;
212 class AutomationMockHostNetworkRequestStart
213 : public AutomationMockDelegate<AutomationMockHostNetworkRequestStart> {
214 public:
215 typedef AutomationMockDelegate<AutomationMockHostNetworkRequestStart> Base;
216 AutomationMockHostNetworkRequestStart(base::MessageLoop* caller_message_loop,
217 int launch_timeout)
218 : Base(caller_message_loop, launch_timeout, true, L"", L"", false,
219 false),
220 request_start_result_(false) {
221 if (automation()) {
222 automation()->set_use_chrome_network(false);
225 bool request_start_result() const {
226 return request_start_result_;
228 virtual void OnRequestStart(int request_id,
229 const AutomationURLRequest& request) {
230 request_start_result_ = true;
231 QuitMessageLoop();
233 virtual void OnLoad(const GURL& url) {
234 Base::OnLoad(url);
236 private:
237 bool request_start_result_;
240 #endif // CHROME_FRAME_TEST_CHROME_FRAME_AUTOMATION_MOCK_H_