Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / test / content_browser_test.cc
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.
4
5 #include "content/public/test/content_browser_test.h"
6
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"
23
24 #if defined(OS_ANDROID)
25 #include "content/shell/app/shell_main_delegate.h"
26 #endif
27
28 #if defined(OS_MACOSX)
29 #include "base/mac/scoped_nsautorelease_pool.h"
30 #endif
31
32 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
33 #include "ui/base/ime/input_method_initializer.h"
34 #endif
35
36 namespace content {
37
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);
56 }
57
58 ContentBrowserTest::~ContentBrowserTest() {
59   CHECK(setup_called_) << "Overridden SetUp() did not call parent "
60                           "implementation, so test not run.";
61 }
62
63 void ContentBrowserTest::SetUp() {
64   CommandLine* command_line = CommandLine::ForCurrentProcess();
65   command_line->AppendSwitch(switches::kContentBrowserTest);
66
67   SetUpCommandLine(command_line);
68
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::kDumpRenderTree)
78             ? SetRendererClientForTesting(new LayoutTestContentRendererClient)
79             : SetRendererClientForTesting(new ShellContentRendererClient);
80     // No-one should have set this value before we did.
81     DCHECK(!old_client);
82   }
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
94
95   // LinuxInputMethodContextFactory has to be initialized.
96 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
97   ui::InitializeInputMethodForTesting();
98 #endif
99
100   setup_called_ = true;
101
102   BrowserTestBase::SetUp();
103 }
104
105 void ContentBrowserTest::TearDown() {
106   BrowserTestBase::TearDown();
107
108   // LinuxInputMethodContextFactory has to be shutdown.
109 #if !defined(OS_CHROMEOS) && defined(OS_LINUX)
110   ui::ShutdownInputMethodForTesting();
111 #endif
112
113 #if defined(OS_ANDROID)
114   shell_main_delegate_.reset();
115 #endif
116 }
117
118 void ContentBrowserTest::RunTestOnMainThreadLoop() {
119   if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
120     CHECK_EQ(Shell::windows().size(), 1u);
121     shell_ = Shell::windows()[0];
122   }
123
124 #if defined(OS_MACOSX)
125   // On Mac, without the following autorelease pool, code which is directly
126   // executed (as opposed to executed inside a message loop) would autorelease
127   // objects into a higher-level pool. This pool is not recycled in-sync with
128   // the message loops' pools and causes problems with code relying on
129   // deallocation via an autorelease pool (such as browser window closure and
130   // browser shutdown). To avoid this, the following pool is recycled after each
131   // time code is directly executed.
132   base::mac::ScopedNSAutoreleasePool pool;
133 #endif
134
135   // Pump startup related events.
136   base::MessageLoopForUI::current()->RunUntilIdle();
137
138 #if defined(OS_MACOSX)
139   pool.Recycle();
140 #endif
141
142   SetUpOnMainThread();
143
144   RunTestOnMainThread();
145
146   TearDownOnMainThread();
147 #if defined(OS_MACOSX)
148   pool.Recycle();
149 #endif
150
151   for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
152        !i.IsAtEnd(); i.Advance()) {
153     i.GetCurrentValue()->FastShutdownIfPossible();
154   }
155
156   Shell::CloseAllWindows();
157 }
158
159 Shell* ContentBrowserTest::CreateBrowser() {
160   return Shell::CreateNewWindow(
161       ShellContentBrowserClient::Get()->browser_context(),
162       GURL(url::kAboutBlankURL),
163       NULL,
164       MSG_ROUTING_NONE,
165       gfx::Size());
166 }
167
168 Shell* ContentBrowserTest::CreateOffTheRecordBrowser() {
169   return Shell::CreateNewWindow(
170       ShellContentBrowserClient::Get()->off_the_record_browser_context(),
171       GURL(url::kAboutBlankURL),
172       NULL,
173       MSG_ROUTING_NONE,
174       gfx::Size());
175 }
176
177 }  // namespace content