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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/process_selection_deferring_condition_runner.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/test/mock_callback.h"
#include "content/browser/renderer_host/navigation_request.h"
#include "content/public/browser/process_selection_deferring_condition.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "content/test/mock_process_selection_deferring_condition_tester.h"
#include "content/test/test_content_browser_client.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace content {
namespace {
class ProcessSelectionDeferringBrowserClient : public TestContentBrowserClient {
public:
ProcessSelectionDeferringBrowserClient() = default;
~ProcessSelectionDeferringBrowserClient() override = default;
std::vector<std::unique_ptr<ProcessSelectionDeferringCondition>>
CreateProcessSelectionDeferringConditionsForNavigation(
NavigationHandle& navigation_handle) override {
return std::move(conditions_);
}
void AddCondition(
std::unique_ptr<ProcessSelectionDeferringCondition> condition) {
conditions_.push_back(std::move(condition));
}
private:
std::vector<std::unique_ptr<ProcessSelectionDeferringCondition>> conditions_;
};
} // namespace
class ProcessSelectionDeferringConditionRunnerTest
: public RenderViewHostTestHarness {
public:
ProcessSelectionDeferringConditionRunnerTest() = default;
void SetUp() override {
RenderViewHostTestHarness::SetUp();
browser_client_ =
std::make_unique<ProcessSelectionDeferringBrowserClient>();
old_browser_client_ = SetBrowserClientForTesting(browser_client_.get());
navigation_ = NavigationSimulator::CreateRendererInitiated(
GURL("https://example.com"), main_rfh());
navigation_->Start();
}
void TearDown() override {
SetBrowserClientForTesting(old_browser_client_);
// Release the runner so that the NavigationHandle is released before
// tearing down the test harness.
runner_.reset();
RenderViewHostTestHarness::TearDown();
}
ProcessSelectionDeferringBrowserClient* browser_client() {
return browser_client_.get();
}
ProcessSelectionDeferringConditionRunner* CreateRunner() {
CHECK(navigation_);
runner_ = ProcessSelectionDeferringConditionRunner::Create(
*NavigationRequest::From(navigation_->GetNavigationHandle()));
return runner_.get();
}
NavigationHandle* navigation_handle() {
return navigation_->GetNavigationHandle();
}
private:
std::unique_ptr<ProcessSelectionDeferringBrowserClient> browser_client_;
raw_ptr<ContentBrowserClient> old_browser_client_ = nullptr;
std::unique_ptr<NavigationSimulator> navigation_;
std::unique_ptr<ProcessSelectionDeferringConditionRunner> runner_;
};
TEST_F(ProcessSelectionDeferringConditionRunnerTest,
NoConditionsRunsTheProvidedCallback) {
auto* runner = CreateRunner();
base::MockCallback<base::OnceClosure> callback;
EXPECT_CALL(callback, Run());
runner->WillSelectFinalProcess(callback.Get());
}
TEST_F(ProcessSelectionDeferringConditionRunnerTest,
DoesNotCallCallbackUntilDeferringConditionResumes) {
MockProcessSelectionDeferringConditionTester condition(
*navigation_handle(), ProcessSelectionDeferringCondition::Result::kDefer);
browser_client()->AddCondition(condition.Release());
auto* runner = CreateRunner();
base::MockCallback<base::OnceClosure> callback;
EXPECT_CALL(callback, Run()).Times(0);
runner->WillSelectFinalProcess(callback.Get());
// The callback should not be called because the condition should be
// deferring.
EXPECT_TRUE(condition.WasOnWillSelectFinalProcessCalled());
testing::Mock::VerifyAndClearExpectations(&callback);
EXPECT_CALL(callback, Run());
condition.CallResumeClosure();
}
TEST_F(ProcessSelectionDeferringConditionRunnerTest,
ProceedConditionCallbackIsCalledSynchronously) {
MockProcessSelectionDeferringConditionTester condition(
*navigation_handle(),
ProcessSelectionDeferringCondition::Result::kProceed);
browser_client()->AddCondition(condition.Release());
auto* runner = CreateRunner();
base::MockCallback<base::OnceClosure> callback;
EXPECT_CALL(callback, Run());
runner->WillSelectFinalProcess(callback.Get());
EXPECT_TRUE(condition.WasOnWillSelectFinalProcessCalled());
}
TEST_F(ProcessSelectionDeferringConditionRunnerTest,
ProvidedCallbackIsRunOnlyAfterAllConditionsAreDone) {
MockProcessSelectionDeferringConditionTester condition1(
*navigation_handle(), ProcessSelectionDeferringCondition::Result::kDefer);
MockProcessSelectionDeferringConditionTester condition2(
*navigation_handle(), ProcessSelectionDeferringCondition::Result::kDefer);
browser_client()->AddCondition(condition1.Release());
browser_client()->AddCondition(condition2.Release());
auto* runner = CreateRunner();
base::MockCallback<base::OnceClosure> callback;
runner->WillSelectFinalProcess(callback.Get());
EXPECT_CALL(callback, Run()).Times(0);
EXPECT_TRUE(condition1.WasOnWillSelectFinalProcessCalled());
EXPECT_FALSE(condition2.WasOnWillSelectFinalProcessCalled());
condition1.CallResumeClosure();
EXPECT_TRUE(condition2.WasOnWillSelectFinalProcessCalled());
// The callback should not have been called yet.
testing::Mock::VerifyAndClearExpectations(&callback);
// The callback should run after condition2 resumes.
EXPECT_CALL(callback, Run());
condition2.CallResumeClosure();
}
TEST_F(ProcessSelectionDeferringConditionRunnerTest,
ProceedConditionAfterDeferConditionCallsCallback) {
MockProcessSelectionDeferringConditionTester condition1(
*navigation_handle(), ProcessSelectionDeferringCondition::Result::kDefer);
MockProcessSelectionDeferringConditionTester condition2(
*navigation_handle(),
ProcessSelectionDeferringCondition::Result::kProceed);
browser_client()->AddCondition(condition1.Release());
browser_client()->AddCondition(condition2.Release());
auto* runner = CreateRunner();
base::MockCallback<base::OnceClosure> callback;
EXPECT_CALL(callback, Run()).Times(0);
runner->WillSelectFinalProcess(callback.Get());
EXPECT_TRUE(condition1.WasOnWillSelectFinalProcessCalled());
EXPECT_FALSE(condition2.WasOnWillSelectFinalProcessCalled());
// The callback should not have been called yet.
testing::Mock::VerifyAndClearExpectations(&callback);
EXPECT_CALL(callback, Run());
condition1.CallResumeClosure();
EXPECT_TRUE(condition2.WasOnWillSelectFinalProcessCalled());
}
TEST_F(ProcessSelectionDeferringConditionRunnerTest,
OnRequestRedirectedCallsOnRequestRedirectedHandlers) {
MockProcessSelectionDeferringConditionTester condition1(
*navigation_handle(),
ProcessSelectionDeferringCondition::Result::kProceed);
MockProcessSelectionDeferringConditionTester condition2(
*navigation_handle(),
ProcessSelectionDeferringCondition::Result::kProceed);
browser_client()->AddCondition(condition1.Release());
browser_client()->AddCondition(condition2.Release());
auto* runner = CreateRunner();
runner->OnRequestRedirected();
// All conditions should have their request redirected call count incremented.
EXPECT_EQ(condition1.GetOnRequestRedirectedCallCount(), 1);
EXPECT_EQ(condition2.GetOnRequestRedirectedCallCount(), 1);
// Redirecting again should call the handlers again.
runner->OnRequestRedirected();
EXPECT_EQ(condition1.GetOnRequestRedirectedCallCount(), 2);
EXPECT_EQ(condition2.GetOnRequestRedirectedCallCount(), 2);
}
} // namespace content
|