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.
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 "content/public/test/test_content_client_initializer.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/base/ui_base_paths.h"
16 #if defined(OS_MACOSX)
17 #include "base/mac/bundle_locations.h"
21 #include "ui/gl/gl_surface.h"
24 #if defined(OS_ANDROID)
25 #include "base/android/jni_android.h"
26 #include "ui/base/android/ui_base_jni_registrar.h"
27 #include "ui/gfx/android/gfx_jni_registrar.h"
32 class ComponentsTestSuite : public base::TestSuite {
34 ComponentsTestSuite(int argc, char** argv) : base::TestSuite(argc, argv) {}
37 virtual void Initialize() OVERRIDE {
38 base::TestSuite::Initialize();
40 // Initialize the histograms subsystem, so that any histograms hit in tests
41 // are correctly registered with the statistics recorder and can be queried
43 base::StatisticsRecorder::Initialize();
46 gfx::GLSurface::InitializeOneOffForTests();
48 #if defined(OS_ANDROID)
49 // Register JNI bindings for android.
50 JNIEnv* env = base::android::AttachCurrentThread();
51 gfx::android::RegisterJni(env);
52 ui::android::RegisterJni(env);
55 #if defined(OS_MACOSX) && !defined(OS_IOS)
56 // Look in the framework bundle for resources.
58 PathService::Get(base::DIR_EXE, &path);
60 // TODO(tfarina): This is temporary. The right fix is to write a
61 // framework-Info.plist and integrate that into the build.
62 // Hardcode the framework name here to avoid having to depend on chrome's
63 // common target for chrome::kFrameworkName.
64 #if defined(GOOGLE_CHROME_BUILD)
65 path = path.AppendASCII("Google Chrome Framework.framework");
66 #elif defined(CHROMIUM_BUILD)
67 path = path.AppendASCII("Chromium Framework.framework");
69 #error Unknown branding
72 base::mac::SetOverrideFrameworkBundlePath(path);
75 ui::RegisterPathProvider();
77 // TODO(tfarina): This should be changed to InitSharedInstanceWithPakFile()
78 // so we can load our pak file instead of chrome.pak. crbug.com/348563
79 ui::ResourceBundle::InitSharedInstanceWithLocale(
80 "en-US", NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
81 base::FilePath resources_pack_path;
82 PathService::Get(base::DIR_MODULE, &resources_pack_path);
83 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
84 resources_pack_path.AppendASCII("resources.pak"),
85 ui::SCALE_FACTOR_NONE);
88 virtual void Shutdown() OVERRIDE {
89 ui::ResourceBundle::CleanupSharedInstance();
91 #if defined(OS_MACOSX) && !defined(OS_IOS)
92 base::mac::SetOverrideFrameworkBundle(NULL);
95 base::TestSuite::Shutdown();
98 DISALLOW_COPY_AND_ASSIGN(ComponentsTestSuite);
101 class ComponentsUnitTestEventListener : public testing::EmptyTestEventListener {
103 ComponentsUnitTestEventListener() {}
104 virtual ~ComponentsUnitTestEventListener() {}
106 virtual void OnTestStart(const testing::TestInfo& test_info) OVERRIDE {
107 content_initializer_.reset(new content::TestContentClientInitializer());
110 virtual void OnTestEnd(const testing::TestInfo& test_info) OVERRIDE {
111 content_initializer_.reset();
115 scoped_ptr<content::TestContentClientInitializer> content_initializer_;
117 DISALLOW_COPY_AND_ASSIGN(ComponentsUnitTestEventListener);
122 int main(int argc, char** argv) {
123 ComponentsTestSuite test_suite(argc, argv);
125 // The listener will set up common test environment for all components unit
127 testing::TestEventListeners& listeners =
128 testing::UnitTest::GetInstance()->listeners();
129 listeners.Append(new ComponentsUnitTestEventListener());
131 return base::LaunchUnitTests(
132 argc, argv, base::Bind(&base::TestSuite::Run,
133 base::Unretained(&test_suite)));