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