OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/test/base/in_process_browser_test.h" | 5 #include "chrome/test/base/in_process_browser_test.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 68 |
69 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 69 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
70 #include "components/storage_monitor/test_storage_monitor.h" | 70 #include "components/storage_monitor/test_storage_monitor.h" |
71 #endif | 71 #endif |
72 | 72 |
73 namespace { | 73 namespace { |
74 | 74 |
75 // Passed as value of kTestType. | 75 // Passed as value of kTestType. |
76 const char kBrowserTestType[] = "browser"; | 76 const char kBrowserTestType[] = "browser"; |
77 | 77 |
78 // Used when running in single-process mode. | |
79 base::LazyInstance<ChromeContentRendererClient>::Leaky | |
80 g_chrome_content_renderer_client = LAZY_INSTANCE_INITIALIZER; | |
81 | |
82 // A BrowserListObserver that makes sure that all browsers created are on the | 78 // A BrowserListObserver that makes sure that all browsers created are on the |
83 // |allowed_desktop_|. | 79 // |allowed_desktop_|. |
84 class SingleDesktopTestObserver : public chrome::BrowserListObserver, | 80 class SingleDesktopTestObserver : public chrome::BrowserListObserver, |
85 public base::NonThreadSafe { | 81 public base::NonThreadSafe { |
86 public: | 82 public: |
87 explicit SingleDesktopTestObserver(chrome::HostDesktopType allowed_desktop); | 83 explicit SingleDesktopTestObserver(chrome::HostDesktopType allowed_desktop); |
88 virtual ~SingleDesktopTestObserver(); | 84 virtual ~SingleDesktopTestObserver(); |
89 | 85 |
90 // chrome::BrowserListObserver: | 86 // chrome::BrowserListObserver: |
91 virtual void OnBrowserAdded(Browser* browser) OVERRIDE; | 87 virtual void OnBrowserAdded(Browser* browser) OVERRIDE; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // Before we run the browser, we have to hack the path to the exe to match | 122 // Before we run the browser, we have to hack the path to the exe to match |
127 // what it would be if Chrome was running, because it is used to fork renderer | 123 // what it would be if Chrome was running, because it is used to fork renderer |
128 // processes, on Linux at least (failure to do so will cause a browser_test to | 124 // processes, on Linux at least (failure to do so will cause a browser_test to |
129 // be run instead of a renderer). | 125 // be run instead of a renderer). |
130 base::FilePath chrome_path; | 126 base::FilePath chrome_path; |
131 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); | 127 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); |
132 chrome_path = chrome_path.DirName(); | 128 chrome_path = chrome_path.DirName(); |
133 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath); | 129 chrome_path = chrome_path.Append(chrome::kBrowserProcessExecutablePath); |
134 CHECK(PathService::Override(base::FILE_EXE, chrome_path)); | 130 CHECK(PathService::Override(base::FILE_EXE, chrome_path)); |
135 #endif // defined(OS_MACOSX) | 131 #endif // defined(OS_MACOSX) |
| 132 |
136 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | 133 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
137 base::FilePath src_dir; | 134 base::FilePath src_dir; |
138 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); | 135 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); |
139 embedded_test_server()->ServeFilesFromDirectory( | 136 base::FilePath test_data_dir = src_dir.AppendASCII("chrome/test/data"); |
140 src_dir.AppendASCII("chrome/test/data")); | 137 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| 138 |
| 139 // chrome::DIR_TEST_DATA isn't going to be setup until after we call |
| 140 // ContentMain. However that is after tests' constructors or SetUp methods, |
| 141 // which sometimes need it. So just override it. |
| 142 CHECK(PathService::Override(chrome::DIR_TEST_DATA, test_data_dir)); |
141 } | 143 } |
142 | 144 |
143 InProcessBrowserTest::~InProcessBrowserTest() { | 145 InProcessBrowserTest::~InProcessBrowserTest() { |
144 } | 146 } |
145 | 147 |
146 void InProcessBrowserTest::SetUp() { | 148 void InProcessBrowserTest::SetUp() { |
147 // Browser tests will create their own g_browser_process later. | 149 // Browser tests will create their own g_browser_process later. |
148 DCHECK(!g_browser_process); | 150 DCHECK(!g_browser_process); |
149 | 151 |
150 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 152 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
151 // Allow subclasses to change the command line before running any tests. | 153 // Allow subclasses to change the command line before running any tests. |
152 SetUpCommandLine(command_line); | 154 SetUpCommandLine(command_line); |
153 // Add command line arguments that are used by all InProcessBrowserTests. | 155 // Add command line arguments that are used by all InProcessBrowserTests. |
154 PrepareTestCommandLine(command_line); | 156 PrepareTestCommandLine(command_line); |
155 | 157 |
156 // Create a temporary user data directory if required. | 158 // Create a temporary user data directory if required. |
157 ASSERT_TRUE(CreateUserDataDirectory()) | 159 ASSERT_TRUE(CreateUserDataDirectory()) |
158 << "Could not create user data directory."; | 160 << "Could not create user data directory."; |
159 | 161 |
160 // Allow subclasses the opportunity to make changes to the default user data | 162 // Allow subclasses the opportunity to make changes to the default user data |
161 // dir before running any tests. | 163 // dir before running any tests. |
162 ASSERT_TRUE(SetUpUserDataDirectory()) | 164 ASSERT_TRUE(SetUpUserDataDirectory()) |
163 << "Could not set up user data directory."; | 165 << "Could not set up user data directory."; |
164 | 166 |
165 // Single-process mode is not set in BrowserMain, so process it explicitly, | |
166 // and set up renderer. | |
167 if (command_line->HasSwitch(switches::kSingleProcess)) { | |
168 content::SetRendererClientForTesting( | |
169 g_chrome_content_renderer_client.Pointer()); | |
170 } | |
171 | |
172 #if defined(OS_CHROMEOS) | 167 #if defined(OS_CHROMEOS) |
173 // Make sure that the log directory exists. | 168 // Make sure that the log directory exists. |
174 base::FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName(); | 169 base::FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName(); |
175 base::CreateDirectory(log_dir); | 170 base::CreateDirectory(log_dir); |
176 #endif // defined(OS_CHROMEOS) | 171 #endif // defined(OS_CHROMEOS) |
177 | 172 |
178 #if defined(OS_MACOSX) | 173 #if defined(OS_MACOSX) |
179 // On Mac, without the following autorelease pool, code which is directly | 174 // On Mac, without the following autorelease pool, code which is directly |
180 // executed (as opposed to executed inside a message loop) would autorelease | 175 // executed (as opposed to executed inside a message loop) would autorelease |
181 // objects into a higher-level pool. This pool is not recycled in-sync with | 176 // objects into a higher-level pool. This pool is not recycled in-sync with |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 // On the Mac, this eventually reaches | 466 // On the Mac, this eventually reaches |
472 // -[BrowserWindowController windowWillClose:], which will post a deferred | 467 // -[BrowserWindowController windowWillClose:], which will post a deferred |
473 // -autorelease on itself to ultimately destroy the Browser object. The line | 468 // -autorelease on itself to ultimately destroy the Browser object. The line |
474 // below is necessary to pump these pending messages to ensure all Browsers | 469 // below is necessary to pump these pending messages to ensure all Browsers |
475 // get deleted. | 470 // get deleted. |
476 content::RunAllPendingInMessageLoop(); | 471 content::RunAllPendingInMessageLoop(); |
477 delete autorelease_pool_; | 472 delete autorelease_pool_; |
478 autorelease_pool_ = NULL; | 473 autorelease_pool_ = NULL; |
479 #endif | 474 #endif |
480 } | 475 } |
OLD | NEW |