content_test_launcher.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/test/test_suite.h"
12#include "content/public/common/content_switches.h"
13#include "content/public/test/content_test_suite_base.h"
14#include "content/shell/shell_content_browser_client.h"
15#include "content/shell/shell_content_client.h"
16#include "content/shell/shell_main_delegate.h"
17#include "content/shell/shell_switches.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20#if defined(OS_ANDROID)
21#include "base/message_loop.h"
22#include "content/test/browser_test_message_pump_android.h"
23#endif
24
25#if defined(OS_WIN)
26#include "content/public/app/startup_helper_win.h"
27#include "sandbox/win/src/sandbox_types.h"
28#endif  // defined(OS_WIN)
29
30namespace content {
31
32class ContentShellTestSuiteInitializer
33    : public testing::EmptyTestEventListener {
34 public:
35  ContentShellTestSuiteInitializer() {
36  }
37
38  virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
39    content_client_.reset(new ShellContentClient);
40    browser_content_client_.reset(new ShellContentBrowserClient());
41    SetContentClient(content_client_.get());
42    SetBrowserClientForTesting(browser_content_client_.get());
43  }
44
45  virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
46    browser_content_client_.reset();
47    content_client_.reset();
48    SetContentClient(NULL);
49  }
50
51 private:
52  scoped_ptr<ShellContentClient> content_client_;
53  scoped_ptr<ShellContentBrowserClient> browser_content_client_;
54
55  DISALLOW_COPY_AND_ASSIGN(ContentShellTestSuiteInitializer);
56};
57
58#if defined(OS_ANDROID)
59base::MessagePump* CreateMessagePumpForUI() {
60  return new BrowserTestMessagePumpAndroid();
61};
62#endif
63
64class ContentBrowserTestSuite : public ContentTestSuiteBase {
65 public:
66  ContentBrowserTestSuite(int argc, char** argv)
67      : ContentTestSuiteBase(argc, argv) {
68  }
69  virtual ~ContentBrowserTestSuite() {
70  }
71
72 protected:
73  virtual void Initialize() OVERRIDE {
74
75#if defined(OS_ANDROID)
76    // This needs to be done before base::TestSuite::Initialize() is called,
77    // as it also tries to set MessagePumpForUIFactory.
78    if (!base::MessageLoop::InitMessagePumpForUIFactory(
79            &CreateMessagePumpForUI))
80      LOG(INFO) << "MessagePumpForUIFactory already set, unable to override.";
81#endif
82
83    ContentTestSuiteBase::Initialize();
84
85    testing::TestEventListeners& listeners =
86      testing::UnitTest::GetInstance()->listeners();
87    listeners.Append(new ContentShellTestSuiteInitializer);
88  }
89  virtual void Shutdown() OVERRIDE {
90    base::TestSuite::Shutdown();
91  }
92
93  virtual ContentClient* CreateClientForInitialization() OVERRIDE {
94    return new ShellContentClient();
95  }
96
97  DISALLOW_COPY_AND_ASSIGN(ContentBrowserTestSuite);
98};
99
100class ContentTestLauncherDelegate : public TestLauncherDelegate {
101 public:
102  ContentTestLauncherDelegate() {}
103  virtual ~ContentTestLauncherDelegate() {}
104
105  virtual std::string GetEmptyTestName() OVERRIDE {
106    return std::string();
107  }
108
109  virtual int RunTestSuite(int argc, char** argv) OVERRIDE {
110    return ContentBrowserTestSuite(argc, argv).Run();
111  }
112
113  virtual bool AdjustChildProcessCommandLine(
114      CommandLine* command_line, const base::FilePath& temp_data_dir) OVERRIDE {
115    command_line->AppendSwitchPath(switches::kContentShellDataPath,
116                                   temp_data_dir);
117    command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
118    return true;
119  }
120
121 protected:
122  virtual ContentMainDelegate* CreateContentMainDelegate() OVERRIDE {
123    return new ShellMainDelegate();
124  }
125
126 private:
127  DISALLOW_COPY_AND_ASSIGN(ContentTestLauncherDelegate);
128};
129
130}  // namespace content
131
132int main(int argc, char** argv) {
133  content::ContentTestLauncherDelegate launcher_delegate;
134  return LaunchTests(&launcher_delegate, argc, argv);
135}
136