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_
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"
23 class AutomationMockDelegate
24 : public CWindowImpl
<T
>,
25 public ChromeFramePlugin
<T
> {
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
;
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
;
66 // Navigate external tab to the specified url through automation
67 bool Navigate(const std::string
& url
) {
68 NavigationConstraintsImpl navigation_constraints
;
70 bool result
= automation_client_
->InitiateNavigation(
71 url
, std::string(), &navigation_constraints
);
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
);
103 virtual void OnAutomationServerLaunchFailed() {
107 virtual void OnLoad(const GURL
& url
) {
109 navigation_result_
= true;
115 virtual void OnLoadFailed(int error_code
, const std::string
& url
) {
116 navigation_result_
= false;
120 ChromeFrameAutomationClient
* automation() {
121 return automation_client_
.get();
123 const GURL
& url() const {
126 bool is_connected() const {
127 return is_connected_
;
129 bool navigation_result() const {
130 return navigation_result_
;
133 BEGIN_MSG_MAP(AutomationMockDelegate
)
137 void QuitMessageLoop() {
138 // Quit on the caller message loop has to be called on the caller
140 caller_message_loop_
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
144 testing::StrictMock
<MockWebServer
> mock_server_
;
145 base::MessageLoop
* caller_message_loop_
;
148 bool navigation_result_
;
151 class AutomationMockLaunch
152 : public AutomationMockDelegate
<AutomationMockLaunch
> {
154 typedef AutomationMockDelegate
<AutomationMockLaunch
> Base
;
155 AutomationMockLaunch(base::MessageLoop
* caller_message_loop
,
157 : Base(caller_message_loop
, launch_timeout
, true, L
"", L
"", false,
160 virtual void OnAutomationServerReady() {
161 Base::OnAutomationServerReady();
164 bool launch_result() const {
165 return is_connected();
169 class AutomationMockNavigate
170 : public AutomationMockDelegate
<AutomationMockNavigate
> {
172 typedef AutomationMockDelegate
<AutomationMockNavigate
> Base
;
173 AutomationMockNavigate(base::MessageLoop
* caller_message_loop
,
175 : Base(caller_message_loop
, launch_timeout
, true, L
"", L
"", false,
178 virtual void OnLoad(const GURL
& url
) {
184 class AutomationMockPostMessage
185 : public AutomationMockDelegate
<AutomationMockPostMessage
> {
187 typedef AutomationMockDelegate
<AutomationMockPostMessage
> Base
;
188 AutomationMockPostMessage(base::MessageLoop
* caller_message_loop
,
190 : Base(caller_message_loop
, launch_timeout
, true, L
"", L
"", false,
192 postmessage_result_(false) {}
193 bool postmessage_result() const {
194 return postmessage_result_
;
196 virtual void OnLoad(const GURL
& 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;
209 bool postmessage_result_
;
212 class AutomationMockHostNetworkRequestStart
213 : public AutomationMockDelegate
<AutomationMockHostNetworkRequestStart
> {
215 typedef AutomationMockDelegate
<AutomationMockHostNetworkRequestStart
> Base
;
216 AutomationMockHostNetworkRequestStart(base::MessageLoop
* caller_message_loop
,
218 : Base(caller_message_loop
, launch_timeout
, true, L
"", L
"", false,
220 request_start_result_(false) {
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;
233 virtual void OnLoad(const GURL
& url
) {
237 bool request_start_result_
;
240 #endif // CHROME_FRAME_TEST_CHROME_FRAME_AUTOMATION_MOCK_H_