content_test_launcher.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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 "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"
20
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
25
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)
30
31namespace content {
32
33class ContentShellTestSuiteInitializer
34    : public testing::EmptyTestEventListener {
35 public:
36  ContentShellTestSuiteInitializer() {
37  }
38
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());
44  }
45
46  virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
47    browser_content_client_.reset();
48    content_client_.reset();
49    SetContentClient(NULL);
50  }
51
52 private:
53  scoped_ptr<ShellContentClient> content_client_;
54  scoped_ptr<ShellContentBrowserClient> browser_content_client_;
55
56  DISALLOW_COPY_AND_ASSIGN(ContentShellTestSuiteInitializer);
57};
58
59#if defined(OS_ANDROID)
60base::MessagePump* CreateMessagePumpForUI() {
61  return new NestedMessagePumpAndroid();
62};
63#endif
64
65class ContentBrowserTestSuite : public ContentTestSuiteBase {
66 public:
67  ContentBrowserTestSuite(int argc, char** argv)
68      : ContentTestSuiteBase(argc, argv) {
69  }
70  virtual ~ContentBrowserTestSuite() {
71  }
72
73 protected:
74  virtual void Initialize() OVERRIDE {
75
76#if defined(OS_ANDROID)
77    // This needs to be done before base::TestSuite::Initialize() is called,
78    // as it also tries to set MessagePumpForUIFactory.
79    if (!base::MessageLoop::InitMessagePumpForUIFactory(
80            &CreateMessagePumpForUI))
81      LOG(INFO) << "MessagePumpForUIFactory already set, unable to override.";
82#endif
83
84    ContentTestSuiteBase::Initialize();
85
86    testing::TestEventListeners& listeners =
87      testing::UnitTest::GetInstance()->listeners();
88    listeners.Append(new ContentShellTestSuiteInitializer);
89  }
90  virtual void Shutdown() OVERRIDE {
91    base::TestSuite::Shutdown();
92  }
93
94  virtual ContentClient* CreateClientForInitialization() OVERRIDE {
95    return new ShellContentClient();
96  }
97
98  DISALLOW_COPY_AND_ASSIGN(ContentBrowserTestSuite);
99};
100
101class ContentTestLauncherDelegate : public TestLauncherDelegate {
102 public:
103  ContentTestLauncherDelegate() {}
104  virtual ~ContentTestLauncherDelegate() {}
105
106  virtual int RunTestSuite(int argc, char** argv) OVERRIDE {
107    return ContentBrowserTestSuite(argc, argv).Run();
108  }
109
110  virtual bool AdjustChildProcessCommandLine(
111      CommandLine* command_line, const base::FilePath& temp_data_dir) OVERRIDE {
112    command_line->AppendSwitchPath(switches::kContentShellDataPath,
113                                   temp_data_dir);
114    command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
115    command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
116    return true;
117  }
118
119 protected:
120  virtual ContentMainDelegate* CreateContentMainDelegate() OVERRIDE {
121    return new ShellMainDelegate();
122  }
123
124 private:
125  DISALLOW_COPY_AND_ASSIGN(ContentTestLauncherDelegate);
126};
127
128}  // namespace content
129
130int main(int argc, char** argv) {
131  int default_jobs = std::max(1, base::SysInfo::NumberOfProcessors() / 2);
132  content::ContentTestLauncherDelegate launcher_delegate;
133  return LaunchTests(&launcher_delegate, default_jobs, argc, argv);
134}
135