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