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 "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"
18 #if defined(OS_MACOSX)
19 #include "base/mac/bundle_locations.h"
23 #include "ui/gl/gl_surface.h"
26 #if defined(OS_ANDROID)
27 #include "base/android/jni_android.h"
28 #include "components/invalidation/android/component_jni_registrar.h"
29 #include "ui/base/android/ui_base_jni_registrar.h"
30 #include "ui/gfx/android/gfx_jni_registrar.h"
35 class ComponentsTestSuite
: public base::TestSuite
{
37 ComponentsTestSuite(int argc
, char** argv
) : base::TestSuite(argc
, argv
) {}
40 void Initialize() override
{
41 base::TestSuite::Initialize();
43 // Initialize the histograms subsystem, so that any histograms hit in tests
44 // are correctly registered with the statistics recorder and can be queried
46 base::StatisticsRecorder::Initialize();
49 gfx::GLSurface::InitializeOneOffForTests();
51 #if defined(OS_ANDROID)
52 // Register JNI bindings for android.
53 JNIEnv
* env
= base::android::AttachCurrentThread();
54 gfx::android::RegisterJni(env
);
55 ui::android::RegisterJni(env
);
56 invalidation::android::RegisterInvalidationJni(env
);
59 #if defined(OS_MACOSX) && !defined(OS_IOS)
60 // Look in the framework bundle for resources.
62 PathService::Get(base::DIR_EXE
, &path
);
64 // TODO(tfarina): This is temporary. The right fix is to write a
65 // framework-Info.plist and integrate that into the build.
66 // Hardcode the framework name here to avoid having to depend on chrome's
67 // common target for chrome::kFrameworkName.
68 #if defined(GOOGLE_CHROME_BUILD)
69 path
= path
.AppendASCII("Google Chrome Framework.framework");
70 #elif defined(CHROMIUM_BUILD)
71 path
= path
.AppendASCII("Chromium Framework.framework");
73 #error Unknown branding
76 base::mac::SetOverrideFrameworkBundlePath(path
);
79 ui::RegisterPathProvider();
81 // TODO(tfarina): This should be changed to InitSharedInstanceWithPakFile()
82 // so we can load our pak file instead of chrome.pak. crbug.com/348563
83 ui::ResourceBundle::InitSharedInstanceWithLocale(
84 "en-US", NULL
, ui::ResourceBundle::LOAD_COMMON_RESOURCES
);
85 base::FilePath resources_pack_path
;
86 PathService::Get(base::DIR_MODULE
, &resources_pack_path
);
87 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
88 resources_pack_path
.AppendASCII("resources.pak"),
89 ui::SCALE_FACTOR_NONE
);
91 // These schemes need to be added globally to pass tests of
92 // autocomplete_input_unittest.cc and content_settings_pattern*
93 url::AddStandardScheme("chrome");
94 url::AddStandardScheme("chrome-extension");
95 url::AddStandardScheme("chrome-devtools");
96 url::AddStandardScheme("chrome-search");
98 // Not using kExtensionScheme to avoid the dependency to extensions.
99 ContentSettingsPattern::SetNonWildcardDomainNonPortScheme(
103 void Shutdown() override
{
104 ui::ResourceBundle::CleanupSharedInstance();
106 #if defined(OS_MACOSX) && !defined(OS_IOS)
107 base::mac::SetOverrideFrameworkBundle(NULL
);
110 base::TestSuite::Shutdown();
113 DISALLOW_COPY_AND_ASSIGN(ComponentsTestSuite
);
116 class ComponentsUnitTestEventListener
: public testing::EmptyTestEventListener
{
118 ComponentsUnitTestEventListener() {}
119 virtual ~ComponentsUnitTestEventListener() {}
121 virtual void OnTestStart(const testing::TestInfo
& test_info
) override
{
122 content_initializer_
.reset(new content::TestContentClientInitializer());
125 virtual void OnTestEnd(const testing::TestInfo
& test_info
) override
{
126 content_initializer_
.reset();
130 scoped_ptr
<content::TestContentClientInitializer
> content_initializer_
;
132 DISALLOW_COPY_AND_ASSIGN(ComponentsUnitTestEventListener
);
137 int main(int argc
, char** argv
) {
138 ComponentsTestSuite
test_suite(argc
, argv
);
140 // The listener will set up common test environment for all components unit
142 testing::TestEventListeners
& listeners
=
143 testing::UnitTest::GetInstance()->listeners();
144 listeners
.Append(new ComponentsUnitTestEventListener());
146 return base::LaunchUnitTests(
147 argc
, argv
, base::Bind(&base::TestSuite::Run
,
148 base::Unretained(&test_suite
)));