- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / base / chrome_test_launcher.cc
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 "content/public/test/test_launcher.h"
6
7 #include <stack>
8
9 #include "base/command_line.h"
10 #include "base/debug/leak_annotations.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/process/process_metrics.h"
16 #include "base/run_loop.h"
17 #include "base/strings/string_util.h"
18 #include "base/test/test_file_util.h"
19 #include "chrome/app/chrome_main_delegate.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/test/base/chrome_test_suite.h"
24 #include "content/public/app/content_main.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/test/test_utils.h"
27 #include "ui/base/test/ui_controls.h"
28
29 #if defined(OS_MACOSX)
30 #include "chrome/browser/chrome_browser_application_mac.h"
31 #endif  // defined(OS_MACOSX)
32
33 #if defined(OS_WIN)
34 #include "content/public/app/startup_helper_win.h"
35 #include "sandbox/win/src/sandbox_types.h"
36 #endif  // defined(OS_WIN)
37
38 #if defined(TOOLKIT_VIEWS)
39 #include "ui/views/focus/accelerator_handler.h"
40 #endif
41
42 #if defined(USE_AURA)
43 #include "ui/aura/test/ui_controls_factory_aura.h"
44 #include "ui/base/test/ui_controls_aura.h"
45 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
46 #include "ui/views/test/ui_controls_factory_desktop_aurax11.h"
47 #endif
48 #endif
49
50 #if defined(OS_CHROMEOS)
51 #include "ash/test/ui_controls_factory_ash.h"
52 #endif
53
54 #if defined(OS_LINUX) || defined(OS_ANDROID)
55 #include "chrome/app/chrome_breakpad_client.h"
56 #endif
57
58 namespace {
59
60 class ChromeTestLauncherDelegate : public content::TestLauncherDelegate {
61  public:
62   ChromeTestLauncherDelegate() {}
63   virtual ~ChromeTestLauncherDelegate() {}
64
65   virtual int RunTestSuite(int argc, char** argv) OVERRIDE {
66     return ChromeTestSuite(argc, argv).Run();
67   }
68
69   virtual bool AdjustChildProcessCommandLine(
70       CommandLine* command_line, const base::FilePath& temp_data_dir) OVERRIDE {
71     CommandLine new_command_line(command_line->GetProgram());
72     CommandLine::SwitchMap switches = command_line->GetSwitches();
73
74     // Strip out user-data-dir if present.  We will add it back in again later.
75     switches.erase(switches::kUserDataDir);
76
77     for (CommandLine::SwitchMap::const_iterator iter = switches.begin();
78          iter != switches.end(); ++iter) {
79       new_command_line.AppendSwitchNative((*iter).first, (*iter).second);
80     }
81
82     new_command_line.AppendSwitchPath(switches::kUserDataDir, temp_data_dir);
83
84     // file:// access for ChromeOS.
85     new_command_line.AppendSwitch(switches::kAllowFileAccess);
86
87     *command_line = new_command_line;
88     return true;
89   }
90
91   virtual void PreRunMessageLoop(base::RunLoop* run_loop) OVERRIDE {
92 #if !defined(USE_AURA) && defined(TOOLKIT_VIEWS)
93     if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
94       linked_ptr<views::AcceleratorHandler> handler(
95           new views::AcceleratorHandler);
96       handlers_.push(handler);
97       run_loop->set_dispatcher(handler.get());
98     }
99 #endif
100   }
101
102   virtual void PostRunMessageLoop() OVERRIDE {
103 #if !defined(USE_AURA) && defined(TOOLKIT_VIEWS)
104     if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
105       DCHECK_EQ(handlers_.empty(), false);
106       handlers_.pop();
107     }
108 #endif
109   }
110
111  protected:
112   virtual content::ContentMainDelegate* CreateContentMainDelegate() OVERRIDE {
113 #if defined(OS_WIN) || defined (OS_LINUX)
114     return new ChromeMainDelegate();
115 #else
116     // This delegate is only guaranteed to link on linux and windows, so just
117     // bail out if we are on any other platform.
118     NOTREACHED();
119     return NULL;
120 #endif
121   }
122
123  private:
124 #if !defined(USE_AURA) && defined(TOOLKIT_VIEWS)
125   std::stack<linked_ptr<views::AcceleratorHandler> > handlers_;
126 #endif
127
128   DISALLOW_COPY_AND_ASSIGN(ChromeTestLauncherDelegate);
129 };
130
131 }  // namespace
132
133 int LaunchChromeTests(int default_jobs, int argc, char** argv) {
134 #if defined(OS_MACOSX)
135   chrome_browser_application_mac::RegisterBrowserCrApp();
136 #endif
137
138 #if defined(OS_LINUX) || defined(OS_ANDROID)
139   // We leak this pointer intentionally. The breakpad client needs to outlive
140   // all other code.
141   chrome::ChromeBreakpadClient* breakpad_client =
142       new chrome::ChromeBreakpadClient();
143   ANNOTATE_LEAKING_OBJECT_PTR(breakpad_client);
144   breakpad::SetBreakpadClient(breakpad_client);
145 #endif
146
147   ChromeTestLauncherDelegate launcher_delegate;
148   return content::LaunchTests(&launcher_delegate, default_jobs, argc, argv);
149 }