summaryrefslogtreecommitdiff
path: root/chromium/media/blink/run_all_unittests.cc
blob: 1e61f7d4cbf4aaaaaaea330c1e31d8076dcee997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/macros.h"
#include "base/test/launcher/unit_test_launcher.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/test_suite.h"
#include "build/build_config.h"
#include "media/base/media.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
#include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
#include "third_party/blink/public/web/blink.h"

#if defined(OS_ANDROID)
#include "media/base/android/media_codec_util.h"
#endif

#if !defined(OS_IOS)
#include "mojo/core/embedder/embedder.h"
#endif

#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
#include "gin/v8_initializer.h"

constexpr gin::V8Initializer::V8SnapshotFileType kSnapshotType =
#if defined(USE_V8_CONTEXT_SNAPSHOT)
    gin::V8Initializer::V8SnapshotFileType::kWithAdditionalContext;
#else
    gin::V8Initializer::V8SnapshotFileType::kDefault;
#endif  // defined(USE_V8_CONTEXT_SNAPSHOT)
#endif  // defined(V8_USE_EXTERNAL_STARTUP_DATA)

// We must use a custom blink::Platform that ensures the main thread scheduler
// knows about the ScopedTaskEnvironment.
class BlinkPlatformWithTaskEnvironment : public blink::Platform {
 public:
  BlinkPlatformWithTaskEnvironment()
      : main_thread_scheduler_(
            blink::scheduler::WebThreadScheduler::CreateMainThreadScheduler()) {
  }

  ~BlinkPlatformWithTaskEnvironment() override {
    main_thread_scheduler_->Shutdown();
  }

  blink::scheduler::WebThreadScheduler* GetMainThreadScheduler() {
    return main_thread_scheduler_.get();
  }

 private:
  base::test::ScopedTaskEnvironment scoped_task_environment_;
  std::unique_ptr<blink::scheduler::WebThreadScheduler> main_thread_scheduler_;

  DISALLOW_COPY_AND_ASSIGN(BlinkPlatformWithTaskEnvironment);
};

class MediaBlinkTestSuite : public base::TestSuite {
 public:
  MediaBlinkTestSuite(int argc, char** argv) : base::TestSuite(argc, argv) {}

 private:
  void Initialize() override {
    base::TestSuite::Initialize();

#if defined(OS_ANDROID)
    if (media::MediaCodecUtil::IsMediaCodecAvailable())
      media::EnablePlatformDecoderSupport();
#endif

    // Run this here instead of main() to ensure an AtExitManager is already
    // present.
    media::InitializeMediaLibrary();

#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
    gin::V8Initializer::LoadV8Snapshot(kSnapshotType);
    gin::V8Initializer::LoadV8Natives();
#endif

#if !defined(OS_IOS)
    // Initialize mojo firstly to enable Blink initialization to use it.
    mojo::core::Init();
#endif

    platform_ = std::make_unique<BlinkPlatformWithTaskEnvironment>();
    blink::Initialize(platform_.get(), &empty_registry_,
                      platform_->GetMainThreadScheduler());
  }

  void Shutdown() override {
    platform_.reset();
    base::TestSuite::Shutdown();
  }

  std::unique_ptr<BlinkPlatformWithTaskEnvironment> platform_;
  service_manager::BinderRegistry empty_registry_;

  DISALLOW_COPY_AND_ASSIGN(MediaBlinkTestSuite);
};

int main(int argc, char** argv) {
  MediaBlinkTestSuite test_suite(argc, argv);
  return base::LaunchUnitTests(
      argc, argv,
      base::BindRepeating(&MediaBlinkTestSuite::Run,
                          base::Unretained(&test_suite)));
}