Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_TEST_AUTOMATION_PROXY_LAUNCHER_H_ | 5 #ifndef CHROME_TEST_AUTOMATION_PROXY_LAUNCHER_H_ |
| 6 #define CHROME_TEST_AUTOMATION_PROXY_LAUNCHER_H_ | 6 #define CHROME_TEST_AUTOMATION_PROXY_LAUNCHER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/command_line.h" | |
| 12 #include "base/process.h" | |
| 13 #include "base/scoped_ptr.h" | |
| 14 #include "base/scoped_temp_dir.h" | |
| 15 #include "base/time.h" | |
| 11 | 16 |
| 12 class AutomationProxy; | 17 class AutomationProxy; |
| 13 class UITestBase; | 18 |
| 14 | 19 // Base class for all ProxyLauncher implementations. Contains functionality |
| 15 // Subclass from this class to use a different implementation of AutomationProxy | 20 // fo launching, terminating, and connecting tests to browser processes. This |
| 16 // or to use different channel IDs inside a class that derives from UITest. | 21 // class determines which AutomationProxy implementation is used by a test. |
| 22 // Command line arguments passed to the browser are set in this class. | |
| 23 // | |
| 24 // Subclass from this class to use a different AutomationProxy | |
| 25 // implementation or to override browser launching behavior. | |
| 17 class ProxyLauncher { | 26 class ProxyLauncher { |
| 18 public: | 27 public: |
| 19 ProxyLauncher() {} | 28 // Profile theme type choices. |
| 20 virtual ~ProxyLauncher() {} | 29 enum ProfileType { |
| 30 DEFAULT_THEME = 0, | |
| 31 COMPLEX_THEME = 1, | |
| 32 NATIVE_THEME = 2, | |
| 33 CUSTOM_FRAME = 3, | |
| 34 CUSTOM_FRAME_NATIVE_THEME = 4, | |
| 35 }; | |
| 36 | |
| 37 // Different ways to quit the browser. | |
| 38 enum ShutdownType { | |
| 39 WINDOW_CLOSE, | |
| 40 USER_QUIT, | |
| 41 SESSION_ENDING, | |
| 42 }; | |
| 43 | |
| 44 // POD containing state variables that determine how to launch browser. | |
| 45 typedef struct { | |
|
Paweł Hajdan Jr.
2011/01/08 06:45:25
nit: *Again* why is this typedef needed here?
| |
| 46 // If true the profile is cleared before launching. | |
| 47 bool clear_profile; | |
| 48 | |
| 49 // If set, the profiles in this path are copied | |
| 50 // into the user data directory for the test. | |
| 51 FilePath& template_user_data; | |
|
Paweł Hajdan Jr.
2011/01/08 06:45:25
Why is this a reference? It seems quite dangerous.
| |
| 52 | |
| 53 // Profile theme type. | |
| 54 ProfileType profile_type; | |
| 55 | |
| 56 // Path to the browser executable. | |
| 57 FilePath browser_directory; | |
| 58 | |
| 59 // Command line arguments passed to the browser. | |
| 60 CommandLine& arguments; | |
|
Paweł Hajdan Jr.
2011/01/08 06:45:25
Why is this a reference? It seems quite dangerous.
| |
| 61 | |
| 62 // Should we supply the testing channel id on the command line? | |
| 63 bool include_testing_id; | |
| 64 | |
| 65 // If true, the window is shown. Otherwise it is hidden. | |
| 66 bool show_window; | |
| 67 } LaunchState; | |
| 68 | |
| 69 ProxyLauncher(); | |
| 70 | |
| 71 virtual ~ProxyLauncher(); | |
| 21 | 72 |
| 22 // Creates an automation proxy. | 73 // Creates an automation proxy. |
| 23 virtual AutomationProxy* CreateAutomationProxy( | 74 virtual AutomationProxy* CreateAutomationProxy( |
| 24 int execution_timeout) = 0; | 75 int execution_timeout) = 0; |
| 25 | 76 |
| 26 // Launches the browser if needed and establishes a connection | 77 // Launches the browser if needed and establishes a connection with it. |
| 27 // connection with it using the specified UITestBase. | 78 virtual void InitializeConnection(const LaunchState& state, |
| 28 virtual void InitializeConnection(UITestBase* ui_test_base) const = 0; | 79 bool wait_for_initial_loads) = 0; |
| 29 | 80 |
| 30 // Returns the automation proxy's channel with any prefixes prepended, | 81 // Returns the automation proxy's channel with any prefixes prepended, |
| 31 // for passing as a command line parameter over to the browser. | 82 // for passing as a command line parameter over to the browser. |
| 32 virtual std::string PrefixedChannelID() const = 0; | 83 virtual std::string PrefixedChannelID() const = 0; |
| 33 | 84 |
| 85 // Launches the browser and IPC testing connection in server mode. | |
| 86 void LaunchBrowserAndServer(const LaunchState& state, | |
| 87 bool wait_for_initial_loads); | |
| 88 | |
| 89 // Launches the IPC testing connection in client mode, | |
| 90 // which then attempts to connect to a browser. | |
| 91 void ConnectToRunningBrowser(bool wait_for_initial_loads); | |
| 92 | |
| 93 // Only for pyauto. | |
| 94 void set_command_execution_timeout_ms(int timeout); | |
| 95 | |
| 96 // Closes the browser and IPC testing server. | |
| 97 void CloseBrowserAndServer(ShutdownType shutdown_type); | |
| 98 | |
| 99 // Launches the browser with the given command line. | |
| 100 // TODO(phajdan.jr): Make LaunchBrowser private. Tests should use | |
| 101 // LaunchAnotherBrowserBlockUntilClosed. | |
| 102 void LaunchBrowser(const LaunchState& state); | |
| 103 | |
| 104 #if !defined(OS_MACOSX) | |
| 105 // This function is not defined on the Mac because the concept | |
| 106 // doesn't apply to Mac; you can't have N browser processes. | |
| 107 | |
| 108 // Launches another browser process and waits for it to finish. | |
| 109 // Returns true on success. | |
| 110 bool LaunchAnotherBrowserBlockUntilClosed(const LaunchState& state); | |
| 111 #endif | |
| 112 | |
| 113 // Exits out of browser instance. | |
| 114 void QuitBrowser(ShutdownType shutdown_type); | |
| 115 | |
| 116 // Terminates the browser, simulates end of session. | |
| 117 void TerminateBrowser(); | |
| 118 | |
| 119 // Check that no processes related to Chrome exist, displaying | |
| 120 // the given message if any do. | |
| 121 void AssertAppNotRunning(const std::wstring& error_message); | |
| 122 | |
| 123 // Returns true when the browser process is running, independent if any | |
| 124 // renderer process exists or not. It will returns false if an user closed the | |
| 125 // window or if the browser process died by itself. | |
| 126 bool IsBrowserRunning(); | |
| 127 | |
| 128 // Returns true when timeout_ms milliseconds have elapsed. | |
| 129 // Returns false if the browser process died while waiting. | |
| 130 bool CrashAwareSleep(int timeout_ms); | |
| 131 | |
| 132 // Wait for the browser process to shut down on its own (i.e. as a result of | |
| 133 // some action that your test has taken). | |
| 134 bool WaitForBrowserProcessToQuit(int timeout); | |
| 135 | |
| 136 AutomationProxy* automation() const; | |
| 137 | |
| 138 // Return the user data directory being used by the browser instance. | |
| 139 FilePath user_data_dir() const; | |
| 140 | |
| 141 // Get the handle of browser process connected to the automation. This | |
| 142 // function only returns a reference to the handle so the caller does not | |
| 143 // own the handle returned. | |
| 144 base::ProcessHandle process() const; | |
| 145 | |
| 146 // Return the process id of the browser process (-1 on error). | |
| 147 base::ProcessId process_id() const; | |
| 148 | |
| 149 // Return the time when the browser was run. | |
| 150 base::TimeTicks browser_launch_time() const; | |
| 151 | |
| 152 // Return how long the shutdown took. | |
| 153 base::TimeDelta browser_quit_time() const; | |
| 154 | |
| 155 // Get/Set a flag to run the renderer in-process when running the tests. | |
| 156 static bool in_process_renderer() { return in_process_renderer_; } | |
| 157 static void set_in_process_renderer(bool value) { | |
| 158 in_process_renderer_ = value; | |
| 159 } | |
| 160 | |
| 161 // Get/Set a flag to run the renderer outside the sandbox when running tests. | |
| 162 static bool no_sandbox() { return no_sandbox_; } | |
| 163 static void set_no_sandbox(bool value) { | |
| 164 no_sandbox_ = value; | |
| 165 } | |
| 166 | |
| 167 // Get/Set a flag to run with DCHECKs enabled in release. | |
| 168 static bool enable_dcheck() { return enable_dcheck_; } | |
| 169 static void set_enable_dcheck(bool value) { | |
| 170 enable_dcheck_ = value; | |
| 171 } | |
| 172 | |
| 173 // Get/Set a flag to dump the process memory without crashing on DCHECKs. | |
| 174 static bool silent_dump_on_dcheck() { return silent_dump_on_dcheck_; } | |
| 175 static void set_silent_dump_on_dcheck(bool value) { | |
| 176 silent_dump_on_dcheck_ = value; | |
| 177 } | |
| 178 | |
| 179 // Get/Set a flag to disable breakpad handling. | |
| 180 static bool disable_breakpad() { return disable_breakpad_; } | |
| 181 static void set_disable_breakpad(bool value) { | |
| 182 disable_breakpad_ = value; | |
| 183 } | |
| 184 | |
| 185 // Get/Set a flag to run the plugin processes inside the sandbox when running | |
| 186 // the tests | |
| 187 static bool safe_plugins() { return safe_plugins_; } | |
| 188 static void set_safe_plugins(bool value) { | |
| 189 safe_plugins_ = value; | |
| 190 } | |
| 191 | |
| 192 static bool show_error_dialogs() { return show_error_dialogs_; } | |
| 193 static void set_show_error_dialogs(bool value) { | |
| 194 show_error_dialogs_ = value; | |
| 195 } | |
| 196 | |
| 197 static bool full_memory_dump() { return full_memory_dump_; } | |
| 198 static void set_full_memory_dump(bool value) { | |
| 199 full_memory_dump_ = value; | |
| 200 } | |
| 201 | |
| 202 static bool dump_histograms_on_exit() { return dump_histograms_on_exit_; } | |
| 203 static void set_dump_histograms_on_exit(bool value) { | |
| 204 dump_histograms_on_exit_ = value; | |
| 205 } | |
| 206 | |
| 207 static const std::string& js_flags() { return js_flags_; } | |
| 208 static void set_js_flags(const std::string& value) { | |
| 209 js_flags_ = value; | |
| 210 } | |
| 211 | |
| 212 static const std::string& log_level() { return log_level_; } | |
| 213 static void set_log_level(const std::string& value) { | |
| 214 log_level_ = value; | |
| 215 } | |
| 216 | |
| 217 protected: | |
| 218 virtual bool ShouldFilterInet() { | |
| 219 return true; | |
| 220 } | |
| 221 | |
| 34 private: | 222 private: |
| 223 void WaitForBrowserLaunch(bool wait_for_initial_loads); | |
| 224 | |
| 225 // Prepare command line that will be used to launch the child browser process. | |
| 226 void PrepareTestCommandline(CommandLine* command_line, | |
| 227 bool include_testing_id); | |
| 228 | |
| 229 bool LaunchBrowserHelper(const LaunchState& state, bool wait, | |
| 230 base::ProcessHandle* process); | |
| 231 | |
| 232 // Wait a certain amount of time for all the app processes to exit, | |
| 233 // forcibly killing them if they haven't exited by then. | |
| 234 // It has the side-effect of killing every browser window opened in your | |
| 235 // session, even those unrelated in the test. | |
| 236 void CleanupAppProcesses(); | |
| 237 | |
| 238 scoped_ptr<AutomationProxy> automation_proxy_; | |
| 239 | |
| 240 // We use a temporary directory for profile to avoid issues with being | |
| 241 // unable to delete some files because they're in use, etc. | |
| 242 ScopedTempDir temp_profile_dir_; | |
| 243 | |
| 244 // Handle to the first Chrome process. | |
| 245 base::ProcessHandle process_; | |
| 246 | |
| 247 // PID of |process_| (for debugging). | |
| 248 base::ProcessId process_id_; | |
| 249 | |
| 250 // Time when the browser was run. | |
| 251 base::TimeTicks browser_launch_time_; | |
| 252 | |
| 253 // How long the shutdown took. | |
| 254 base::TimeDelta browser_quit_time_; | |
| 255 | |
| 256 // True if we're in single process mode. | |
| 257 static bool in_process_renderer_; | |
| 258 | |
| 259 // If true, runs the renderer outside the sandbox. | |
| 260 static bool no_sandbox_; | |
| 261 | |
| 262 // If true, runs plugin processes inside the sandbox. | |
| 263 static bool safe_plugins_; | |
| 264 | |
| 265 // If true, write full memory dump during crash. | |
| 266 static bool full_memory_dump_; | |
| 267 | |
| 268 // If true, a user is paying attention to the test, so show error dialogs. | |
| 269 static bool show_error_dialogs_; | |
| 270 | |
| 271 // Include histograms in log on exit. | |
| 272 static bool dump_histograms_on_exit_; | |
| 273 | |
| 274 // Enable dchecks in release mode. | |
| 275 static bool enable_dcheck_; | |
| 276 | |
| 277 // Dump process memory on dcheck without crashing. | |
| 278 static bool silent_dump_on_dcheck_; | |
| 279 | |
| 280 // Disable breakpad on the browser. | |
| 281 static bool disable_breakpad_; | |
| 282 | |
| 283 // Flags passed to the JS engine. | |
| 284 static std::string js_flags_; | |
| 285 | |
| 286 // Logging level. | |
| 287 static std::string log_level_; | |
| 288 | |
| 35 DISALLOW_COPY_AND_ASSIGN(ProxyLauncher); | 289 DISALLOW_COPY_AND_ASSIGN(ProxyLauncher); |
| 36 }; | 290 }; |
| 37 | 291 |
| 38 // Uses an automation proxy that communicates over a named socket. | 292 // Uses an automation proxy that communicates over a named socket. |
| 39 // This is used if you want to connect an AutomationProxy | 293 // This is used if you want to connect an AutomationProxy |
| 40 // to a browser process that is already running. | 294 // to a browser process that is already running. |
| 41 // The channel id of the proxy is a constant specified by kInterfacePath. | 295 // The channel id of the proxy is a constant specified by kInterfacePath. |
| 42 class NamedProxyLauncher : public ProxyLauncher { | 296 class NamedProxyLauncher : public ProxyLauncher { |
| 43 public: | 297 public: |
| 44 // If launch_browser is true, launches Chrome with named interface enabled. | 298 // If launch_browser is true, launches Chrome with named interface enabled. |
| 45 // Otherwise, there should be an existing instance the proxy can connect to. | 299 // Otherwise, there should be an existing instance the proxy can connect to. |
| 46 NamedProxyLauncher(bool launch_browser, bool disconnect_on_failure); | 300 NamedProxyLauncher(bool launch_browser, bool disconnect_on_failure); |
| 47 | 301 |
| 48 virtual AutomationProxy* CreateAutomationProxy(int execution_timeout); | 302 virtual AutomationProxy* CreateAutomationProxy(int execution_timeout); |
| 49 virtual void InitializeConnection(UITestBase* ui_test_base) const; | 303 virtual void InitializeConnection(const LaunchState& state, |
| 304 bool wait_for_initial_loads); | |
| 50 virtual std::string PrefixedChannelID() const; | 305 virtual std::string PrefixedChannelID() const; |
| 51 | 306 |
| 52 protected: | 307 protected: |
| 53 std::string channel_id_; // Channel id of automation proxy. | 308 std::string channel_id_; // Channel id of automation proxy. |
| 54 bool launch_browser_; // True if we should launch the browser too. | 309 bool launch_browser_; // True if we should launch the browser too. |
| 55 bool disconnect_on_failure_; // True if we disconnect on IPC channel failure. | 310 bool disconnect_on_failure_; // True if we disconnect on IPC channel failure. |
| 56 | 311 |
| 57 private: | 312 private: |
| 58 DISALLOW_COPY_AND_ASSIGN(NamedProxyLauncher); | 313 DISALLOW_COPY_AND_ASSIGN(NamedProxyLauncher); |
| 59 }; | 314 }; |
| 60 | 315 |
| 61 // Uses an automation proxy that communicates over an anonymous socket. | 316 // Uses an automation proxy that communicates over an anonymous socket. |
| 62 class AnonymousProxyLauncher : public ProxyLauncher { | 317 class AnonymousProxyLauncher : public ProxyLauncher { |
| 63 public: | 318 public: |
| 64 explicit AnonymousProxyLauncher(bool disconnect_on_failure); | 319 explicit AnonymousProxyLauncher(bool disconnect_on_failure); |
| 65 virtual AutomationProxy* CreateAutomationProxy(int execution_timeout); | 320 virtual AutomationProxy* CreateAutomationProxy(int execution_timeout); |
| 66 virtual void InitializeConnection(UITestBase* ui_test_base) const; | 321 virtual void InitializeConnection(const LaunchState& state, |
| 322 bool wait_for_initial_loads); | |
| 67 virtual std::string PrefixedChannelID() const; | 323 virtual std::string PrefixedChannelID() const; |
| 68 | 324 |
| 69 protected: | 325 protected: |
| 70 std::string channel_id_; // Channel id of automation proxy. | 326 std::string channel_id_; // Channel id of automation proxy. |
| 71 bool disconnect_on_failure_; // True if we disconnect on IPC channel failure. | 327 bool disconnect_on_failure_; // True if we disconnect on IPC channel failure. |
| 72 | 328 |
| 73 private: | 329 private: |
| 74 DISALLOW_COPY_AND_ASSIGN(AnonymousProxyLauncher); | 330 DISALLOW_COPY_AND_ASSIGN(AnonymousProxyLauncher); |
| 75 }; | 331 }; |
| 76 | 332 |
| 77 #endif // CHROME_TEST_AUTOMATION_PROXY_LAUNCHER_H_ | 333 #endif // CHROME_TEST_AUTOMATION_PROXY_LAUNCHER_H_ |
| 78 | |
| OLD | NEW |