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. 4 5 #include "chrome/test/base/chrome_unit_test_suite.h" 6 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/omaha_query_params/chrome_omaha_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/omaha_query_params/omaha_query_params.h" 18 #include "content/public/common/content_paths.h" 19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "ui/base/resource/resource_bundle.h" 21 #include "ui/base/resource/resource_handle.h" 22 #include "ui/base/ui_base_paths.h" 23 24 #if defined(OS_CHROMEOS) 25 #include "chromeos/chromeos_paths.h" 26 #endif 27 28 #if !defined(OS_IOS) 29 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" 30 #include "chrome/common/extensions/chrome_extensions_client.h" 31 #include "extensions/common/extension_paths.h" 32 #include "ui/gl/gl_surface.h" 33 #endif 34 35 #if defined(OS_POSIX) 36 #include "base/memory/shared_memory.h" 37 #endif 38 39 namespace { 40 41 // Creates a TestingBrowserProcess for each test. 42 class ChromeUnitTestSuiteInitializer : public testing::EmptyTestEventListener { 43 public: ChromeUnitTestSuiteInitializer()44 ChromeUnitTestSuiteInitializer() {} ~ChromeUnitTestSuiteInitializer()45 virtual ~ChromeUnitTestSuiteInitializer() {} 46 OnTestStart(const testing::TestInfo & test_info)47 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE { 48 content_client_.reset(new ChromeContentClient); 49 content::SetContentClient(content_client_.get()); 50 // TODO(ios): Bring this back once ChromeContentBrowserClient is building. 51 #if !defined(OS_IOS) 52 browser_content_client_.reset(new chrome::ChromeContentBrowserClient()); 53 content::SetBrowserClientForTesting(browser_content_client_.get()); 54 utility_content_client_.reset(new ChromeContentUtilityClient()); 55 content::SetUtilityClientForTesting(utility_content_client_.get()); 56 #endif 57 58 TestingBrowserProcess::CreateInstance(); 59 } 60 OnTestEnd(const testing::TestInfo & test_info)61 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { 62 // TODO(ios): Bring this back once ChromeContentBrowserClient is building. 63 #if !defined(OS_IOS) 64 browser_content_client_.reset(); 65 utility_content_client_.reset(); 66 #endif 67 content_client_.reset(); 68 content::SetContentClient(NULL); 69 70 TestingBrowserProcess::DeleteInstance(); 71 } 72 73 private: 74 // Client implementations for the content module. 75 scoped_ptr<ChromeContentClient> content_client_; 76 // TODO(ios): Bring this back once ChromeContentBrowserClient is building. 77 #if !defined(OS_IOS) 78 scoped_ptr<chrome::ChromeContentBrowserClient> browser_content_client_; 79 scoped_ptr<ChromeContentUtilityClient> utility_content_client_; 80 #endif 81 82 DISALLOW_COPY_AND_ASSIGN(ChromeUnitTestSuiteInitializer); 83 }; 84 85 } // namespace 86 ChromeUnitTestSuite(int argc,char ** argv)87ChromeUnitTestSuite::ChromeUnitTestSuite(int argc, char** argv) 88 : ChromeTestSuite(argc, argv) {} 89 ~ChromeUnitTestSuite()90ChromeUnitTestSuite::~ChromeUnitTestSuite() {} 91 Initialize()92void ChromeUnitTestSuite::Initialize() { 93 // Add an additional listener to do the extra initialization for unit tests. 94 // It will be started before the base class listeners and ended after the 95 // base class listeners. 96 testing::TestEventListeners& listeners = 97 testing::UnitTest::GetInstance()->listeners(); 98 listeners.Append(new ChromeUnitTestSuiteInitializer); 99 100 InitializeProviders(); 101 RegisterInProcessThreads(); 102 103 ChromeTestSuite::Initialize(); 104 105 // This needs to run after ChromeTestSuite::Initialize which calls content's 106 // intialization which calls base's which initializes ICU. 107 InitializeResourceBundle(); 108 } 109 Shutdown()110void ChromeUnitTestSuite::Shutdown() { 111 ResourceBundle::CleanupSharedInstance(); 112 ChromeTestSuite::Shutdown(); 113 } 114 InitializeProviders()115void ChromeUnitTestSuite::InitializeProviders() { 116 { 117 ChromeContentClient content_client; 118 RegisterContentSchemes(&content_client); 119 } 120 121 chrome::RegisterPathProvider(); 122 content::RegisterPathProvider(); 123 ui::RegisterPathProvider(); 124 component_updater::RegisterPathProvider(chrome::DIR_USER_DATA); 125 126 #if defined(OS_CHROMEOS) 127 chromeos::RegisterPathProvider(); 128 #endif 129 130 #if !defined(OS_IOS) 131 extensions::RegisterPathProvider(); 132 133 extensions::ExtensionsClient::Set( 134 extensions::ChromeExtensionsClient::GetInstance()); 135 136 content::WebUIControllerFactory::RegisterFactory( 137 ChromeWebUIControllerFactory::GetInstance()); 138 139 gfx::GLSurface::InitializeOneOffForTests(); 140 141 omaha_query_params::OmahaQueryParams::SetDelegate( 142 ChromeOmahaQueryParamsDelegate::GetInstance()); 143 #endif 144 } 145 InitializeResourceBundle()146void ChromeUnitTestSuite::InitializeResourceBundle() { 147 // Force unittests to run using en-US so if we test against string 148 // output, it'll pass regardless of the system language. 149 ui::ResourceBundle::InitSharedInstanceWithLocale( 150 "en-US", NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES); 151 base::FilePath resources_pack_path; 152 #if defined(OS_MACOSX) && !defined(OS_IOS) 153 PathService::Get(base::DIR_MODULE, &resources_pack_path); 154 resources_pack_path = 155 resources_pack_path.Append(FILE_PATH_LITERAL("resources.pak")); 156 #else 157 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); 158 #endif 159 ResourceBundle::GetSharedInstance().AddDataPackFromPath( 160 resources_pack_path, ui::SCALE_FACTOR_NONE); 161 } 162