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/gl_surface.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();
115 void ChromeUnitTestSuite::Shutdown() {
116 ResourceBundle::CleanupSharedInstance();
117 ChromeTestSuite::Shutdown();
120 void ChromeUnitTestSuite::InitializeProviders() {
122 ChromeContentClient content_client
;
123 RegisterContentSchemes(&content_client
);
126 chrome::RegisterPathProvider();
127 content::RegisterPathProvider();
128 ui::RegisterPathProvider();
129 translate::BrowserCldDataProviderFactory::SetDefault(
130 new translate::BrowserCldDataProviderFactory());
131 translate::CldDataSource::SetDefault(
132 translate::CldDataSource::GetStaticDataSource());
133 component_updater::RegisterPathProvider(chrome::DIR_USER_DATA
);
135 #if defined(OS_CHROMEOS)
136 chromeos::RegisterPathProvider();
139 #if defined(ENABLE_EXTENSIONS)
140 extensions::RegisterPathProvider();
142 extensions::ExtensionsClient::Set(
143 extensions::ChromeExtensionsClient::GetInstance());
147 content::WebUIControllerFactory::RegisterFactory(
148 ChromeWebUIControllerFactory::GetInstance());
150 gfx::GLSurface::InitializeOneOffForTests();
152 update_client::UpdateQueryParams::SetDelegate(
153 ChromeUpdateQueryParamsDelegate::GetInstance());
157 void ChromeUnitTestSuite::InitializeResourceBundle() {
158 // Force unittests to run using en-US so if we test against string
159 // output, it'll pass regardless of the system language.
160 ui::ResourceBundle::InitSharedInstanceWithLocale(
161 "en-US", NULL
, ui::ResourceBundle::LOAD_COMMON_RESOURCES
);
162 base::FilePath resources_pack_path
;
163 #if defined(OS_MACOSX) && !defined(OS_IOS)
164 PathService::Get(base::DIR_MODULE
, &resources_pack_path
);
165 resources_pack_path
=
166 resources_pack_path
.Append(FILE_PATH_LITERAL("resources.pak"));
168 PathService::Get(chrome::FILE_RESOURCES_PACK
, &resources_pack_path
);
170 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
171 resources_pack_path
, ui::SCALE_FACTOR_NONE
);