Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / test / base / chrome_unit_test_suite.cc
blobe26a253f82db0240556f5d963a97d3ed904353e4
1 // Copyright 2013 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.
5 #include "chrome/test/base/chrome_unit_test_suite.h"
7 #include "base/path_service.h"
8 #include "base/process/process_handle.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/chrome_content_browser_client.h"
11 #include "chrome/browser/update_client/chrome_update_query_params_delegate.h"
12 #include "chrome/common/chrome_content_client.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/test/base/testing_browser_process.h"
15 #include "chrome/utility/chrome_content_utility_client.h"
16 #include "components/component_updater/component_updater_paths.h"
17 #include "components/translate/content/browser/browser_cld_data_provider_factory.h"
18 #include "components/translate/content/common/cld_data_source.h"
19 #include "components/update_client/update_query_params.h"
20 #include "content/public/common/content_paths.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/base/resource/resource_handle.h"
24 #include "ui/base/ui_base_paths.h"
26 #if defined(OS_CHROMEOS)
27 #include "chromeos/chromeos_paths.h"
28 #endif
30 #if !defined(OS_IOS)
31 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
32 #include "ui/gl/test/gl_surface_test_support.h"
33 #endif
35 #if defined(ENABLE_EXTENSIONS)
36 #include "chrome/common/extensions/chrome_extensions_client.h"
37 #include "extensions/common/extension_paths.h"
38 #endif
40 namespace {
42 // Creates a TestingBrowserProcess for each test.
43 class ChromeUnitTestSuiteInitializer : public testing::EmptyTestEventListener {
44 public:
45 ChromeUnitTestSuiteInitializer() {}
46 virtual ~ChromeUnitTestSuiteInitializer() {}
48 void OnTestStart(const testing::TestInfo& test_info) override {
49 content_client_.reset(new ChromeContentClient);
50 content::SetContentClient(content_client_.get());
51 // TODO(ios): Bring this back once ChromeContentBrowserClient is building.
52 #if !defined(OS_IOS)
53 browser_content_client_.reset(new ChromeContentBrowserClient());
54 content::SetBrowserClientForTesting(browser_content_client_.get());
55 utility_content_client_.reset(new ChromeContentUtilityClient());
56 content::SetUtilityClientForTesting(utility_content_client_.get());
57 #endif
59 TestingBrowserProcess::CreateInstance();
62 void OnTestEnd(const testing::TestInfo& test_info) override {
63 // TODO(ios): Bring this back once ChromeContentBrowserClient is building.
64 #if !defined(OS_IOS)
65 browser_content_client_.reset();
66 utility_content_client_.reset();
67 #endif
68 content_client_.reset();
69 content::SetContentClient(NULL);
71 TestingBrowserProcess::DeleteInstance();
74 private:
75 // Client implementations for the content module.
76 scoped_ptr<ChromeContentClient> content_client_;
77 // TODO(ios): Bring this back once ChromeContentBrowserClient is building.
78 #if !defined(OS_IOS)
79 scoped_ptr<ChromeContentBrowserClient> browser_content_client_;
80 scoped_ptr<ChromeContentUtilityClient> utility_content_client_;
81 #endif
83 DISALLOW_COPY_AND_ASSIGN(ChromeUnitTestSuiteInitializer);
86 } // namespace
88 ChromeUnitTestSuite::ChromeUnitTestSuite(int argc, char** argv)
89 : ChromeTestSuite(argc, argv) {}
91 ChromeUnitTestSuite::~ChromeUnitTestSuite() {}
93 void ChromeUnitTestSuite::Initialize() {
94 // Add an additional listener to do the extra initialization for unit tests.
95 // It will be started before the base class listeners and ended after the
96 // base class listeners.
97 testing::TestEventListeners& listeners =
98 testing::UnitTest::GetInstance()->listeners();
99 listeners.Append(new ChromeUnitTestSuiteInitializer);
101 InitializeProviders();
102 RegisterInProcessThreads();
104 ChromeTestSuite::Initialize();
106 // This needs to run after ChromeTestSuite::Initialize which calls content's
107 // intialization which calls base's which initializes ICU.
108 InitializeResourceBundle();
110 base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator_);
113 void ChromeUnitTestSuite::Shutdown() {
114 ResourceBundle::CleanupSharedInstance();
115 ChromeTestSuite::Shutdown();
118 void ChromeUnitTestSuite::InitializeProviders() {
120 ChromeContentClient content_client;
121 RegisterContentSchemes(&content_client);
124 chrome::RegisterPathProvider();
125 content::RegisterPathProvider();
126 ui::RegisterPathProvider();
127 translate::BrowserCldDataProviderFactory::SetDefault(
128 new translate::BrowserCldDataProviderFactory());
129 translate::CldDataSource::SetDefault(
130 translate::CldDataSource::GetStaticDataSource());
131 component_updater::RegisterPathProvider(chrome::DIR_USER_DATA);
133 #if defined(OS_CHROMEOS)
134 chromeos::RegisterPathProvider();
135 #endif
137 #if defined(ENABLE_EXTENSIONS)
138 extensions::RegisterPathProvider();
140 extensions::ExtensionsClient::Set(
141 extensions::ChromeExtensionsClient::GetInstance());
142 #endif
144 #if !defined(OS_IOS)
145 content::WebUIControllerFactory::RegisterFactory(
146 ChromeWebUIControllerFactory::GetInstance());
148 gfx::GLSurfaceTestSupport::InitializeOneOff();
150 update_client::UpdateQueryParams::SetDelegate(
151 ChromeUpdateQueryParamsDelegate::GetInstance());
152 #endif
155 void ChromeUnitTestSuite::InitializeResourceBundle() {
156 // Force unittests to run using en-US so if we test against string
157 // output, it'll pass regardless of the system language.
158 ui::ResourceBundle::InitSharedInstanceWithLocale(
159 "en-US", NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
160 base::FilePath resources_pack_path;
161 #if defined(OS_MACOSX) && !defined(OS_IOS)
162 PathService::Get(base::DIR_MODULE, &resources_pack_path);
163 resources_pack_path =
164 resources_pack_path.Append(FILE_PATH_LITERAL("resources.pak"));
165 #else
166 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
167 #endif
168 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
169 resources_pack_path, ui::SCALE_FACTOR_NONE);