Get foreground tab on Android
[chromium-blink-merge.git] / content / test / content_test_launcher.cc
blob09078b23dd905dc86b8a201f1d8c7f3aedde491c
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 #include "content/public/test/test_launcher.h"
7 #include "base/base_paths.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/path_service.h"
11 #include "base/sys_info.h"
12 #include "base/test/test_suite.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/test/content_test_suite_base.h"
15 #include "content/shell/app/shell_main_delegate.h"
16 #include "content/shell/browser/shell_content_browser_client.h"
17 #include "content/shell/common/shell_content_client.h"
18 #include "content/shell/common/shell_switches.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 #if defined(OS_ANDROID)
22 #include "base/message_loop/message_loop.h"
23 #include "content/public/test/nested_message_pump_android.h"
24 #endif
26 #if defined(OS_WIN)
27 #include "content/public/app/startup_helper_win.h"
28 #include "sandbox/win/src/sandbox_types.h"
29 #endif // defined(OS_WIN)
31 namespace content {
33 class ContentShellTestSuiteInitializer
34 : public testing::EmptyTestEventListener {
35 public:
36 ContentShellTestSuiteInitializer() {
39 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
40 content_client_.reset(new ShellContentClient);
41 browser_content_client_.reset(new ShellContentBrowserClient());
42 SetContentClient(content_client_.get());
43 SetBrowserClientForTesting(browser_content_client_.get());
46 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
47 #if !defined(OS_ANDROID)
48 // On Android, production code doesn't reset ContentClient during shutdown.
49 // We try to do the same thing as production. Refer to crbug.com/181069.
50 browser_content_client_.reset();
51 content_client_.reset();
52 SetContentClient(NULL);
53 #endif
56 private:
57 scoped_ptr<ShellContentClient> content_client_;
58 scoped_ptr<ShellContentBrowserClient> browser_content_client_;
60 DISALLOW_COPY_AND_ASSIGN(ContentShellTestSuiteInitializer);
63 #if defined(OS_ANDROID)
64 base::MessagePump* CreateMessagePumpForUI() {
65 return new NestedMessagePumpAndroid();
67 #endif
69 class ContentBrowserTestSuite : public ContentTestSuiteBase {
70 public:
71 ContentBrowserTestSuite(int argc, char** argv)
72 : ContentTestSuiteBase(argc, argv) {
74 virtual ~ContentBrowserTestSuite() {
77 protected:
78 virtual void Initialize() OVERRIDE {
80 #if defined(OS_ANDROID)
81 // This needs to be done before base::TestSuite::Initialize() is called,
82 // as it also tries to set MessagePumpForUIFactory.
83 if (!base::MessageLoop::InitMessagePumpForUIFactory(
84 &CreateMessagePumpForUI))
85 VLOG(0) << "MessagePumpForUIFactory already set, unable to override.";
86 #endif
88 ContentTestSuiteBase::Initialize();
90 testing::TestEventListeners& listeners =
91 testing::UnitTest::GetInstance()->listeners();
92 listeners.Append(new ContentShellTestSuiteInitializer);
94 virtual void Shutdown() OVERRIDE {
95 base::TestSuite::Shutdown();
98 virtual ContentClient* CreateClientForInitialization() OVERRIDE {
99 return new ShellContentClient();
102 DISALLOW_COPY_AND_ASSIGN(ContentBrowserTestSuite);
105 class ContentTestLauncherDelegate : public TestLauncherDelegate {
106 public:
107 ContentTestLauncherDelegate() {}
108 virtual ~ContentTestLauncherDelegate() {}
110 virtual int RunTestSuite(int argc, char** argv) OVERRIDE {
111 return ContentBrowserTestSuite(argc, argv).Run();
114 virtual bool AdjustChildProcessCommandLine(
115 CommandLine* command_line, const base::FilePath& temp_data_dir) OVERRIDE {
116 command_line->AppendSwitchPath(switches::kContentShellDataPath,
117 temp_data_dir);
118 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
119 command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
120 return true;
123 protected:
124 virtual ContentMainDelegate* CreateContentMainDelegate() OVERRIDE {
125 return new ShellMainDelegate();
128 private:
129 DISALLOW_COPY_AND_ASSIGN(ContentTestLauncherDelegate);
132 } // namespace content
134 int main(int argc, char** argv) {
135 int default_jobs = std::max(1, base::SysInfo::NumberOfProcessors() / 2);
136 content::ContentTestLauncherDelegate launcher_delegate;
137 return LaunchTests(&launcher_delegate, default_jobs, argc, argv);