run_all_unittests.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2014 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. #include "base/bind.h"
  5. #include "base/files/file_path.h"
  6. #include "base/path_service.h"
  7. #include "base/test/launcher/unit_test_launcher.h"
  8. #include "base/test/test_suite.h"
  9. #include "build/build_config.h"
  10. #include "mojo/core/embedder/embedder.h"
  11. #include "ui/base/resource/resource_bundle.h"
  12. #include "ui/base/ui_base_paths.h"
  13. #if BUILDFLAG(IS_MAC)
  14. #include "base/mac/bundle_locations.h"
  15. #include "base/test/mock_chrome_application_mac.h"
  16. #endif
  17. #if BUILDFLAG(IS_WIN)
  18. #include "ui/display/win/dpi.h"
  19. #endif
  20. namespace {
  21. class UIBaseTestSuite : public base::TestSuite {
  22. public:
  23. UIBaseTestSuite(int argc, char** argv);
  24. UIBaseTestSuite(const UIBaseTestSuite&) = delete;
  25. UIBaseTestSuite& operator=(const UIBaseTestSuite&) = delete;
  26. protected:
  27. // base::TestSuite:
  28. void Initialize() override;
  29. void Shutdown() override;
  30. };
  31. UIBaseTestSuite::UIBaseTestSuite(int argc, char** argv)
  32. : base::TestSuite(argc, argv) {}
  33. void UIBaseTestSuite::Initialize() {
  34. base::TestSuite::Initialize();
  35. #if BUILDFLAG(IS_WIN)
  36. display::win::SetDefaultDeviceScaleFactor(1.0);
  37. #endif
  38. ui::RegisterPathProvider();
  39. #if BUILDFLAG(IS_MAC)
  40. base::FilePath exe_path;
  41. base::PathService::Get(base::DIR_EXE, &exe_path);
  42. mock_cr_app::RegisterMockCrApp();
  43. // On Mac, a test Framework bundle is created that links locale.pak and
  44. // chrome_100_percent.pak at the appropriate places to ui_test.pak.
  45. base::mac::SetOverrideFrameworkBundlePath(
  46. exe_path.AppendASCII("ui_unittests Framework.framework"));
  47. ui::ResourceBundle::InitSharedInstanceWithLocale(
  48. "en-US", NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
  49. #elif BUILDFLAG(IS_IOS) || BUILDFLAG(IS_ANDROID)
  50. // On iOS, the ui_base_unittests binary is itself a mini bundle, with
  51. // resources built in. On Android, ui_base_unittests_apk provides the
  52. // necessary framework.
  53. ui::ResourceBundle::InitSharedInstanceWithLocale(
  54. "en-US", NULL, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
  55. #else
  56. // On other platforms, the (hardcoded) paths for chrome_100_percent.pak and
  57. // locale.pak get populated by later build steps. To avoid clobbering them,
  58. // load the test .pak files directly.
  59. base::FilePath assets_path;
  60. base::PathService::Get(base::DIR_ASSETS, &assets_path);
  61. ui::ResourceBundle::InitSharedInstanceWithPakPath(
  62. assets_path.AppendASCII("ui_test.pak"));
  63. // ui_base_unittests can't depend on the locales folder which Chrome will make
  64. // later, so use the path created by ui_test_pak.
  65. base::PathService::Override(ui::DIR_LOCALES, assets_path.AppendASCII("ui"));
  66. #endif
  67. base::FilePath dir_resources;
  68. bool result;
  69. #if BUILDFLAG(IS_ANDROID)
  70. result =
  71. base::PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &dir_resources);
  72. #elif BUILDFLAG(IS_APPLE)
  73. result = base::PathService::Get(base::DIR_MODULE, &dir_resources);
  74. #else
  75. dir_resources = assets_path;
  76. result = true;
  77. #endif
  78. DCHECK(result);
  79. base::FilePath ui_base_test_resources_pak =
  80. dir_resources.Append(FILE_PATH_LITERAL("ui_base_test_resources.pak"));
  81. ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
  82. ui_base_test_resources_pak, ui::kScaleFactorNone);
  83. }
  84. void UIBaseTestSuite::Shutdown() {
  85. ui::ResourceBundle::CleanupSharedInstance();
  86. #if BUILDFLAG(IS_MAC)
  87. base::mac::SetOverrideFrameworkBundle(NULL);
  88. #endif
  89. base::TestSuite::Shutdown();
  90. }
  91. } // namespace
  92. int main(int argc, char** argv) {
  93. UIBaseTestSuite test_suite(argc, argv);
  94. mojo::core::Init();
  95. return base::LaunchUnitTests(
  96. argc, argv,
  97. base::BindOnce(&UIBaseTestSuite::Run, base::Unretained(&test_suite)));
  98. }