content_test_launcher.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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    DCHECK(!GetContentClient());
40    content_client_.reset(new ShellContentClient);
41    browser_content_client_.reset(new ShellContentBrowserClient());
42    content_client_->set_browser_for_testing(browser_content_client_.get());
43    SetContentClient(content_client_.get());
44  }
45
46  virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
47    DCHECK_EQ(content_client_.get(), GetContentClient());
48    browser_content_client_.reset();
49    content_client_.reset();
50    SetContentClient(NULL);
51  }
52
53 private:
54  scoped_ptr<ShellContentClient> content_client_;
55  scoped_ptr<ShellContentBrowserClient> browser_content_client_;
56
57  DISALLOW_COPY_AND_ASSIGN(ContentShellTestSuiteInitializer);
58};
59
60#if defined(OS_ANDROID)
61base::MessagePump* CreateMessagePumpForUI() {
62  return new BrowserTestMessagePumpAndroid();
63};
64#endif
65
66class ContentBrowserTestSuite : public ContentTestSuiteBase {
67 public:
68  ContentBrowserTestSuite(int argc, char** argv)
69      : ContentTestSuiteBase(argc, argv) {
70  }
71  virtual ~ContentBrowserTestSuite() {
72  }
73
74 protected:
75  virtual void Initialize() OVERRIDE {
76
77#if defined(OS_ANDROID)
78    // This needs to be done before base::TestSuite::Initialize() is called,
79    // as it also tries to set MessagePumpForUIFactory.
80    if (!MessageLoop::InitMessagePumpForUIFactory(&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 std::string GetEmptyTestName() OVERRIDE {
107    return std::string();
108  }
109
110  virtual int RunTestSuite(int argc, char** argv) OVERRIDE {
111    return ContentBrowserTestSuite(argc, argv).Run();
112  }
113
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    return true;
120  }
121
122 protected:
123  virtual ContentMainDelegate* CreateContentMainDelegate() OVERRIDE {
124    return new ShellMainDelegate();
125  }
126
127 private:
128  DISALLOW_COPY_AND_ASSIGN(ContentTestLauncherDelegate);
129};
130
131}  // namespace content
132
133int main(int argc, char** argv) {
134  content::ContentTestLauncherDelegate launcher_delegate;
135  return LaunchTests(&launcher_delegate, argc, argv);
136}
137