| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/test/content_browser_test.h" | 5 #include "content/test/content_browser_test.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "content/public/browser/render_process_host.h" | 12 #include "content/public/browser/render_process_host.h" |
| 13 #include "content/public/common/content_paths.h" | 13 #include "content/public/common/content_paths.h" |
| 14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
| 15 #include "content/public/common/url_constants.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" | 16 #include "content/shell/browser/shell.h" |
| 18 #include "content/shell/browser/shell_browser_context.h" | 17 #include "content/shell/browser/shell_browser_context.h" |
| 19 #include "content/shell/browser/shell_content_browser_client.h" | 18 #include "content/shell/browser/shell_content_browser_client.h" |
| 20 #include "content/shell/common/shell_switches.h" | 19 #include "content/shell/common/shell_switches.h" |
| 21 #include "content/shell/renderer/shell_content_renderer_client.h" | 20 #include "content/shell/renderer/shell_content_renderer_client.h" |
| 22 #include "content/test/test_content_client.h" | 21 #include "content/test/test_content_client.h" |
| 23 #include "net/test/embedded_test_server/embedded_test_server.h" | 22 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 24 | 23 |
| 25 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
| 26 #include "base/mac/scoped_nsautorelease_pool.h" | 25 #include "base/mac/scoped_nsautorelease_pool.h" |
| 27 #endif | 26 #endif |
| 28 | 27 |
| 29 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) | 28 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) |
| 30 #include "ui/base/ime/input_method_initializer.h" | 29 #include "ui/base/ime/input_method_initializer.h" |
| 31 #endif | 30 #endif |
| 32 | 31 |
| 33 namespace content { | 32 namespace content { |
| 34 | 33 |
| 35 ContentBrowserTest::ContentBrowserTest() | 34 ContentBrowserTest::ContentBrowserTest() |
| 36 : setup_called_(false) { | 35 : setup_called_(false) { |
| 37 #if defined(OS_MACOSX) | 36 #if defined(OS_MACOSX) |
| 38 // See comment in InProcessBrowserTest::InProcessBrowserTest(). | 37 // See comment in InProcessBrowserTest::InProcessBrowserTest(). |
| 39 base::FilePath content_shell_path; | 38 base::FilePath content_shell_path; |
| 40 CHECK(PathService::Get(base::FILE_EXE, &content_shell_path)); | 39 CHECK(PathService::Get(base::FILE_EXE, &content_shell_path)); |
| 41 content_shell_path = content_shell_path.DirName(); | 40 content_shell_path = content_shell_path.DirName(); |
| 42 content_shell_path = content_shell_path.Append( | 41 content_shell_path = content_shell_path.Append( |
| 43 FILE_PATH_LITERAL("Content Shell.app/Contents/MacOS/Content Shell")); | 42 FILE_PATH_LITERAL("Content Shell.app/Contents/MacOS/Content Shell")); |
| 44 CHECK(PathService::Override(base::FILE_EXE, content_shell_path)); | 43 CHECK(PathService::Override(base::FILE_EXE, content_shell_path)); |
| 45 #endif | 44 #endif |
| 46 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("content/test/data"))); | 45 base::FilePath content_test_data(FILE_PATH_LITERAL("content/test/data")); |
| 47 base::FilePath content_test_data_dir; | 46 CreateTestServer(content_test_data); |
| 48 CHECK(PathService::Get(DIR_TEST_DATA, &content_test_data_dir)); | 47 base::FilePath content_test_data_absolute; |
| 49 embedded_test_server()->ServeFilesFromDirectory(content_test_data_dir); | 48 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &content_test_data_absolute)); |
| 49 content_test_data_absolute = |
| 50 content_test_data_absolute.Append(content_test_data); |
| 51 embedded_test_server()->ServeFilesFromDirectory(content_test_data_absolute); |
| 50 } | 52 } |
| 51 | 53 |
| 52 ContentBrowserTest::~ContentBrowserTest() { | 54 ContentBrowserTest::~ContentBrowserTest() { |
| 53 CHECK(setup_called_) << "Overridden SetUp() did not call parent " | 55 CHECK(setup_called_) << "Overridden SetUp() did not call parent " |
| 54 "implementation, so test not run."; | 56 "implementation, so test not run."; |
| 55 } | 57 } |
| 56 | 58 |
| 57 void ContentBrowserTest::SetUp() { | 59 void ContentBrowserTest::SetUp() { |
| 58 shell_main_delegate_.reset(new ShellMainDelegate); | |
| 59 shell_main_delegate_->PreSandboxStartup(); | |
| 60 | |
| 61 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 60 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 62 command_line->AppendSwitch(switches::kContentBrowserTest); | 61 command_line->AppendSwitch(switches::kContentBrowserTest); |
| 63 | 62 |
| 64 SetUpCommandLine(command_line); | 63 SetUpCommandLine(command_line); |
| 65 | 64 |
| 66 // Single-process mode is not set in BrowserMain, so if a subclass hasn't | |
| 67 // modified it yet, process it explicitly, and set up renderer. | |
| 68 if (command_line->HasSwitch(switches::kSingleProcess)) { | |
| 69 if (!single_process_renderer_client_) | |
| 70 single_process_renderer_client_.reset(new ShellContentRendererClient()); | |
| 71 SetRendererClientForTesting(single_process_renderer_client_.get()); | |
| 72 } else { | |
| 73 // Confirm no test has called SetContentRendererClient() without | |
| 74 // setting up single process mode. | |
| 75 DCHECK(!single_process_renderer_client_); | |
| 76 } | |
| 77 | |
| 78 #if defined(OS_MACOSX) | 65 #if defined(OS_MACOSX) |
| 79 // See InProcessBrowserTest::PrepareTestCommandLine(). | 66 // See InProcessBrowserTest::PrepareTestCommandLine(). |
| 80 base::FilePath subprocess_path; | 67 base::FilePath subprocess_path; |
| 81 PathService::Get(base::FILE_EXE, &subprocess_path); | 68 PathService::Get(base::FILE_EXE, &subprocess_path); |
| 82 subprocess_path = subprocess_path.DirName().DirName(); | 69 subprocess_path = subprocess_path.DirName().DirName(); |
| 83 DCHECK_EQ(subprocess_path.BaseName().value(), "Contents"); | 70 DCHECK_EQ(subprocess_path.BaseName().value(), "Contents"); |
| 84 subprocess_path = subprocess_path.Append( | 71 subprocess_path = subprocess_path.Append( |
| 85 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper")
; | 72 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper")
; |
| 86 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, | 73 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, |
| 87 subprocess_path); | 74 subprocess_path); |
| 88 #endif | 75 #endif |
| 89 | 76 |
| 90 // LinuxInputMethodContextFactory has to be initialized. | 77 // LinuxInputMethodContextFactory has to be initialized. |
| 91 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) | 78 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) |
| 92 ui::InitializeInputMethodForTesting(); | 79 ui::InitializeInputMethodForTesting(); |
| 93 #endif | 80 #endif |
| 94 | 81 |
| 95 setup_called_ = true; | 82 setup_called_ = true; |
| 96 | 83 |
| 97 BrowserTestBase::SetUp(); | 84 BrowserTestBase::SetUp(); |
| 98 } | 85 } |
| 99 | 86 |
| 100 void ContentBrowserTest::TearDown() { | 87 void ContentBrowserTest::TearDown() { |
| 101 BrowserTestBase::TearDown(); | 88 BrowserTestBase::TearDown(); |
| 102 | 89 |
| 103 // LinuxInputMethodContextFactory has to be shutdown. | 90 // LinuxInputMethodContextFactory has to be shutdown. |
| 104 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) | 91 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) |
| 105 ui::ShutdownInputMethodForTesting(); | 92 ui::ShutdownInputMethodForTesting(); |
| 106 #endif | 93 #endif |
| 107 | |
| 108 shell_main_delegate_.reset(); | |
| 109 } | 94 } |
| 110 | 95 |
| 111 void ContentBrowserTest::RunTestOnMainThreadLoop() { | 96 void ContentBrowserTest::RunTestOnMainThreadLoop() { |
| 112 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { | 97 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { |
| 113 CHECK_EQ(Shell::windows().size(), 1u); | 98 CHECK_EQ(Shell::windows().size(), 1u); |
| 114 shell_ = Shell::windows()[0]; | 99 shell_ = Shell::windows()[0]; |
| 115 } | 100 } |
| 116 | 101 |
| 117 #if defined(OS_MACOSX) | 102 #if defined(OS_MACOSX) |
| 118 // On Mac, without the following autorelease pool, code which is directly | 103 // On Mac, without the following autorelease pool, code which is directly |
| (...skipping 23 matching lines...) Expand all Loading... |
| 142 #endif | 127 #endif |
| 143 | 128 |
| 144 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 129 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 145 !i.IsAtEnd(); i.Advance()) { | 130 !i.IsAtEnd(); i.Advance()) { |
| 146 i.GetCurrentValue()->FastShutdownIfPossible(); | 131 i.GetCurrentValue()->FastShutdownIfPossible(); |
| 147 } | 132 } |
| 148 | 133 |
| 149 Shell::CloseAllWindows(); | 134 Shell::CloseAllWindows(); |
| 150 } | 135 } |
| 151 | 136 |
| 152 void ContentBrowserTest::SetContentRendererClient( | |
| 153 scoped_ptr<ContentRendererClient> renderer_client) { | |
| 154 // This routine must be called before SetUp(). | |
| 155 DCHECK(!setup_called_); | |
| 156 DCHECK(!single_process_renderer_client_); | |
| 157 single_process_renderer_client_ = renderer_client.Pass(); | |
| 158 } | |
| 159 | |
| 160 Shell* ContentBrowserTest::CreateBrowser() { | 137 Shell* ContentBrowserTest::CreateBrowser() { |
| 161 return Shell::CreateNewWindow( | 138 return Shell::CreateNewWindow( |
| 162 ShellContentBrowserClient::Get()->browser_context(), | 139 ShellContentBrowserClient::Get()->browser_context(), |
| 163 GURL(kAboutBlankURL), | 140 GURL(kAboutBlankURL), |
| 164 NULL, | 141 NULL, |
| 165 MSG_ROUTING_NONE, | 142 MSG_ROUTING_NONE, |
| 166 gfx::Size()); | 143 gfx::Size()); |
| 167 } | 144 } |
| 168 | 145 |
| 169 Shell* ContentBrowserTest::CreateOffTheRecordBrowser() { | 146 Shell* ContentBrowserTest::CreateOffTheRecordBrowser() { |
| 170 return Shell::CreateNewWindow( | 147 return Shell::CreateNewWindow( |
| 171 ShellContentBrowserClient::Get()->off_the_record_browser_context(), | 148 ShellContentBrowserClient::Get()->off_the_record_browser_context(), |
| 172 GURL(kAboutBlankURL), | 149 GURL(kAboutBlankURL), |
| 173 NULL, | 150 NULL, |
| 174 MSG_ROUTING_NONE, | 151 MSG_ROUTING_NONE, |
| 175 gfx::Size()); | 152 gfx::Size()); |
| 176 } | 153 } |
| 177 | 154 |
| 178 } // namespace content | 155 } // namespace content |
| OLD | NEW |