| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "content/public/test/test_launcher.h" | 5 #include "content/public/test/test_launcher.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 // that span browser restarts. | 53 // that span browser restarts. |
| 54 const char kPreTestPrefix[] = "PRE_"; | 54 const char kPreTestPrefix[] = "PRE_"; |
| 55 | 55 |
| 56 // Manual tests only run when --run-manual is specified. This allows writing | 56 // Manual tests only run when --run-manual is specified. This allows writing |
| 57 // tests that don't run automatically but are still in the same test binary. | 57 // tests that don't run automatically but are still in the same test binary. |
| 58 // This is useful so that a team that wants to run a few tests doesn't have to | 58 // This is useful so that a team that wants to run a few tests doesn't have to |
| 59 // add a new binary that must be compiled on all builds. | 59 // add a new binary that must be compiled on all builds. |
| 60 const char kManualTestPrefix[] = "MANUAL_"; | 60 const char kManualTestPrefix[] = "MANUAL_"; |
| 61 | 61 |
| 62 TestLauncherDelegate* g_launcher_delegate; | 62 TestLauncherDelegate* g_launcher_delegate; |
| 63 ContentMainParams* g_params; |
| 63 | 64 |
| 64 std::string RemoveAnyPrePrefixes(const std::string& test_name) { | 65 std::string RemoveAnyPrePrefixes(const std::string& test_name) { |
| 65 std::string result(test_name); | 66 std::string result(test_name); |
| 66 ReplaceSubstringsAfterOffset(&result, 0, kPreTestPrefix, std::string()); | 67 ReplaceSubstringsAfterOffset(&result, 0, kPreTestPrefix, std::string()); |
| 67 return result; | 68 return result; |
| 68 } | 69 } |
| 69 | 70 |
| 70 void PrintUsage() { | 71 void PrintUsage() { |
| 71 fprintf(stdout, | 72 fprintf(stdout, |
| 72 "Runs tests using the gtest framework, each batch of tests being\n" | 73 "Runs tests using the gtest framework, each batch of tests being\n" |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 421 |
| 421 // See kManualTestPrefix above. | 422 // See kManualTestPrefix above. |
| 422 const char kRunManualTestsFlag[] = "run-manual"; | 423 const char kRunManualTestsFlag[] = "run-manual"; |
| 423 | 424 |
| 424 const char kSingleProcessTestsFlag[] = "single_process"; | 425 const char kSingleProcessTestsFlag[] = "single_process"; |
| 425 | 426 |
| 426 | 427 |
| 427 TestLauncherDelegate::~TestLauncherDelegate() { | 428 TestLauncherDelegate::~TestLauncherDelegate() { |
| 428 } | 429 } |
| 429 | 430 |
| 430 #if defined(OS_WIN) || defined(OS_LINUX) | |
| 431 bool ShouldRunContentMain() { | |
| 432 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 433 return command_line->HasSwitch(switches::kProcessType) || | |
| 434 command_line->HasSwitch(kLaunchAsBrowser); | |
| 435 return false; | |
| 436 } | |
| 437 | |
| 438 int RunContentMain(int argc, char** argv, | |
| 439 TestLauncherDelegate* launcher_delegate) { | |
| 440 scoped_ptr<ContentMainDelegate> chrome_main_delegate( | |
| 441 launcher_delegate->CreateContentMainDelegate()); | |
| 442 ContentMainParams params(chrome_main_delegate.get()); | |
| 443 | |
| 444 #if defined(OS_WIN) | |
| 445 sandbox::SandboxInterfaceInfo sandbox_info = {0}; | |
| 446 InitializeSandboxInfo(&sandbox_info); | |
| 447 | |
| 448 params.instance = GetModuleHandle(NULL); | |
| 449 params.sandbox_info = &sandbox_info; | |
| 450 #elif defined(OS_LINUX) | |
| 451 params.argc = argc; | |
| 452 params.argv = const_cast<const char**>(argv); | |
| 453 #endif // defined(OS_WIN) | |
| 454 | |
| 455 return ContentMain(params); | |
| 456 } | |
| 457 #endif | |
| 458 | |
| 459 int LaunchTests(TestLauncherDelegate* launcher_delegate, | 431 int LaunchTests(TestLauncherDelegate* launcher_delegate, |
| 460 int default_jobs, | 432 int default_jobs, |
| 461 int argc, | 433 int argc, |
| 462 char** argv) { | 434 char** argv) { |
| 463 DCHECK(!g_launcher_delegate); | 435 DCHECK(!g_launcher_delegate); |
| 464 g_launcher_delegate = launcher_delegate; | 436 g_launcher_delegate = launcher_delegate; |
| 465 | 437 |
| 466 CommandLine::Init(argc, argv); | 438 CommandLine::Init(argc, argv); |
| 467 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 439 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 468 | 440 |
| 469 if (command_line->HasSwitch(kHelpFlag)) { | 441 if (command_line->HasSwitch(kHelpFlag)) { |
| 470 PrintUsage(); | 442 PrintUsage(); |
| 471 return 0; | 443 return 0; |
| 472 } | 444 } |
| 473 | 445 |
| 446 scoped_ptr<ContentMainDelegate> chrome_main_delegate( |
| 447 launcher_delegate->CreateContentMainDelegate()); |
| 448 ContentMainParams params(chrome_main_delegate.get()); |
| 449 |
| 450 #if defined(OS_WIN) |
| 451 sandbox::SandboxInterfaceInfo sandbox_info = {0}; |
| 452 InitializeSandboxInfo(&sandbox_info); |
| 453 |
| 454 params.instance = GetModuleHandle(NULL); |
| 455 params.sandbox_info = &sandbox_info; |
| 456 #elif !defined(OS_ANDROID) |
| 457 params.argc = argc; |
| 458 params.argv = const_cast<const char**>(argv); |
| 459 #endif // defined(OS_WIN) |
| 460 |
| 474 if (command_line->HasSwitch(kSingleProcessTestsFlag) || | 461 if (command_line->HasSwitch(kSingleProcessTestsFlag) || |
| 475 (command_line->HasSwitch(switches::kSingleProcess) && | 462 (command_line->HasSwitch(switches::kSingleProcess) && |
| 476 command_line->HasSwitch(base::kGTestFilterFlag)) || | 463 command_line->HasSwitch(base::kGTestFilterFlag)) || |
| 477 command_line->HasSwitch(base::kGTestListTestsFlag) || | 464 command_line->HasSwitch(base::kGTestListTestsFlag) || |
| 478 command_line->HasSwitch(base::kGTestHelpFlag)) { | 465 command_line->HasSwitch(base::kGTestHelpFlag)) { |
| 479 #if defined(OS_WIN) | 466 g_params = ¶ms; |
| 480 if (command_line->HasSwitch(kSingleProcessTestsFlag)) { | |
| 481 sandbox::SandboxInterfaceInfo sandbox_info; | |
| 482 InitializeSandboxInfo(&sandbox_info); | |
| 483 InitializeSandbox(&sandbox_info); | |
| 484 } | |
| 485 #endif | |
| 486 return launcher_delegate->RunTestSuite(argc, argv); | 467 return launcher_delegate->RunTestSuite(argc, argv); |
| 487 } | 468 } |
| 488 | 469 |
| 489 #if defined(OS_WIN) || defined(OS_LINUX) | 470 #if !defined(OS_ANDROID) |
| 490 if (ShouldRunContentMain()) | 471 if (command_line->HasSwitch(switches::kProcessType) || |
| 491 return RunContentMain(argc, argv, launcher_delegate); | 472 command_line->HasSwitch(kLaunchAsBrowser)) { |
| 473 return ContentMain(params); |
| 474 } |
| 492 #endif | 475 #endif |
| 493 | 476 |
| 494 base::AtExitManager at_exit; | 477 base::AtExitManager at_exit; |
| 495 testing::InitGoogleTest(&argc, argv); | 478 testing::InitGoogleTest(&argc, argv); |
| 496 TestTimeouts::Initialize(); | 479 TestTimeouts::Initialize(); |
| 497 | 480 |
| 498 fprintf(stdout, | 481 fprintf(stdout, |
| 499 "IMPORTANT DEBUGGING NOTE: each test is run inside its own process.\n" | 482 "IMPORTANT DEBUGGING NOTE: each test is run inside its own process.\n" |
| 500 "For debugging a test inside a debugger, use the\n" | 483 "For debugging a test inside a debugger, use the\n" |
| 501 "--gtest_filter=<your_test_name> flag along with either\n" | 484 "--gtest_filter=<your_test_name> flag along with either\n" |
| 502 "--single_process (to run the test in one launcher/browser process) or\n" | 485 "--single_process (to run the test in one launcher/browser process) or\n" |
| 503 "--single-process (to do the above, and also run Chrome in single-" | 486 "--single-process (to do the above, and also run Chrome in single-" |
| 504 "process mode).\n"); | 487 "process mode).\n"); |
| 505 | 488 |
| 506 base::MessageLoopForIO message_loop; | 489 base::MessageLoopForIO message_loop; |
| 507 | 490 |
| 508 // Allow the |launcher_delegate| to modify |default_jobs|. | 491 // Allow the |launcher_delegate| to modify |default_jobs|. |
| 509 launcher_delegate->AdjustDefaultParallelJobs(&default_jobs); | 492 launcher_delegate->AdjustDefaultParallelJobs(&default_jobs); |
| 510 | 493 |
| 511 WrapperTestLauncherDelegate delegate(launcher_delegate); | 494 WrapperTestLauncherDelegate delegate(launcher_delegate); |
| 512 base::TestLauncher launcher(&delegate, default_jobs); | 495 base::TestLauncher launcher(&delegate, default_jobs); |
| 513 bool success = launcher.Run(argc, argv); | 496 bool success = launcher.Run(argc, argv); |
| 514 return (success ? 0 : 1); | 497 return (success ? 0 : 1); |
| 515 } | 498 } |
| 516 | 499 |
| 517 TestLauncherDelegate* GetCurrentTestLauncherDelegate() { | 500 TestLauncherDelegate* GetCurrentTestLauncherDelegate() { |
| 518 return g_launcher_delegate; | 501 return g_launcher_delegate; |
| 519 } | 502 } |
| 520 | 503 |
| 504 ContentMainParams* GetContentMainParams() { |
| 505 return g_params; |
| 506 } |
| 507 |
| 521 } // namespace content | 508 } // namespace content |
| OLD | NEW |