Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / public / test / content_browser_test.cc
blobaa4c44a1766f727aa92f237b103c30eca69e4103
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/shell_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 CommandLine* command_line = 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 SetRendererClientForTesting(new ShellContentRendererClient());
78 // No-one should have set this value before we did.
79 DCHECK(!old_client);
81 #elif defined(OS_MACOSX)
82 // See InProcessBrowserTest::PrepareTestCommandLine().
83 base::FilePath subprocess_path;
84 PathService::Get(base::FILE_EXE, &subprocess_path);
85 subprocess_path = subprocess_path.DirName().DirName();
86 DCHECK_EQ(subprocess_path.BaseName().value(), "Contents");
87 subprocess_path = subprocess_path.Append(
88 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper");
89 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
90 subprocess_path);
91 #endif
93 // LinuxInputMethodContextFactory has to be initialized.
94 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
95 ui::InitializeInputMethodForTesting();
96 #endif
98 setup_called_ = true;
100 BrowserTestBase::SetUp();
103 void ContentBrowserTest::TearDown() {
104 BrowserTestBase::TearDown();
106 // LinuxInputMethodContextFactory has to be shutdown.
107 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
108 ui::ShutdownInputMethodForTesting();
109 #endif
111 #if defined(OS_ANDROID)
112 shell_main_delegate_.reset();
113 #endif
116 void ContentBrowserTest::RunTestOnMainThreadLoop() {
117 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
118 CHECK_EQ(Shell::windows().size(), 1u);
119 shell_ = Shell::windows()[0];
122 #if defined(OS_MACOSX)
123 // On Mac, without the following autorelease pool, code which is directly
124 // executed (as opposed to executed inside a message loop) would autorelease
125 // objects into a higher-level pool. This pool is not recycled in-sync with
126 // the message loops' pools and causes problems with code relying on
127 // deallocation via an autorelease pool (such as browser window closure and
128 // browser shutdown). To avoid this, the following pool is recycled after each
129 // time code is directly executed.
130 base::mac::ScopedNSAutoreleasePool pool;
131 #endif
133 // Pump startup related events.
134 base::MessageLoopForUI::current()->RunUntilIdle();
136 #if defined(OS_MACOSX)
137 pool.Recycle();
138 #endif
140 SetUpOnMainThread();
142 RunTestOnMainThread();
144 TearDownOnMainThread();
145 #if defined(OS_MACOSX)
146 pool.Recycle();
147 #endif
149 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
150 !i.IsAtEnd(); i.Advance()) {
151 i.GetCurrentValue()->FastShutdownIfPossible();
154 Shell::CloseAllWindows();
157 Shell* ContentBrowserTest::CreateBrowser() {
158 return Shell::CreateNewWindow(
159 ShellContentBrowserClient::Get()->browser_context(),
160 GURL(url::kAboutBlankURL),
161 NULL,
162 MSG_ROUTING_NONE,
163 gfx::Size());
166 Shell* ContentBrowserTest::CreateOffTheRecordBrowser() {
167 return Shell::CreateNewWindow(
168 ShellContentBrowserClient::Get()->off_the_record_browser_context(),
169 GURL(url::kAboutBlankURL),
170 NULL,
171 MSG_ROUTING_NONE,
172 gfx::Size());
175 } // namespace content