OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <memory> | 5 #include "components/test/components_test_suite.h" |
6 | |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | |
9 #include "base/files/file_path.h" | |
10 #include "base/macros.h" | |
11 #include "base/metrics/statistics_recorder.h" | |
12 #include "base/path_service.h" | |
13 #include "base/test/launcher/unit_test_launcher.h" | |
14 #include "base/test/test_suite.h" | |
15 #include "build/build_config.h" | |
16 #include "components/content_settings/core/common/content_settings_pattern.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "ui/base/resource/resource_bundle.h" | |
19 #include "ui/base/ui_base_paths.h" | |
20 #include "url/url_util.h" | |
21 | |
22 #if !defined(OS_IOS) | |
23 #include "content/public/test/test_content_client_initializer.h" | |
24 #include "content/public/test/unittest_test_suite.h" | |
25 #include "mojo/edk/embedder/embedder.h" | |
26 #include "ui/gl/test/gl_surface_test_support.h" | |
27 #endif | |
28 | |
29 #if defined(OS_ANDROID) | |
30 #include "base/android/jni_android.h" | |
31 #include "components/gcm_driver/instance_id/android/component_jni_registrar.h" | |
32 #include "components/invalidation/impl/android/component_jni_registrar.h" | |
33 #include "components/policy/core/browser/android/component_jni_registrar.h" | |
34 #include "components/safe_json/android/component_jni_registrar.h" | |
35 #include "components/signin/core/browser/android/component_jni_registrar.h" | |
36 #include "content/public/test/test_utils.h" | |
37 #include "net/android/net_jni_registrar.h" | |
38 #include "ui/base/android/ui_base_jni_registrar.h" | |
39 #include "ui/gfx/android/gfx_jni_registrar.h" | |
40 #endif | |
41 | |
42 namespace { | |
43 | |
44 class ComponentsTestSuite : public base::TestSuite { | |
45 public: | |
46 ComponentsTestSuite(int argc, char** argv) : base::TestSuite(argc, argv) {} | |
47 | |
48 private: | |
49 void Initialize() override { | |
50 base::TestSuite::Initialize(); | |
51 | |
52 // Initialize the histograms subsystem, so that any histograms hit in tests | |
53 // are correctly registered with the statistics recorder and can be queried | |
54 // by tests. | |
55 base::StatisticsRecorder::Initialize(); | |
56 | |
57 #if !defined(OS_IOS) | |
58 gl::GLSurfaceTestSupport::InitializeOneOff(); | |
59 #endif | |
60 #if defined(OS_ANDROID) | |
61 // Register JNI bindings for android. | |
62 JNIEnv* env = base::android::AttachCurrentThread(); | |
63 ASSERT_TRUE(content::RegisterJniForTesting(env)); | |
64 ASSERT_TRUE(gfx::android::RegisterJni(env)); | |
65 ASSERT_TRUE(instance_id::android::RegisterInstanceIDJni(env)); | |
66 ASSERT_TRUE(invalidation::android::RegisterInvalidationJni(env)); | |
67 ASSERT_TRUE(policy::android::RegisterPolicy(env)); | |
68 ASSERT_TRUE(safe_json::android::RegisterSafeJsonJni(env)); | |
69 ASSERT_TRUE(signin::android::RegisterSigninJni(env)); | |
70 ASSERT_TRUE(net::android::RegisterJni(env)); | |
71 ASSERT_TRUE(ui::android::RegisterJni(env)); | |
72 #endif | |
73 | |
74 ui::RegisterPathProvider(); | |
75 | |
76 base::FilePath pak_path; | |
77 #if defined(OS_ANDROID) | |
78 PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_path); | |
79 #else | |
80 PathService::Get(base::DIR_MODULE, &pak_path); | |
81 #endif | |
82 | |
83 base::FilePath ui_test_pak_path; | |
84 ASSERT_TRUE(PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path)); | |
85 ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path); | |
86 | |
87 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( | |
88 pak_path.AppendASCII("components_tests_resources.pak"), | |
89 ui::SCALE_FACTOR_NONE); | |
90 | |
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", url::SCHEME_WITHOUT_PORT); | |
94 url::AddStandardScheme("chrome-extension", url::SCHEME_WITHOUT_PORT); | |
95 url::AddStandardScheme("chrome-devtools", url::SCHEME_WITHOUT_PORT); | |
96 url::AddStandardScheme("chrome-search", url::SCHEME_WITHOUT_PORT); | |
97 | |
98 // Not using kExtensionScheme to avoid the dependency to extensions. | |
99 ContentSettingsPattern::SetNonWildcardDomainNonPortScheme( | |
100 "chrome-extension"); | |
101 } | |
102 | |
103 void Shutdown() override { | |
104 ui::ResourceBundle::CleanupSharedInstance(); | |
105 | |
106 base::TestSuite::Shutdown(); | |
107 } | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(ComponentsTestSuite); | |
110 }; | |
111 | |
112 class ComponentsUnitTestEventListener : public testing::EmptyTestEventListener { | |
113 public: | |
114 ComponentsUnitTestEventListener() {} | |
115 ~ComponentsUnitTestEventListener() override {} | |
116 | |
117 void OnTestStart(const testing::TestInfo& test_info) override { | |
118 #if !defined(OS_IOS) | |
119 content_initializer_.reset(new content::TestContentClientInitializer()); | |
120 #endif | |
121 } | |
122 | |
123 void OnTestEnd(const testing::TestInfo& test_info) override { | |
124 #if !defined(OS_IOS) | |
125 content_initializer_.reset(); | |
126 #endif | |
127 } | |
128 | |
129 private: | |
130 #if !defined(OS_IOS) | |
131 std::unique_ptr<content::TestContentClientInitializer> content_initializer_; | |
132 #endif | |
133 | |
134 DISALLOW_COPY_AND_ASSIGN(ComponentsUnitTestEventListener); | |
135 }; | |
136 | |
137 } // namespace | |
138 | 6 |
139 int main(int argc, char** argv) { | 7 int main(int argc, char** argv) { |
140 #if !defined(OS_IOS) | 8 return base::LaunchUnitTests(argc, argv, GetLaunchCallback(argc, argv)); |
141 content::UnitTestTestSuite test_suite(new ComponentsTestSuite(argc, argv)); | |
142 #else | |
143 ComponentsTestSuite test_suite(argc, argv); | |
144 #endif | |
145 | |
146 // The listener will set up common test environment for all components unit | |
147 // tests. | |
148 testing::TestEventListeners& listeners = | |
149 testing::UnitTest::GetInstance()->listeners(); | |
150 listeners.Append(new ComponentsUnitTestEventListener()); | |
151 | |
152 #if !defined(OS_IOS) | |
153 mojo::edk::Init(); | |
154 return base::LaunchUnitTests(argc, argv, | |
155 base::Bind(&content::UnitTestTestSuite::Run, | |
156 base::Unretained(&test_suite))); | |
157 #else | |
158 return base::LaunchUnitTests( | |
159 argc, argv, base::Bind(&base::TestSuite::Run, | |
160 base::Unretained(&test_suite))); | |
161 #endif | |
162 } | 9 } |
OLD | NEW |