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 3 // found in the LICENSE file. 4 5 #include "content/public/test/test_launcher.h" 6 7 #include "base/base_paths.h" 8 #include "base/command_line.h" 9 #include "base/logging.h" 10 #include "base/path_service.h" 11 #include "base/sys_info.h" 12 #include "base/test/test_suite.h" 13 #include "content/public/common/content_switches.h" 14 #include "content/public/test/content_test_suite_base.h" 15 #include "content/shell/app/shell_main_delegate.h" 16 #include "content/shell/browser/shell_content_browser_client.h" 17 #include "content/shell/common/shell_content_client.h" 18 #include "content/shell/common/shell_switches.h" 19 #include "testing/gtest/include/gtest/gtest.h" 20 21 #if defined(OS_ANDROID) 22 #include "base/message_loop/message_loop.h" 23 #include "content/public/test/nested_message_pump_android.h" 24 #endif 25 26 #if defined(OS_WIN) 27 #include "content/public/app/startup_helper_win.h" 28 #include "sandbox/win/src/sandbox_types.h" 29 #endif // defined(OS_WIN) 30 31 namespace content { 32 33 class ContentShellTestSuiteInitializer 34 : public testing::EmptyTestEventListener { 35 public: 36 ContentShellTestSuiteInitializer() { 37 } 38 39 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE { 40 content_client_.reset(new ShellContentClient); 41 browser_content_client_.reset(new ShellContentBrowserClient()); 42 SetContentClient(content_client_.get()); 43 SetBrowserClientForTesting(browser_content_client_.get()); 44 } 45 46 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE { 47 #if !defined(OS_ANDROID) 48 // On Android, production code doesn't reset ContentClient during shutdown. 49 // We try to do the same thing as production. Refer to crbug.com/181069. 50 browser_content_client_.reset(); 51 content_client_.reset(); 52 SetContentClient(NULL); 53 #endif 54 } 55 56 private: 57 scoped_ptr<ShellContentClient> content_client_; 58 scoped_ptr<ShellContentBrowserClient> browser_content_client_; 59 60 DISALLOW_COPY_AND_ASSIGN(ContentShellTestSuiteInitializer); 61 }; 62 63 #if defined(OS_ANDROID) 64 base::MessagePump* CreateMessagePumpForUI() { 65 return new NestedMessagePumpAndroid(); 66 }; 67 #endif 68 69 class ContentBrowserTestSuite : public ContentTestSuiteBase { 70 public: 71 ContentBrowserTestSuite(int argc, char** argv) 72 : ContentTestSuiteBase(argc, argv) { 73 } 74 virtual ~ContentBrowserTestSuite() { 75 } 76 77 protected: 78 virtual void Initialize() OVERRIDE { 79 80 #if defined(OS_ANDROID) 81 // This needs to be done before base::TestSuite::Initialize() is called, 82 // as it also tries to set MessagePumpForUIFactory. 83 if (!base::MessageLoop::InitMessagePumpForUIFactory( 84 &CreateMessagePumpForUI)) 85 VLOG(0) << "MessagePumpForUIFactory already set, unable to override."; 86 #endif 87 88 ContentTestSuiteBase::Initialize(); 89 90 testing::TestEventListeners& listeners = 91 testing::UnitTest::GetInstance()->listeners(); 92 listeners.Append(new ContentShellTestSuiteInitializer); 93 } 94 virtual void Shutdown() OVERRIDE { 95 base::TestSuite::Shutdown(); 96 } 97 98 virtual ContentClient* CreateClientForInitialization() OVERRIDE { 99 return new ShellContentClient(); 100 } 101 102 DISALLOW_COPY_AND_ASSIGN(ContentBrowserTestSuite); 103 }; 104 105 class ContentTestLauncherDelegate : public TestLauncherDelegate { 106 public: 107 ContentTestLauncherDelegate() {} 108 virtual ~ContentTestLauncherDelegate() {} 109 110 virtual int RunTestSuite(int argc, char** argv) OVERRIDE { 111 return ContentBrowserTestSuite(argc, argv).Run(); 112 } 113 114 virtual bool AdjustChildProcessCommandLine( 115 CommandLine* command_line, const base::FilePath& temp_data_dir) OVERRIDE { 116 command_line->AppendSwitchPath(switches::kContentShellDataPath, 117 temp_data_dir); 118 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); 119 command_line->AppendSwitch(switches::kUseFakeUIForMediaStream); 120 return true; 121 } 122 123 protected: 124 virtual ContentMainDelegate* CreateContentMainDelegate() OVERRIDE { 125 return new ShellMainDelegate(); 126 } 127 128 private: 129 DISALLOW_COPY_AND_ASSIGN(ContentTestLauncherDelegate); 130 }; 131 132 } // namespace content 133 134 int main(int argc, char** argv) { 135 int default_jobs = std::max(1, base::SysInfo::NumberOfProcessors() / 2); 136 content::ContentTestLauncherDelegate launcher_delegate; 137 return LaunchTests(&launcher_delegate, default_jobs, argc, argv); 138 } 139