[email protected] | 4a5b5695 | 2010-04-27 04:55:06 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #ifndef CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_ |
| 6 | #define CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | 4b68e0d | 2009-05-02 00:56:57 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[email protected] | 0044b810 | 2009-09-29 23:09:40 | [diff] [blame] | 11 | #include "build/build_config.h" |
| 12 | |
[email protected] | 94060c1 | 2009-05-15 22:26:16 | [diff] [blame] | 13 | #include "app/app_paths.h" |
[email protected] | 9929da9 | 2009-05-05 02:05:11 | [diff] [blame] | 14 | #include "app/resource_bundle.h" |
[email protected] | 033704f | 2010-06-25 02:17:44 | [diff] [blame] | 15 | #include "base/stats_table.h" |
[email protected] | 0d6335f361 | 2009-05-11 05:32:31 | [diff] [blame] | 16 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "base/path_service.h" |
[email protected] | 5e05717 | 2009-03-27 16:22:26 | [diff] [blame] | 18 | #include "base/ref_counted.h" |
[email protected] | f66c110c | 2008-12-05 20:26:29 | [diff] [blame] | 19 | #include "base/scoped_nsautorelease_pool.h" |
[email protected] | fb895c6 | 2009-10-09 18:20:30 | [diff] [blame] | 20 | #include "base/test/test_suite.h" |
[email protected] | 4bf4135 | 2010-03-08 21:21:36 | [diff] [blame] | 21 | #include "base/utf_string_conversions.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 22 | #include "chrome/app/scoped_ole_initializer.h" |
[email protected] | 6f2660e7 | 2008-12-08 14:44:44 | [diff] [blame] | 23 | #include "chrome/browser/browser_process.h" |
[email protected] | 6a271dba | 2009-10-08 20:03:54 | [diff] [blame] | 24 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | #include "chrome/common/chrome_paths.h" |
| 26 | #include "chrome/common/chrome_switches.h" |
[email protected] | 7fc13ed | 2010-03-06 05:06:20 | [diff] [blame] | 27 | #include "chrome/common/url_constants.h" |
[email protected] | 6f2660e7 | 2008-12-08 14:44:44 | [diff] [blame] | 28 | #include "chrome/test/testing_browser_process.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 29 | #include "net/base/mock_host_resolver.h" |
[email protected] | 7844fc5 | 2009-04-07 08:58:48 | [diff] [blame] | 30 | #include "net/base/net_util.h" |
[email protected] | 5e05717 | 2009-03-27 16:22:26 | [diff] [blame] | 31 | |
[email protected] | 7ba0e1a | 2009-12-30 14:14:01 | [diff] [blame] | 32 | #if defined(OS_MACOSX) |
| 33 | #include "base/mac_util.h" |
| 34 | #endif |
| 35 | |
| 36 | #if defined(OS_POSIX) |
| 37 | #include "base/shared_memory.h" |
| 38 | #endif |
| 39 | |
[email protected] | 4a5b5695 | 2010-04-27 04:55:06 | [diff] [blame] | 40 | static void RemoveSharedMemoryFile(const std::string& filename) { |
[email protected] | 7ba0e1a | 2009-12-30 14:14:01 | [diff] [blame] | 41 | // Stats uses SharedMemory under the hood. On posix, this results in a file |
| 42 | // on disk. |
| 43 | #if defined(OS_POSIX) |
| 44 | base::SharedMemory memory; |
| 45 | memory.Delete(UTF8ToWide(filename)); |
| 46 | #endif |
| 47 | } |
| 48 | |
[email protected] | 5e05717 | 2009-03-27 16:22:26 | [diff] [blame] | 49 | // In many cases it may be not obvious that a test makes a real DNS lookup. |
| 50 | // We generally don't want to rely on external DNS servers for our tests, |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 51 | // so this host resolver procedure catches external queries. |
| 52 | class WarningHostResolverProc : public net::HostResolverProc { |
[email protected] | 5e05717 | 2009-03-27 16:22:26 | [diff] [blame] | 53 | public: |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 54 | WarningHostResolverProc() : HostResolverProc(NULL) {} |
| 55 | |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 56 | virtual int Resolve(const std::string& host, |
| 57 | net::AddressFamily address_family, |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 58 | net::HostResolverFlags host_resolver_flags, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 59 | net::AddressList* addrlist, |
| 60 | int* os_error) { |
[email protected] | 5e05717 | 2009-03-27 16:22:26 | [diff] [blame] | 61 | const char* kLocalHostNames[] = {"localhost", "127.0.0.1"}; |
| 62 | bool local = false; |
[email protected] | 7844fc5 | 2009-04-07 08:58:48 | [diff] [blame] | 63 | |
| 64 | if (host == net::GetHostName()) { |
| 65 | local = true; |
| 66 | } else { |
| 67 | for (size_t i = 0; i < arraysize(kLocalHostNames); i++) |
| 68 | if (host == kLocalHostNames[i]) { |
| 69 | local = true; |
| 70 | break; |
| 71 | } |
| 72 | } |
[email protected] | 5e05717 | 2009-03-27 16:22:26 | [diff] [blame] | 73 | |
| 74 | // Make the test fail so it's harder to ignore. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 75 | // If you really need to make real DNS query, use |
| 76 | // net::RuleBasedHostResolverProc and its AllowDirectLookup method. |
[email protected] | 5e05717 | 2009-03-27 16:22:26 | [diff] [blame] | 77 | EXPECT_TRUE(local) << "Making external DNS lookup of " << host; |
| 78 | |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 79 | return ResolveUsingPrevious(host, address_family, host_resolver_flags, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 80 | addrlist, os_error); |
[email protected] | 5e05717 | 2009-03-27 16:22:26 | [diff] [blame] | 81 | } |
| 82 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | |
[email protected] | 85f6335 | 2010-08-09 17:32:32 | [diff] [blame] | 84 | class ChromeTestSuite : public TestSuite { |
[email protected] | 4b68e0d | 2009-05-02 00:56:57 | [diff] [blame] | 85 | public: |
[email protected] | 0044b810 | 2009-09-29 23:09:40 | [diff] [blame] | 86 | ChromeTestSuite(int argc, char** argv) |
[email protected] | 85f6335 | 2010-08-09 17:32:32 | [diff] [blame] | 87 | : TestSuite(argc, argv), |
[email protected] | 4a5b5695 | 2010-04-27 04:55:06 | [diff] [blame] | 88 | stats_table_(NULL), |
| 89 | created_user_data_dir_(false) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 09ac4c43 | 2010-06-25 01:46:20 | [diff] [blame] | 92 | protected: |
[email protected] | 033704f | 2010-06-25 02:17:44 | [diff] [blame] | 93 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | virtual void Initialize() { |
[email protected] | f66c110c | 2008-12-05 20:26:29 | [diff] [blame] | 95 | base::ScopedNSAutoreleasePool autorelease_pool; |
[email protected] | 6f2660e7 | 2008-12-08 14:44:44 | [diff] [blame] | 96 | |
[email protected] | 85f6335 | 2010-08-09 17:32:32 | [diff] [blame] | 97 | TestSuite::Initialize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | |
[email protected] | 7fc13ed | 2010-03-06 05:06:20 | [diff] [blame] | 99 | chrome::RegisterChromeSchemes(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 100 | host_resolver_proc_ = new WarningHostResolverProc(); |
| 101 | scoped_host_resolver_proc_.Init(host_resolver_proc_.get()); |
[email protected] | 5e05717 | 2009-03-27 16:22:26 | [diff] [blame] | 102 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | chrome::RegisterPathProvider(); |
[email protected] | 94060c1 | 2009-05-15 22:26:16 | [diff] [blame] | 104 | app::RegisterPathProvider(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | g_browser_process = new TestingBrowserProcess; |
| 106 | |
| 107 | // Notice a user data override, and otherwise default to using a custom |
| 108 | // user data directory that lives alongside the current app. |
| 109 | // NOTE: The user data directory will be erased before each UI test that |
| 110 | // uses it, in order to ensure consistency. |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 111 | FilePath user_data_dir = |
| 112 | CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 113 | switches::kUserDataDir); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | if (user_data_dir.empty() && |
[email protected] | 0d6335f361 | 2009-05-11 05:32:31 | [diff] [blame] | 115 | file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("chrome_test_"), |
| 116 | &user_data_dir)) { |
[email protected] | 4b68e0d | 2009-05-02 00:56:57 | [diff] [blame] | 117 | user_data_dir = user_data_dir.AppendASCII("test_user_data"); |
[email protected] | 4a5b5695 | 2010-04-27 04:55:06 | [diff] [blame] | 118 | created_user_data_dir_ = true; |
[email protected] | 0d6335f361 | 2009-05-11 05:32:31 | [diff] [blame] | 119 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | if (!user_data_dir.empty()) |
[email protected] | 53c5804 | 2009-08-26 20:00:14 | [diff] [blame] | 121 | PathService::Override(chrome::DIR_USER_DATA, user_data_dir); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | |
[email protected] | f4f41b9 | 2010-04-02 07:56:27 | [diff] [blame] | 123 | if (!browser_dir_.empty()) { |
[email protected] | 76bb9c3 | 2010-03-09 18:26:23 | [diff] [blame] | 124 | PathService::Override(base::DIR_EXE, browser_dir_); |
[email protected] | f4f41b9 | 2010-04-02 07:56:27 | [diff] [blame] | 125 | PathService::Override(base::DIR_MODULE, browser_dir_); |
| 126 | } |
[email protected] | 76bb9c3 | 2010-03-09 18:26:23 | [diff] [blame] | 127 | |
[email protected] | 6f33add3 | 2009-03-03 16:26:03 | [diff] [blame] | 128 | #if defined(OS_MACOSX) |
[email protected] | e4221ad5 | 2009-10-07 16:42:16 | [diff] [blame] | 129 | // Look in the framework bundle for resources. |
[email protected] | 6f33add3 | 2009-03-03 16:26:03 | [diff] [blame] | 130 | FilePath path; |
| 131 | PathService::Get(base::DIR_EXE, &path); |
[email protected] | 6a271dba | 2009-10-08 20:03:54 | [diff] [blame] | 132 | path = path.Append(chrome::kFrameworkName); |
[email protected] | 6f33add3 | 2009-03-03 16:26:03 | [diff] [blame] | 133 | mac_util::SetOverrideAppBundlePath(path); |
| 134 | #endif |
| 135 | |
[email protected] | d70539de | 2009-06-24 22:17:06 | [diff] [blame] | 136 | // Force unittests to run using en-US so if we test against string |
[email protected] | a4258dbc7 | 2008-08-26 23:52:27 | [diff] [blame] | 137 | // output, it'll pass regardless of the system language. |
[email protected] | ff622aa | 2010-08-04 17:07:46 | [diff] [blame] | 138 | ResourceBundle::InitSharedInstance("en-US"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | |
[email protected] | 7ba0e1a | 2009-12-30 14:14:01 | [diff] [blame] | 140 | // initialize the global StatsTable for unit_tests (make sure the file |
| 141 | // doesn't exist before opening it so the test gets a clean slate) |
| 142 | stats_filename_ = "unit_tests"; |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 143 | std::string pid_string = StringPrintf("-%d", base::GetCurrentProcId()); |
[email protected] | 7ba0e1a | 2009-12-30 14:14:01 | [diff] [blame] | 144 | stats_filename_ += pid_string; |
| 145 | RemoveSharedMemoryFile(stats_filename_); |
| 146 | stats_table_ = new StatsTable(stats_filename_, 20, 200); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | StatsTable::set_current(stats_table_); |
| 148 | } |
| 149 | |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 150 | virtual void Shutdown() { |
| 151 | ResourceBundle::CleanupSharedInstance(); |
| 152 | |
[email protected] | 6f33add3 | 2009-03-03 16:26:03 | [diff] [blame] | 153 | #if defined(OS_MACOSX) |
| 154 | mac_util::SetOverrideAppBundle(NULL); |
| 155 | #endif |
| 156 | |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 157 | delete g_browser_process; |
| 158 | g_browser_process = NULL; |
| 159 | |
| 160 | // Tear down shared StatsTable; prevents unit_tests from leaking it. |
| 161 | StatsTable::set_current(NULL); |
| 162 | delete stats_table_; |
[email protected] | 7ba0e1a | 2009-12-30 14:14:01 | [diff] [blame] | 163 | RemoveSharedMemoryFile(stats_filename_); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 164 | |
[email protected] | 0d6335f361 | 2009-05-11 05:32:31 | [diff] [blame] | 165 | // Delete the test_user_data dir recursively |
[email protected] | 4a5b5695 | 2010-04-27 04:55:06 | [diff] [blame] | 166 | // NOTE: user_data_dir will be deleted only if it was automatically |
| 167 | // created. |
[email protected] | 0d6335f361 | 2009-05-11 05:32:31 | [diff] [blame] | 168 | FilePath user_data_dir; |
[email protected] | 4a5b5695 | 2010-04-27 04:55:06 | [diff] [blame] | 169 | if (created_user_data_dir_ && |
| 170 | PathService::Get(chrome::DIR_USER_DATA, &user_data_dir) && |
[email protected] | 0d6335f361 | 2009-05-11 05:32:31 | [diff] [blame] | 171 | !user_data_dir.empty()) { |
| 172 | file_util::Delete(user_data_dir, true); |
| 173 | file_util::Delete(user_data_dir.DirName(), false); |
| 174 | } |
[email protected] | 85f6335 | 2010-08-09 17:32:32 | [diff] [blame] | 175 | TestSuite::Shutdown(); |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 76bb9c3 | 2010-03-09 18:26:23 | [diff] [blame] | 178 | void SetBrowserDirectory(const FilePath& browser_dir) { |
| 179 | browser_dir_ = browser_dir; |
| 180 | } |
| 181 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | StatsTable* stats_table_; |
[email protected] | 7ba0e1a | 2009-12-30 14:14:01 | [diff] [blame] | 183 | // The name used for the stats file so it can be cleaned up on posix during |
| 184 | // test shutdown. |
| 185 | std::string stats_filename_; |
| 186 | |
[email protected] | 76bb9c3 | 2010-03-09 18:26:23 | [diff] [blame] | 187 | // Alternative path to browser binaries. |
| 188 | FilePath browser_dir_; |
| 189 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 190 | ScopedOleInitializer ole_initializer_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 191 | scoped_refptr<WarningHostResolverProc> host_resolver_proc_; |
| 192 | net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_; |
[email protected] | 4a5b5695 | 2010-04-27 04:55:06 | [diff] [blame] | 193 | |
| 194 | // Flag indicating whether user_data_dir was automatically created or not. |
| 195 | bool created_user_data_dir_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | }; |
| 197 | |
[email protected] | 4b68e0d | 2009-05-02 00:56:57 | [diff] [blame] | 198 | #endif // CHROME_TEST_UNIT_CHROME_TEST_SUITE_H_ |