File: components_test_suite.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (176 lines) | stat: -rw-r--r-- 6,232 bytes parent folder | download | duplicates (4)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/test/components_test_suite.h"

#include <memory>
#include <utility>

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/path_service.h"
#include "base/test/launcher/unit_test_launcher.h"
#include "base/test/test_suite.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "components/breadcrumbs/core/breadcrumb_manager.h"
#include "components/breadcrumbs/core/crash_reporter_breadcrumb_observer.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/test/test_switches.h"
#include "mojo/core/embedder/embedder.h"
#include "services/network/public/cpp/features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
#include "url/url_util.h"

#if !BUILDFLAG(USE_BLINK)
#include "components/test/ios_components_test_initializer.h"
#else
#include "content/public/browser/network_service_util.h"
#include "content/public/common/content_client.h"
#include "content/public/test/content_test_suite_base.h"
#include "content/public/test/unittest_test_suite.h"
#include "ui/gl/test/gl_surface_test_support.h"
#endif

namespace {

// Not using kExtensionScheme and kChromeSearchScheme to avoid the dependency
// to extensions and chrome/common.
const char* const kNonWildcardDomainNonPortSchemes[] = {
    "chrome-extension", "chrome-search", "chrome", "chrome-untrusted",
    "devtools", "isolated-app"};

class ComponentsTestSuite : public base::TestSuite {
 public:
  ComponentsTestSuite(int argc, char** argv) : base::TestSuite(argc, argv) {}
  ComponentsTestSuite(const ComponentsTestSuite&) = delete;
  ComponentsTestSuite& operator=(const ComponentsTestSuite&) = delete;

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

    // These schemes need to be added globally to pass tests of
    // autocomplete_input_unittest.cc and content_settings_pattern*
    // TODO(crbug.com/40116981): Move this scheme initialization into the
    //    individual tests that need these schemes.
    url::AddStandardScheme("chrome-extension", url::SCHEME_WITH_HOST);
    url::AddStandardScheme("chrome-search", url::SCHEME_WITH_HOST);
    url::AddStandardScheme("chrome-distiller", url::SCHEME_WITH_HOST);
    url::AddStandardScheme("isolated-app", url::SCHEME_WITH_HOST);

#if BUILDFLAG(USE_BLINK)
    gl::GLSurfaceTestSupport::InitializeOneOff();

    content::ForceInProcessNetworkService();

    // Setup content scheme statics.
    {
      content::ContentClient content_client;
      content::ContentTestSuiteBase::RegisterContentSchemes(&content_client);
    }
#else
    url::AddStandardScheme("chrome", url::SCHEME_WITH_HOST);
    url::AddStandardScheme("chrome-untrusted", url::SCHEME_WITH_HOST);
    url::AddStandardScheme("devtools", url::SCHEME_WITH_HOST);

#endif

    ui::RegisterPathProvider();

    base::FilePath pak_path;
#if BUILDFLAG(IS_ANDROID)
    base::PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_path);
#else
    base::PathService::Get(base::DIR_ASSETS, &pak_path);
#endif

    base::FilePath ui_test_pak_path;
    ASSERT_TRUE(base::PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path));
    ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path);

    ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
        pak_path.AppendASCII("components_tests_resources.pak"),
        ui::kScaleFactorNone);

    ContentSettingsPattern::SetNonWildcardDomainNonPortSchemes(
        kNonWildcardDomainNonPortSchemes,
        std::size(kNonWildcardDomainNonPortSchemes));
  }

  void Shutdown() override {
    ui::ResourceBundle::CleanupSharedInstance();
    base::TestSuite::Shutdown();
  }
};

class ComponentsUnitTestEventListener : public testing::EmptyTestEventListener {
 public:
  ComponentsUnitTestEventListener() = default;
  ComponentsUnitTestEventListener(const ComponentsUnitTestEventListener&) =
      delete;
  ComponentsUnitTestEventListener& operator=(
      const ComponentsUnitTestEventListener&) = delete;
  ~ComponentsUnitTestEventListener() override = default;

#if !BUILDFLAG(USE_BLINK)
  void OnTestStart(const testing::TestInfo& test_info) override {
    ios_initializer_.reset(new IosComponentsTestInitializer());
  }
#endif

  void OnTestEnd(const testing::TestInfo& test_info) override {
    breadcrumbs::BreadcrumbManager::GetInstance().ResetForTesting();
#if !BUILDFLAG(USE_BLINK)
    ios_initializer_.reset();
#endif
  }

#if !BUILDFLAG(USE_BLINK)
 private:
  std::unique_ptr<IosComponentsTestInitializer> ios_initializer_;
#endif
};

}  // namespace

base::RunTestSuiteCallback GetLaunchCallback(int argc, char** argv) {
  auto components_test_suite =
      std::make_unique<ComponentsTestSuite>(argc, argv);

  // In the main test process, Mojo must be initialized as a broker. By
  // default child processes are initialized as non-brokers, but tests may
  // override this by passing kInitializeMojoAsBroker when launching children.
  const auto& cmd = *base::CommandLine::ForCurrentProcess();
  const bool is_test_child = cmd.HasSwitch(switches::kTestChildProcess);
  const bool force_broker = cmd.HasSwitch(switches::kInitializeMojoAsBroker);
  const mojo::core::Configuration mojo_config{
      .is_broker_process = !is_test_child || force_broker,
  };

#if BUILDFLAG(USE_BLINK)
  auto test_suite = std::make_unique<content::UnitTestTestSuite>(
      components_test_suite.release(),
      base::BindRepeating(content::UnitTestTestSuite::CreateTestContentClients),
      mojo_config);
#else
  mojo::core::Init(mojo_config);
#endif

  testing::TestEventListeners& listeners =
      testing::UnitTest::GetInstance()->listeners();
  listeners.Append(new ComponentsUnitTestEventListener());

#if BUILDFLAG(USE_BLINK)
  return base::BindOnce(&content::UnitTestTestSuite::Run,
                        std::move(test_suite));
#else
  return base::BindOnce(&base::TestSuite::Run,
                        std::move(components_test_suite));
#endif
}