run_all_unittests.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2020 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 <memory>
  5. #include "base/base_paths.h"
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/files/file_path.h"
  9. #include "base/path_service.h"
  10. #include "base/test/launcher/unit_test_launcher.h"
  11. #include "base/test/task_environment.h"
  12. #include "base/test/test_suite.h"
  13. #include "mojo/core/embedder/embedder.h"
  14. #include "mojo/public/cpp/bindings/binder_map.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "third_party/blink/public/platform/platform.h"
  17. #include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
  18. #include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
  19. #include "third_party/blink/public/web/blink.h"
  20. #include "ui/base/resource/resource_bundle.h"
  21. #include "ui/base/ui_base_paths.h"
  22. #include "v8/include/v8.h"
  23. #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
  24. #include "gin/v8_initializer.h"
  25. #endif
  26. namespace {
  27. #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
  28. constexpr gin::V8SnapshotFileType kSnapshotType =
  29. #if defined(USE_V8_CONTEXT_SNAPSHOT)
  30. gin::V8SnapshotFileType::kWithAdditionalContext;
  31. #else
  32. gin::V8SnapshotFileType::kDefault;
  33. #endif // defined(USE_V8_CONTEXT_SNAPSHOT)
  34. #endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
  35. class BlinkPlatformForTesting : public blink::Platform {
  36. public:
  37. BlinkPlatformForTesting() = default;
  38. BlinkPlatformForTesting(const BlinkPlatformForTesting&) = delete;
  39. BlinkPlatformForTesting& operator=(const BlinkPlatformForTesting&) = delete;
  40. ~BlinkPlatformForTesting() override { main_thread_scheduler_->Shutdown(); }
  41. blink::scheduler::WebThreadScheduler* GetMainThreadScheduler() {
  42. return main_thread_scheduler_.get();
  43. }
  44. private:
  45. base::test::TaskEnvironment task_environment_;
  46. std::unique_ptr<blink::scheduler::WebThreadScheduler> main_thread_scheduler_ =
  47. blink::scheduler::CreateWebMainThreadSchedulerForTests();
  48. };
  49. class PdfTestSuite final : public base::TestSuite {
  50. public:
  51. using TestSuite::TestSuite;
  52. PdfTestSuite(const PdfTestSuite&) = delete;
  53. PdfTestSuite& operator=(const PdfTestSuite&) = delete;
  54. protected:
  55. void Initialize() override {
  56. TestSuite::Initialize();
  57. mojo::core::Init();
  58. #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
  59. gin::V8Initializer::LoadV8Snapshot(kSnapshotType);
  60. #endif
  61. blink::Platform::InitializeBlink();
  62. platform_ = std::make_unique<BlinkPlatformForTesting>();
  63. mojo::BinderMap binders;
  64. blink::Initialize(platform_.get(), &binders,
  65. platform_->GetMainThreadScheduler());
  66. InitializeResourceBundle();
  67. }
  68. void Shutdown() override {
  69. platform_.reset();
  70. ui::ResourceBundle::CleanupSharedInstance();
  71. base::TestSuite::Shutdown();
  72. }
  73. private:
  74. void InitializeResourceBundle() {
  75. ui::RegisterPathProvider();
  76. base::FilePath ui_test_pak_path;
  77. ASSERT_TRUE(base::PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path));
  78. ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path);
  79. base::FilePath pdf_tests_pak_path;
  80. ASSERT_TRUE(base::PathService::Get(base::DIR_ASSETS, &pdf_tests_pak_path));
  81. pdf_tests_pak_path =
  82. pdf_tests_pak_path.AppendASCII("pdf_tests_resources.pak");
  83. ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
  84. pdf_tests_pak_path, ui::kScaleFactorNone);
  85. }
  86. std::unique_ptr<BlinkPlatformForTesting> platform_;
  87. };
  88. } // namespace
  89. int main(int argc, char** argv) {
  90. PdfTestSuite test_suite(argc, argv);
  91. return base::LaunchUnitTests(
  92. argc, argv,
  93. base::BindOnce(&PdfTestSuite::Run, base::Unretained(&test_suite)));
  94. }