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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_TEST_BASE_MIXIN_BASED_IN_PROCESS_BROWSER_TEST_H_
#define CHROME_TEST_BASE_MIXIN_BASED_IN_PROCESS_BROWSER_TEST_H_
#include <concepts>
#include <memory>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/test/base/android/android_browser_test.h"
#else
#include "chrome/test/base/in_process_browser_test.h"
#endif
//
// InProcessBrowserTestMixin enables writing isolated test helpers which depend
// on the standard test lifecycle but should not be test bases.
//
// A new mixin is created by deriving from InProcessBrowserTestMixin and
// overriding methods as needed.
//
// class MyMixin : public InProcessBrowserTestMixin {
// public:
// explicit MyMixin(InProcessBrowserTestMixinHost* host)
// : InProcessBrowserTestMixin(host) {}
// MyMixin(const MyMixin&) = delete;
// MyMixin& operator=(const MyMixin&) = delete;
// ~MyMixin() override = default;
//
// // InProcessBrowserTestMixin:
// void SetUpCommandLine(base::CommandLine* command_line) { /* ... */ }
//
// };
//
//
// To use the mixin, declare it as a member variable on the class and call the
// constructor with the InProcessBrowserTestMixinHost also declared on the class
// (or parent class). The mixin will register itself with the host and the host
// will invoke all registered mixin methods.
//
// For example, here is how to use MixinBasedInProcessBrowserTest:
//
// class SimpleUsage : public MixinBasedInProcessBrowserTest {
// public:
// SimpleUsage() = default;
// SimpleUsage(const SimpleUsage&) = delete;
// SimpleUsage& operator=(const SimpleUsage&) = delete;
// ~SimpleUsage() override = default;
//
// private:
// MyMixin my_mixin_{&mixin_host_};
// SomeOtherMixin some_other_mixin_{&mixin_host_};
// };
//
//
// See WizardInProcessBrowserTest for an example of how to correctly embed a
// mixin host.
//
class InProcessBrowserTestMixinHost;
class PrefService;
// Derive from this type to create a class which depends on the test lifecycle
// without also becoming a test base.
class InProcessBrowserTestMixin {
public:
explicit InProcessBrowserTestMixin(InProcessBrowserTestMixinHost* host);
InProcessBrowserTestMixin(const InProcessBrowserTestMixin&) = delete;
InProcessBrowserTestMixin& operator=(const InProcessBrowserTestMixin&) =
delete;
virtual ~InProcessBrowserTestMixin() = default;
// See InProcessBrowserTest for docs. The call order is:
//
// SetUp
// SetUpCommandLine
// SetUpDefaultCommandLine
// SetUpUserDataDirectory
// SetUpInProcessBrowserTestFixture
// SetUpLocalStatePrefService
// CreatedBrowserMainParts
// SetUpOnMainThread
// TearDownOnMainThread
// PostRunTestOnMainThread
// TearDownInProcessBrowserTestFixture
// TearDown
//
// SetUp is the function which calls SetUpCommandLine,
// SetUpDefaultCommandLine, etc.
virtual void SetUp();
virtual void SetUpCommandLine(base::CommandLine* command_line);
virtual void SetUpDefaultCommandLine(base::CommandLine* command_line);
virtual bool SetUpUserDataDirectory();
virtual void SetUpInProcessBrowserTestFixture();
virtual void SetUpLocalStatePrefService(PrefService* local_state);
virtual void CreatedBrowserMainParts(
content::BrowserMainParts* browser_main_parts);
virtual void SetUpOnMainThread();
virtual void TearDownOnMainThread();
virtual void PostRunTestOnMainThread();
virtual void TearDownInProcessBrowserTestFixture();
virtual void TearDown();
};
// The mixin host executes the callbacks on the mixin instances.
class InProcessBrowserTestMixinHost final {
public:
InProcessBrowserTestMixinHost();
InProcessBrowserTestMixinHost(const InProcessBrowserTestMixinHost&) = delete;
InProcessBrowserTestMixinHost& operator=(
const InProcessBrowserTestMixinHost&) = delete;
~InProcessBrowserTestMixinHost();
void SetUp();
void SetUpCommandLine(base::CommandLine* command_line);
void SetUpDefaultCommandLine(base::CommandLine* command_line);
bool SetUpUserDataDirectory();
void SetUpInProcessBrowserTestFixture();
void SetUpLocalStatePrefService(PrefService* local_state);
void CreatedBrowserMainParts(content::BrowserMainParts* browser_main_parts);
void SetUpOnMainThread();
void TearDownOnMainThread();
void PostRunTestOnMainThread();
void TearDownInProcessBrowserTestFixture();
void TearDown();
private:
// The constructor of InProcessBrowserTestMixin injects itself directly into
// mixins_. This is done instead of an explicit AddMixin to make API usage
// simpler.
friend class InProcessBrowserTestMixin;
std::vector<raw_ptr<InProcessBrowserTestMixin, VectorExperimental>> mixins_;
};
template <typename Fixture>
#if BUILDFLAG(IS_ANDROID)
requires std::derived_from<Fixture, AndroidBrowserTest>
#else
requires std::derived_from<Fixture, InProcessBrowserTest>
#endif
class InProcessBrowserTestMixinHostSupport : public Fixture {
public:
// Inherit ctors from the base class.
using Fixture::Fixture;
// Fixture:
void SetUp() override {
mixin_host_.SetUp();
Fixture::SetUp();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
mixin_host_.SetUpCommandLine(command_line);
Fixture::SetUpCommandLine(command_line);
}
void SetUpDefaultCommandLine(base::CommandLine* command_line) override {
mixin_host_.SetUpDefaultCommandLine(command_line);
Fixture::SetUpDefaultCommandLine(command_line);
}
bool SetUpUserDataDirectory() override {
return mixin_host_.SetUpUserDataDirectory() &&
Fixture::SetUpUserDataDirectory();
}
void SetUpInProcessBrowserTestFixture() override {
mixin_host_.SetUpInProcessBrowserTestFixture();
Fixture::SetUpInProcessBrowserTestFixture();
}
void SetUpLocalStatePrefService(PrefService* local_state) override {
mixin_host_.SetUpLocalStatePrefService(local_state);
Fixture::SetUpLocalStatePrefService(local_state);
}
void CreatedBrowserMainParts(
content::BrowserMainParts* browser_main_parts) override {
mixin_host_.CreatedBrowserMainParts(browser_main_parts);
Fixture::CreatedBrowserMainParts(browser_main_parts);
}
void SetUpOnMainThread() override {
mixin_host_.SetUpOnMainThread();
Fixture::SetUpOnMainThread();
}
void TearDownOnMainThread() override {
mixin_host_.TearDownOnMainThread();
Fixture::TearDownOnMainThread();
}
void PostRunTestOnMainThread() override {
mixin_host_.PostRunTestOnMainThread();
Fixture::PostRunTestOnMainThread();
}
void TearDownInProcessBrowserTestFixture() override {
mixin_host_.TearDownInProcessBrowserTestFixture();
Fixture::TearDownInProcessBrowserTestFixture();
}
void TearDown() override {
mixin_host_.TearDown();
Fixture::TearDown();
}
protected:
InProcessBrowserTestMixinHost mixin_host_;
};
#if BUILDFLAG(IS_ANDROID)
// An AndroidBrowserTest which supports mixins.
using MixinBasedAndroidBrowserTest =
InProcessBrowserTestMixinHostSupport<AndroidBrowserTest>;
// The implementation is included in mixin_based_in_process_browser_test.cc
extern template class InProcessBrowserTestMixinHostSupport<AndroidBrowserTest>;
#else
// An InProcessBrowserTest which supports mixins.
using MixinBasedInProcessBrowserTest =
InProcessBrowserTestMixinHostSupport<InProcessBrowserTest>;
// The implementation is included in mixin_based_in_process_browser_test.cc
extern template class InProcessBrowserTestMixinHostSupport<
InProcessBrowserTest>;
#endif
#endif // CHROME_TEST_BASE_MIXIN_BASED_IN_PROCESS_BROWSER_TEST_H_
|