Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / test / content_browser_test.cc
bloba6802124f9c59e70e1382c5d7bdb95fd851a364a
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"
26 #endif
28 #if defined(OS_MACOSX)
29 #include "base/mac/scoped_nsautorelease_pool.h"
30 #endif
32 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
33 #include "ui/base/ime/input_method_initializer.h"
34 #endif
36 namespace content {
38 ContentBrowserTest::ContentBrowserTest()
39 : setup_called_(false) {
40 #if defined(OS_MACOSX)
41 // See comment in InProcessBrowserTest::InProcessBrowserTest().
42 base::FilePath content_shell_path;
43 CHECK(PathService::Get(base::FILE_EXE, &content_shell_path));
44 content_shell_path = content_shell_path.DirName();
45 content_shell_path = content_shell_path.Append(
46 FILE_PATH_LITERAL("Content Shell.app/Contents/MacOS/Content Shell"));
47 CHECK(PathService::Override(base::FILE_EXE, content_shell_path));
48 #endif
49 base::FilePath content_test_data(FILE_PATH_LITERAL("content/test/data"));
50 CreateTestServer(content_test_data);
51 base::FilePath content_test_data_absolute;
52 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &content_test_data_absolute));
53 content_test_data_absolute =
54 content_test_data_absolute.Append(content_test_data);
55 embedded_test_server()->ServeFilesFromDirectory(content_test_data_absolute);
58 ContentBrowserTest::~ContentBrowserTest() {
59 CHECK(setup_called_) << "Overridden SetUp() did not call parent "
60 "implementation, so test not run.";
63 void ContentBrowserTest::SetUp() {
64 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
65 command_line->AppendSwitch(switches::kContentBrowserTest);
67 SetUpCommandLine(command_line);
69 #if defined(OS_ANDROID)
70 shell_main_delegate_.reset(new ShellMainDelegate);
71 shell_main_delegate_->PreSandboxStartup();
72 if (command_line->HasSwitch(switches::kSingleProcess)) {
73 // We explicitly leak the new ContentRendererClient as we're
74 // setting a global that may be used after ContentBrowserTest is
75 // destroyed.
76 ContentRendererClient* old_client =
77 command_line->HasSwitch(switches::kRunLayoutTest)
78 ? SetRendererClientForTesting(new LayoutTestContentRendererClient)
79 : SetRendererClientForTesting(new ShellContentRendererClient);
80 // No-one should have set this value before we did.
81 DCHECK(!old_client);
83 #elif defined(OS_MACOSX)
84 // See InProcessBrowserTest::PrepareTestCommandLine().
85 base::FilePath subprocess_path;
86 PathService::Get(base::FILE_EXE, &subprocess_path);
87 subprocess_path = subprocess_path.DirName().DirName();
88 DCHECK_EQ(subprocess_path.BaseName().value(), "Contents");
89 subprocess_path = subprocess_path.Append(
90 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper");
91 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
92 subprocess_path);
93 #endif
95 // LinuxInputMethodContextFactory has to be initialized.
96 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
97 ui::InitializeInputMethodForTesting();
98 #endif
100 setup_called_ = true;
102 BrowserTestBase::SetUp();
105 void ContentBrowserTest::TearDown() {
106 BrowserTestBase::TearDown();
108 // LinuxInputMethodContextFactory has to be shutdown.
109 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
110 ui::ShutdownInputMethodForTesting();
111 #endif
113 #if defined(OS_ANDROID)
114 shell_main_delegate_.reset();
115 #endif
118 void ContentBrowserTest::RunTestOnMainThreadLoop() {
119 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
120 switches::kRunLayoutTest)) {
121 CHECK_EQ(Shell::windows().size(), 1u);
122 shell_ = Shell::windows()[0];
125 #if defined(OS_MACOSX)
126 // On Mac, without the following autorelease pool, code which is directly
127 // executed (as opposed to executed inside a message loop) would autorelease
128 // objects into a higher-level pool. This pool is not recycled in-sync with
129 // the message loops' pools and causes problems with code relying on
130 // deallocation via an autorelease pool (such as browser window closure and
131 // browser shutdown). To avoid this, the following pool is recycled after each
132 // time code is directly executed.
133 base::mac::ScopedNSAutoreleasePool pool;
134 #endif
136 // Pump startup related events.
137 base::MessageLoopForUI::current()->RunUntilIdle();
139 #if defined(OS_MACOSX)
140 pool.Recycle();
141 #endif
143 SetUpOnMainThread();
145 RunTestOnMainThread();
147 TearDownOnMainThread();
148 #if defined(OS_MACOSX)
149 pool.Recycle();
150 #endif
152 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
153 !i.IsAtEnd(); i.Advance()) {
154 i.GetCurrentValue()->FastShutdownIfPossible();
157 Shell::CloseAllWindows();
160 Shell* ContentBrowserTest::CreateBrowser() {
161 return Shell::CreateNewWindow(
162 ShellContentBrowserClient::Get()->browser_context(),
163 GURL(url::kAboutBlankURL),
164 NULL,
165 gfx::Size());
168 Shell* ContentBrowserTest::CreateOffTheRecordBrowser() {
169 return Shell::CreateNewWindow(
170 ShellContentBrowserClient::Get()->off_the_record_browser_context(),
171 GURL(url::kAboutBlankURL),
172 NULL,
173 gfx::Size());
176 } // namespace content