1600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang// Use of this source code is governed by a BSD-style license that can be 3600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang// found in the LICENSE file. 4600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang 5600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "base/bind.h" 6600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "base/memory/scoped_ptr.h" 7600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "base/metrics/statistics_recorder.h" 8600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "base/path_service.h" 9600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "base/test/launcher/unit_test_launcher.h" 10600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "base/test/test_suite.h" 11600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "components/content_settings/core/common/content_settings_pattern.h" 12600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "content/public/test/test_content_client_initializer.h" 13600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "testing/gtest/include/gtest/gtest.h" 14600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "ui/base/resource/resource_bundle.h" 15600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "ui/base/ui_base_paths.h" 16600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "url/url_util.h" 17600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang 18600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#if defined(OS_MACOSX) 19600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "base/mac/bundle_locations.h" 20600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#endif 21600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang 22600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#if !defined(OS_IOS) 23600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "ui/gl/gl_surface.h" 24600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#endif 25600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang 26600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#if defined(OS_ANDROID) 27600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "base/android/jni_android.h" 28600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "ui/base/android/ui_base_jni_registrar.h" 29600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#include "ui/gfx/android/gfx_jni_registrar.h" 30600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang#endif 31600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang 32600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wangnamespace { 33600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang 34600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wangclass ComponentsTestSuite : public base::TestSuite { 35600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang public: 36600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang ComponentsTestSuite(int argc, char** argv) : base::TestSuite(argc, argv) {} 37600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang 38600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang private: 39600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang virtual void Initialize() OVERRIDE { 40600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang base::TestSuite::Initialize(); 41600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang 42600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang // Initialize the histograms subsystem, so that any histograms hit in tests 43600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang // are correctly registered with the statistics recorder and can be queried 44600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang // by tests. 45600c7a4bbc7348167293eac928192e695b4ad5baChung-yih Wang 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 114class 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 135int 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