blob: d5b9c8f9782081d810012d8e19dc39020ff08afd [file] [log] [blame]
[email protected]f573ed6b2012-02-10 15:58:521// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]bb36d822011-09-30 20:55:092// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7d855a12012-06-04 06:08:095#include "content/public/test/test_launcher.h"
[email protected]bb36d822011-09-30 20:55:096
[email protected]c1f87b22011-10-05 21:59:337#include "base/base_paths.h"
[email protected]bb36d822011-09-30 20:55:098#include "base/command_line.h"
9#include "base/logging.h"
[email protected]c1f87b22011-10-05 21:59:3310#include "base/path_service.h"
[email protected]bb36d822011-09-30 20:55:0911#include "base/scoped_temp_dir.h"
[email protected]c1f87b22011-10-05 21:59:3312#include "base/test/test_suite.h"
[email protected]f573ed6b2012-02-10 15:58:5213#include "content/public/app/content_main.h"
[email protected]c08950d22011-10-13 22:20:2914#include "content/public/common/content_switches.h"
[email protected]78089f02012-07-19 06:11:2815#include "content/public/test/content_test_suite_base.h"
16#include "content/shell/shell_content_browser_client.h"
17#include "content/shell/shell_content_client.h"
[email protected]c1f87b22011-10-05 21:59:3318#include "content/shell/shell_main_delegate.h"
[email protected]78089f02012-07-19 06:11:2819#include "testing/gtest/include/gtest/gtest.h"
[email protected]c1f87b22011-10-05 21:59:3320
21#if defined(OS_WIN)
[email protected]badf5cf2011-10-29 03:44:4422#include "content/public/app/startup_helper_win.h"
[email protected]181491782012-07-18 00:59:1523#include "sandbox/win/src/sandbox_types.h"
[email protected]78089f02012-07-19 06:11:2824#include "ui/base/win/scoped_ole_initializer.h"
[email protected]c1f87b22011-10-05 21:59:3325#endif // defined(OS_WIN)
[email protected]bb36d822011-09-30 20:55:0926
[email protected]78089f02012-07-19 06:11:2827namespace content {
28
29class ContentShellTestSuiteInitializer
30 : public testing::EmptyTestEventListener {
31 public:
32 ContentShellTestSuiteInitializer() {
33 }
34
35 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
36 DCHECK(!GetContentClient());
37 content_client_.reset(new ShellContentClient);
38 browser_content_client_.reset(new ShellContentBrowserClient());
39 content_client_->set_browser_for_testing(browser_content_client_.get());
40 SetContentClient(content_client_.get());
41 }
42
43 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
44 DCHECK_EQ(content_client_.get(), GetContentClient());
45 browser_content_client_.reset();
46 content_client_.reset();
47 SetContentClient(NULL);
48 }
49
50 private:
51 scoped_ptr<ShellContentClient> content_client_;
52 scoped_ptr<ShellContentBrowserClient> browser_content_client_;
53
54 DISALLOW_COPY_AND_ASSIGN(ContentShellTestSuiteInitializer);
55};
56
57class ContentBrowserTestSuite : public ContentTestSuiteBase {
58 public:
59 ContentBrowserTestSuite(int argc, char** argv)
60 : ContentTestSuiteBase(argc, argv) {
61 }
62 virtual ~ContentBrowserTestSuite() {
63 }
64
65 protected:
66 virtual void Initialize() OVERRIDE {
67 ContentTestSuiteBase::Initialize();
68
69 testing::TestEventListeners& listeners =
70 testing::UnitTest::GetInstance()->listeners();
71 listeners.Append(new ContentShellTestSuiteInitializer);
72 }
73 virtual void Shutdown() OVERRIDE {
74 base::TestSuite::Shutdown();
75 }
76
77 virtual ContentClient* CreateClientForInitialization() OVERRIDE {
78 return new ShellContentClient();
79 }
80
81#if defined(OS_WIN)
82 ui::ScopedOleInitializer ole_initializer_;
83#endif
84
85 DISALLOW_COPY_AND_ASSIGN(ContentBrowserTestSuite);
86};
87
[email protected]bb36d822011-09-30 20:55:0988class ContentTestLauncherDelegate : public test_launcher::TestLauncherDelegate {
89 public:
90 ContentTestLauncherDelegate() {
91 }
92
93 virtual ~ContentTestLauncherDelegate() {
94 }
95
96 virtual void EarlyInitialize() OVERRIDE {
97 }
98
99 virtual bool Run(int argc, char** argv, int* return_code) OVERRIDE {
[email protected]78089f02012-07-19 06:11:28100#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]bb36d822011-09-30 20:55:09101 CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]c1f87b22011-10-05 21:59:33102 if (command_line->HasSwitch(switches::kProcessType)) {
[email protected]78089f02012-07-19 06:11:28103 ShellMainDelegate delegate;
104#if defined(OS_WIN)
[email protected]c1f87b22011-10-05 21:59:33105 sandbox::SandboxInterfaceInfo sandbox_info = {0};
[email protected]bdcf9152012-07-19 17:43:21106 InitializeSandboxInfo(&sandbox_info);
[email protected]c1f87b22011-10-05 21:59:33107 *return_code =
[email protected]bdcf9152012-07-19 17:43:21108 ContentMain(GetModuleHandle(NULL), &sandbox_info, &delegate);
[email protected]78089f02012-07-19 06:11:28109#elif defined(OS_LINUX)
[email protected]bdcf9152012-07-19 17:43:21110 *return_code = ContentMain(argc,
[email protected]78089f02012-07-19 06:11:28111 const_cast<const char**>(argv),
112 &delegate);
113#endif // defined(OS_WIN)
[email protected]bb36d822011-09-30 20:55:09114 return true;
115 }
[email protected]78089f02012-07-19 06:11:28116#endif // defined(OS_WIN) || defined(OS_LINUX)
[email protected]bb36d822011-09-30 20:55:09117
118 return false;
119 }
120
[email protected]c1f87b22011-10-05 21:59:33121 virtual int RunTestSuite(int argc, char** argv) OVERRIDE {
[email protected]bdcf9152012-07-19 17:43:21122 return ContentBrowserTestSuite(argc, argv).Run();
[email protected]c1f87b22011-10-05 21:59:33123 }
124
[email protected]bb36d822011-09-30 20:55:09125 virtual bool AdjustChildProcessCommandLine(
126 CommandLine* command_line) OVERRIDE {
127 return true;
128 }
129
130 private:
131 DISALLOW_COPY_AND_ASSIGN(ContentTestLauncherDelegate);
132};
133
[email protected]bdcf9152012-07-19 17:43:21134} // namespace content
135
[email protected]bb36d822011-09-30 20:55:09136int main(int argc, char** argv) {
[email protected]84e0de02012-07-19 07:16:03137#if defined(USE_AURA)
138 LOG(INFO) << "content_browsertests not supported on aura yet.";
139 return 0;
140#endif
[email protected]bdcf9152012-07-19 17:43:21141 content::ContentTestLauncherDelegate launcher_delegate;
[email protected]bb36d822011-09-30 20:55:09142 return test_launcher::LaunchTests(&launcher_delegate, argc, argv);
[email protected]76df8e362011-09-30 21:23:58143}