| 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 <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/environment.h" | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/hash_tables.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 14 #include "base/memory/linked_ptr.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/process_util.h" | |
| 17 #include "base/scoped_temp_dir.h" | |
| 18 #include "base/string_number_conversions.h" | |
| 19 #include "base/string_util.h" | |
| 20 #include "base/test/test_suite.h" | |
| 21 #include "base/test/test_timeouts.h" | |
| 22 #include "base/time.h" | |
| 23 #include "base/utf_string_conversions.h" | |
| 24 #include "chrome/common/chrome_switches.h" | |
| 25 #include "chrome/test/base/chrome_test_suite.h" | |
| 26 #include "chrome/test/base/in_process_browser_test.h" | |
| 27 #include "chrome/test/base/test_launcher_utils.h" | |
| 28 #include "net/base/escape.h" | |
| 29 #include "testing/gtest/include/gtest/gtest.h" | |
| 30 | |
| 31 #if defined(OS_WIN) | |
| 32 #include "base/base_switches.h" | |
| 33 #include "chrome/common/chrome_constants.h" | |
| 34 #include "content/common/sandbox_policy.h" | |
| 35 #include "sandbox/src/dep.h" | |
| 36 #include "sandbox/src/sandbox_factory.h" | |
| 37 #include "sandbox/src/sandbox_types.h" | |
| 38 #endif // defined(OS_WIN) | |
| 39 | |
| 40 #if defined(OS_MACOSX) | |
| 41 #include "chrome/browser/chrome_browser_application_mac.h" | |
| 42 #endif // defined(OS_MACOSX) | |
| 43 | |
| 44 #if defined(OS_WIN) | |
| 45 // The entry point signature of chrome.dll. | |
| 46 typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*, wchar_t*); | |
| 47 #endif // defined(OS_WIN) | |
| 48 | |
| 49 namespace { | |
| 50 | |
| 51 const char kGTestFilterFlag[] = "gtest_filter"; | |
| 52 const char kGTestHelpFlag[] = "gtest_help"; | |
| 53 const char kGTestListTestsFlag[] = "gtest_list_tests"; | |
| 54 const char kGTestRepeatFlag[] = "gtest_repeat"; | |
| 55 const char kGTestRunDisabledTestsFlag[] = "gtest_also_run_disabled_tests"; | |
| 56 const char kGTestOutputFlag[] = "gtest_output"; | |
| 57 | |
| 58 const char kSingleProcessTestsFlag[] = "single_process"; | |
| 59 const char kSingleProcessTestsAndChromeFlag[] = "single-process"; | |
| 60 // The following is kept for historical reasons (so people that are used to | |
| 61 // using it don't get surprised). | |
| 62 const char kChildProcessFlag[] = "child"; | |
| 63 | |
| 64 const char kHelpFlag[] = "help"; | |
| 65 | |
| 66 // The environment variable name for the total number of test shards. | |
| 67 static const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS"; | |
| 68 // The environment variable name for the test shard index. | |
| 69 static const char kTestShardIndex[] = "GTEST_SHARD_INDEX"; | |
| 70 | |
| 71 // The default output file for XML output. | |
| 72 static const FilePath::CharType kDefaultOutputFile[] = FILE_PATH_LITERAL( | |
| 73 "test_detail.xml"); | |
| 74 | |
| 75 // Name of the empty test below. | |
| 76 static const char kEmptyTestName[] = "InProcessBrowserTest.Empty"; | |
| 77 | |
| 78 // An empty test (it starts up and shuts down the browser as part of its | |
| 79 // setup and teardown) used to prefetch all of the browser code into memory. | |
| 80 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, Empty) { | |
| 81 } | |
| 82 | |
| 83 // Parses the environment variable var as an Int32. If it is unset, returns | |
| 84 // default_val. If it is set, unsets it then converts it to Int32 before | |
| 85 // returning it. If unsetting or converting to an Int32 fails, print an | |
| 86 // error and exit with failure. | |
| 87 int32 Int32FromEnvOrDie(const char* const var, int32 default_val) { | |
| 88 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 89 std::string str_val; | |
| 90 int32 result; | |
| 91 if (!env->GetVar(var, &str_val)) | |
| 92 return default_val; | |
| 93 if (!env->UnSetVar(var)) { | |
| 94 LOG(ERROR) << "Invalid environment: we could not unset " << var << ".\n"; | |
| 95 exit(EXIT_FAILURE); | |
| 96 } | |
| 97 if (!base::StringToInt(str_val, &result)) { | |
| 98 LOG(ERROR) << "Invalid environment: " << var << " is not an integer.\n"; | |
| 99 exit(EXIT_FAILURE); | |
| 100 } | |
| 101 return result; | |
| 102 } | |
| 103 | |
| 104 // Checks whether sharding is enabled by examining the relevant | |
| 105 // environment variable values. If the variables are present, | |
| 106 // but inconsistent (i.e., shard_index >= total_shards), prints | |
| 107 // an error and exits. | |
| 108 bool ShouldShard(int32* total_shards, int32* shard_index) { | |
| 109 *total_shards = Int32FromEnvOrDie(kTestTotalShards, -1); | |
| 110 *shard_index = Int32FromEnvOrDie(kTestShardIndex, -1); | |
| 111 | |
| 112 if (*total_shards == -1 && *shard_index == -1) { | |
| 113 return false; | |
| 114 } else if (*total_shards == -1 && *shard_index != -1) { | |
| 115 LOG(ERROR) << "Invalid environment variables: you have " | |
| 116 << kTestShardIndex << " = " << *shard_index | |
| 117 << ", but have left " << kTestTotalShards << " unset.\n"; | |
| 118 exit(EXIT_FAILURE); | |
| 119 } else if (*total_shards != -1 && *shard_index == -1) { | |
| 120 LOG(ERROR) << "Invalid environment variables: you have " | |
| 121 << kTestTotalShards << " = " << *total_shards | |
| 122 << ", but have left " << kTestShardIndex << " unset.\n"; | |
| 123 exit(EXIT_FAILURE); | |
| 124 } else if (*shard_index < 0 || *shard_index >= *total_shards) { | |
| 125 LOG(ERROR) << "Invalid environment variables: we require 0 <= " | |
| 126 << kTestShardIndex << " < " << kTestTotalShards | |
| 127 << ", but you have " << kTestShardIndex << "=" << *shard_index | |
| 128 << ", " << kTestTotalShards << "=" << *total_shards << ".\n"; | |
| 129 exit(EXIT_FAILURE); | |
| 130 } | |
| 131 | |
| 132 return *total_shards > 1; | |
| 133 } | |
| 134 | |
| 135 // Given the total number of shards, the shard index, and the test id, returns | |
| 136 // true iff the test should be run on this shard. The test id is some arbitrary | |
| 137 // but unique non-negative integer assigned by this launcher to each test | |
| 138 // method. Assumes that 0 <= shard_index < total_shards, which is first | |
| 139 // verified in ShouldShard(). | |
| 140 bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) { | |
| 141 return (test_id % total_shards) == shard_index; | |
| 142 } | |
| 143 | |
| 144 // A helper class to output results. | |
| 145 // Note: as currently XML is the only supported format by gtest, we don't | |
| 146 // check output format (e.g. "xml:" prefix) here and output an XML file | |
| 147 // unconditionally. | |
| 148 // Note: we don't output per-test-case or total summary info like | |
| 149 // total failed_test_count, disabled_test_count, elapsed_time and so on. | |
| 150 // Only each test (testcase element in the XML) will have the correct | |
| 151 // failed/disabled/elapsed_time information. Each test won't include | |
| 152 // detailed failure messages either. | |
| 153 class ResultsPrinter { | |
| 154 public: | |
| 155 explicit ResultsPrinter(const CommandLine& command_line); | |
| 156 ~ResultsPrinter(); | |
| 157 void OnTestCaseStart(const char* name, int test_count) const; | |
| 158 void OnTestCaseEnd() const; | |
| 159 | |
| 160 void OnTestEnd(const char* name, const char* case_name, bool run, | |
| 161 bool failed, bool failure_ignored, double elapsed_time) const; | |
| 162 private: | |
| 163 FILE* out_; | |
| 164 | |
| 165 DISALLOW_COPY_AND_ASSIGN(ResultsPrinter); | |
| 166 }; | |
| 167 | |
| 168 ResultsPrinter::ResultsPrinter(const CommandLine& command_line) : out_(NULL) { | |
| 169 if (!command_line.HasSwitch(kGTestOutputFlag)) | |
| 170 return; | |
| 171 std::string flag = command_line.GetSwitchValueASCII(kGTestOutputFlag); | |
| 172 size_t colon_pos = flag.find(':'); | |
| 173 FilePath path; | |
| 174 if (colon_pos != std::string::npos) { | |
| 175 FilePath flag_path = command_line.GetSwitchValuePath(kGTestOutputFlag); | |
| 176 FilePath::StringType path_string = flag_path.value(); | |
| 177 path = FilePath(path_string.substr(colon_pos + 1)); | |
| 178 // If the given path ends with '/', consider it is a directory. | |
| 179 // Note: This does NOT check that a directory (or file) actually exists | |
| 180 // (the behavior is same as what gtest does). | |
| 181 if (file_util::EndsWithSeparator(path)) { | |
| 182 FilePath executable = command_line.GetProgram().BaseName(); | |
| 183 path = path.Append(executable.ReplaceExtension( | |
| 184 FilePath::StringType(FILE_PATH_LITERAL("xml")))); | |
| 185 } | |
| 186 } | |
| 187 if (path.value().empty()) | |
| 188 path = FilePath(kDefaultOutputFile); | |
| 189 FilePath dir_name = path.DirName(); | |
| 190 if (!file_util::DirectoryExists(dir_name)) { | |
| 191 LOG(WARNING) << "The output directory does not exist. " | |
| 192 << "Creating the directory: " << dir_name.value(); | |
| 193 // Create the directory if necessary (because the gtest does the same). | |
| 194 file_util::CreateDirectory(dir_name); | |
| 195 } | |
| 196 out_ = file_util::OpenFile(path, "w"); | |
| 197 if (!out_) { | |
| 198 LOG(ERROR) << "Cannot open output file: " | |
| 199 << path.value() << "."; | |
| 200 return; | |
| 201 } | |
| 202 fprintf(out_, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); | |
| 203 fprintf(out_, "<testsuites name=\"AllTests\" tests=\"\" failures=\"\"" | |
| 204 " disabled=\"\" errors=\"\" time=\"\">\n"); | |
| 205 } | |
| 206 | |
| 207 ResultsPrinter::~ResultsPrinter() { | |
| 208 if (!out_) | |
| 209 return; | |
| 210 fprintf(out_, "</testsuites>\n"); | |
| 211 fclose(out_); | |
| 212 } | |
| 213 | |
| 214 void ResultsPrinter::OnTestCaseStart(const char* name, int test_count) const { | |
| 215 if (!out_) | |
| 216 return; | |
| 217 fprintf(out_, " <testsuite name=\"%s\" tests=\"%d\" failures=\"\"" | |
| 218 " disabled=\"\" errors=\"\" time=\"\">\n", name, test_count); | |
| 219 } | |
| 220 | |
| 221 void ResultsPrinter::OnTestCaseEnd() const { | |
| 222 if (!out_) | |
| 223 return; | |
| 224 fprintf(out_, " </testsuite>\n"); | |
| 225 } | |
| 226 | |
| 227 void ResultsPrinter::OnTestEnd(const char* name, | |
| 228 const char* case_name, | |
| 229 bool run, | |
| 230 bool failed, | |
| 231 bool failure_ignored, | |
| 232 double elapsed_time) const { | |
| 233 if (!out_) | |
| 234 return; | |
| 235 fprintf(out_, " <testcase name=\"%s\" status=\"%s\" time=\"%.3f\"" | |
| 236 " classname=\"%s\"", | |
| 237 name, run ? "run" : "notrun", elapsed_time / 1000.0, case_name); | |
| 238 if (!failed) { | |
| 239 fprintf(out_, " />\n"); | |
| 240 return; | |
| 241 } | |
| 242 fprintf(out_, ">\n"); | |
| 243 fprintf(out_, " <failure message=\"\" type=\"\"%s></failure>\n", | |
| 244 failure_ignored ? " ignored=\"true\"" : ""); | |
| 245 fprintf(out_, " </testcase>\n"); | |
| 246 } | |
| 247 | |
| 248 class TestCasePrinterHelper { | |
| 249 public: | |
| 250 TestCasePrinterHelper(const ResultsPrinter& printer, | |
| 251 const char* name, | |
| 252 int total_test_count) | |
| 253 : printer_(printer) { | |
| 254 printer_.OnTestCaseStart(name, total_test_count); | |
| 255 } | |
| 256 ~TestCasePrinterHelper() { | |
| 257 printer_.OnTestCaseEnd(); | |
| 258 } | |
| 259 private: | |
| 260 const ResultsPrinter& printer_; | |
| 261 | |
| 262 DISALLOW_COPY_AND_ASSIGN(TestCasePrinterHelper); | |
| 263 }; | |
| 264 | |
| 265 // For a basic pattern matching for gtest_filter options. (Copied from | |
| 266 // gtest.cc, see the comment below and http://crbug.com/44497) | |
| 267 bool PatternMatchesString(const char* pattern, const char* str) { | |
| 268 switch (*pattern) { | |
| 269 case '\0': | |
| 270 case ':': // Either ':' or '\0' marks the end of the pattern. | |
| 271 return *str == '\0'; | |
| 272 case '?': // Matches any single character. | |
| 273 return *str != '\0' && PatternMatchesString(pattern + 1, str + 1); | |
| 274 case '*': // Matches any string (possibly empty) of characters. | |
| 275 return (*str != '\0' && PatternMatchesString(pattern, str + 1)) || | |
| 276 PatternMatchesString(pattern + 1, str); | |
| 277 default: // Non-special character. Matches itself. | |
| 278 return *pattern == *str && | |
| 279 PatternMatchesString(pattern + 1, str + 1); | |
| 280 } | |
| 281 } | |
| 282 | |
| 283 // TODO(phajdan.jr): Avoid duplicating gtest code. (http://crbug.com/44497) | |
| 284 // For basic pattern matching for gtest_filter options. (Copied from | |
| 285 // gtest.cc) | |
| 286 bool MatchesFilter(const std::string& name, const std::string& filter) { | |
| 287 const char *cur_pattern = filter.c_str(); | |
| 288 for (;;) { | |
| 289 if (PatternMatchesString(cur_pattern, name.c_str())) { | |
| 290 return true; | |
| 291 } | |
| 292 | |
| 293 // Finds the next pattern in the filter. | |
| 294 cur_pattern = strchr(cur_pattern, ':'); | |
| 295 | |
| 296 // Returns if no more pattern can be found. | |
| 297 if (cur_pattern == NULL) { | |
| 298 return false; | |
| 299 } | |
| 300 | |
| 301 // Skips the pattern separater (the ':' character). | |
| 302 cur_pattern++; | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 // Runs test specified by |test_name| in a child process, | |
| 307 // and returns the exit code. | |
| 308 int RunTest(const std::string& test_name, int default_timeout_ms) { | |
| 309 // Some of the below method calls will leak objects if there is no | |
| 310 // autorelease pool in place. | |
| 311 base::mac::ScopedNSAutoreleasePool pool; | |
| 312 | |
| 313 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
| 314 CommandLine new_cmd_line(cmd_line->GetProgram()); | |
| 315 CommandLine::SwitchMap switches = cmd_line->GetSwitches(); | |
| 316 | |
| 317 // Strip out gtest_output flag because otherwise we would overwrite results | |
| 318 // of the previous test. We will generate the final output file later | |
| 319 // in RunTests(). | |
| 320 switches.erase(kGTestOutputFlag); | |
| 321 | |
| 322 // Strip out gtest_repeat flag because we can only run one test in the child | |
| 323 // process (restarting the browser in the same process is illegal after it | |
| 324 // has been shut down and will actually crash). | |
| 325 switches.erase(kGTestRepeatFlag); | |
| 326 | |
| 327 // Strip out user-data-dir if present. We will add it back in again later. | |
| 328 switches.erase(switches::kUserDataDir); | |
| 329 | |
| 330 for (CommandLine::SwitchMap::const_iterator iter = switches.begin(); | |
| 331 iter != switches.end(); ++iter) { | |
| 332 new_cmd_line.AppendSwitchNative((*iter).first, (*iter).second); | |
| 333 } | |
| 334 | |
| 335 // Always enable disabled tests. This method is not called with disabled | |
| 336 // tests unless this flag was specified to the browser test executable. | |
| 337 new_cmd_line.AppendSwitch("gtest_also_run_disabled_tests"); | |
| 338 new_cmd_line.AppendSwitchASCII("gtest_filter", test_name); | |
| 339 new_cmd_line.AppendSwitch(kChildProcessFlag); | |
| 340 | |
| 341 // Do not let the child ignore failures. We need to propagate the | |
| 342 // failure status back to the parent. | |
| 343 new_cmd_line.AppendSwitch(base::TestSuite::kStrictFailureHandling); | |
| 344 | |
| 345 // Create a new user data dir and pass it to the child. | |
| 346 ScopedTempDir temp_dir; | |
| 347 if (!temp_dir.CreateUniqueTempDir() || !temp_dir.IsValid()) { | |
| 348 LOG(ERROR) << "Error creating temp profile directory"; | |
| 349 return false; | |
| 350 } | |
| 351 new_cmd_line.AppendSwitchPath(switches::kUserDataDir, temp_dir.path()); | |
| 352 | |
| 353 // file:// access for ChromeOS. | |
| 354 new_cmd_line.AppendSwitch(switches::kAllowFileAccess); | |
| 355 | |
| 356 const char* browser_wrapper = getenv("BROWSER_WRAPPER"); | |
| 357 if (browser_wrapper) { | |
| 358 #if defined(OS_WIN) | |
| 359 new_cmd_line.PrependWrapper(ASCIIToWide(browser_wrapper)); | |
| 360 #elif defined(OS_POSIX) | |
| 361 new_cmd_line.PrependWrapper(browser_wrapper); | |
| 362 #endif | |
| 363 VLOG(1) << "BROWSER_WRAPPER was set, prefixing command_line with " | |
| 364 << browser_wrapper; | |
| 365 } | |
| 366 | |
| 367 base::ProcessHandle process_handle; | |
| 368 base::LaunchOptions options; | |
| 369 | |
| 370 #if defined(OS_POSIX) | |
| 371 // On POSIX, we launch the test in a new process group with pgid equal to | |
| 372 // its pid. Any child processes that the test may create will inherit the | |
| 373 // same pgid. This way, if the test is abruptly terminated, we can clean up | |
| 374 // any orphaned child processes it may have left behind. | |
| 375 options.new_process_group = true; | |
| 376 #endif | |
| 377 | |
| 378 if (!base::LaunchProcess(new_cmd_line, options, &process_handle)) | |
| 379 return false; | |
| 380 | |
| 381 int timeout_ms = | |
| 382 test_launcher_utils::GetTestTerminationTimeout(test_name, | |
| 383 default_timeout_ms); | |
| 384 | |
| 385 int exit_code = 0; | |
| 386 if (!base::WaitForExitCodeWithTimeout(process_handle, &exit_code, | |
| 387 timeout_ms)) { | |
| 388 LOG(ERROR) << "Test timeout (" << timeout_ms | |
| 389 << " ms) exceeded for " << test_name; | |
| 390 | |
| 391 exit_code = -1; // Set a non-zero exit code to signal a failure. | |
| 392 | |
| 393 // Ensure that the process terminates. | |
| 394 base::KillProcess(process_handle, -1, true); | |
| 395 } | |
| 396 | |
| 397 #if defined(OS_POSIX) | |
| 398 if (exit_code != 0) { | |
| 399 // On POSIX, in case the test does not exit cleanly, either due to a crash | |
| 400 // or due to it timing out, we need to clean up any child processes that | |
| 401 // it might have created. On Windows, child processes are automatically | |
| 402 // cleaned up using JobObjects. | |
| 403 base::KillProcessGroup(process_handle); | |
| 404 } | |
| 405 #endif | |
| 406 | |
| 407 base::CloseProcessHandle(process_handle); | |
| 408 | |
| 409 return exit_code; | |
| 410 } | |
| 411 | |
| 412 bool RunTests(bool should_shard, int total_shards, int shard_index) { | |
| 413 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 414 | |
| 415 DCHECK(!command_line->HasSwitch(kGTestListTestsFlag)); | |
| 416 | |
| 417 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); | |
| 418 | |
| 419 std::string filter = command_line->GetSwitchValueASCII(kGTestFilterFlag); | |
| 420 | |
| 421 // Split --gtest_filter at '-', if there is one, to separate into | |
| 422 // positive filter and negative filter portions. | |
| 423 std::string positive_filter = filter; | |
| 424 std::string negative_filter = ""; | |
| 425 size_t dash_pos = filter.find('-'); | |
| 426 if (dash_pos != std::string::npos) { | |
| 427 positive_filter = filter.substr(0, dash_pos); // Everything up to the dash. | |
| 428 negative_filter = filter.substr(dash_pos + 1); // Everything after the dash. | |
| 429 } | |
| 430 | |
| 431 int num_runnable_tests = 0; | |
| 432 int test_run_count = 0; | |
| 433 int ignored_failure_count = 0; | |
| 434 std::vector<std::string> failed_tests; | |
| 435 | |
| 436 ResultsPrinter printer(*command_line); | |
| 437 for (int i = 0; i < unit_test->total_test_case_count(); ++i) { | |
| 438 const testing::TestCase* test_case = unit_test->GetTestCase(i); | |
| 439 TestCasePrinterHelper helper(printer, test_case->name(), | |
| 440 test_case->total_test_count()); | |
| 441 for (int j = 0; j < test_case->total_test_count(); ++j) { | |
| 442 const testing::TestInfo* test_info = test_case->GetTestInfo(j); | |
| 443 std::string test_name = test_info->test_case_name(); | |
| 444 test_name.append("."); | |
| 445 test_name.append(test_info->name()); | |
| 446 | |
| 447 // Skip our special test so it's not run twice. That confuses | |
| 448 // the log parser. | |
| 449 if (test_name == kEmptyTestName) | |
| 450 continue; | |
| 451 | |
| 452 // Skip disabled tests. | |
| 453 if (test_name.find("DISABLED") != std::string::npos && | |
| 454 !command_line->HasSwitch(kGTestRunDisabledTestsFlag)) { | |
| 455 printer.OnTestEnd(test_info->name(), test_case->name(), | |
| 456 false, false, false, 0); | |
| 457 continue; | |
| 458 } | |
| 459 | |
| 460 // Skip the test that doesn't match the filter string (if given). | |
| 461 if ((!positive_filter.empty() && | |
| 462 !MatchesFilter(test_name, positive_filter)) || | |
| 463 MatchesFilter(test_name, negative_filter)) { | |
| 464 printer.OnTestEnd(test_info->name(), test_case->name(), | |
| 465 false, false, false, 0); | |
| 466 continue; | |
| 467 } | |
| 468 | |
| 469 // Decide if this test should be run. | |
| 470 bool should_run = true; | |
| 471 if (should_shard) { | |
| 472 should_run = ShouldRunTestOnShard(total_shards, shard_index, | |
| 473 num_runnable_tests); | |
| 474 } | |
| 475 num_runnable_tests += 1; | |
| 476 // If sharding is enabled and the test should not be run, skip it. | |
| 477 if (!should_run) { | |
| 478 continue; | |
| 479 } | |
| 480 | |
| 481 base::Time start_time = base::Time::Now(); | |
| 482 ++test_run_count; | |
| 483 int exit_code = RunTest(test_name, TestTimeouts::action_max_timeout_ms()); | |
| 484 if (exit_code == 0) { | |
| 485 // Test passed. | |
| 486 printer.OnTestEnd(test_info->name(), test_case->name(), true, false, | |
| 487 false, | |
| 488 (base::Time::Now() - start_time).InMillisecondsF()); | |
| 489 } else { | |
| 490 failed_tests.push_back(test_name); | |
| 491 | |
| 492 bool ignore_failure = false; | |
| 493 | |
| 494 // -1 exit code means a crash or hang. Never ignore those failures, | |
| 495 // they are serious and should always be visible. | |
| 496 if (exit_code != -1) | |
| 497 ignore_failure = base::TestSuite::ShouldIgnoreFailure(*test_info); | |
| 498 | |
| 499 printer.OnTestEnd(test_info->name(), test_case->name(), true, true, | |
| 500 ignore_failure, | |
| 501 (base::Time::Now() - start_time).InMillisecondsF()); | |
| 502 if (ignore_failure) | |
| 503 ++ignored_failure_count; | |
| 504 } | |
| 505 } | |
| 506 } | |
| 507 | |
| 508 printf("%d test%s run\n", test_run_count, test_run_count > 1 ? "s" : ""); | |
| 509 printf("%d test%s failed (%d ignored)\n", | |
| 510 static_cast<int>(failed_tests.size()), | |
| 511 failed_tests.size() > 1 ? "s" : "", ignored_failure_count); | |
| 512 if (failed_tests.size() - ignored_failure_count == 0) | |
| 513 return true; | |
| 514 | |
| 515 printf("Failing tests:\n"); | |
| 516 for (std::vector<std::string>::const_iterator iter = failed_tests.begin(); | |
| 517 iter != failed_tests.end(); ++iter) { | |
| 518 printf("%s\n", iter->c_str()); | |
| 519 } | |
| 520 | |
| 521 return false; | |
| 522 } | |
| 523 | |
| 524 void PrintUsage() { | |
| 525 fprintf(stdout, | |
| 526 "Runs tests using the gtest framework, each test being run in its own\n" | |
| 527 "process. Any gtest flags can be specified.\n" | |
| 528 " --single_process\n" | |
| 529 " Runs the tests and the launcher in the same process. Useful for \n" | |
| 530 " debugging a specific test in a debugger.\n" | |
| 531 " --single-process\n" | |
| 532 " Same as above, and also runs Chrome in single-process mode.\n" | |
| 533 " --help\n" | |
| 534 " Shows this message.\n" | |
| 535 " --gtest_help\n" | |
| 536 " Shows the gtest help message.\n"); | |
| 537 } | |
| 538 | |
| 539 } // namespace | |
| 540 | |
| 541 int main(int argc, char** argv) { | |
| 542 #if defined(OS_MACOSX) | |
| 543 chrome_browser_application_mac::RegisterBrowserCrApp(); | |
| 544 #endif | |
| 545 | |
| 546 CommandLine::Init(argc, argv); | |
| 547 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 548 | |
| 549 if (command_line->HasSwitch(kHelpFlag)) { | |
| 550 PrintUsage(); | |
| 551 return 0; | |
| 552 } | |
| 553 | |
| 554 // TODO(pkasting): This "single_process vs. single-process" design is terrible | |
| 555 // UI. Instead, there should be some sort of signal flag on the command line, | |
| 556 // with all subsequent arguments passed through to the underlying browser. | |
| 557 if (command_line->HasSwitch(kChildProcessFlag) || | |
| 558 command_line->HasSwitch(kSingleProcessTestsFlag) || | |
| 559 command_line->HasSwitch(kSingleProcessTestsAndChromeFlag) || | |
| 560 command_line->HasSwitch(kGTestListTestsFlag) || | |
| 561 command_line->HasSwitch(kGTestHelpFlag)) { | |
| 562 | |
| 563 #if defined(OS_WIN) | |
| 564 if (command_line->HasSwitch(kChildProcessFlag) || | |
| 565 command_line->HasSwitch(kSingleProcessTestsFlag)) { | |
| 566 // This is the browser process, so setup the sandbox broker. | |
| 567 sandbox::BrokerServices* broker_services = | |
| 568 sandbox::SandboxFactory::GetBrokerServices(); | |
| 569 if (broker_services) { | |
| 570 sandbox::InitBrokerServices(broker_services); | |
| 571 // Precreate the desktop and window station used by the renderers. | |
| 572 sandbox::TargetPolicy* policy = broker_services->CreatePolicy(); | |
| 573 sandbox::ResultCode result = policy->CreateAlternateDesktop(true); | |
| 574 CHECK(sandbox::SBOX_ERROR_FAILED_TO_SWITCH_BACK_WINSTATION != result); | |
| 575 policy->Release(); | |
| 576 } | |
| 577 } | |
| 578 #endif | |
| 579 return ChromeTestSuite(argc, argv).Run(); | |
| 580 } | |
| 581 | |
| 582 #if defined(OS_WIN) | |
| 583 if (command_line->HasSwitch(switches::kProcessType)) { | |
| 584 // This is a child process, call ChromeMain. | |
| 585 FilePath chrome_path(command_line->GetProgram().DirName()); | |
| 586 chrome_path = chrome_path.Append(chrome::kBrowserResourcesDll); | |
| 587 HMODULE dll = LoadLibrary(chrome_path.value().c_str()); | |
| 588 DLL_MAIN entry_point = | |
| 589 reinterpret_cast<DLL_MAIN>(::GetProcAddress(dll, "ChromeMain")); | |
| 590 if (!entry_point) | |
| 591 return -1; | |
| 592 | |
| 593 // Initialize the sandbox services. | |
| 594 sandbox::SandboxInterfaceInfo sandbox_info = {0}; | |
| 595 sandbox_info.target_services = sandbox::SandboxFactory::GetTargetServices(); | |
| 596 return entry_point(GetModuleHandle(NULL), &sandbox_info, GetCommandLineW()); | |
| 597 } | |
| 598 #endif | |
| 599 | |
| 600 int32 total_shards; | |
| 601 int32 shard_index; | |
| 602 bool should_shard = ShouldShard(&total_shards, &shard_index); | |
| 603 | |
| 604 fprintf(stdout, | |
| 605 "Starting tests...\n" | |
| 606 "IMPORTANT DEBUGGING NOTE: each test is run inside its own process.\n" | |
| 607 "For debugging a test inside a debugger, use the\n" | |
| 608 "--gtest_filter=<your_test_name> flag along with either\n" | |
| 609 "--single_process (to run all tests in one launcher/browser process) or\n" | |
| 610 "--single-process (to do the above, and also run Chrome in single-\n" | |
| 611 "process mode).\n"); | |
| 612 | |
| 613 testing::InitGoogleTest(&argc, argv); | |
| 614 TestTimeouts::Initialize(); | |
| 615 | |
| 616 // Make sure the entire browser code is loaded into memory. Reading it | |
| 617 // from disk may be slow on a busy bot, and can easily exceed the default | |
| 618 // timeout causing flaky test failures. Use an empty test that only starts | |
| 619 // and closes a browser with a long timeout to avoid those problems. | |
| 620 RunTest(kEmptyTestName, TestTimeouts::large_test_timeout_ms()); | |
| 621 | |
| 622 int cycles = 1; | |
| 623 if (command_line->HasSwitch(kGTestRepeatFlag)) { | |
| 624 base::StringToInt(command_line->GetSwitchValueASCII(kGTestRepeatFlag), | |
| 625 &cycles); | |
| 626 } | |
| 627 | |
| 628 int exit_code = 0; | |
| 629 while (cycles != 0) { | |
| 630 if (!RunTests(should_shard, total_shards, shard_index)) { | |
| 631 exit_code = 1; | |
| 632 break; | |
| 633 } | |
| 634 | |
| 635 // Special value "-1" means "repeat indefinitely". | |
| 636 if (cycles != -1) | |
| 637 cycles--; | |
| 638 } | |
| 639 return exit_code; | |
| 640 } | |
| OLD | NEW |