OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_unit_test_suite.h" | 5 #include "chrome/test/base/chrome_unit_test_suite.h" |
6 | 6 |
| 7 #include "base/path_service.h" |
| 8 #include "base/process/process_handle.h" |
| 9 #include "base/metrics/stats_table.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "chrome/browser/chrome_content_browser_client.h" |
| 12 #include "chrome/common/chrome_content_client.h" |
| 13 #include "chrome/common/chrome_paths.h" |
7 #include "chrome/test/base/testing_browser_process.h" | 14 #include "chrome/test/base/testing_browser_process.h" |
| 15 #include "chrome/utility/chrome_content_utility_client.h" |
| 16 #include "content/public/common/content_paths.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/base/resource/resource_handle.h" |
| 20 #include "ui/base/ui_base_paths.h" |
| 21 |
| 22 #if defined(OS_CHROMEOS) |
| 23 #include "chromeos/chromeos_paths.h" |
| 24 #endif |
| 25 |
| 26 #if !defined(OS_IOS) |
| 27 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
| 28 #include "chrome/common/extensions/chrome_extensions_client.h" |
| 29 #include "extensions/common/extension_paths.h" |
| 30 #include "ui/gl/gl_surface.h" |
| 31 #endif |
| 32 |
| 33 #if defined(OS_POSIX) |
| 34 #include "base/memory/shared_memory.h" |
| 35 #endif |
9 | 36 |
10 namespace { | 37 namespace { |
11 | 38 |
| 39 void RemoveSharedMemoryFile(const std::string& filename) { |
| 40 // Stats uses SharedMemory under the hood. On posix, this results in a file |
| 41 // on disk. |
| 42 #if defined(OS_POSIX) |
| 43 base::SharedMemory memory; |
| 44 memory.Delete(filename); |
| 45 #endif |
| 46 } |
| 47 |
12 // Creates a TestingBrowserProcess for each test. | 48 // Creates a TestingBrowserProcess for each test. |
13 class ChromeUnitTestSuiteInitializer : public testing::EmptyTestEventListener { | 49 class ChromeUnitTestSuiteInitializer : public testing::EmptyTestEventListener { |
14 public: | 50 public: |
15 ChromeUnitTestSuiteInitializer() {} | 51 ChromeUnitTestSuiteInitializer() {} |
16 virtual ~ChromeUnitTestSuiteInitializer() {} | 52 virtual ~ChromeUnitTestSuiteInitializer() {} |
17 | 53 |
18 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE { | 54 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE { |
| 55 content_client_.reset(new ChromeContentClient); |
| 56 content::SetContentClient(content_client_.get()); |
| 57 // TODO(ios): Bring this back once ChromeContentBrowserClient is building. |
| 58 #if !defined(OS_IOS) |
| 59 browser_content_client_.reset(new chrome::ChromeContentBrowserClient()); |
| 60 content::SetBrowserClientForTesting(browser_content_client_.get()); |
| 61 utility_content_client_.reset(new chrome::ChromeContentUtilityClient()); |
| 62 content::SetUtilityClientForTesting(utility_content_client_.get()); |
| 63 #endif |
| 64 |
19 TestingBrowserProcess::CreateInstance(); | 65 TestingBrowserProcess::CreateInstance(); |
20 } | 66 } |
21 | 67 |
22 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { | 68 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { |
| 69 // TODO(ios): Bring this back once ChromeContentBrowserClient is building. |
| 70 #if !defined(OS_IOS) |
| 71 browser_content_client_.reset(); |
| 72 utility_content_client_.reset(); |
| 73 #endif |
| 74 content_client_.reset(); |
| 75 content::SetContentClient(NULL); |
| 76 |
23 TestingBrowserProcess::DeleteInstance(); | 77 TestingBrowserProcess::DeleteInstance(); |
24 } | 78 } |
25 | 79 |
26 private: | 80 private: |
| 81 // Client implementations for the content module. |
| 82 scoped_ptr<ChromeContentClient> content_client_; |
| 83 // TODO(ios): Bring this back once ChromeContentBrowserClient is building. |
| 84 #if !defined(OS_IOS) |
| 85 scoped_ptr<chrome::ChromeContentBrowserClient> browser_content_client_; |
| 86 scoped_ptr<chrome::ChromeContentUtilityClient> utility_content_client_; |
| 87 #endif |
| 88 |
27 DISALLOW_COPY_AND_ASSIGN(ChromeUnitTestSuiteInitializer); | 89 DISALLOW_COPY_AND_ASSIGN(ChromeUnitTestSuiteInitializer); |
28 }; | 90 }; |
29 | 91 |
30 } // namespace | 92 } // namespace |
31 | 93 |
32 ChromeUnitTestSuite::ChromeUnitTestSuite(int argc, char** argv) | 94 ChromeUnitTestSuite::ChromeUnitTestSuite(int argc, char** argv) |
33 : ChromeTestSuite(argc, argv) {} | 95 : ChromeTestSuite(argc, argv) {} |
34 | 96 |
35 ChromeUnitTestSuite::~ChromeUnitTestSuite() {} | 97 ChromeUnitTestSuite::~ChromeUnitTestSuite() {} |
36 | 98 |
37 void ChromeUnitTestSuite::Initialize() { | 99 void ChromeUnitTestSuite::Initialize() { |
38 // Add an additional listener to do the extra initialization for unit tests. | 100 // Add an additional listener to do the extra initialization for unit tests. |
39 // It will be started before the base class listeners and ended after the | 101 // It will be started before the base class listeners and ended after the |
40 // base class listeners. | 102 // base class listeners. |
41 testing::TestEventListeners& listeners = | 103 testing::TestEventListeners& listeners = |
42 testing::UnitTest::GetInstance()->listeners(); | 104 testing::UnitTest::GetInstance()->listeners(); |
43 listeners.Append(new ChromeUnitTestSuiteInitializer); | 105 listeners.Append(new ChromeUnitTestSuiteInitializer); |
44 | 106 |
| 107 InitializeProviders(); |
| 108 RegisterInProcessThreads(); |
| 109 |
| 110 stats_filename_ = base::StringPrintf("unit_tests-%d", |
| 111 base::GetCurrentProcId()); |
| 112 RemoveSharedMemoryFile(stats_filename_); |
| 113 stats_table_.reset(new base::StatsTable(stats_filename_, 20, 200)); |
| 114 base::StatsTable::set_current(stats_table_.get()); |
| 115 |
45 ChromeTestSuite::Initialize(); | 116 ChromeTestSuite::Initialize(); |
| 117 |
| 118 // This needs to run after ChromeTestSuite::Initialize which calls content's |
| 119 // intialization which calls base's which initializes ICU. |
| 120 InitializeResourceBundle(); |
46 } | 121 } |
47 | 122 |
48 bool ChromeUnitTestSuite::IsBrowserTestSuite() { return false; } | 123 void ChromeUnitTestSuite::Shutdown() { |
| 124 ResourceBundle::CleanupSharedInstance(); |
| 125 |
| 126 base::StatsTable::set_current(NULL); |
| 127 stats_table_.reset(); |
| 128 RemoveSharedMemoryFile(stats_filename_); |
| 129 |
| 130 ChromeTestSuite::Shutdown(); |
| 131 } |
| 132 |
| 133 void ChromeUnitTestSuite::InitializeProviders() { |
| 134 { |
| 135 ChromeContentClient content_client; |
| 136 RegisterContentSchemes(&content_client); |
| 137 } |
| 138 |
| 139 chrome::RegisterPathProvider(); |
| 140 content::RegisterPathProvider(); |
| 141 ui::RegisterPathProvider(); |
| 142 |
| 143 #if defined(OS_CHROMEOS) |
| 144 chromeos::RegisterPathProvider(); |
| 145 #endif |
| 146 |
| 147 #if !defined(OS_IOS) |
| 148 extensions::RegisterPathProvider(); |
| 149 |
| 150 extensions::ExtensionsClient::Set( |
| 151 extensions::ChromeExtensionsClient::GetInstance()); |
| 152 |
| 153 content::WebUIControllerFactory::RegisterFactory( |
| 154 ChromeWebUIControllerFactory::GetInstance()); |
| 155 |
| 156 gfx::GLSurface::InitializeOneOffForTests(); |
| 157 #endif |
| 158 } |
| 159 |
| 160 void ChromeUnitTestSuite::InitializeResourceBundle() { |
| 161 // Force unittests to run using en-US so if we test against string |
| 162 // output, it'll pass regardless of the system language. |
| 163 ResourceBundle::InitSharedInstanceWithLocale("en-US", NULL); |
| 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")); |
| 169 #else |
| 170 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
| 171 #endif |
| 172 ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
| 173 resources_pack_path, ui::SCALE_FACTOR_NONE); |
| 174 } |
OLD | NEW |