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/chrome_test_suite.h" | 5 #include "chrome/test/base/chrome_test_suite.h" |
6 | 6 |
7 #if defined(OS_CHROMEOS) | 7 #if defined(OS_CHROMEOS) |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 #endif | 10 #endif |
11 | 11 |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/metrics/stats_table.h" | |
15 #include "base/path_service.h" | 14 #include "base/path_service.h" |
16 #include "base/strings/stringprintf.h" | |
17 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
18 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/chrome_content_browser_client.h" | |
20 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" | |
21 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
22 #include "chrome/common/chrome_content_client.h" | |
23 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
24 #include "chrome/common/extensions/chrome_extensions_client.h" | |
25 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
26 #include "chrome/utility/chrome_content_utility_client.h" | |
27 #include "content/public/test/test_launcher.h" | 20 #include "content/public/test/test_launcher.h" |
28 #include "extensions/common/extension_paths.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "ui/base/resource/resource_bundle.h" | |
31 #include "ui/base/resource/resource_handle.h" | |
32 | 22 |
33 #if defined(OS_ANDROID) | 23 #if defined(OS_ANDROID) |
34 #include "base/android/jni_android.h" | 24 #include "base/android/jni_android.h" |
35 #include "chrome/browser/android/chrome_jni_registrar.h" | 25 #include "chrome/browser/android/chrome_jni_registrar.h" |
36 #include "net/android/net_jni_registrar.h" | 26 #include "net/android/net_jni_registrar.h" |
37 #include "ui/base/android/ui_base_jni_registrar.h" | 27 #include "ui/base/android/ui_base_jni_registrar.h" |
38 #include "ui/gfx/android/gfx_jni_registrar.h" | 28 #include "ui/gfx/android/gfx_jni_registrar.h" |
39 #include "ui/gl/android/gl_jni_registrar.h" | 29 #include "ui/gl/android/gl_jni_registrar.h" |
40 #endif | 30 #endif |
41 | 31 |
42 #if defined(OS_CHROMEOS) | 32 #if defined(OS_CHROMEOS) |
43 #include "base/process/process_metrics.h" | 33 #include "base/process/process_metrics.h" |
44 #include "chromeos/chromeos_paths.h" | 34 #include "chromeos/chromeos_paths.h" |
45 #endif | 35 #endif |
46 | 36 |
47 #if !defined(OS_IOS) | |
48 #include "ui/gl/gl_surface.h" | |
49 #endif | |
50 | |
51 #if defined(OS_MACOSX) | 37 #if defined(OS_MACOSX) |
52 #include "base/mac/bundle_locations.h" | 38 #include "base/mac/bundle_locations.h" |
53 #include "base/mac/scoped_nsautorelease_pool.h" | 39 #include "base/mac/scoped_nsautorelease_pool.h" |
54 #if !defined(OS_IOS) | 40 #if !defined(OS_IOS) |
55 #include "base/mac/mac_util.h" | 41 #include "base/mac/mac_util.h" |
56 #include "chrome/browser/chrome_browser_application_mac.h" | 42 #include "chrome/browser/chrome_browser_application_mac.h" |
57 #endif // !defined(OS_IOS) | 43 #endif // !defined(OS_IOS) |
58 #endif | 44 #endif |
59 | 45 |
60 #if defined(OS_POSIX) | 46 #if !defined(OS_IOS) |
61 #include "base/memory/shared_memory.h" | 47 #include "media/base/media.h" |
62 #endif | 48 #endif |
63 | 49 |
64 namespace { | 50 namespace { |
65 | 51 |
66 void RemoveSharedMemoryFile(const std::string& filename) { | |
67 // Stats uses SharedMemory under the hood. On posix, this results in a file | |
68 // on disk. | |
69 #if defined(OS_POSIX) | |
70 base::SharedMemory memory; | |
71 memory.Delete(filename); | |
72 #endif | |
73 } | |
74 | |
75 bool IsCrosPythonProcess() { | 52 bool IsCrosPythonProcess() { |
76 #if defined(OS_CHROMEOS) | 53 #if defined(OS_CHROMEOS) |
77 char buf[80]; | 54 char buf[80]; |
78 int num_read = readlink(base::kProcSelfExe, buf, sizeof(buf) - 1); | 55 int num_read = readlink(base::kProcSelfExe, buf, sizeof(buf) - 1); |
79 if (num_read == -1) | 56 if (num_read == -1) |
80 return false; | 57 return false; |
81 buf[num_read] = 0; | 58 buf[num_read] = 0; |
82 const char kPythonPrefix[] = "/python"; | 59 const char kPythonPrefix[] = "/python"; |
83 return !strncmp(strrchr(buf, '/'), kPythonPrefix, sizeof(kPythonPrefix) - 1); | 60 return !strncmp(strrchr(buf, '/'), kPythonPrefix, sizeof(kPythonPrefix) - 1); |
84 #else | 61 #else |
85 return false; | 62 return false; |
86 #endif // defined(OS_CHROMEOS) | 63 #endif // defined(OS_CHROMEOS) |
87 } | 64 } |
88 | 65 |
89 // Initializes services needed by both unit tests and browser tests. | |
90 // See also ChromeUnitTestSuite for additional services created for unit tests. | |
91 class ChromeTestSuiteInitializer : public testing::EmptyTestEventListener { | |
92 public: | |
93 ChromeTestSuiteInitializer() { | |
94 } | |
95 | |
96 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE { | |
97 content_client_.reset(new ChromeContentClient); | |
98 content::SetContentClient(content_client_.get()); | |
99 // TODO(ios): Bring this back once ChromeContentBrowserClient is building. | |
100 #if !defined(OS_IOS) | |
101 browser_content_client_.reset(new chrome::ChromeContentBrowserClient()); | |
102 content::SetBrowserClientForTesting(browser_content_client_.get()); | |
103 utility_content_client_.reset(new chrome::ChromeContentUtilityClient()); | |
104 content::SetUtilityClientForTesting(utility_content_client_.get()); | |
105 #endif | |
106 } | |
107 | |
108 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { | |
109 // TODO(ios): Bring this back once ChromeContentBrowserClient is building. | |
110 #if !defined(OS_IOS) | |
111 browser_content_client_.reset(); | |
112 utility_content_client_.reset(); | |
113 #endif | |
114 content_client_.reset(); | |
115 content::SetContentClient(NULL); | |
116 } | |
117 | |
118 private: | |
119 // Client implementations for the content module. | |
120 scoped_ptr<ChromeContentClient> content_client_; | |
121 // TODO(ios): Bring this back once ChromeContentBrowserClient is building. | |
122 #if !defined(OS_IOS) | |
123 scoped_ptr<chrome::ChromeContentBrowserClient> browser_content_client_; | |
124 scoped_ptr<chrome::ChromeContentUtilityClient> utility_content_client_; | |
125 #endif | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(ChromeTestSuiteInitializer); | |
128 }; | |
129 | |
130 } // namespace | 66 } // namespace |
131 | 67 |
132 ChromeTestSuite::ChromeTestSuite(int argc, char** argv) | 68 ChromeTestSuite::ChromeTestSuite(int argc, char** argv) |
133 : content::ContentTestSuiteBase(argc, argv) { | 69 : content::ContentTestSuiteBase(argc, argv) { |
134 } | 70 } |
135 | 71 |
136 ChromeTestSuite::~ChromeTestSuite() { | 72 ChromeTestSuite::~ChromeTestSuite() { |
137 } | 73 } |
138 | 74 |
139 void ChromeTestSuite::Initialize() { | 75 void ChromeTestSuite::Initialize() { |
140 #if defined(OS_MACOSX) | 76 #if defined(OS_MACOSX) |
141 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 77 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
142 #if !defined(OS_IOS) | 78 #if !defined(OS_IOS) |
143 chrome_browser_application_mac::RegisterBrowserCrApp(); | 79 chrome_browser_application_mac::RegisterBrowserCrApp(); |
144 #endif // !defined(OS_IOS) | 80 #endif // !defined(OS_IOS) |
145 #endif | 81 #endif |
146 | 82 |
147 #if defined(OS_ANDROID) | 83 #if defined(OS_ANDROID) |
148 // Register JNI bindings for android. | 84 // Register JNI bindings for android. |
149 gfx::android::RegisterJni(base::android::AttachCurrentThread()); | 85 gfx::android::RegisterJni(base::android::AttachCurrentThread()); |
150 net::android::RegisterJni(base::android::AttachCurrentThread()); | 86 net::android::RegisterJni(base::android::AttachCurrentThread()); |
151 ui::android::RegisterJni(base::android::AttachCurrentThread()); | 87 ui::android::RegisterJni(base::android::AttachCurrentThread()); |
152 ui::gl::android::RegisterJni(base::android::AttachCurrentThread()); | 88 ui::gl::android::RegisterJni(base::android::AttachCurrentThread()); |
153 chrome::android::RegisterJni(base::android::AttachCurrentThread()); | 89 chrome::android::RegisterJni(base::android::AttachCurrentThread()); |
154 #endif | 90 #endif |
155 | 91 |
156 chrome::RegisterPathProvider(); | |
157 #if defined(OS_CHROMEOS) | |
158 chromeos::RegisterPathProvider(); | |
159 #endif | |
160 if (!browser_dir_.empty()) { | 92 if (!browser_dir_.empty()) { |
161 PathService::Override(base::DIR_EXE, browser_dir_); | 93 PathService::Override(base::DIR_EXE, browser_dir_); |
162 PathService::Override(base::DIR_MODULE, browser_dir_); | 94 PathService::Override(base::DIR_MODULE, browser_dir_); |
163 } | 95 } |
164 | 96 |
165 #if !defined(OS_IOS) | 97 #if !defined(OS_IOS) |
166 extensions::RegisterPathProvider(); | |
167 | |
168 extensions::ExtensionsClient::Set( | |
169 extensions::ChromeExtensionsClient::GetInstance()); | |
170 | |
171 // Only want to do this for unit tests. | |
172 if (!content::GetCurrentTestLauncherDelegate()) { | |
173 // For browser tests, this won't create the right object since | |
174 // TestChromeWebUIControllerFactory is used. That's created and | |
175 // registered in ChromeBrowserMainParts as in normal startup. | |
176 content::WebUIControllerFactory::RegisterFactory( | |
177 ChromeWebUIControllerFactory::GetInstance()); | |
178 } | |
179 #endif | |
180 | |
181 // Disable external libraries load if we are under python process in | 98 // Disable external libraries load if we are under python process in |
182 // ChromeOS. That means we are autotest and, if ASAN is used, | 99 // ChromeOS. That means we are autotest and, if ASAN is used, |
183 // external libraries load crashes. | 100 // external libraries load crashes. |
184 content::ContentTestSuiteBase::set_external_libraries_enabled( | 101 if (!IsCrosPythonProcess()) |
185 !IsCrosPythonProcess()); | 102 media::InitializeMediaLibraryForTesting(); |
| 103 #endif |
186 | 104 |
187 // Initialize after overriding paths as some content paths depend on correct | 105 // Initialize after overriding paths as some content paths depend on correct |
188 // values for DIR_EXE and DIR_MODULE. | 106 // values for DIR_EXE and DIR_MODULE. |
189 content::ContentTestSuiteBase::Initialize(); | 107 content::ContentTestSuiteBase::Initialize(); |
190 | 108 |
191 #if !defined(OS_IOS) | |
192 // For browser tests, a full chrome instance is initialized which will set up | |
193 // GLSurface itself. For unit tests, we need to set this up for them. | |
194 if (!IsBrowserTestSuite()) | |
195 gfx::GLSurface::InitializeOneOffForTests(); | |
196 #endif | |
197 | |
198 #if defined(OS_MACOSX) && !defined(OS_IOS) | 109 #if defined(OS_MACOSX) && !defined(OS_IOS) |
199 // Look in the framework bundle for resources. | 110 // Look in the framework bundle for resources. |
200 base::FilePath path; | 111 base::FilePath path; |
201 PathService::Get(base::DIR_EXE, &path); | 112 PathService::Get(base::DIR_EXE, &path); |
202 path = path.Append(chrome::kFrameworkName); | 113 path = path.Append(chrome::kFrameworkName); |
203 base::mac::SetOverrideFrameworkBundlePath(path); | 114 base::mac::SetOverrideFrameworkBundlePath(path); |
204 #endif | 115 #endif |
205 | |
206 // Force unittests to run using en-US so if we test against string | |
207 // output, it'll pass regardless of the system language. | |
208 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL); | |
209 base::FilePath resources_pack_path; | |
210 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
211 PathService::Get(base::DIR_MODULE, &resources_pack_path); | |
212 resources_pack_path = | |
213 resources_pack_path.Append(FILE_PATH_LITERAL("resources.pak")); | |
214 #else | |
215 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); | |
216 #endif | |
217 ResourceBundle::GetSharedInstance().AddDataPackFromPath( | |
218 resources_pack_path, ui::SCALE_FACTOR_NONE); | |
219 | |
220 stats_filename_ = base::StringPrintf("unit_tests-%d", | |
221 base::GetCurrentProcId()); | |
222 RemoveSharedMemoryFile(stats_filename_); | |
223 stats_table_.reset(new base::StatsTable(stats_filename_, 20, 200)); | |
224 base::StatsTable::set_current(stats_table_.get()); | |
225 | |
226 testing::TestEventListeners& listeners = | |
227 testing::UnitTest::GetInstance()->listeners(); | |
228 listeners.Append(new ChromeTestSuiteInitializer); | |
229 } | |
230 | |
231 content::ContentClient* ChromeTestSuite::CreateClientForInitialization() { | |
232 return new ChromeContentClient(); | |
233 } | 116 } |
234 | 117 |
235 void ChromeTestSuite::Shutdown() { | 118 void ChromeTestSuite::Shutdown() { |
236 ResourceBundle::CleanupSharedInstance(); | |
237 | |
238 #if defined(OS_MACOSX) && !defined(OS_IOS) | 119 #if defined(OS_MACOSX) && !defined(OS_IOS) |
239 base::mac::SetOverrideFrameworkBundle(NULL); | 120 base::mac::SetOverrideFrameworkBundle(NULL); |
240 #endif | 121 #endif |
241 | 122 |
242 base::StatsTable::set_current(NULL); | |
243 stats_table_.reset(); | |
244 RemoveSharedMemoryFile(stats_filename_); | |
245 | |
246 content::ContentTestSuiteBase::Shutdown(); | 123 content::ContentTestSuiteBase::Shutdown(); |
247 } | 124 } |
OLD | NEW |