1 // Copyright (c) 2011 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/content_browser_test.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/common/content_paths.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/common/url_constants.h"
16 #include "content/shell/browser/shell.h"
17 #include "content/shell/browser/shell_browser_context.h"
18 #include "content/shell/browser/shell_content_browser_client.h"
19 #include "content/shell/common/shell_switches.h"
20 #include "content/shell/renderer/layout_test/layout_test_content_renderer_client.h"
21 #include "content/test/test_content_client.h"
22 #include "net/test/embedded_test_server/embedded_test_server.h"
24 #if defined(OS_ANDROID)
25 #include "content/shell/app/shell_main_delegate.h"
28 #if defined(OS_MACOSX)
29 #include "base/mac/scoped_nsautorelease_pool.h"
32 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
33 #include "ui/base/ime/input_method_initializer.h"
38 ContentBrowserTest::ContentBrowserTest() {
39 #if defined(OS_MACOSX)
40 // See comment in InProcessBrowserTest::InProcessBrowserTest().
41 base::FilePath content_shell_path
;
42 CHECK(PathService::Get(base::FILE_EXE
, &content_shell_path
));
43 content_shell_path
= content_shell_path
.DirName();
44 content_shell_path
= content_shell_path
.Append(
45 FILE_PATH_LITERAL("Content Shell.app/Contents/MacOS/Content Shell"));
46 CHECK(PathService::Override(base::FILE_EXE
, content_shell_path
));
48 base::FilePath
content_test_data(FILE_PATH_LITERAL("content/test/data"));
49 CreateTestServer(content_test_data
);
50 base::FilePath content_test_data_absolute
;
51 CHECK(PathService::Get(base::DIR_SOURCE_ROOT
, &content_test_data_absolute
));
52 content_test_data_absolute
=
53 content_test_data_absolute
.Append(content_test_data
);
54 embedded_test_server()->ServeFilesFromDirectory(content_test_data_absolute
);
57 ContentBrowserTest::~ContentBrowserTest() {
60 void ContentBrowserTest::SetUp() {
61 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
62 command_line
->AppendSwitch(switches::kContentBrowserTest
);
64 SetUpCommandLine(command_line
);
66 #if defined(OS_ANDROID)
67 shell_main_delegate_
.reset(new ShellMainDelegate
);
68 shell_main_delegate_
->PreSandboxStartup();
69 if (command_line
->HasSwitch(switches::kSingleProcess
)) {
70 // We explicitly leak the new ContentRendererClient as we're
71 // setting a global that may be used after ContentBrowserTest is
73 ContentRendererClient
* old_client
=
74 command_line
->HasSwitch(switches::kRunLayoutTest
)
75 ? SetRendererClientForTesting(new LayoutTestContentRendererClient
)
76 : SetRendererClientForTesting(new ShellContentRendererClient
);
77 // No-one should have set this value before we did.
80 #elif defined(OS_MACOSX)
81 // See InProcessBrowserTest::PrepareTestCommandLine().
82 base::FilePath subprocess_path
;
83 PathService::Get(base::FILE_EXE
, &subprocess_path
);
84 subprocess_path
= subprocess_path
.DirName().DirName();
85 DCHECK_EQ(subprocess_path
.BaseName().value(), "Contents");
86 subprocess_path
= subprocess_path
.Append(
87 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper");
88 command_line
->AppendSwitchPath(switches::kBrowserSubprocessPath
,
92 // LinuxInputMethodContextFactory has to be initialized.
93 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
94 ui::InitializeInputMethodForTesting();
97 BrowserTestBase::SetUp();
100 void ContentBrowserTest::TearDown() {
101 BrowserTestBase::TearDown();
103 // LinuxInputMethodContextFactory has to be shutdown.
104 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
105 ui::ShutdownInputMethodForTesting();
108 #if defined(OS_ANDROID)
109 shell_main_delegate_
.reset();
113 void ContentBrowserTest::RunTestOnMainThreadLoop() {
114 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
115 switches::kRunLayoutTest
)) {
116 CHECK_EQ(Shell::windows().size(), 1u);
117 shell_
= Shell::windows()[0];
120 #if defined(OS_MACOSX)
121 // On Mac, without the following autorelease pool, code which is directly
122 // executed (as opposed to executed inside a message loop) would autorelease
123 // objects into a higher-level pool. This pool is not recycled in-sync with
124 // the message loops' pools and causes problems with code relying on
125 // deallocation via an autorelease pool (such as browser window closure and
126 // browser shutdown). To avoid this, the following pool is recycled after each
127 // time code is directly executed.
128 base::mac::ScopedNSAutoreleasePool pool
;
131 // Pump startup related events.
132 base::MessageLoopForUI::current()->RunUntilIdle();
134 #if defined(OS_MACOSX)
140 RunTestOnMainThread();
142 TearDownOnMainThread();
143 #if defined(OS_MACOSX)
147 for (RenderProcessHost::iterator
i(RenderProcessHost::AllHostsIterator());
148 !i
.IsAtEnd(); i
.Advance()) {
149 i
.GetCurrentValue()->FastShutdownIfPossible();
152 Shell::CloseAllWindows();
155 Shell
* ContentBrowserTest::CreateBrowser() {
156 return Shell::CreateNewWindow(
157 ShellContentBrowserClient::Get()->browser_context(),
158 GURL(url::kAboutBlankURL
),
163 Shell
* ContentBrowserTest::CreateOffTheRecordBrowser() {
164 return Shell::CreateNewWindow(
165 ShellContentBrowserClient::Get()->off_the_record_browser_context(),
166 GURL(url::kAboutBlankURL
),
171 } // namespace content