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