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/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/app/shell_main_delegate.h" 17 #include "content/shell/browser/shell.h" 18 #include "content/shell/browser/shell_browser_context.h" 19 #include "content/shell/browser/shell_content_browser_client.h" 20 #include "content/shell/common/shell_switches.h" 21 #include "content/shell/renderer/shell_content_renderer_client.h" 22 #include "content/test/test_content_client.h" 23 #include "net/test/embedded_test_server/embedded_test_server.h" 24 25 #if defined(OS_MACOSX) 26 #include "base/mac/scoped_nsautorelease_pool.h" 27 #endif 28 29 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) 30 #include "ui/base/ime/input_method_initializer.h" 31 #endif 32 33 namespace content { 34 35 ContentBrowserTest::ContentBrowserTest() 36 : setup_called_(false) { 37 #if defined(OS_MACOSX) 38 // See comment in InProcessBrowserTest::InProcessBrowserTest(). 39 base::FilePath content_shell_path; 40 CHECK(PathService::Get(base::FILE_EXE, &content_shell_path)); 41 content_shell_path = content_shell_path.DirName(); 42 content_shell_path = content_shell_path.Append( 43 FILE_PATH_LITERAL("Content Shell.app/Contents/MacOS/Content Shell")); 44 CHECK(PathService::Override(base::FILE_EXE, content_shell_path)); 45 #endif 46 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("content/test/data"))); 47 base::FilePath content_test_data_dir; 48 CHECK(PathService::Get(DIR_TEST_DATA, &content_test_data_dir)); 49 embedded_test_server()->ServeFilesFromDirectory(content_test_data_dir); 50 } 51 52 ContentBrowserTest::~ContentBrowserTest() { 53 CHECK(setup_called_) << "Overridden SetUp() did not call parent " 54 "implementation, so test not run."; 55 } 56 57 void ContentBrowserTest::SetUp() { 58 setup_called_ = true; 59 60 shell_main_delegate_.reset(new ShellMainDelegate); 61 shell_main_delegate_->PreSandboxStartup(); 62 63 CommandLine* command_line = CommandLine::ForCurrentProcess(); 64 command_line->AppendSwitch(switches::kContentBrowserTest); 65 66 SetUpCommandLine(command_line); 67 68 // Single-process mode is not set in BrowserMain, so process it explicitly, 69 // and set up renderer. 70 if (command_line->HasSwitch(switches::kSingleProcess)) { 71 single_process_renderer_client_.reset(new ShellContentRendererClient); 72 SetRendererClientForTesting(single_process_renderer_client_.get()); 73 } 74 75 #if defined(OS_MACOSX) 76 // See InProcessBrowserTest::PrepareTestCommandLine(). 77 base::FilePath subprocess_path; 78 PathService::Get(base::FILE_EXE, &subprocess_path); 79 subprocess_path = subprocess_path.DirName().DirName(); 80 DCHECK_EQ(subprocess_path.BaseName().value(), "Contents"); 81 subprocess_path = subprocess_path.Append( 82 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper"); 83 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, 84 subprocess_path); 85 #endif 86 87 // LinuxInputMethodContextFactory has to be initialized. 88 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) 89 ui::InitializeInputMethodForTesting(); 90 #endif 91 92 BrowserTestBase::SetUp(); 93 } 94 95 void ContentBrowserTest::TearDown() { 96 BrowserTestBase::TearDown(); 97 98 // LinuxInputMethodContextFactory has to be shutdown. 99 #if !defined(OS_CHROMEOS) && defined(USE_AURA) && defined(USE_X11) 100 ui::ShutdownInputMethodForTesting(); 101 #endif 102 103 shell_main_delegate_.reset(); 104 } 105 106 void ContentBrowserTest::RunTestOnMainThreadLoop() { 107 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { 108 CHECK_EQ(Shell::windows().size(), 1u); 109 shell_ = Shell::windows()[0]; 110 } 111 112 #if defined(OS_MACOSX) 113 // On Mac, without the following autorelease pool, code which is directly 114 // executed (as opposed to executed inside a message loop) would autorelease 115 // objects into a higher-level pool. This pool is not recycled in-sync with 116 // the message loops' pools and causes problems with code relying on 117 // deallocation via an autorelease pool (such as browser window closure and 118 // browser shutdown). To avoid this, the following pool is recycled after each 119 // time code is directly executed. 120 base::mac::ScopedNSAutoreleasePool pool; 121 #endif 122 123 // Pump startup related events. 124 base::MessageLoopForUI::current()->RunUntilIdle(); 125 126 #if defined(OS_MACOSX) 127 pool.Recycle(); 128 #endif 129 130 SetUpOnMainThread(); 131 132 RunTestOnMainThread(); 133 134 TearDownOnMainThread(); 135 #if defined(OS_MACOSX) 136 pool.Recycle(); 137 #endif 138 139 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); 140 !i.IsAtEnd(); i.Advance()) { 141 i.GetCurrentValue()->FastShutdownIfPossible(); 142 } 143 144 Shell::CloseAllWindows(); 145 } 146 147 Shell* ContentBrowserTest::CreateBrowser() { 148 return Shell::CreateNewWindow( 149 ShellContentBrowserClient::Get()->browser_context(), 150 GURL(kAboutBlankURL), 151 NULL, 152 MSG_ROUTING_NONE, 153 gfx::Size()); 154 } 155 156 Shell* ContentBrowserTest::CreateOffTheRecordBrowser() { 157 return Shell::CreateNewWindow( 158 ShellContentBrowserClient::Get()->off_the_record_browser_context(), 159 GURL(kAboutBlankURL), 160 NULL, 161 MSG_ROUTING_NONE, 162 gfx::Size()); 163 } 164 165 } // namespace content 166