OLD | NEW |
| (Empty) |
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_CHROME_FRAME_AUTOMATION_MOCK_H_ | |
6 #define CHROME_FRAME_TEST_CHROME_FRAME_AUTOMATION_MOCK_H_ | |
7 | |
8 #include <string> | |
9 | |
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" | |
21 | |
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()) { | |
36 | |
37 // Endeavour to only kill off Chrome Frame derived Chrome processes. | |
38 base::KillAllNamedProcessesWithArgument( | |
39 base::UTF8ToWide(chrome_frame_test::kChromeImageName), | |
40 base::UTF8ToWide(switches::kChromeFrame)); | |
41 | |
42 mock_server_.ExpectAndServeAnyRequests(CFInvocation(CFInvocation::NONE)); | |
43 | |
44 base::FilePath profile_path; | |
45 GetChromeFrameProfilePath(profile_name, &profile_path); | |
46 chrome_frame_test::OverrideDataDirectoryForThisTest(profile_path.value()); | |
47 | |
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); | |
56 } | |
57 ~AutomationMockDelegate() { | |
58 if (automation_client_.get()) { | |
59 automation_client_->Uninitialize(); | |
60 automation_client_ = NULL; | |
61 } | |
62 if (IsWindow()) | |
63 DestroyWindow(); | |
64 } | |
65 | |
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; | |
75 } | |
76 | |
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(base::WideToUTF8(file_url)); | |
87 } | |
88 | |
89 bool NavigateRelative(const std::wstring& relative_url) { | |
90 return Navigate(base::WideToUTF8( | |
91 mock_server_.Resolve(relative_url.c_str()))); | |
92 } | |
93 | |
94 virtual void OnAutomationServerReady() { | |
95 if (automation_client_.get()) { | |
96 Create(NULL, 0, NULL, WS_OVERLAPPEDWINDOW); | |
97 DCHECK(IsWindow()); | |
98 is_connected_ = true; | |
99 } else { | |
100 NOTREACHED(); | |
101 } | |
102 } | |
103 | |
104 virtual void OnAutomationServerLaunchFailed() { | |
105 QuitMessageLoop(); | |
106 } | |
107 | |
108 virtual void OnLoadFailed(int error_code, const std::string& url) { | |
109 navigation_result_ = false; | |
110 QuitMessageLoop(); | |
111 } | |
112 | |
113 ChromeFrameAutomationClient* automation() { | |
114 return automation_client_.get(); | |
115 } | |
116 const GURL& url() const { | |
117 return url_; | |
118 } | |
119 bool is_connected() const { | |
120 return is_connected_; | |
121 } | |
122 bool navigation_result() const { | |
123 return navigation_result_; | |
124 } | |
125 | |
126 BEGIN_MSG_MAP(AutomationMockDelegate) | |
127 END_MSG_MAP() | |
128 | |
129 protected: | |
130 void QuitMessageLoop() { | |
131 // Quit on the caller message loop has to be called on the caller | |
132 // thread. | |
133 caller_message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | |
134 } | |
135 | |
136 private: | |
137 testing::StrictMock<MockWebServer> mock_server_; | |
138 base::MessageLoop* caller_message_loop_; | |
139 GURL url_; | |
140 bool is_connected_; | |
141 bool navigation_result_; | |
142 }; | |
143 | |
144 class AutomationMockLaunch | |
145 : public AutomationMockDelegate<AutomationMockLaunch> { | |
146 public: | |
147 typedef AutomationMockDelegate<AutomationMockLaunch> Base; | |
148 AutomationMockLaunch(base::MessageLoop* caller_message_loop, | |
149 int launch_timeout) | |
150 : Base(caller_message_loop, launch_timeout, true, L"", L"", false, | |
151 false) { | |
152 } | |
153 virtual void OnAutomationServerReady() { | |
154 Base::OnAutomationServerReady(); | |
155 QuitMessageLoop(); | |
156 } | |
157 bool launch_result() const { | |
158 return is_connected(); | |
159 } | |
160 }; | |
161 | |
162 #endif // CHROME_FRAME_TEST_CHROME_FRAME_AUTOMATION_MOCK_H_ | |
OLD | NEW |