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/common/shell_switches.h" 18 #include "content/shell/renderer/shell_content_renderer_client.h" 19 #include "content/shell/shell.h" 20 #include "content/shell/shell_browser_context.h" 21 #include "content/shell/shell_content_browser_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 namespace content { 30 31 IN_PROC_BROWSER_TEST_F(ContentBrowserTest, MANUAL_ShouldntRun) { 32 // Ensures that tests with MANUAL_ prefix don't run automatically. 33 ASSERT_TRUE(false); 34 } 35 36 ContentBrowserTest::ContentBrowserTest() { 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 } 54 55 void ContentBrowserTest::SetUp() { 56 shell_main_delegate_.reset(new ShellMainDelegate); 57 shell_main_delegate_->PreSandboxStartup(); 58 59 CommandLine* command_line = CommandLine::ForCurrentProcess(); 60 command_line->AppendSwitch(switches::kContentBrowserTest); 61 62 SetUpCommandLine(command_line); 63 64 // Single-process mode is not set in BrowserMain, so process it explicitly, 65 // and set up renderer. 66 if (command_line->HasSwitch(switches::kSingleProcess)) { 67 single_process_renderer_client_.reset(new ShellContentRendererClient); 68 SetRendererClientForTesting(single_process_renderer_client_.get()); 69 } 70 71 #if defined(OS_MACOSX) 72 // See InProcessBrowserTest::PrepareTestCommandLine(). 73 base::FilePath subprocess_path; 74 PathService::Get(base::FILE_EXE, &subprocess_path); 75 subprocess_path = subprocess_path.DirName().DirName(); 76 DCHECK_EQ(subprocess_path.BaseName().value(), "Contents"); 77 subprocess_path = subprocess_path.Append( 78 "Frameworks/Content Shell Helper.app/Contents/MacOS/Content Shell Helper"); 79 command_line->AppendSwitchPath(switches::kBrowserSubprocessPath, 80 subprocess_path); 81 #endif 82 83 // NOTE: should be kept in sync with 84 // chrome/browser/resources/software_rendering_list.json 85 #if !defined(OS_WIN) && !defined(OS_CHROMEOS) 86 command_line->AppendSwitch(switches::kDisableAcceleratedVideoDecode); 87 #endif 88 89 BrowserTestBase::SetUp(); 90 } 91 92 void ContentBrowserTest::TearDown() { 93 BrowserTestBase::TearDown(); 94 95 shell_main_delegate_.reset(); 96 } 97 98 void ContentBrowserTest::RunTestOnMainThreadLoop() { 99 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { 100 CHECK_EQ(Shell::windows().size(), 1u); 101 shell_ = Shell::windows()[0]; 102 } 103 104 #if defined(OS_MACOSX) 105 // On Mac, without the following autorelease pool, code which is directly 106 // executed (as opposed to executed inside a message loop) would autorelease 107 // objects into a higher-level pool. This pool is not recycled in-sync with 108 // the message loops' pools and causes problems with code relying on 109 // deallocation via an autorelease pool (such as browser window closure and 110 // browser shutdown). To avoid this, the following pool is recycled after each 111 // time code is directly executed. 112 base::mac::ScopedNSAutoreleasePool pool; 113 #endif 114 115 // Pump startup related events. 116 base::MessageLoopForUI::current()->RunUntilIdle(); 117 118 #if defined(OS_MACOSX) 119 pool.Recycle(); 120 #endif 121 122 SetUpOnMainThread(); 123 124 RunTestOnMainThread(); 125 126 TearDownOnMainThread(); 127 #if defined(OS_MACOSX) 128 pool.Recycle(); 129 #endif 130 131 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); 132 !i.IsAtEnd(); i.Advance()) { 133 i.GetCurrentValue()->FastShutdownIfPossible(); 134 } 135 136 Shell::CloseAllWindows(); 137 } 138 139 Shell* ContentBrowserTest::CreateBrowser() { 140 return Shell::CreateNewWindow( 141 ShellContentBrowserClient::Get()->browser_context(), 142 GURL(kAboutBlankURL), 143 NULL, 144 MSG_ROUTING_NONE, 145 gfx::Size()); 146 } 147 148 Shell* ContentBrowserTest::CreateOffTheRecordBrowser() { 149 return Shell::CreateNewWindow( 150 ShellContentBrowserClient::Get()->off_the_record_browser_context(), 151 GURL(kAboutBlankURL), 152 NULL, 153 MSG_ROUTING_NONE, 154 gfx::Size()); 155 } 156 157 } // namespace content 158