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/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.h"
11 #include "base/path_service.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/common/url_constants.h"
15 #include "content/shell/shell.h"
16 #include "content/shell/shell_browser_context.h"
17 #include "content/shell/shell_content_browser_client.h"
18 #include "content/shell/shell_content_renderer_client.h"
19 #include "content/shell/shell_main_delegate.h"
20 #include "content/shell/shell_switches.h"
21 #include "content/test/test_content_client.h"
23 #if defined(OS_MACOSX)
24 #include "base/mac/scoped_nsautorelease_pool.h"
29 IN_PROC_BROWSER_TEST_F(ContentBrowserTest
, MANUAL_ShouldntRun
) {
30 // Ensures that tests with MANUAL_ prefix don't run automatically.
34 ContentBrowserTest::ContentBrowserTest() {
35 #if defined(OS_MACOSX)
36 // See comment in InProcessBrowserTest::InProcessBrowserTest().
37 base::FilePath content_shell_path
;
38 CHECK(PathService::Get(base::FILE_EXE
, &content_shell_path
));
39 content_shell_path
= content_shell_path
.DirName();
40 content_shell_path
= content_shell_path
.Append(
41 FILE_PATH_LITERAL("Content Shell.app/Contents/MacOS/Content Shell"));
42 CHECK(PathService::Override(base::FILE_EXE
, content_shell_path
));
44 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("content/test/data")));
47 ContentBrowserTest::~ContentBrowserTest() {
50 void ContentBrowserTest::SetUp() {
51 shell_main_delegate_
.reset(new ShellMainDelegate
);
52 shell_main_delegate_
->PreSandboxStartup();
54 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
55 command_line
->AppendSwitch(switches::kContentBrowserTest
);
57 SetUpCommandLine(command_line
);
59 // Single-process mode is not set in BrowserMain, so process it explicitly,
60 // and set up renderer.
61 if (command_line
->HasSwitch(switches::kSingleProcess
)) {
62 single_process_renderer_client_
.reset(new ShellContentRendererClient
);
63 GetContentClient()->set_renderer_for_testing(
64 single_process_renderer_client_
.get());
67 #if defined(OS_MACOSX)
68 // See InProcessBrowserTest::PrepareTestCommandLine().
69 base::FilePath subprocess_path
;
70 PathService::Get(base::FILE_EXE
, &subprocess_path
);
71 subprocess_path
= subprocess_path
.DirName().DirName();
72 DCHECK_EQ(subprocess_path
.BaseName().value(), "Contents");
73 subprocess_path
= subprocess_path
.Append(
74 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper");
75 command_line
->AppendSwitchPath(switches::kBrowserSubprocessPath
,
79 // NOTE: should be kept in sync with
80 // chrome/browser/resources/software_rendering_list.json
81 #if !defined(OS_WIN) && !defined(OS_CHROMEOS)
82 command_line
->AppendSwitch(switches::kDisableAcceleratedVideoDecode
);
85 BrowserTestBase::SetUp();
88 void ContentBrowserTest::TearDown() {
89 BrowserTestBase::TearDown();
91 shell_main_delegate_
.reset();
94 void ContentBrowserTest::RunTestOnMainThreadLoop() {
95 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
96 CHECK_EQ(Shell::windows().size(), 1u);
97 shell_
= Shell::windows()[0];
100 #if defined(OS_MACOSX)
101 // On Mac, without the following autorelease pool, code which is directly
102 // executed (as opposed to executed inside a message loop) would autorelease
103 // objects into a higher-level pool. This pool is not recycled in-sync with
104 // the message loops' pools and causes problems with code relying on
105 // deallocation via an autorelease pool (such as browser window closure and
106 // browser shutdown). To avoid this, the following pool is recycled after each
107 // time code is directly executed.
108 base::mac::ScopedNSAutoreleasePool pool
;
111 // Pump startup related events.
112 MessageLoopForUI::current()->RunUntilIdle();
114 #if defined(OS_MACOSX)
120 RunTestOnMainThread();
121 #if defined(OS_MACOSX)
125 for (RenderProcessHost::iterator
i(RenderProcessHost::AllHostsIterator());
126 !i
.IsAtEnd(); i
.Advance()) {
127 i
.GetCurrentValue()->FastShutdownIfPossible();
130 Shell::CloseAllWindows();
133 Shell
* ContentBrowserTest::CreateBrowser() {
134 ShellContentBrowserClient
* browser_client
=
135 static_cast<ShellContentBrowserClient
*>(GetContentClient()->browser());
136 return Shell::CreateNewWindow(
137 browser_client
->browser_context(),
138 GURL(chrome::kAboutBlankURL
),
144 Shell
* ContentBrowserTest::CreateOffTheRecordBrowser() {
145 ShellContentBrowserClient
* browser_client
=
146 static_cast<ShellContentBrowserClient
*>(GetContentClient()->browser());
147 return Shell::CreateNewWindow(
148 browser_client
->off_the_record_browser_context(),
149 GURL(chrome::kAboutBlankURL
),
155 } // namespace content