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#include "chrome/test/base/chrome_test_launcher.h"
6
7#include "base/command_line.h"
8#include "base/debug/leak_annotations.h"
9#include "base/file_util.h"
10#include "base/files/file_path.h"
11#include "base/logging.h"
12#include "base/memory/linked_ptr.h"
13#include "base/process/process_metrics.h"
14#include "base/run_loop.h"
15#include "base/strings/string_util.h"
16#include "base/test/test_file_util.h"
17#include "chrome/app/chrome_main_delegate.h"
18#include "chrome/common/chrome_constants.h"
19#include "chrome/common/chrome_switches.h"
20#include "chrome/test/base/chrome_test_suite.h"
21#include "chrome/test/base/test_switches.h"
22#include "content/public/app/content_main.h"
23#include "content/public/test/test_launcher.h"
24#include "content/public/test/test_utils.h"
25#include "ui/base/test/ui_controls.h"
26
27#if defined(OS_MACOSX)
28#include "chrome/browser/chrome_browser_application_mac.h"
29#endif  // defined(OS_MACOSX)
30
31#if defined(OS_WIN)
32#include "content/public/app/startup_helper_win.h"
33#include "sandbox/win/src/sandbox_types.h"
34#endif  // defined(OS_WIN)
35
36#if defined(USE_AURA)
37#include "ui/aura/test/ui_controls_factory_aura.h"
38#include "ui/base/test/ui_controls_aura.h"
39#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
40#include "ui/views/test/ui_controls_factory_desktop_aurax11.h"
41#endif
42#endif
43
44#if defined(OS_CHROMEOS)
45#include "ash/test/ui_controls_factory_ash.h"
46#endif
47
48#if defined(OS_LINUX) || defined(OS_ANDROID)
49#include "chrome/app/chrome_breakpad_client.h"
50#endif
51
52namespace {
53
54class ChromeTestLauncherDelegate : public content::TestLauncherDelegate {
55 public:
56  explicit ChromeTestLauncherDelegate(ChromeTestSuiteRunner* runner)
57      : runner_(runner) {}
58  virtual ~ChromeTestLauncherDelegate() {}
59
60  virtual int RunTestSuite(int argc, char** argv) OVERRIDE {
61    return runner_->RunTestSuite(argc, argv);
62  }
63
64  virtual bool AdjustChildProcessCommandLine(
65      CommandLine* command_line, const base::FilePath& temp_data_dir) OVERRIDE {
66    CommandLine new_command_line(command_line->GetProgram());
67    CommandLine::SwitchMap switches = command_line->GetSwitches();
68
69    // Strip out user-data-dir if present.  We will add it back in again later.
70    switches.erase(switches::kUserDataDir);
71
72    for (CommandLine::SwitchMap::const_iterator iter = switches.begin();
73         iter != switches.end(); ++iter) {
74      new_command_line.AppendSwitchNative((*iter).first, (*iter).second);
75    }
76
77    new_command_line.AppendSwitchPath(switches::kUserDataDir, temp_data_dir);
78
79    // file:// access for ChromeOS.
80    new_command_line.AppendSwitch(switches::kAllowFileAccess);
81
82    *command_line = new_command_line;
83    return true;
84  }
85
86 protected:
87  virtual content::ContentMainDelegate* CreateContentMainDelegate() OVERRIDE {
88    return new ChromeMainDelegate();
89  }
90
91  virtual void AdjustDefaultParallelJobs(int* default_jobs) OVERRIDE {
92#if defined(OS_WIN)
93    if (CommandLine::ForCurrentProcess()->HasSwitch(
94            switches::kAshBrowserTests)) {
95      *default_jobs = 1;
96      fprintf(stdout,
97              "Disabling test parallelization for --ash-browsertests.\n");
98      fflush(stdout);
99    }
100#endif  // defined(OS_WIN)
101  }
102
103 private:
104  ChromeTestSuiteRunner* runner_;
105
106  DISALLOW_COPY_AND_ASSIGN(ChromeTestLauncherDelegate);
107};
108
109}  // namespace
110
111int LaunchChromeTests(int default_jobs,
112                      ChromeTestSuiteRunner* runner,
113                      int argc,
114                      char** argv) {
115#if defined(OS_MACOSX)
116  chrome_browser_application_mac::RegisterBrowserCrApp();
117#endif
118
119#if defined(OS_LINUX) || defined(OS_ANDROID)
120  // We leak this pointer intentionally. The breakpad client needs to outlive
121  // all other code.
122  chrome::ChromeBreakpadClient* breakpad_client =
123      new chrome::ChromeBreakpadClient();
124  ANNOTATE_LEAKING_OBJECT_PTR(breakpad_client);
125  breakpad::SetBreakpadClient(breakpad_client);
126#endif
127
128  ChromeTestLauncherDelegate launcher_delegate(runner);
129  return content::LaunchTests(&launcher_delegate, default_jobs, argc, argv);
130}
131