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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
// 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 "base/synchronization/lock.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/test/test_timeouts.h"
#include "chrome/browser/actor/actor_features.h"
#include "chrome/browser/actor/actor_task.h"
#include "chrome/browser/actor/actor_test_util.h"
#include "chrome/browser/actor/execution_engine.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/optimization_guide/proto/features/actions_data.pb.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/strings/str_format.h"
namespace actor {
namespace {
using base::test::ScopedFeatureList;
using base::test::TestFuture;
using content::EvalJs;
using content::ExecJs;
using ::content::JsReplace;
using ::content::RenderFrameHost;
using ::content::TestNavigationManager;
using ::content::WebContents;
using optimization_guide::proto::BrowserAction;
using optimization_guide::proto::ClickAction;
// Note: this file doesn't actually exist, the response is manually provided by
// tests.
const char* kFetchPath = "/fetchtarget.html";
// Tests for the PageStabilityMonitor's functionality of delaying renderer-tool
// completion until the page is ready for an observation.
class ActorPageStabilityTest : public InProcessBrowserTest {
public:
ActorPageStabilityTest() {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{features::kGlic, features::kTabstripComboButton,
features::kGlicActor},
/*disabled_features=*/{features::kGlicWarming});
}
ActorPageStabilityTest(const ActorPageStabilityTest&) = delete;
ActorPageStabilityTest& operator=(const ActorPageStabilityTest&) = delete;
~ActorPageStabilityTest() override = default;
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
fetch_response_ =
std::make_unique<net::test_server::ControllableHttpResponse>(
embedded_test_server(), kFetchPath);
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(embedded_https_test_server().Start());
auto execution_engine = std::make_unique<ExecutionEngine>(
browser()->profile(), browser()->GetActiveTabInterface());
actor_task_ = std::make_unique<ActorTask>(std::move(execution_engine));
}
void TearDownOnMainThread() override {
// The execution engine has a pointer to the profile, which must be released
// before the browser is torn down to avoid a dangling pointer.
actor_task_.reset();
}
// Pause execution for 300ms - matching the busy work delay in
// page_stability.html
void Sleep300ms() {
base::RunLoop run_loop;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(300));
run_loop.Run();
}
WebContents* web_contents() {
return chrome_test_utils::GetActiveWebContents(this);
}
RenderFrameHost* main_frame() {
return web_contents()->GetPrimaryMainFrame();
}
std::string GetFetchOutput() {
return EvalJs(web_contents(), "document.getElementById('output').innerText")
.ExtractString();
}
ExecutionEngine& execution_engine() {
return *actor_task_->GetExecutionEngine();
}
net::test_server::ControllableHttpResponse& fetch_response() {
return *fetch_response_;
}
void Respond(std::string_view text) {
fetch_response_->Send(net::HTTP_OK, /*content_type=*/"text/html",
/*content=*/"",
/*cookies=*/{}, /*extra_headers=*/{});
fetch_response_->Send(std::string(text));
fetch_response_->Done();
}
private:
std::unique_ptr<net::test_server::ControllableHttpResponse> fetch_response_;
ScopedFeatureList scoped_feature_list_;
std::unique_ptr<ActorTask> actor_task_;
};
// Ensure the page isn't considered stable until after a network fetch is
// resolved.
IN_PROC_BROWSER_TEST_F(ActorPageStabilityTest, WaitOnNetworkFetch) {
const GURL url = embedded_test_server()->GetURL("/actor/page_stability.html");
const GURL url_fetch = embedded_test_server()->GetURL(kFetchPath);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url));
ASSERT_EQ(GetFetchOutput(), "INITIAL");
std::optional<int> button_id = GetDOMNodeId(*main_frame(), "#btnFetch");
ASSERT_TRUE(button_id);
BrowserAction action = MakeClick(*main_frame(), button_id.value());
TestFuture<mojom::ActionResultPtr> result;
execution_engine().Act(action, result.GetCallback());
fetch_response().WaitForRequest();
Sleep300ms();
// The fetch hasn't resolved yet, the tool use shouldn't have returned yet
// either.
ASSERT_EQ(GetFetchOutput(), "INITIAL");
EXPECT_FALSE(result.IsReady());
Respond("NETWORK DONE");
ExpectOkResult(result);
ASSERT_EQ(GetFetchOutput(), "NETWORK DONE");
}
// Simulate a network fetch followed by heavy main thread activity. Ensure the
// page isn't considered stable until after the main thread work finishes.
IN_PROC_BROWSER_TEST_F(ActorPageStabilityTest, WaitOnFetchAndWork) {
const GURL url = embedded_test_server()->GetURL("/actor/page_stability.html");
const GURL url_fetch = embedded_test_server()->GetURL(kFetchPath);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url));
ASSERT_EQ(GetFetchOutput(), "INITIAL");
std::optional<int> button_id =
GetDOMNodeId(*main_frame(), "#btnFetchAndWork");
ASSERT_TRUE(button_id);
BrowserAction action = MakeClick(*main_frame(), button_id.value());
TestFuture<mojom::ActionResultPtr> result;
execution_engine().Act(action, result.GetCallback());
fetch_response().WaitForRequest();
Sleep300ms();
EXPECT_FALSE(result.IsReady());
ASSERT_EQ(GetFetchOutput(), "INITIAL");
// Respond to the fetch, this will start 3 tasks of 300ms each on the main
// thead.
Respond("NETWORK DONE");
Sleep300ms();
// The fetch should have resolved but the main thread is busy so the
// page isn't yet stable.
ASSERT_EQ(GetFetchOutput(), "NETWORK DONE");
EXPECT_FALSE(result.IsReady());
Sleep300ms();
EXPECT_FALSE(result.IsReady());
// Wait and the main thread will eventually finish.
ExpectOkResult(result);
ASSERT_EQ(GetFetchOutput(), "WORK DONE");
}
// Shorten timeouts to test they work.
// LocalTimeout is the timeout delay used when waiting on non-network actions
// like an idle main thread and display compositor frame presentation.
// GlobalTimeout is the timeout delay used end-to-end in the
template <int LocalTimeout, int GlobalTimeout>
class ActorPageStabilityTimeoutTest : public ActorPageStabilityTest {
public:
ActorPageStabilityTimeoutTest() {
std::string local_timeout = absl::StrFormat("%dms", LocalTimeout);
std::string global_timeout = absl::StrFormat("%dms", GlobalTimeout);
timeout_scoped_feature_list_.InitWithFeaturesAndParameters(
/*enabled_features=*/{{features::kGlic, {}},
{features::kTabstripComboButton, {}},
{features::kGlicActor,
{{"glic-actor-observation-delay", local_timeout},
{"glic-actor-page-stability-timeout",
global_timeout}}}},
/*disabled_features=*/{features::kGlicWarming});
}
ActorPageStabilityTimeoutTest(const ActorPageStabilityTimeoutTest&) = delete;
ActorPageStabilityTimeoutTest& operator=(
const ActorPageStabilityTimeoutTest&) = delete;
~ActorPageStabilityTimeoutTest() override = default;
private:
ScopedFeatureList timeout_scoped_feature_list_;
};
// Shorten the timeout under test and make the other timeout very long to avoid
// tripping it.
using ActorPageStabilityLocalTimeoutTest =
ActorPageStabilityTimeoutTest<100, 100000>;
using ActorPageStabilityGlobalTimeoutTest =
ActorPageStabilityTimeoutTest<100000, 100>;
// Ensure that if a network request runs long, the stability monitor will
// eventually timeout.
IN_PROC_BROWSER_TEST_F(ActorPageStabilityGlobalTimeoutTest, NetworkTimeout) {
const GURL url = embedded_test_server()->GetURL("/actor/page_stability.html");
const GURL url_fetch = embedded_test_server()->GetURL(kFetchPath);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url));
ASSERT_EQ(GetFetchOutput(), "INITIAL");
std::optional<int> button_id =
GetDOMNodeId(*main_frame(), "#btnFetchAndWork");
ASSERT_TRUE(button_id);
BrowserAction action = MakeClick(*main_frame(), button_id.value());
TestFuture<mojom::ActionResultPtr> result;
execution_engine().Act(action, result.GetCallback());
// Never respond to the request
fetch_response().WaitForRequest();
// Ensure the stability monitor eventually allows completion.
ExpectOkResult(result);
ASSERT_EQ(GetFetchOutput(), "INITIAL");
}
// Ensure that if the main thread never becomes idle the stability monitor will
// eventually timeout.
IN_PROC_BROWSER_TEST_F(ActorPageStabilityGlobalTimeoutTest, BusyMainThread) {
const GURL url = embedded_test_server()->GetURL("/actor/page_stability.html");
const GURL url_fetch = embedded_test_server()->GetURL(kFetchPath);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url));
std::optional<int> button_id = GetDOMNodeId(*main_frame(), "#btnWorkForever");
ASSERT_TRUE(button_id);
BrowserAction action = MakeClick(*main_frame(), button_id.value());
TestFuture<mojom::ActionResultPtr> result;
execution_engine().Act(action, result.GetCallback());
// Ensure the stability monitor eventually allows completion.
ExpectOkResult(result);
}
// Ensure that if the main thread never becomes idle the stability monitor will
// eventually timeout on the local timeout.
IN_PROC_BROWSER_TEST_F(ActorPageStabilityLocalTimeoutTest, BusyMainThread) {
const GURL url = embedded_test_server()->GetURL("/actor/page_stability.html");
const GURL url_fetch = embedded_test_server()->GetURL(kFetchPath);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url));
std::optional<int> button_id = GetDOMNodeId(*main_frame(), "#btnWorkForever");
ASSERT_TRUE(button_id);
BrowserAction action = MakeClick(*main_frame(), button_id.value());
TestFuture<mojom::ActionResultPtr> result;
execution_engine().Act(action, result.GetCallback());
// Ensure the stability monitor eventually allows completion.
ExpectOkResult(result);
}
enum class NavigationDelay { kInstant, kDelayed };
enum class NavigationType { kSameDocument, kSameSite, kCrossSite };
// Run the following test using same and cross site navigations to exercise
// paths where the RenderFrameHost is swapped or kept as well as same document
// where the navigation is synchronous in the renderer.
//
// Also run with the navigation completing without delay as well as with some
// induced delay.
// TODO(crbug.com/414662842): Move to page_stability_browsertest.cc.
class ActorPageStabilityNavigationTypesTest
: public ActorPageStabilityTest,
public testing::WithParamInterface<
std::tuple<NavigationDelay, NavigationType>> {
public:
// Provides meaningful param names instead of /0, /1, ...
static std::string DescribeParams(
const testing::TestParamInfo<ParamType>& info) {
auto [delay, navigation_type] = info.param;
std::stringstream params_description;
switch (delay) {
case NavigationDelay::kInstant:
params_description << "Instant";
break;
case NavigationDelay::kDelayed:
params_description << "Delayed";
break;
}
switch (navigation_type) {
case NavigationType::kSameDocument:
params_description << "_SameDocument";
break;
case NavigationType::kSameSite:
params_description << "_SameSite";
break;
case NavigationType::kCrossSite:
params_description << "_CrossSite";
break;
}
return params_description.str();
}
ActorPageStabilityNavigationTypesTest() {
base::FieldTrialParams allowlist_params;
allowlist_params["allowlist"] = "foo.com,bar.com";
allowlist_params["allowlist_only"] = "true";
page_tools_feature_list_.InitWithFeaturesAndParameters(
/*enabled_features=*/{{features::kGlic, {}},
{features::kTabstripComboButton, {}},
{features::kGlicActor, {}},
{kGlicActionAllowlist, allowlist_params}},
/*disabled_features=*/{features::kGlicWarming});
}
NavigationType NavigationTypeParam() const { return std::get<1>(GetParam()); }
NavigationDelay DelayTypeParam() const {
// Note: the delay is 5s but in practice the RenderFrame is torn down by
// navigation so this won't block the test.
return std::get<0>(GetParam());
}
private:
ScopedFeatureList page_tools_feature_list_;
};
// Ensure a page tool (click, in this case) causing a navigation of various
// types (same-doc, same-site, cross-site) works successfully waits for loading
// to finish in cases where the navigation finishes quickly or is delayed at
// various points.
IN_PROC_BROWSER_TEST_P(ActorPageStabilityNavigationTypesTest, Test) {
const GURL url_start = embedded_https_test_server().GetURL(
"foo.com", "/actor/cross_document_nav.html");
GURL url_next;
switch (NavigationTypeParam()) {
case NavigationType::kSameDocument:
if (DelayTypeParam() == NavigationDelay::kDelayed) {
// Same document navigations are synchronous so it doesn't make sense
// for there to be a delay.
GTEST_SKIP();
}
url_next = embedded_https_test_server().GetURL(
"foo.com", "/actor/cross_document_nav.html#next");
break;
case NavigationType::kSameSite:
url_next = embedded_https_test_server().GetURL(
"foo.com", "/actor/simple_iframe.html");
break;
case NavigationType::kCrossSite:
url_next = embedded_https_test_server().GetURL(
"bar.com", "/actor/simple_iframe.html");
break;
}
// The subframe in the destination page is used to delay the load event (by
// deferring its navigation commit).
GURL::Replacements replacement;
replacement.SetPathStr("/actor/blank.html");
GURL url_subframe = url_next.ReplaceComponents(replacement);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url_start));
// The link in the file is relative so replace it to include the mock
// hostname.
ASSERT_TRUE(
ExecJs(web_contents(),
JsReplace("document.getElementById('link').href = $1", url_next)));
// To ensure coverage of the case where a RenderFrameHost is reused across
// same-site navigation, disable proactive browsing instance swaps.
DisableProactiveBrowsingInstanceSwapFor(main_frame());
// Send a click to the link.
std::optional<int> link_id = GetDOMNodeId(*main_frame(), "#link");
ASSERT_TRUE(link_id);
// In the delay variant of the test, delay the main frame commit to ensure
// page observation doesn't return early after a slow network response. Delay
// the subframe in the new page as well to ensure the page tool waits on a
// cross-document load in this case.
std::optional<TestNavigationManager> main_frame_delay;
std::optional<TestNavigationManager> subframe_delay;
if (DelayTypeParam() == NavigationDelay::kDelayed) {
main_frame_delay.emplace(web_contents(), url_next);
subframe_delay.emplace(web_contents(), url_subframe);
}
BrowserAction action = MakeClick(*main_frame(), link_id.value());
TestFuture<mojom::ActionResultPtr> result;
execution_engine().Act(action, result.GetCallback());
if (main_frame_delay) {
CHECK(subframe_delay);
ASSERT_TRUE(main_frame_delay->WaitForResponse());
Sleep300ms();
EXPECT_FALSE(result.IsReady());
ASSERT_TRUE(main_frame_delay->WaitForNavigationFinished());
// Now delay the subframe to delay main document load completion.
ASSERT_TRUE(subframe_delay->WaitForResponse());
Sleep300ms();
EXPECT_FALSE(result.IsReady());
ASSERT_TRUE(subframe_delay->WaitForNavigationFinished());
}
ExpectOkResult(result);
EXPECT_EQ(web_contents()->GetURL(), url_next);
}
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
ActorPageStabilityNavigationTypesTest,
testing::Combine(testing::Values(NavigationDelay::kInstant,
NavigationDelay::kDelayed),
testing::Values(NavigationType::kSameDocument,
NavigationType::kSameSite,
NavigationType::kCrossSite)),
ActorPageStabilityNavigationTypesTest::DescribeParams);
} // namespace
} // namespace actor
|