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/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/app/shell_main_delegate.h"
17 #include "content/shell/browser/shell.h"
18 #include "content/shell/browser/shell_browser_context.h"
19 #include "content/shell/browser/shell_content_browser_client.h"
20 #include "content/shell/common/shell_switches.h"
21 #include "content/shell/renderer/shell_content_renderer_client.h"
22 #include "content/test/test_content_client.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h"
25 #if defined(OS_MACOSX)
26 #include "base/mac/scoped_nsautorelease_pool.h"
29 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11)
30 #include "ui/base/ime/input_method_initializer.h"
35 IN_PROC_BROWSER_TEST_F(ContentBrowserTest
, MANUAL_ShouldntRun
) {
36 // Ensures that tests with MANUAL_ prefix don't run automatically.
40 ContentBrowserTest::ContentBrowserTest()
41 : setup_called_(false) {
42 #if defined(OS_MACOSX)
43 // See comment in InProcessBrowserTest::InProcessBrowserTest().
44 base::FilePath content_shell_path
;
45 CHECK(PathService::Get(base::FILE_EXE
, &content_shell_path
));
46 content_shell_path
= content_shell_path
.DirName();
47 content_shell_path
= content_shell_path
.Append(
48 FILE_PATH_LITERAL("Content Shell.app/Contents/MacOS/Content Shell"));
49 CHECK(PathService::Override(base::FILE_EXE
, content_shell_path
));
51 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("content/test/data")));
52 base::FilePath content_test_data_dir
;
53 CHECK(PathService::Get(DIR_TEST_DATA
, &content_test_data_dir
));
54 embedded_test_server()->ServeFilesFromDirectory(content_test_data_dir
);
57 ContentBrowserTest::~ContentBrowserTest() {
58 CHECK(setup_called_
) << "Overridden SetUp() did not call parent "
59 "implementation, so test not run.";
62 void ContentBrowserTest::SetUp() {
65 shell_main_delegate_
.reset(new ShellMainDelegate
);
66 shell_main_delegate_
->PreSandboxStartup();
68 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
69 command_line
->AppendSwitch(switches::kContentBrowserTest
);
71 SetUpCommandLine(command_line
);
73 // Single-process mode is not set in BrowserMain, so process it explicitly,
74 // and set up renderer.
75 if (command_line
->HasSwitch(switches::kSingleProcess
)) {
76 single_process_renderer_client_
.reset(new ShellContentRendererClient
);
77 SetRendererClientForTesting(single_process_renderer_client_
.get());
80 #if 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 // NOTE: should be kept in sync with
93 // chrome/browser/resources/software_rendering_list.json
94 #if !defined(OS_WIN) && !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
95 command_line
->AppendSwitch(switches::kDisableAcceleratedVideoDecode
);
98 // LinuxInputMethodContextFactory has to be initialized.
99 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11)
100 ui::InitializeInputMethodForTesting();
103 BrowserTestBase::SetUp();
106 void ContentBrowserTest::TearDown() {
107 BrowserTestBase::TearDown();
109 // LinuxInputMethodContextFactory has to be shutdown.
110 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11)
111 ui::ShutdownInputMethodForTesting();
114 shell_main_delegate_
.reset();
117 void ContentBrowserTest::RunTestOnMainThreadLoop() {
118 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
119 CHECK_EQ(Shell::windows().size(), 1u);
120 shell_
= Shell::windows()[0];
123 #if defined(OS_MACOSX)
124 // On Mac, without the following autorelease pool, code which is directly
125 // executed (as opposed to executed inside a message loop) would autorelease
126 // objects into a higher-level pool. This pool is not recycled in-sync with
127 // the message loops' pools and causes problems with code relying on
128 // deallocation via an autorelease pool (such as browser window closure and
129 // browser shutdown). To avoid this, the following pool is recycled after each
130 // time code is directly executed.
131 base::mac::ScopedNSAutoreleasePool pool
;
134 // Pump startup related events.
135 base::MessageLoopForUI::current()->RunUntilIdle();
137 #if defined(OS_MACOSX)
143 RunTestOnMainThread();
145 TearDownOnMainThread();
146 #if defined(OS_MACOSX)
150 for (RenderProcessHost::iterator
i(RenderProcessHost::AllHostsIterator());
151 !i
.IsAtEnd(); i
.Advance()) {
152 i
.GetCurrentValue()->FastShutdownIfPossible();
155 Shell::CloseAllWindows();
158 Shell
* ContentBrowserTest::CreateBrowser() {
159 return Shell::CreateNewWindow(
160 ShellContentBrowserClient::Get()->browser_context(),
161 GURL(kAboutBlankURL
),
167 Shell
* ContentBrowserTest::CreateOffTheRecordBrowser() {
168 return Shell::CreateNewWindow(
169 ShellContentBrowserClient::Get()->off_the_record_browser_context(),
170 GURL(kAboutBlankURL
),
176 } // namespace content