| 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 |
| 24 #if defined(OS_ANDROID) |
| 25 #include "content/shell/app/shell_main_delegate.h" |
| 26 #endif |
| 27 |
| 25 #if defined(OS_MACOSX) | 28 #if defined(OS_MACOSX) |
| 26 #include "base/mac/scoped_nsautorelease_pool.h" | 29 #include "base/mac/scoped_nsautorelease_pool.h" |
| 27 #endif | 30 #endif |
| 28 | 31 |
| 29 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) | 32 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) |
| 30 #include "ui/base/ime/input_method_initializer.h" | 33 #include "ui/base/ime/input_method_initializer.h" |
| 31 #endif | 34 #endif |
| 32 | 35 |
| 33 namespace content { | 36 namespace content { |
| 34 | 37 |
| 35 ContentBrowserTest::ContentBrowserTest() | 38 ContentBrowserTest::ContentBrowserTest() |
| 36 : setup_called_(false) { | 39 : setup_called_(false) { |
| 37 #if defined(OS_MACOSX) | 40 #if defined(OS_MACOSX) |
| 38 // See comment in InProcessBrowserTest::InProcessBrowserTest(). | 41 // See comment in InProcessBrowserTest::InProcessBrowserTest(). |
| 39 base::FilePath content_shell_path; | 42 base::FilePath content_shell_path; |
| 40 CHECK(PathService::Get(base::FILE_EXE, &content_shell_path)); | 43 CHECK(PathService::Get(base::FILE_EXE, &content_shell_path)); |
| 41 content_shell_path = content_shell_path.DirName(); | 44 content_shell_path = content_shell_path.DirName(); |
| 42 content_shell_path = content_shell_path.Append( | 45 content_shell_path = content_shell_path.Append( |
| 43 FILE_PATH_LITERAL("Content Shell.app/Contents/MacOS/Content Shell")); | 46 FILE_PATH_LITERAL("Content Shell.app/Contents/MacOS/Content Shell")); |
| 44 CHECK(PathService::Override(base::FILE_EXE, content_shell_path)); | 47 CHECK(PathService::Override(base::FILE_EXE, content_shell_path)); |
| 45 #endif | 48 #endif |
| 46 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("content/test/data"))); | 49 base::FilePath content_test_data(FILE_PATH_LITERAL("content/test/data")); |
| 47 base::FilePath content_test_data_dir; | 50 CreateTestServer(content_test_data); |
| 48 CHECK(PathService::Get(DIR_TEST_DATA, &content_test_data_dir)); | 51 base::FilePath content_test_data_absolute; |
| 49 embedded_test_server()->ServeFilesFromDirectory(content_test_data_dir); | 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); |
| 50 } | 56 } |
| 51 | 57 |
| 52 ContentBrowserTest::~ContentBrowserTest() { | 58 ContentBrowserTest::~ContentBrowserTest() { |
| 53 CHECK(setup_called_) << "Overridden SetUp() did not call parent " | 59 CHECK(setup_called_) << "Overridden SetUp() did not call parent " |
| 54 "implementation, so test not run."; | 60 "implementation, so test not run."; |
| 55 } | 61 } |
| 56 | 62 |
| 57 void ContentBrowserTest::SetUp() { | 63 void ContentBrowserTest::SetUp() { |
| 58 shell_main_delegate_.reset(new ShellMainDelegate); | |
| 59 shell_main_delegate_->PreSandboxStartup(); | |
| 60 | |
| 61 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 64 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 62 command_line->AppendSwitch(switches::kContentBrowserTest); | 65 command_line->AppendSwitch(switches::kContentBrowserTest); |
| 63 | 66 |
| 64 SetUpCommandLine(command_line); | 67 SetUpCommandLine(command_line); |
| 65 | 68 |
| 66 // Single-process mode is not set in BrowserMain, so if a subclass hasn't | 69 #if defined(OS_ANDROID) |
| 67 // modified it yet, process it explicitly, and set up renderer. | 70 shell_main_delegate_.reset(new ShellMainDelegate); |
| 71 shell_main_delegate_->PreSandboxStartup(); |
| 68 if (command_line->HasSwitch(switches::kSingleProcess)) { | 72 if (command_line->HasSwitch(switches::kSingleProcess)) { |
| 69 if (!single_process_renderer_client_) | 73 single_process_renderer_client_.reset(new ShellContentRendererClient()); |
| 70 single_process_renderer_client_.reset(new ShellContentRendererClient()); | |
| 71 SetRendererClientForTesting(single_process_renderer_client_.get()); | 74 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 } | 75 } |
| 77 | 76 #elif defined(OS_MACOSX) |
| 78 #if defined(OS_MACOSX) | |
| 79 // See InProcessBrowserTest::PrepareTestCommandLine(). | 77 // See InProcessBrowserTest::PrepareTestCommandLine(). |
| 80 base::FilePath subprocess_path; | 78 base::FilePath subprocess_path; |
| 81 PathService::Get(base::FILE_EXE, &subprocess_path); | 79 PathService::Get(base::FILE_EXE, &subprocess_path); |
| 82 subprocess_path = subprocess_path.DirName().DirName(); | 80 subprocess_path = subprocess_path.DirName().DirName(); |
| 83 DCHECK_EQ(subprocess_path.BaseName().value(), "Contents"); | 81 DCHECK_EQ(subprocess_path.BaseName().value(), "Contents"); |
| 84 subprocess_path = subprocess_path.Append( | 82 subprocess_path = subprocess_path.Append( |
| 85 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper")
; | 83 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper")
; |
| 86 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, | 84 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, |
| 87 subprocess_path); | 85 subprocess_path); |
| 88 #endif | 86 #endif |
| 89 | 87 |
| 90 // LinuxInputMethodContextFactory has to be initialized. | 88 // LinuxInputMethodContextFactory has to be initialized. |
| 91 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) | 89 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) |
| 92 ui::InitializeInputMethodForTesting(); | 90 ui::InitializeInputMethodForTesting(); |
| 93 #endif | 91 #endif |
| 94 | 92 |
| 95 setup_called_ = true; | 93 setup_called_ = true; |
| 96 | 94 |
| 97 BrowserTestBase::SetUp(); | 95 BrowserTestBase::SetUp(); |
| 98 } | 96 } |
| 99 | 97 |
| 100 void ContentBrowserTest::TearDown() { | 98 void ContentBrowserTest::TearDown() { |
| 101 BrowserTestBase::TearDown(); | 99 BrowserTestBase::TearDown(); |
| 102 | 100 |
| 103 // LinuxInputMethodContextFactory has to be shutdown. | 101 // LinuxInputMethodContextFactory has to be shutdown. |
| 104 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) | 102 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) |
| 105 ui::ShutdownInputMethodForTesting(); | 103 ui::ShutdownInputMethodForTesting(); |
| 106 #endif | 104 #endif |
| 107 | 105 |
| 106 #if defined(OS_ANDROID) |
| 108 shell_main_delegate_.reset(); | 107 shell_main_delegate_.reset(); |
| 108 #endif |
| 109 } | 109 } |
| 110 | 110 |
| 111 void ContentBrowserTest::RunTestOnMainThreadLoop() { | 111 void ContentBrowserTest::RunTestOnMainThreadLoop() { |
| 112 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { | 112 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { |
| 113 CHECK_EQ(Shell::windows().size(), 1u); | 113 CHECK_EQ(Shell::windows().size(), 1u); |
| 114 shell_ = Shell::windows()[0]; | 114 shell_ = Shell::windows()[0]; |
| 115 } | 115 } |
| 116 | 116 |
| 117 #if defined(OS_MACOSX) | 117 #if defined(OS_MACOSX) |
| 118 // On Mac, without the following autorelease pool, code which is directly | 118 // On Mac, without the following autorelease pool, code which is directly |
| (...skipping 23 matching lines...) Expand all Loading... |
| 142 #endif | 142 #endif |
| 143 | 143 |
| 144 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 144 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 145 !i.IsAtEnd(); i.Advance()) { | 145 !i.IsAtEnd(); i.Advance()) { |
| 146 i.GetCurrentValue()->FastShutdownIfPossible(); | 146 i.GetCurrentValue()->FastShutdownIfPossible(); |
| 147 } | 147 } |
| 148 | 148 |
| 149 Shell::CloseAllWindows(); | 149 Shell::CloseAllWindows(); |
| 150 } | 150 } |
| 151 | 151 |
| 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() { | 152 Shell* ContentBrowserTest::CreateBrowser() { |
| 161 return Shell::CreateNewWindow( | 153 return Shell::CreateNewWindow( |
| 162 ShellContentBrowserClient::Get()->browser_context(), | 154 ShellContentBrowserClient::Get()->browser_context(), |
| 163 GURL(kAboutBlankURL), | 155 GURL(kAboutBlankURL), |
| 164 NULL, | 156 NULL, |
| 165 MSG_ROUTING_NONE, | 157 MSG_ROUTING_NONE, |
| 166 gfx::Size()); | 158 gfx::Size()); |
| 167 } | 159 } |
| 168 | 160 |
| 169 Shell* ContentBrowserTest::CreateOffTheRecordBrowser() { | 161 Shell* ContentBrowserTest::CreateOffTheRecordBrowser() { |
| 170 return Shell::CreateNewWindow( | 162 return Shell::CreateNewWindow( |
| 171 ShellContentBrowserClient::Get()->off_the_record_browser_context(), | 163 ShellContentBrowserClient::Get()->off_the_record_browser_context(), |
| 172 GURL(kAboutBlankURL), | 164 GURL(kAboutBlankURL), |
| 173 NULL, | 165 NULL, |
| 174 MSG_ROUTING_NONE, | 166 MSG_ROUTING_NONE, |
| 175 gfx::Size()); | 167 gfx::Size()); |
| 176 } | 168 } |
| 177 | 169 |
| 178 } // namespace content | 170 } // namespace content |
| OLD | NEW |