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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "build/build_config.h"
#include "content/public/app/content_main_delegate.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/common/main_function_params.h"
#include "content/public/gpu/content_gpu_client.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_shell_main_delegate.h"
#include "content/public/utility/content_utility_client.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
namespace variations {
class VariationsIdsProvider;
}
namespace content {
namespace {
using ::testing::_;
using ::testing::AtMost;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::Return;
using InvokedIn = ContentMainDelegate::InvokedIn;
using VariationsIdsProvider = variations::VariationsIdsProvider;
// Mocks only the cross-platform methods of ContentMainDelegate. Also calls the
// parent implementation of each method, since the test setup may depend on it.
class MockContentMainDelegate : public ContentBrowserTestShellMainDelegate {
public:
using Super = ContentBrowserTestShellMainDelegate;
MOCK_METHOD(std::optional<int>, MockBasicStartupComplete, ());
std::optional<int> BasicStartupComplete() override {
std::optional<int> result = MockBasicStartupComplete();
// Check for early exit code.
if (result.has_value())
return result;
return Super::BasicStartupComplete();
}
MOCK_METHOD(void, MockPreSandboxStartup, ());
void PreSandboxStartup() override {
MockPreSandboxStartup();
Super::PreSandboxStartup();
}
MOCK_METHOD(void, MockSandboxInitialized, (const std::string&));
void SandboxInitialized(const std::string& process_type) override {
MockSandboxInitialized(process_type);
Super::SandboxInitialized(process_type);
}
// The return value of RunProcess is platform-dependent and the startup
// sequence depends heavily on it, so don't allow it to be mocked.
MOCK_METHOD(void, MockRunProcess, (const std::string&, MainFunctionParams));
std::variant<int, MainFunctionParams> RunProcess(
const std::string& process_type,
MainFunctionParams main_function_params) override {
// MainFunctionParams is move-only so pass a dummy to the mock.
MainFunctionParams dummy_main_function_params(
base::CommandLine::ForCurrentProcess());
MockRunProcess(process_type, std::move(dummy_main_function_params));
return Super::RunProcess(process_type, std::move(main_function_params));
}
MOCK_METHOD(void, MockProcessExiting, (const std::string&));
void ProcessExiting(const std::string& process_type) override {
MockProcessExiting(process_type);
Super::ProcessExiting(process_type);
}
// The return value of ShouldLockSchemeRegistry is dangerous to override so
// don't allow it to be mocked.
MOCK_METHOD(void, MockShouldLockSchemeRegistry, ());
bool ShouldLockSchemeRegistry() override {
MockShouldLockSchemeRegistry();
return Super::ShouldLockSchemeRegistry();
}
MOCK_METHOD(std::optional<int>, MockPreBrowserMain, ());
std::optional<int> PreBrowserMain() override {
std::optional<int> result = MockPreBrowserMain();
// Check for early exit code.
if (result.has_value())
return result;
return Super::PreBrowserMain();
}
// No need to call the parent delegate for these methods since they have no
// side effects.
MOCK_METHOD(bool, ShouldCreateFeatureList, (InvokedIn), (override));
MOCK_METHOD(bool, ShouldInitializeMojo, (InvokedIn), (override));
MOCK_METHOD(VariationsIdsProvider*, MockCreateVariationsIdsProvider, ());
VariationsIdsProvider* CreateVariationsIdsProvider() override {
VariationsIdsProvider* result = MockCreateVariationsIdsProvider();
if (result)
return result;
return Super::CreateVariationsIdsProvider();
}
MOCK_METHOD(std::optional<int>, MockPostEarlyInitialization, (InvokedIn));
std::optional<int> PostEarlyInitialization(InvokedIn invoked_in) override {
std::optional<int> result = MockPostEarlyInitialization(invoked_in);
// Check for early exit code.
if (result.has_value())
return result;
return Super::PostEarlyInitialization(invoked_in);
}
MOCK_METHOD(ContentClient*, MockCreateContentClient, ());
ContentClient* CreateContentClient() override {
ContentClient* result = MockCreateContentClient();
if (result)
return result;
return Super::CreateContentClient();
}
MOCK_METHOD(ContentBrowserClient*, MockCreateContentBrowserClient, ());
ContentBrowserClient* CreateContentBrowserClient() override {
ContentBrowserClient* result = MockCreateContentBrowserClient();
if (result)
return result;
return Super::CreateContentBrowserClient();
}
MOCK_METHOD(ContentGpuClient*, MockCreateContentGpuClient, ());
ContentGpuClient* CreateContentGpuClient() override {
ContentGpuClient* result = MockCreateContentGpuClient();
if (result)
return result;
return Super::CreateContentGpuClient();
}
MOCK_METHOD(ContentRendererClient*, MockCreateContentRendererClient, ());
ContentRendererClient* CreateContentRendererClient() override {
ContentRendererClient* result = MockCreateContentRendererClient();
if (result)
return result;
return Super::CreateContentRendererClient();
}
MOCK_METHOD(ContentUtilityClient*, MockCreateContentUtilityClient, ());
ContentUtilityClient* CreateContentUtilityClient() override {
ContentUtilityClient* result = MockCreateContentUtilityClient();
if (result)
return result;
return Super::CreateContentUtilityClient();
}
};
MATCHER_P(InvokedInMatcher, process_type, "") {
// `arg` is an std::variant. Return true if the type held by the variant is
// correct for `process_type` (empty means the browser process).
return std::visit(absl::Overload{
[&](ContentMainDelegate::InvokedInBrowserProcess) {
return process_type.empty();
},
[&](ContentMainDelegate::InvokedInChildProcess) {
return !process_type.empty();
},
},
arg);
}
// Tests that methods of ContentMainDelegate are called in the expected order.
class ContentMainRunnerImplBrowserTest : public ContentBrowserTest {
protected:
using Self = ContentMainRunnerImplBrowserTest;
using Super = ContentBrowserTest;
void SetUp() override {
// Empty process name means the browser process.
const std::string kBrowserProcessType = "";
// These methods may or may not be called, depending on configuration.
EXPECT_CALL(mock_delegate_, MockShouldLockSchemeRegistry())
.Times(AtMost(1));
EXPECT_CALL(mock_delegate_, MockCreateVariationsIdsProvider())
.Times(AtMost(1));
// CreateContentClient() is only called if GetContentClient() returns null.
EXPECT_CALL(mock_delegate_, MockCreateContentClient()).Times(AtMost(1));
// ContentBrowserTestShellMainDelegate calls these internally, so allow
// extra calls to them out of sequence.
EXPECT_CALL(mock_delegate_, ShouldCreateFeatureList(_))
.WillRepeatedly(Return(true));
EXPECT_CALL(mock_delegate_, ShouldInitializeMojo(_))
.WillRepeatedly(Return(true));
// Expect the following entry points to be called, in order.
//
// BrowserTestBase::SetUp() calls ContentMain(), which instantiates a
// ContentMainRunnerImpl, which calls the entry points in
// ContentMainDelegate. So test expectations must be installed before
// calling the inherited SetUp().
::testing::InSequence s;
EXPECT_CALL(mock_delegate_, MockBasicStartupComplete())
.WillOnce(DoAll(
// Test the starting state of ContentMainRunnerImpl.
Invoke(this, &Self::TestBasicStartupComplete),
Return(std::nullopt)));
EXPECT_CALL(mock_delegate_, MockCreateContentBrowserClient());
EXPECT_CALL(mock_delegate_, MockPreSandboxStartup());
EXPECT_CALL(mock_delegate_, MockSandboxInitialized(kBrowserProcessType));
EXPECT_CALL(mock_delegate_,
ShouldCreateFeatureList(InvokedInMatcher(kBrowserProcessType)))
.WillOnce(Return(true));
EXPECT_CALL(mock_delegate_,
ShouldInitializeMojo(InvokedInMatcher(kBrowserProcessType)))
.WillOnce(Return(true));
EXPECT_CALL(mock_delegate_, MockPreBrowserMain())
.WillOnce(Return(std::nullopt));
EXPECT_CALL(mock_delegate_, MockPostEarlyInitialization(
InvokedInMatcher(kBrowserProcessType)))
.WillOnce(DoAll(Invoke(this, &Self::TestPostEarlyInitialization),
Return(std::nullopt)));
EXPECT_CALL(mock_delegate_, MockRunProcess(kBrowserProcessType, _));
#if !BUILDFLAG(IS_ANDROID)
// Android never calls ProcessExiting, since it leaks its ContentMainRunner
// and ProcessExiting is called from the destructor.
EXPECT_CALL(mock_delegate_, MockProcessExiting(kBrowserProcessType));
#endif
// This will call ContentMain(), which should satisfy the expectations
// above.
Super::SetUp();
}
ContentMainDelegate* GetOptionalContentMainDelegateOverride() override {
return &mock_delegate_;
}
void TestBasicStartupComplete() {
// The PostEarlyInitialization test checks that ContentMainRunnerImpl set up
// the FeatureList. This test is invalid if it already exists
// before starting.
EXPECT_FALSE(base::FeatureList::GetInstance());
}
void TestPostEarlyInitialization() {
// ContentMainRunnerImpl should have set up the ThreadPoolInstance and
// FeatureList by this point.
EXPECT_TRUE(base::ThreadPoolInstance::Get());
EXPECT_TRUE(base::FeatureList::GetInstance());
}
::testing::StrictMock<MockContentMainDelegate> mock_delegate_;
};
IN_PROC_BROWSER_TEST_F(ContentMainRunnerImplBrowserTest, StartupSequence) {
// All of the work is done in SetUp().
}
} // namespace
} // namespace content
|