| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/test/unit/chrome_test_suite.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 9 #include "base/metrics/stats_table.h" | |
| 10 #include "base/process_util.h" | |
| 11 #include "base/stringprintf.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/common/chrome_constants.h" | |
| 15 #include "chrome/common/chrome_content_client.h" | |
| 16 #include "chrome/common/chrome_paths.h" | |
| 17 #include "chrome/common/url_constants.h" | |
| 18 #include "chrome/test/testing_browser_process.h" | |
| 19 #include "content/common/content_paths.h" | |
| 20 #include "net/base/net_errors.h" | |
| 21 #include "ui/base/resource/resource_bundle.h" | |
| 22 #include "ui/base/ui_base_paths.h" | |
| 23 #if defined(TOOLKIT_VIEWS) | |
| 24 #include "views/view.h" | |
| 25 #endif | |
| 26 | |
| 27 #if defined(OS_MACOSX) | |
| 28 #include "base/mac/mac_util.h" | |
| 29 #include "content/common/chrome_application_mac.h" | |
| 30 #endif | |
| 31 | |
| 32 #if defined(OS_POSIX) | |
| 33 #include "base/shared_memory.h" | |
| 34 #endif | |
| 35 | |
| 36 static void RemoveSharedMemoryFile(const std::string& filename) { | |
| 37 // Stats uses SharedMemory under the hood. On posix, this results in a file | |
| 38 // on disk. | |
| 39 #if defined(OS_POSIX) | |
| 40 base::SharedMemory memory; | |
| 41 memory.Delete(filename); | |
| 42 #endif | |
| 43 } | |
| 44 | |
| 45 LocalHostResolverProc::LocalHostResolverProc() | |
| 46 : HostResolverProc(NULL) { | |
| 47 } | |
| 48 | |
| 49 LocalHostResolverProc::~LocalHostResolverProc() { | |
| 50 } | |
| 51 | |
| 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 } | |
| 69 | |
| 70 // To avoid depending on external resources and to reduce (if not preclude) | |
| 71 // network interactions from tests, we simulate failure for non-local DNS | |
| 72 // queries, rather than perform them. | |
| 73 // If you really need to make an external DNS query, use | |
| 74 // net::RuleBasedHostResolverProc and its AllowDirectLookup method. | |
| 75 if (!local) { | |
| 76 DVLOG(1) << "To avoid external dependencies, simulating failure for " | |
| 77 "external DNS lookup of " << host; | |
| 78 return net::ERR_NOT_IMPLEMENTED; | |
| 79 } | |
| 80 | |
| 81 return ResolveUsingPrevious(host, address_family, host_resolver_flags, | |
| 82 addrlist, os_error); | |
| 83 } | |
| 84 | |
| 85 ChromeTestSuite::ChromeTestSuite(int argc, char** argv) | |
| 86 : base::TestSuite(argc, argv), | |
| 87 stats_table_(NULL) { | |
| 88 } | |
| 89 | |
| 90 ChromeTestSuite::~ChromeTestSuite() { | |
| 91 } | |
| 92 | |
| 93 void ChromeTestSuite::Initialize() { | |
| 94 #if defined(OS_MACOSX) | |
| 95 chrome_application_mac::RegisterCrApp(); | |
| 96 #endif | |
| 97 | |
| 98 base::mac::ScopedNSAutoreleasePool autorelease_pool; | |
| 99 | |
| 100 base::TestSuite::Initialize(); | |
| 101 | |
| 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(); | |
| 107 host_resolver_proc_ = new LocalHostResolverProc(); | |
| 108 scoped_host_resolver_proc_.Init(host_resolver_proc_.get()); | |
| 109 | |
| 110 chrome::RegisterPathProvider(); | |
| 111 content::RegisterPathProvider(); | |
| 112 ui::RegisterPathProvider(); | |
| 113 g_browser_process = new TestingBrowserProcess; | |
| 114 | |
| 115 if (!browser_dir_.empty()) { | |
| 116 PathService::Override(base::DIR_EXE, browser_dir_); | |
| 117 PathService::Override(base::DIR_MODULE, browser_dir_); | |
| 118 } | |
| 119 | |
| 120 #if defined(OS_MACOSX) | |
| 121 // Look in the framework bundle for resources. | |
| 122 FilePath path; | |
| 123 PathService::Get(base::DIR_EXE, &path); | |
| 124 path = path.Append(chrome::kFrameworkName); | |
| 125 base::mac::SetOverrideAppBundlePath(path); | |
| 126 #endif | |
| 127 | |
| 128 // Force unittests to run using en-US so if we test against string | |
| 129 // output, it'll pass regardless of the system language. | |
| 130 ResourceBundle::InitSharedInstance("en-US"); | |
| 131 FilePath resources_pack_path; | |
| 132 PathService::Get(base::DIR_MODULE, &resources_pack_path); | |
| 133 resources_pack_path = | |
| 134 resources_pack_path.Append(FILE_PATH_LITERAL("resources.pak")); | |
| 135 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); | |
| 136 | |
| 137 // initialize the global StatsTable for unit_tests (make sure the file | |
| 138 // doesn't exist before opening it so the test gets a clean slate) | |
| 139 stats_filename_ = "unit_tests"; | |
| 140 std::string pid_string = base::StringPrintf("-%d", base::GetCurrentProcId()); | |
| 141 stats_filename_ += pid_string; | |
| 142 RemoveSharedMemoryFile(stats_filename_); | |
| 143 stats_table_ = new base::StatsTable(stats_filename_, 20, 200); | |
| 144 base::StatsTable::set_current(stats_table_); | |
| 145 | |
| 146 #if defined(TOOLKIT_VIEWS) && defined(OS_LINUX) | |
| 147 // Turn of GPU compositing in browser during unit tests. | |
| 148 views::View::set_use_acceleration_when_possible(false); | |
| 149 #endif | |
| 150 } | |
| 151 | |
| 152 void ChromeTestSuite::Shutdown() { | |
| 153 ResourceBundle::CleanupSharedInstance(); | |
| 154 | |
| 155 #if defined(OS_MACOSX) | |
| 156 base::mac::SetOverrideAppBundle(NULL); | |
| 157 #endif | |
| 158 | |
| 159 delete g_browser_process; | |
| 160 g_browser_process = NULL; | |
| 161 | |
| 162 // Tear down shared StatsTable; prevents unit_tests from leaking it. | |
| 163 base::StatsTable::set_current(NULL); | |
| 164 delete stats_table_; | |
| 165 RemoveSharedMemoryFile(stats_filename_); | |
| 166 | |
| 167 base::TestSuite::Shutdown(); | |
| 168 } | |
| OLD | NEW |