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/files/file_path.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/metrics/statistics_recorder.h"
9 #include "base/path_service.h"
10 #include "base/test/launcher/unit_test_launcher.h"
11 #include "base/test/test_suite.h"
12 #include "components/content_settings/core/common/content_settings_pattern.h"
13 #include "content/public/test/test_content_client_initializer.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/base/ui_base_paths.h"
17 #include "url/url_util.h"
20 #include "ui/gl/test/gl_surface_test_support.h"
23 #if defined(OS_ANDROID)
24 #include "base/android/jni_android.h"
25 #include "components/invalidation/impl/android/component_jni_registrar.h"
26 #include "components/safe_json/android/component_jni_registrar.h"
27 #include "ui/base/android/ui_base_jni_registrar.h"
28 #include "ui/gfx/android/gfx_jni_registrar.h"
33 class ComponentsTestSuite
: public base::TestSuite
{
35 ComponentsTestSuite(int argc
, char** argv
) : base::TestSuite(argc
, argv
) {}
38 void Initialize() override
{
39 base::TestSuite::Initialize();
41 // Initialize the histograms subsystem, so that any histograms hit in tests
42 // are correctly registered with the statistics recorder and can be queried
44 base::StatisticsRecorder::Initialize();
47 gfx::GLSurfaceTestSupport::InitializeOneOff();
49 #if defined(OS_ANDROID)
50 // Register JNI bindings for android.
51 JNIEnv
* env
= base::android::AttachCurrentThread();
52 ASSERT_TRUE(gfx::android::RegisterJni(env
));
53 ASSERT_TRUE(ui::android::RegisterJni(env
));
54 ASSERT_TRUE(invalidation::android::RegisterInvalidationJni(env
));
55 ASSERT_TRUE(safe_json::android::RegisterSafeJsonJni(env
));
58 ui::RegisterPathProvider();
60 base::FilePath pak_path
;
61 #if defined(OS_ANDROID)
62 PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID
, &pak_path
);
64 PathService::Get(base::DIR_MODULE
, &pak_path
);
67 base::FilePath ui_test_pak_path
;
68 ASSERT_TRUE(PathService::Get(ui::UI_TEST_PAK
, &ui_test_pak_path
));
69 ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path
);
71 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
72 pak_path
.AppendASCII("components_tests_resources.pak"),
73 ui::SCALE_FACTOR_NONE
);
75 // These schemes need to be added globally to pass tests of
76 // autocomplete_input_unittest.cc and content_settings_pattern*
77 url::AddStandardScheme("chrome");
78 url::AddStandardScheme("chrome-extension");
79 url::AddStandardScheme("chrome-devtools");
80 url::AddStandardScheme("chrome-search");
82 // Not using kExtensionScheme to avoid the dependency to extensions.
83 ContentSettingsPattern::SetNonWildcardDomainNonPortScheme(
87 void Shutdown() override
{
88 ui::ResourceBundle::CleanupSharedInstance();
90 base::TestSuite::Shutdown();
93 DISALLOW_COPY_AND_ASSIGN(ComponentsTestSuite
);
96 class ComponentsUnitTestEventListener
: public testing::EmptyTestEventListener
{
98 ComponentsUnitTestEventListener() {}
99 ~ComponentsUnitTestEventListener() override
{}
101 void OnTestStart(const testing::TestInfo
& test_info
) override
{
102 content_initializer_
.reset(new content::TestContentClientInitializer());
105 void OnTestEnd(const testing::TestInfo
& test_info
) override
{
106 content_initializer_
.reset();
110 scoped_ptr
<content::TestContentClientInitializer
> content_initializer_
;
112 DISALLOW_COPY_AND_ASSIGN(ComponentsUnitTestEventListener
);
117 int main(int argc
, char** argv
) {
118 ComponentsTestSuite
test_suite(argc
, argv
);
120 // The listener will set up common test environment for all components unit
122 testing::TestEventListeners
& listeners
=
123 testing::UnitTest::GetInstance()->listeners();
124 listeners
.Append(new ComponentsUnitTestEventListener());
126 return base::LaunchUnitTests(
127 argc
, argv
, base::Bind(&base::TestSuite::Run
,
128 base::Unretained(&test_suite
)));