| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/test/unit/chrome_test_suite.h" |
| 6 |
| 7 #include "app/app_paths.h" |
| 8 #include "app/resource_bundle.h" |
| 9 #include "base/command_line.h" |
| 10 #include "base/process_util.h" |
| 11 #include "base/scoped_nsautorelease_pool.h" |
| 12 #include "base/stats_table.h" |
| 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/common/chrome_constants.h" |
| 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "chrome/common/url_constants.h" |
| 19 #include "chrome/test/testing_browser_process.h" |
| 20 |
| 21 #if defined(OS_MACOSX) |
| 22 #include "base/mac_util.h" |
| 23 #endif |
| 24 |
| 25 #if defined(OS_POSIX) |
| 26 #include "base/shared_memory.h" |
| 27 #endif |
| 28 |
| 29 static void RemoveSharedMemoryFile(const std::string& filename) { |
| 30 // Stats uses SharedMemory under the hood. On posix, this results in a file |
| 31 // on disk. |
| 32 #if defined(OS_POSIX) |
| 33 base::SharedMemory memory; |
| 34 memory.Delete(UTF8ToWide(filename)); |
| 35 #endif |
| 36 } |
| 37 |
| 38 WarningHostResolverProc::WarningHostResolverProc() |
| 39 : HostResolverProc(NULL) { |
| 40 } |
| 41 |
| 42 WarningHostResolverProc::~WarningHostResolverProc() { |
| 43 } |
| 44 |
| 45 int WarningHostResolverProc::Resolve(const std::string& host, |
| 46 net::AddressFamily address_family, |
| 47 net::HostResolverFlags host_resolver_flags, |
| 48 net::AddressList* addrlist, |
| 49 int* os_error) { |
| 50 const char* kLocalHostNames[] = {"localhost", "127.0.0.1"}; |
| 51 bool local = false; |
| 52 |
| 53 if (host == net::GetHostName()) { |
| 54 local = true; |
| 55 } else { |
| 56 for (size_t i = 0; i < arraysize(kLocalHostNames); i++) |
| 57 if (host == kLocalHostNames[i]) { |
| 58 local = true; |
| 59 break; |
| 60 } |
| 61 } |
| 62 |
| 63 // Make the test fail so it's harder to ignore. |
| 64 // If you really need to make real DNS query, use |
| 65 // net::RuleBasedHostResolverProc and its AllowDirectLookup method. |
| 66 EXPECT_TRUE(local) << "Making external DNS lookup of " << host; |
| 67 |
| 68 return ResolveUsingPrevious(host, address_family, host_resolver_flags, |
| 69 addrlist, os_error); |
| 70 } |
| 71 |
| 72 ChromeTestSuite::ChromeTestSuite(int argc, char** argv) |
| 73 : base::TestSuite(argc, argv), |
| 74 stats_table_(NULL), |
| 75 created_user_data_dir_(false) { |
| 76 } |
| 77 |
| 78 ChromeTestSuite::~ChromeTestSuite() { |
| 79 } |
| 80 |
| 81 void ChromeTestSuite::Initialize() { |
| 82 base::ScopedNSAutoreleasePool autorelease_pool; |
| 83 |
| 84 base::TestSuite::Initialize(); |
| 85 |
| 86 chrome::RegisterChromeSchemes(); |
| 87 host_resolver_proc_ = new WarningHostResolverProc(); |
| 88 scoped_host_resolver_proc_.Init(host_resolver_proc_.get()); |
| 89 |
| 90 chrome::RegisterPathProvider(); |
| 91 app::RegisterPathProvider(); |
| 92 g_browser_process = new TestingBrowserProcess; |
| 93 |
| 94 // Notice a user data override, and otherwise default to using a custom |
| 95 // user data directory that lives alongside the current app. |
| 96 // NOTE: The user data directory will be erased before each UI test that |
| 97 // uses it, in order to ensure consistency. |
| 98 FilePath user_data_dir = |
| 99 CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 100 switches::kUserDataDir); |
| 101 if (user_data_dir.empty() && |
| 102 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("chrome_test_"), |
| 103 &user_data_dir)) { |
| 104 user_data_dir = user_data_dir.AppendASCII("test_user_data"); |
| 105 created_user_data_dir_ = true; |
| 106 } |
| 107 if (!user_data_dir.empty()) |
| 108 PathService::Override(chrome::DIR_USER_DATA, user_data_dir); |
| 109 |
| 110 if (!browser_dir_.empty()) { |
| 111 PathService::Override(base::DIR_EXE, browser_dir_); |
| 112 PathService::Override(base::DIR_MODULE, browser_dir_); |
| 113 } |
| 114 |
| 115 #if defined(OS_MACOSX) |
| 116 // Look in the framework bundle for resources. |
| 117 FilePath path; |
| 118 PathService::Get(base::DIR_EXE, &path); |
| 119 path = path.Append(chrome::kFrameworkName); |
| 120 mac_util::SetOverrideAppBundlePath(path); |
| 121 #endif |
| 122 |
| 123 // Force unittests to run using en-US so if we test against string |
| 124 // output, it'll pass regardless of the system language. |
| 125 ResourceBundle::InitSharedInstance("en-US"); |
| 126 |
| 127 // initialize the global StatsTable for unit_tests (make sure the file |
| 128 // doesn't exist before opening it so the test gets a clean slate) |
| 129 stats_filename_ = "unit_tests"; |
| 130 std::string pid_string = StringPrintf("-%d", base::GetCurrentProcId()); |
| 131 stats_filename_ += pid_string; |
| 132 RemoveSharedMemoryFile(stats_filename_); |
| 133 stats_table_ = new StatsTable(stats_filename_, 20, 200); |
| 134 StatsTable::set_current(stats_table_); |
| 135 } |
| 136 |
| 137 void ChromeTestSuite::Shutdown() { |
| 138 ResourceBundle::CleanupSharedInstance(); |
| 139 |
| 140 #if defined(OS_MACOSX) |
| 141 mac_util::SetOverrideAppBundle(NULL); |
| 142 #endif |
| 143 |
| 144 delete g_browser_process; |
| 145 g_browser_process = NULL; |
| 146 |
| 147 // Tear down shared StatsTable; prevents unit_tests from leaking it. |
| 148 StatsTable::set_current(NULL); |
| 149 delete stats_table_; |
| 150 RemoveSharedMemoryFile(stats_filename_); |
| 151 |
| 152 // Delete the test_user_data dir recursively |
| 153 // NOTE: user_data_dir will be deleted only if it was automatically |
| 154 // created. |
| 155 FilePath user_data_dir; |
| 156 if (created_user_data_dir_ && |
| 157 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir) && |
| 158 !user_data_dir.empty()) { |
| 159 file_util::Delete(user_data_dir, true); |
| 160 file_util::Delete(user_data_dir.DirName(), false); |
| 161 } |
| 162 base::TestSuite::Shutdown(); |
| 163 } |
| OLD | NEW |