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 "base/bind.h"
      6 #include "base/memory/scoped_ptr.h"
      7 #include "base/metrics/statistics_recorder.h"
      8 #include "base/path_service.h"
      9 #include "base/test/launcher/unit_test_launcher.h"
     10 #include "base/test/test_suite.h"
     11 #include "components/content_settings/core/common/content_settings_pattern.h"
     12 #include "content/public/test/test_content_client_initializer.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "ui/base/resource/resource_bundle.h"
     15 #include "ui/base/ui_base_paths.h"
     16 #include "url/url_util.h"
     17 
     18 #if defined(OS_MACOSX)
     19 #include "base/mac/bundle_locations.h"
     20 #endif
     21 
     22 #if !defined(OS_IOS)
     23 #include "ui/gl/gl_surface.h"
     24 #endif
     25 
     26 #if defined(OS_ANDROID)
     27 #include "base/android/jni_android.h"
     28 #include "ui/base/android/ui_base_jni_registrar.h"
     29 #include "ui/gfx/android/gfx_jni_registrar.h"
     30 #endif
     31 
     32 namespace {
     33 
     34 class ComponentsTestSuite : public base::TestSuite {
     35  public:
     36   ComponentsTestSuite(int argc, char** argv) : base::TestSuite(argc, argv) {}
     37 
     38  private:
     39   virtual void Initialize() OVERRIDE {
     40     base::TestSuite::Initialize();
     41 
     42     // Initialize the histograms subsystem, so that any histograms hit in tests
     43     // are correctly registered with the statistics recorder and can be queried
     44     // by tests.
     45     base::StatisticsRecorder::Initialize();
     46 
     47 #if !defined(OS_IOS)
     48     gfx::GLSurface::InitializeOneOffForTests();
     49 #endif
     50 #if defined(OS_ANDROID)
     51     // Register JNI bindings for android.
     52     JNIEnv* env = base::android::AttachCurrentThread();
     53     gfx::android::RegisterJni(env);
     54     ui::android::RegisterJni(env);
     55 #endif
     56 
     57 #if defined(OS_MACOSX) && !defined(OS_IOS)
     58     // Look in the framework bundle for resources.
     59     base::FilePath path;
     60     PathService::Get(base::DIR_EXE, &path);
     61 
     62     // TODO(tfarina): This is temporary. The right fix is to write a
     63     // framework-Info.plist and integrate that into the build.
     64     // Hardcode the framework name here to avoid having to depend on chrome's
     65     // common target for chrome::kFrameworkName.
     66 #if defined(GOOGLE_CHROME_BUILD)
     67     path = path.AppendASCII("Google Chrome Framework.framework");
     68 #elif defined(CHROMIUM_BUILD)
     69     path = path.AppendASCII("Chromium Framework.framework");
     70 #else
     71 #error Unknown branding
     72 #endif
     73 
     74     base::mac::SetOverrideFrameworkBundlePath(path);
     75 #endif
     76 
     77     ui::RegisterPathProvider();
     78 
     79     // TODO(tfarina): This should be changed to InitSharedInstanceWithPakFile()
     80     // so we can load our pak file instead of chrome.pak. crbug.com/348563
     81     ui::ResourceBundle::InitSharedInstanceWithLocale(
     82         "en-US", NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
     83     base::FilePath resources_pack_path;
     84     PathService::Get(base::DIR_MODULE, &resources_pack_path);
     85     ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
     86         resources_pack_path.AppendASCII("resources.pak"),
     87         ui::SCALE_FACTOR_NONE);
     88 
     89     // These schemes need to be added globally to pass tests of
     90     // autocomplete_input_unittest.cc and content_settings_pattern*
     91     url::AddStandardScheme("chrome");
     92     url::AddStandardScheme("chrome-extension");
     93     url::AddStandardScheme("chrome-devtools");
     94     url::AddStandardScheme("chrome-search");
     95 
     96     // Not using kExtensionScheme to avoid the dependency to extensions.
     97     ContentSettingsPattern::SetNonWildcardDomainNonPortScheme(
     98         "chrome-extension");
     99   }
    100 
    101   virtual void Shutdown() OVERRIDE {
    102     ui::ResourceBundle::CleanupSharedInstance();
    103 
    104 #if defined(OS_MACOSX) && !defined(OS_IOS)
    105   base::mac::SetOverrideFrameworkBundle(NULL);
    106 #endif
    107 
    108     base::TestSuite::Shutdown();
    109   }
    110 
    111   DISALLOW_COPY_AND_ASSIGN(ComponentsTestSuite);
    112 };
    113 
    114 class ComponentsUnitTestEventListener : public testing::EmptyTestEventListener {
    115  public:
    116   ComponentsUnitTestEventListener() {}
    117   virtual ~ComponentsUnitTestEventListener() {}
    118 
    119   virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
    120     content_initializer_.reset(new content::TestContentClientInitializer());
    121   }
    122 
    123   virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
    124     content_initializer_.reset();
    125   }
    126 
    127  private:
    128   scoped_ptr<content::TestContentClientInitializer> content_initializer_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(ComponentsUnitTestEventListener);
    131 };
    132 
    133 }  // namespace
    134 
    135 int main(int argc, char** argv) {
    136   ComponentsTestSuite test_suite(argc, argv);
    137 
    138   // The listener will set up common test environment for all components unit
    139   // tests.
    140   testing::TestEventListeners& listeners =
    141       testing::UnitTest::GetInstance()->listeners();
    142   listeners.Append(new ComponentsUnitTestEventListener());
    143 
    144   return base::LaunchUnitTests(
    145       argc, argv, base::Bind(&base::TestSuite::Run,
    146                              base::Unretained(&test_suite)));
    147 }
    148