OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_test_suite.h" | 5 #include "chrome/test/base/chrome_test_suite.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | |
8 #include "base/mac/scoped_nsautorelease_pool.h" | 9 #include "base/mac/scoped_nsautorelease_pool.h" |
10 #include "base/memory/ref_counted.h" | |
9 #include "base/metrics/stats_table.h" | 11 #include "base/metrics/stats_table.h" |
12 #include "base/path_service.h" | |
10 #include "base/process_util.h" | 13 #include "base/process_util.h" |
11 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
12 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/chrome_content_browser_client.h" | |
14 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
15 #include "chrome/common/chrome_content_client.h" | 19 #include "chrome/common/chrome_content_client.h" |
16 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
17 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
18 #include "chrome/test/base/testing_browser_process.h" | 22 #include "chrome/test/base/testing_browser_process.h" |
19 #include "content/common/content_paths.h" | 23 #include "content/common/content_paths.h" |
24 #include "net/base/mock_host_resolver.h" | |
20 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
26 #include "net/base/net_util.h" | |
27 #include "testing/gtest/include/gtest/gtest.h" | |
21 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
22 #include "ui/base/ui_base_paths.h" | 29 #include "ui/base/ui_base_paths.h" |
30 | |
23 #if defined(TOOLKIT_VIEWS) | 31 #if defined(TOOLKIT_VIEWS) |
24 #include "views/view.h" | 32 #include "views/view.h" |
25 #endif | 33 #endif |
26 | 34 |
27 #if defined(OS_MACOSX) | 35 #if defined(OS_MACOSX) |
28 #include "base/mac/mac_util.h" | 36 #include "base/mac/mac_util.h" |
29 #include "content/common/chrome_application_mac.h" | 37 #include "content/common/chrome_application_mac.h" |
30 #endif | 38 #endif |
31 | 39 |
32 #if defined(OS_POSIX) | 40 #if defined(OS_POSIX) |
33 #include "base/shared_memory.h" | 41 #include "base/shared_memory.h" |
34 #endif | 42 #endif |
35 | 43 |
36 static void RemoveSharedMemoryFile(const std::string& filename) { | 44 namespace { |
45 | |
46 void RemoveSharedMemoryFile(const std::string& filename) { | |
37 // Stats uses SharedMemory under the hood. On posix, this results in a file | 47 // Stats uses SharedMemory under the hood. On posix, this results in a file |
38 // on disk. | 48 // on disk. |
39 #if defined(OS_POSIX) | 49 #if defined(OS_POSIX) |
40 base::SharedMemory memory; | 50 base::SharedMemory memory; |
41 memory.Delete(filename); | 51 memory.Delete(filename); |
42 #endif | 52 #endif |
43 } | 53 } |
44 | 54 |
45 LocalHostResolverProc::LocalHostResolverProc() | 55 // In many cases it may be not obvious that a test makes a real DNS lookup. |
46 : HostResolverProc(NULL) { | 56 // We generally don't want to rely on external DNS servers for our tests, |
47 } | 57 // so this host resolver procedure catches external queries and returns a failed |
48 | 58 // lookup result. |
49 LocalHostResolverProc::~LocalHostResolverProc() { | 59 class LocalHostResolverProc : public net::HostResolverProc { |
50 } | 60 public: |
51 | 61 LocalHostResolverProc() : HostResolverProc(NULL) { |
52 int LocalHostResolverProc::Resolve(const std::string& host, | |
53 net::AddressFamily address_family, | |
54 net::HostResolverFlags host_resolver_flags, | |
55 net::AddressList* addrlist, | |
56 int* os_error) { | |
57 const char* kLocalHostNames[] = {"localhost", "127.0.0.1", "::1"}; | |
58 bool local = false; | |
59 | |
60 if (host == net::GetHostName()) { | |
61 local = true; | |
62 } else { | |
63 for (size_t i = 0; i < arraysize(kLocalHostNames); i++) | |
64 if (host == kLocalHostNames[i]) { | |
65 local = true; | |
66 break; | |
67 } | |
68 } | 62 } |
69 | 63 |
70 // To avoid depending on external resources and to reduce (if not preclude) | 64 virtual int Resolve(const std::string& host, |
71 // network interactions from tests, we simulate failure for non-local DNS | 65 net::AddressFamily address_family, |
72 // queries, rather than perform them. | 66 net::HostResolverFlags host_resolver_flags, |
73 // If you really need to make an external DNS query, use | 67 net::AddressList* addrlist, |
74 // net::RuleBasedHostResolverProc and its AllowDirectLookup method. | 68 int* os_error) { |
75 if (!local) { | 69 const char* kLocalHostNames[] = {"localhost", "127.0.0.1", "::1"}; |
76 DVLOG(1) << "To avoid external dependencies, simulating failure for " | 70 bool local = false; |
77 "external DNS lookup of " << host; | 71 |
78 return net::ERR_NOT_IMPLEMENTED; | 72 if (host == net::GetHostName()) { |
73 local = true; | |
74 } else { | |
75 for (size_t i = 0; i < arraysize(kLocalHostNames); i++) | |
76 if (host == kLocalHostNames[i]) { | |
77 local = true; | |
78 break; | |
79 } | |
80 } | |
81 | |
82 // To avoid depending on external resources and to reduce (if not preclude) | |
83 // network interactions from tests, we simulate failure for non-local DNS | |
84 // queries, rather than perform them. | |
85 // If you really need to make an external DNS query, use | |
86 // net::RuleBasedHostResolverProc and its AllowDirectLookup method. | |
87 if (!local) { | |
88 DVLOG(1) << "To avoid external dependencies, simulating failure for " | |
89 "external DNS lookup of " << host; | |
90 return net::ERR_NOT_IMPLEMENTED; | |
91 } | |
92 | |
93 return ResolveUsingPrevious(host, address_family, host_resolver_flags, | |
94 addrlist, os_error); | |
95 } | |
96 }; | |
97 | |
98 class ChromeTestSuiteInitializer : public testing::EmptyTestEventListener { | |
99 public: | |
100 ChromeTestSuiteInitializer() { | |
79 } | 101 } |
80 | 102 |
81 return ResolveUsingPrevious(host, address_family, host_resolver_flags, | 103 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE { |
82 addrlist, os_error); | 104 DCHECK(!g_browser_process); |
83 } | 105 g_browser_process = new TestingBrowserProcess; |
106 | |
107 DCHECK(!content::GetContentClient()); | |
jam
2011/09/07 00:48:51
btw, so I found out the hard way that this code do
Paweł Hajdan Jr.
2011/09/07 17:09:10
Ah I see, it's probably indeed that ChromeTestSuit
| |
108 content_client_.reset(new chrome::ChromeContentClient); | |
109 browser_content_client_.reset(new chrome::ChromeContentBrowserClient()); | |
110 content_client_->set_browser(browser_content_client_.get()); | |
111 content::SetContentClient(content_client_.get()); | |
112 | |
113 SetUpHostResolver(); | |
114 } | |
115 | |
116 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { | |
117 if (g_browser_process) { | |
118 delete g_browser_process; | |
119 g_browser_process = NULL; | |
120 } | |
121 | |
122 DCHECK_EQ(content_client_.get(), content::GetContentClient()); | |
123 browser_content_client_.reset(); | |
124 content_client_.reset(); | |
125 content::SetContentClient(NULL); | |
126 | |
127 TearDownHostResolver(); | |
128 } | |
129 | |
130 private: | |
131 void SetUpHostResolver() { | |
132 host_resolver_proc_ = new LocalHostResolverProc; | |
133 scoped_host_resolver_proc_.reset( | |
134 new net::ScopedDefaultHostResolverProc(host_resolver_proc_.get())); | |
135 } | |
136 | |
137 void TearDownHostResolver() { | |
138 scoped_host_resolver_proc_.reset(); | |
139 host_resolver_proc_ = NULL; | |
140 } | |
141 | |
142 scoped_ptr<BrowserProcess> browser_process_; | |
143 | |
144 scoped_ptr<chrome::ChromeContentClient> content_client_; | |
145 scoped_ptr<chrome::ChromeContentBrowserClient> browser_content_client_; | |
146 | |
147 scoped_refptr<LocalHostResolverProc> host_resolver_proc_; | |
148 scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(ChromeTestSuiteInitializer); | |
151 }; | |
152 | |
153 } // namespace | |
84 | 154 |
85 ChromeTestSuite::ChromeTestSuite(int argc, char** argv) | 155 ChromeTestSuite::ChromeTestSuite(int argc, char** argv) |
86 : base::TestSuite(argc, argv), | 156 : base::TestSuite(argc, argv) { |
87 stats_table_(NULL) { | |
88 } | 157 } |
89 | 158 |
90 ChromeTestSuite::~ChromeTestSuite() { | 159 ChromeTestSuite::~ChromeTestSuite() { |
91 } | 160 } |
92 | 161 |
93 void ChromeTestSuite::Initialize() { | 162 void ChromeTestSuite::Initialize() { |
94 #if defined(OS_MACOSX) | 163 #if defined(OS_MACOSX) |
95 chrome_application_mac::RegisterCrApp(); | 164 chrome_application_mac::RegisterCrApp(); |
96 #endif | 165 #endif |
97 | 166 |
98 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 167 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
99 | 168 |
100 base::TestSuite::Initialize(); | 169 base::TestSuite::Initialize(); |
101 | 170 |
102 // Initialize the content client which that code uses to talk to Chrome. | |
103 content::SetContentClient(&chrome_content_client_); | |
104 content::GetContentClient()->set_browser(&chrome_browser_content_client_); | |
105 | |
106 chrome::RegisterChromeSchemes(); | 171 chrome::RegisterChromeSchemes(); |
107 host_resolver_proc_ = new LocalHostResolverProc(); | |
108 scoped_host_resolver_proc_.Init(host_resolver_proc_.get()); | |
109 | 172 |
110 chrome::RegisterPathProvider(); | 173 chrome::RegisterPathProvider(); |
111 content::RegisterPathProvider(); | 174 content::RegisterPathProvider(); |
112 ui::RegisterPathProvider(); | 175 ui::RegisterPathProvider(); |
113 | 176 |
114 if (!browser_dir_.empty()) { | 177 if (!browser_dir_.empty()) { |
115 PathService::Override(base::DIR_EXE, browser_dir_); | 178 PathService::Override(base::DIR_EXE, browser_dir_); |
116 PathService::Override(base::DIR_MODULE, browser_dir_); | 179 PathService::Override(base::DIR_MODULE, browser_dir_); |
117 } | 180 } |
118 | 181 |
119 #if defined(OS_MACOSX) | 182 #if defined(OS_MACOSX) |
120 // Look in the framework bundle for resources. | 183 // Look in the framework bundle for resources. |
121 FilePath path; | 184 FilePath path; |
122 PathService::Get(base::DIR_EXE, &path); | 185 PathService::Get(base::DIR_EXE, &path); |
123 path = path.Append(chrome::kFrameworkName); | 186 path = path.Append(chrome::kFrameworkName); |
124 base::mac::SetOverrideAppBundlePath(path); | 187 base::mac::SetOverrideAppBundlePath(path); |
125 #endif | 188 #endif |
126 | 189 |
127 // Force unittests to run using en-US so if we test against string | 190 // Force unittests to run using en-US so if we test against string |
128 // output, it'll pass regardless of the system language. | 191 // output, it'll pass regardless of the system language. |
129 ResourceBundle::InitSharedInstance("en-US"); | 192 ResourceBundle::InitSharedInstance("en-US"); |
130 FilePath resources_pack_path; | 193 FilePath resources_pack_path; |
131 PathService::Get(base::DIR_MODULE, &resources_pack_path); | 194 PathService::Get(base::DIR_MODULE, &resources_pack_path); |
132 resources_pack_path = | 195 resources_pack_path = |
133 resources_pack_path.Append(FILE_PATH_LITERAL("resources.pak")); | 196 resources_pack_path.Append(FILE_PATH_LITERAL("resources.pak")); |
134 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); | 197 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); |
135 | 198 |
136 // initialize the global StatsTable for unit_tests (make sure the file | |
137 // doesn't exist before opening it so the test gets a clean slate) | |
138 stats_filename_ = "unit_tests"; | |
139 std::string pid_string = base::StringPrintf("-%d", base::GetCurrentProcId()); | |
140 stats_filename_ += pid_string; | |
141 RemoveSharedMemoryFile(stats_filename_); | |
142 stats_table_ = new base::StatsTable(stats_filename_, 20, 200); | |
143 base::StatsTable::set_current(stats_table_); | |
144 | |
145 #if defined(TOOLKIT_VIEWS) && defined(OS_LINUX) | 199 #if defined(TOOLKIT_VIEWS) && defined(OS_LINUX) |
146 // Turn of GPU compositing in browser during unit tests. | 200 // Turn of GPU compositing in browser during unit tests. |
147 views::View::set_use_acceleration_when_possible(false); | 201 views::View::set_use_acceleration_when_possible(false); |
148 #endif | 202 #endif |
203 | |
204 stats_filename_ = base::StringPrintf("unit_tests-%d", | |
205 base::GetCurrentProcId()); | |
206 RemoveSharedMemoryFile(stats_filename_); | |
207 stats_table_.reset(new base::StatsTable(stats_filename_, 20, 200)); | |
208 base::StatsTable::set_current(stats_table_.get()); | |
209 | |
210 testing::TestEventListeners& listeners = | |
211 testing::UnitTest::GetInstance()->listeners(); | |
212 listeners.Append(new ChromeTestSuiteInitializer); | |
149 } | 213 } |
150 | 214 |
151 void ChromeTestSuite::Shutdown() { | 215 void ChromeTestSuite::Shutdown() { |
152 ResourceBundle::CleanupSharedInstance(); | 216 ResourceBundle::CleanupSharedInstance(); |
153 | 217 |
154 #if defined(OS_MACOSX) | 218 #if defined(OS_MACOSX) |
155 base::mac::SetOverrideAppBundle(NULL); | 219 base::mac::SetOverrideAppBundle(NULL); |
156 #endif | 220 #endif |
157 | 221 |
158 // Tear down shared StatsTable; prevents unit_tests from leaking it. | |
159 base::StatsTable::set_current(NULL); | 222 base::StatsTable::set_current(NULL); |
160 delete stats_table_; | 223 stats_table_.reset(); |
161 RemoveSharedMemoryFile(stats_filename_); | 224 RemoveSharedMemoryFile(stats_filename_); |
162 | 225 |
163 base::TestSuite::Shutdown(); | 226 base::TestSuite::Shutdown(); |
164 } | 227 } |
OLD | NEW |