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"
31 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
32 #include "ui/gl/test/gl_surface_test_support.h"
36 #include "base/memory/shared_memory.h"
39 #if defined(ENABLE_EXTENSIONS)
40 #include "chrome/common/extensions/chrome_extensions_client.h"
41 #include "extensions/common/extension_paths.h"
46 // Creates a TestingBrowserProcess for each test.
47 class ChromeUnitTestSuiteInitializer
: public testing::EmptyTestEventListener
{
49 ChromeUnitTestSuiteInitializer() {}
50 virtual ~ChromeUnitTestSuiteInitializer() {}
52 void OnTestStart(const testing::TestInfo
& test_info
) override
{
53 content_client_
.reset(new ChromeContentClient
);
54 content::SetContentClient(content_client_
.get());
55 // TODO(ios): Bring this back once ChromeContentBrowserClient is building.
57 browser_content_client_
.reset(new chrome::ChromeContentBrowserClient());
58 content::SetBrowserClientForTesting(browser_content_client_
.get());
59 utility_content_client_
.reset(new ChromeContentUtilityClient());
60 content::SetUtilityClientForTesting(utility_content_client_
.get());
63 TestingBrowserProcess::CreateInstance();
66 void OnTestEnd(const testing::TestInfo
& test_info
) override
{
67 // TODO(ios): Bring this back once ChromeContentBrowserClient is building.
69 browser_content_client_
.reset();
70 utility_content_client_
.reset();
72 content_client_
.reset();
73 content::SetContentClient(NULL
);
75 TestingBrowserProcess::DeleteInstance();
79 // Client implementations for the content module.
80 scoped_ptr
<ChromeContentClient
> content_client_
;
81 // TODO(ios): Bring this back once ChromeContentBrowserClient is building.
83 scoped_ptr
<chrome::ChromeContentBrowserClient
> browser_content_client_
;
84 scoped_ptr
<ChromeContentUtilityClient
> utility_content_client_
;
87 DISALLOW_COPY_AND_ASSIGN(ChromeUnitTestSuiteInitializer
);
92 ChromeUnitTestSuite::ChromeUnitTestSuite(int argc
, char** argv
)
93 : ChromeTestSuite(argc
, argv
) {}
95 ChromeUnitTestSuite::~ChromeUnitTestSuite() {}
97 void ChromeUnitTestSuite::Initialize() {
98 // Add an additional listener to do the extra initialization for unit tests.
99 // It will be started before the base class listeners and ended after the
100 // base class listeners.
101 testing::TestEventListeners
& listeners
=
102 testing::UnitTest::GetInstance()->listeners();
103 listeners
.Append(new ChromeUnitTestSuiteInitializer
);
105 InitializeProviders();
106 RegisterInProcessThreads();
108 ChromeTestSuite::Initialize();
110 // This needs to run after ChromeTestSuite::Initialize which calls content's
111 // intialization which calls base's which initializes ICU.
112 InitializeResourceBundle();
114 base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator_
);
117 void ChromeUnitTestSuite::Shutdown() {
118 ResourceBundle::CleanupSharedInstance();
119 ChromeTestSuite::Shutdown();
122 void ChromeUnitTestSuite::InitializeProviders() {
124 ChromeContentClient content_client
;
125 RegisterContentSchemes(&content_client
);
128 chrome::RegisterPathProvider();
129 content::RegisterPathProvider();
130 ui::RegisterPathProvider();
131 translate::BrowserCldDataProviderFactory::SetDefault(
132 new translate::BrowserCldDataProviderFactory());
133 translate::CldDataSource::SetDefault(
134 translate::CldDataSource::GetStaticDataSource());
135 component_updater::RegisterPathProvider(chrome::DIR_USER_DATA
);
137 #if defined(OS_CHROMEOS)
138 chromeos::RegisterPathProvider();
141 #if defined(ENABLE_EXTENSIONS)
142 extensions::RegisterPathProvider();
144 extensions::ExtensionsClient::Set(
145 extensions::ChromeExtensionsClient::GetInstance());
149 content::WebUIControllerFactory::RegisterFactory(
150 ChromeWebUIControllerFactory::GetInstance());
152 gfx::GLSurfaceTestSupport::InitializeOneOff();
154 update_client::UpdateQueryParams::SetDelegate(
155 ChromeUpdateQueryParamsDelegate::GetInstance());
159 void ChromeUnitTestSuite::InitializeResourceBundle() {
160 // Force unittests to run using en-US so if we test against string
161 // output, it'll pass regardless of the system language.
162 ui::ResourceBundle::InitSharedInstanceWithLocale(
163 "en-US", NULL
, ui::ResourceBundle::LOAD_COMMON_RESOURCES
);
164 base::FilePath resources_pack_path
;
165 #if defined(OS_MACOSX) && !defined(OS_IOS)
166 PathService::Get(base::DIR_MODULE
, &resources_pack_path
);
167 resources_pack_path
=
168 resources_pack_path
.Append(FILE_PATH_LITERAL("resources.pak"));
170 PathService::Get(chrome::FILE_RESOURCES_PACK
, &resources_pack_path
);
172 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
173 resources_pack_path
, ui::SCALE_FACTOR_NONE
);