OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_ | 5 #ifndef CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_ |
6 #define CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_ | 6 #define CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 | 11 |
(...skipping 17 matching lines...) Expand all Loading... |
29 #include "net/base/net_util.h" | 29 #include "net/base/net_util.h" |
30 | 30 |
31 #if defined(OS_MACOSX) | 31 #if defined(OS_MACOSX) |
32 #include "base/mac_util.h" | 32 #include "base/mac_util.h" |
33 #endif | 33 #endif |
34 | 34 |
35 #if defined(OS_POSIX) | 35 #if defined(OS_POSIX) |
36 #include "base/shared_memory.h" | 36 #include "base/shared_memory.h" |
37 #endif | 37 #endif |
38 | 38 |
39 namespace { | 39 static void RemoveSharedMemoryFile(const std::string& filename) { |
40 | |
41 void RemoveSharedMemoryFile(std::string& filename) { | |
42 // Stats uses SharedMemory under the hood. On posix, this results in a file | 40 // Stats uses SharedMemory under the hood. On posix, this results in a file |
43 // on disk. | 41 // on disk. |
44 #if defined(OS_POSIX) | 42 #if defined(OS_POSIX) |
45 base::SharedMemory memory; | 43 base::SharedMemory memory; |
46 memory.Delete(UTF8ToWide(filename)); | 44 memory.Delete(UTF8ToWide(filename)); |
47 #endif | 45 #endif |
48 } | 46 } |
49 | 47 |
50 } // namespace | |
51 | |
52 | |
53 // In many cases it may be not obvious that a test makes a real DNS lookup. | 48 // In many cases it may be not obvious that a test makes a real DNS lookup. |
54 // We generally don't want to rely on external DNS servers for our tests, | 49 // We generally don't want to rely on external DNS servers for our tests, |
55 // so this host resolver procedure catches external queries. | 50 // so this host resolver procedure catches external queries. |
56 class WarningHostResolverProc : public net::HostResolverProc { | 51 class WarningHostResolverProc : public net::HostResolverProc { |
57 public: | 52 public: |
58 WarningHostResolverProc() : HostResolverProc(NULL) {} | 53 WarningHostResolverProc() : HostResolverProc(NULL) {} |
59 | 54 |
60 virtual int Resolve(const std::string& host, | 55 virtual int Resolve(const std::string& host, |
61 net::AddressFamily address_family, | 56 net::AddressFamily address_family, |
62 net::HostResolverFlags host_resolver_flags, | 57 net::HostResolverFlags host_resolver_flags, |
(...skipping 18 matching lines...) Expand all Loading... |
81 | 76 |
82 return ResolveUsingPrevious(host, address_family, host_resolver_flags, | 77 return ResolveUsingPrevious(host, address_family, host_resolver_flags, |
83 addrlist); | 78 addrlist); |
84 } | 79 } |
85 }; | 80 }; |
86 | 81 |
87 class ChromeTestSuite : public TestSuite { | 82 class ChromeTestSuite : public TestSuite { |
88 public: | 83 public: |
89 ChromeTestSuite(int argc, char** argv) | 84 ChromeTestSuite(int argc, char** argv) |
90 : TestSuite(argc, argv), | 85 : TestSuite(argc, argv), |
91 stats_table_(NULL) { | 86 stats_table_(NULL), |
| 87 created_user_data_dir_(false) { |
92 } | 88 } |
93 | 89 |
94 protected: | 90 protected: |
95 | 91 |
96 virtual void Initialize() { | 92 virtual void Initialize() { |
97 base::ScopedNSAutoreleasePool autorelease_pool; | 93 base::ScopedNSAutoreleasePool autorelease_pool; |
98 | 94 |
99 TestSuite::Initialize(); | 95 TestSuite::Initialize(); |
100 | 96 |
101 chrome::RegisterChromeSchemes(); | 97 chrome::RegisterChromeSchemes(); |
102 host_resolver_proc_ = new WarningHostResolverProc(); | 98 host_resolver_proc_ = new WarningHostResolverProc(); |
103 scoped_host_resolver_proc_.Init(host_resolver_proc_.get()); | 99 scoped_host_resolver_proc_.Init(host_resolver_proc_.get()); |
104 | 100 |
105 chrome::RegisterPathProvider(); | 101 chrome::RegisterPathProvider(); |
106 app::RegisterPathProvider(); | 102 app::RegisterPathProvider(); |
107 g_browser_process = new TestingBrowserProcess; | 103 g_browser_process = new TestingBrowserProcess; |
108 | 104 |
109 // Notice a user data override, and otherwise default to using a custom | 105 // Notice a user data override, and otherwise default to using a custom |
110 // user data directory that lives alongside the current app. | 106 // user data directory that lives alongside the current app. |
111 // NOTE: The user data directory will be erased before each UI test that | 107 // NOTE: The user data directory will be erased before each UI test that |
112 // uses it, in order to ensure consistency. | 108 // uses it, in order to ensure consistency. |
113 FilePath user_data_dir = | 109 FilePath user_data_dir = |
114 CommandLine::ForCurrentProcess()->GetSwitchValuePath( | 110 CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
115 switches::kUserDataDir); | 111 switches::kUserDataDir); |
116 if (user_data_dir.empty() && | 112 if (user_data_dir.empty() && |
117 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("chrome_test_"), | 113 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("chrome_test_"), |
118 &user_data_dir)) { | 114 &user_data_dir)) { |
119 user_data_dir = user_data_dir.AppendASCII("test_user_data"); | 115 user_data_dir = user_data_dir.AppendASCII("test_user_data"); |
| 116 created_user_data_dir_ = true; |
120 } | 117 } |
121 if (!user_data_dir.empty()) | 118 if (!user_data_dir.empty()) |
122 PathService::Override(chrome::DIR_USER_DATA, user_data_dir); | 119 PathService::Override(chrome::DIR_USER_DATA, user_data_dir); |
123 | 120 |
124 if (!browser_dir_.empty()) { | 121 if (!browser_dir_.empty()) { |
125 PathService::Override(base::DIR_EXE, browser_dir_); | 122 PathService::Override(base::DIR_EXE, browser_dir_); |
126 PathService::Override(base::DIR_MODULE, browser_dir_); | 123 PathService::Override(base::DIR_MODULE, browser_dir_); |
127 } | 124 } |
128 | 125 |
129 #if defined(OS_MACOSX) | 126 #if defined(OS_MACOSX) |
(...skipping 27 matching lines...) Expand all Loading... |
157 | 154 |
158 delete g_browser_process; | 155 delete g_browser_process; |
159 g_browser_process = NULL; | 156 g_browser_process = NULL; |
160 | 157 |
161 // Tear down shared StatsTable; prevents unit_tests from leaking it. | 158 // Tear down shared StatsTable; prevents unit_tests from leaking it. |
162 StatsTable::set_current(NULL); | 159 StatsTable::set_current(NULL); |
163 delete stats_table_; | 160 delete stats_table_; |
164 RemoveSharedMemoryFile(stats_filename_); | 161 RemoveSharedMemoryFile(stats_filename_); |
165 | 162 |
166 // Delete the test_user_data dir recursively | 163 // Delete the test_user_data dir recursively |
| 164 // NOTE: user_data_dir will be deleted only if it was automatically |
| 165 // created. |
167 FilePath user_data_dir; | 166 FilePath user_data_dir; |
168 if (PathService::Get(chrome::DIR_USER_DATA, &user_data_dir) && | 167 if (created_user_data_dir_ && |
| 168 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir) && |
169 !user_data_dir.empty()) { | 169 !user_data_dir.empty()) { |
170 file_util::Delete(user_data_dir, true); | 170 file_util::Delete(user_data_dir, true); |
171 file_util::Delete(user_data_dir.DirName(), false); | 171 file_util::Delete(user_data_dir.DirName(), false); |
172 } | 172 } |
173 TestSuite::Shutdown(); | 173 TestSuite::Shutdown(); |
174 } | 174 } |
175 | 175 |
176 void SetBrowserDirectory(const FilePath& browser_dir) { | 176 void SetBrowserDirectory(const FilePath& browser_dir) { |
177 browser_dir_ = browser_dir; | 177 browser_dir_ = browser_dir; |
178 } | 178 } |
179 | 179 |
180 StatsTable* stats_table_; | 180 StatsTable* stats_table_; |
181 // The name used for the stats file so it can be cleaned up on posix during | 181 // The name used for the stats file so it can be cleaned up on posix during |
182 // test shutdown. | 182 // test shutdown. |
183 std::string stats_filename_; | 183 std::string stats_filename_; |
184 | 184 |
185 // Alternative path to browser binaries. | 185 // Alternative path to browser binaries. |
186 FilePath browser_dir_; | 186 FilePath browser_dir_; |
187 | 187 |
188 ScopedOleInitializer ole_initializer_; | 188 ScopedOleInitializer ole_initializer_; |
189 scoped_refptr<WarningHostResolverProc> host_resolver_proc_; | 189 scoped_refptr<WarningHostResolverProc> host_resolver_proc_; |
190 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_; | 190 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_; |
| 191 |
| 192 // Flag indicating whether user_data_dir was automatically created or not. |
| 193 bool created_user_data_dir_; |
191 }; | 194 }; |
192 | 195 |
193 #endif // CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_ | 196 #endif // CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_ |
OLD | NEW |