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 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
// 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 <memory>
#include <string>
#include <vector>
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/callback.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/test/allow_check_is_test_for_testing.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/loader/keep_alive_request_browsertest_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/page_load_metrics/browser/features.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/variations/net/variations_http_headers.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/keep_alive_request_tracker.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/keep_alive_url_loader_utils.h"
#include "content/public/test/test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_status_code.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "url/url_util.h"
namespace {
using content::EvalJs;
using content::JsReplace;
using testing::Contains;
using testing::Eq;
using testing::Key;
using testing::Not;
constexpr char kSRPEndpoing[] = "/search?q=test";
std::string GetRelativeCategoryUrl(std::string_view relative_url,
std::string_view category) {
const std::string sep =
(std::string(relative_url).find("?") == std::string::npos) ? "?" : "&";
return base::StrCat({relative_url, sep, "category=", category});
}
std::string GetKeepAliveCategoryRequestUrl(std::string_view category) {
return GetRelativeCategoryUrl(kKeepAliveEndpoint, category);
}
// Always serves the page navigation-fetch-keepalive.html as the response to all
// navigation requests to search result pages.
std::unique_ptr<net::test_server::HttpResponse> HandleSearchResultPageRequest(
const net::test_server::HttpRequest& request) {
if (request.method != net::test_server::METHOD_GET) {
return nullptr;
}
base::FilePath server_root;
base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &server_root);
base::FilePath file_path(server_root.AppendASCII(
"chrome/test/data/navigation-fetch-keepalive.html"));
std::string file_contents;
CHECK(base::ReadFileToString(file_path, &file_contents));
auto http_response = std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
http_response->AddCustomHeader("Access-Control-Allow-Origin", "*");
http_response->set_content(file_contents);
return http_response;
}
// Returns 200 OK response to all fetch keepalive requests.
std::unique_ptr<net::test_server::HttpResponse> HandleKeepAliveRequest(
const net::test_server::HttpRequest& request) {
auto http_response = std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
http_response->AddCustomHeader("Access-Control-Allow-Origin", "*");
http_response->set_content(k200TextResponse);
return http_response;
}
} // namespace
class ChromeKeepAliveCategoryRequestBrowserTestBase
: public ChromeKeepAliveRequestBrowserTestBase,
public content::KeepAliveRequestUkmMatcher,
public content::NavigationKeepAliveRequestUkmMatcher {
public:
ChromeKeepAliveCategoryRequestBrowserTestBase()
: ChromeKeepAliveRequestBrowserTestBase() {
InitFeatureList({{blink::features::kKeepAliveInBrowserMigration, {}},
{page_load_metrics::features::kBeaconLeakageLogging,
{{"category_prefix", "test-prefix"}}}});
}
protected:
void SetUpOnMainThread() override {
ChromeKeepAliveRequestBrowserTestBase::SetUpOnMainThread();
ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
}
ukm::TestAutoSetUkmRecorder& ukm_recorder() override {
return *ukm_recorder_;
}
private:
std::unique_ptr<ukm::TestAutoSetUkmRecorder> ukm_recorder_;
};
// Browser tests to cover fetch keepalive request with specific "category" in
// the request URL param.
class ChromeKeepAliveCategoryRequestBrowserTest
: public ChromeKeepAliveCategoryRequestBrowserTestBase,
public testing::WithParamInterface<std::string> {
protected:
const std::string& method() const { return GetParam(); }
};
INSTANTIATE_TEST_SUITE_P(
All,
ChromeKeepAliveCategoryRequestBrowserTest,
::testing::Values(net::HttpRequestHeaders::kGetMethod,
net::HttpRequestHeaders::kPostMethod),
[](const testing::TestParamInfo<
ChromeKeepAliveCategoryRequestBrowserTest::ParamType>& info) {
return info.param;
});
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveCategoryRequestBrowserTest, OneRequest) {
const std::string target_url = GetKeepAliveCategoryRequestUrl("test-prefix1");
auto request_handler = std::move(RegisterRequestHandlers({target_url})[0]);
ASSERT_TRUE(server()->Start());
ASSERT_TRUE(NavigateToURL(
web_contents(), GetKeepAlivePageURL(kPrimaryHost, target_url, method())));
// Ensure the keepalive request is sent, but delay response.
request_handler->WaitForRequest();
// End the keepalive request by sending back response.
request_handler->Send(k200TextResponse);
request_handler->Done();
content::TitleWatcher watcher(web_contents(), kPromiseResolvedPageTitle);
EXPECT_EQ(watcher.WaitAndGetTitle(), kPromiseResolvedPageTitle);
loaders_observer().WaitForTotalOnReceiveResponseForwarded(1);
loaders_observer().WaitForTotalOnCompleteForwarded({net::OK});
ExpectCommonUkm(
content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0);
ExpectTimeSortedTimeDeltaUkm(
{"TimeDelta.RequestStarted", "TimeDelta.ResponseReceived",
"TimeDelta.LoaderCompleted", "TimeDelta.EventLogged"});
NavigationKeepAliveRequestUkmMatcher::ExpectNoUkm();
}
// Delays response to a keepalive ping until after the page making the keepalive
// ping has been unloaded. The browser must ensure the response is received and
// processed by the browser.
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveCategoryRequestBrowserTest,
ReceiveResponseAfterPageUnload) {
const std::string target_url = GetKeepAliveCategoryRequestUrl("test-prefix1");
auto request_handler = std::move(RegisterRequestHandlers({target_url})[0]);
ASSERT_TRUE(embedded_test_server()->Start());
LoadPageWithKeepAliveRequestAndSendResponseAfterUnload(
GetKeepAlivePageURL(kPrimaryHost, target_url, method()),
request_handler.get(), k200TextResponse);
// The response should be processed in browser.
loaders_observer().WaitForTotalOnReceiveResponseProcessed(1);
ExpectCommonUkm(
content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/true,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
content::KeepAliveRequestTracker::RequestStageType::
kLoaderDisconnectedFromRenderer);
ExpectTimeSortedTimeDeltaUkm(
{"TimeDelta.RequestStarted", "TimeDelta.LoaderDisconnectedFromRenderer",
"TimeDelta.ResponseReceived", "TimeDelta.EventLogged"});
NavigationKeepAliveRequestUkmMatcher::ExpectNoUkm();
}
// Delays response to a keepalive ping until after the page making the keepalive
// ping is put into BackForwardCache. The response should be processed by the
// renderer after the page is restored from BackForwardCache.
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveCategoryRequestBrowserTest,
ReceiveResponseInBackForwardCache) {
const std::string target_url = GetKeepAliveCategoryRequestUrl("test-prefix1");
auto request_handler = std::move(RegisterRequestHandlers({target_url})[0]);
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(NavigateToURL(
web_contents(), GetKeepAlivePageURL(kPrimaryHost, target_url, method())));
content::RenderFrameHostWrapper rfh_1(current_frame_host());
// Ensure the keepalive request is sent before leaving the current page.
request_handler->WaitForRequest();
// Navigate to cross-origin page.
ASSERT_TRUE(NavigateToURL(web_contents(), GetCrossOriginPageURL()));
// Ensure the previous page has been put into BackForwardCache.
ASSERT_EQ(rfh_1->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kInBackForwardCache);
// Send back response.
request_handler->Send(k200TextResponse);
// The response is immediately forwarded to the in-BackForwardCache renderer.
loaders_observer().WaitForTotalOnReceiveResponseForwarded(1);
// Go back to `rfh_1`.
ASSERT_TRUE(HistoryGoBack(web_contents()));
// The response should be processed in renderer. Hence resolving Promise.
content::TitleWatcher watcher(web_contents(), kPromiseResolvedPageTitle);
EXPECT_EQ(watcher.WaitAndGetTitle(), kPromiseResolvedPageTitle);
request_handler->Done();
loaders_observer().WaitForTotalOnCompleteForwarded({net::OK});
ExpectCommonUkm(
content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0);
ExpectTimeSortedTimeDeltaUkm(
{"TimeDelta.RequestStarted", "TimeDelta.ResponseReceived",
"TimeDelta.LoaderCompleted", "TimeDelta.EventLogged"});
NavigationKeepAliveRequestUkmMatcher::ExpectNoUkm();
}
// Delays handling redirect for a keepalive ping until after the page making the
// keepalive ping has been unloaded. The browser must ensure the redirect is
// verified and properly processed by the browser.
IN_PROC_BROWSER_TEST_P(ChromeKeepAliveCategoryRequestBrowserTest,
ReceiveRedirectAfterPageUnload) {
const std::string target_url = GetKeepAliveCategoryRequestUrl("test-prefix1");
const char redirect_target[] = "/beacon-redirected";
auto request_handlers =
RegisterRequestHandlers({target_url, redirect_target});
ASSERT_TRUE(server()->Start());
// Set up redirects according to the following redirect chain:
// fetch("http://a.com:<port>/beacon", keepalive: true)
// --> http://a.com:<port>/beacon-redirected
LoadPageWithKeepAliveRequestAndSendResponseAfterUnload(
GetKeepAlivePageURL(kPrimaryHost, target_url, method()),
request_handlers[0].get(),
base::StringPrintf(k301ResponseTemplate, redirect_target));
// The redirect request should be processed in browser and gets sent.
request_handlers[1]->WaitForRequest();
// No variations header when redirected to non-Google.
EXPECT_THAT(request_handlers[1]->http_request()->headers,
Not(Contains(Key(variations::kClientDataHeader))));
// End the keepalive request by sending back final response.
request_handlers[1]->Send(k200TextResponse);
request_handlers[1]->Done();
// The response should be processed in browser.
loaders_observer().WaitForTotalOnReceiveResponseProcessed(1);
ExpectCommonUkm(
content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/1,
/*num_retries=*/0,
/*is_context_detached=*/true,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
content::KeepAliveRequestTracker::RequestStageType::
kFirstRedirectReceived);
ExpectTimeSortedTimeDeltaUkm(
{"TimeDelta.RequestStarted", "TimeDelta.LoaderDisconnectedFromRenderer",
"TimeDelta.FirstRedirectReceived", "TimeDelta.ResponseReceived",
"TimeDelta.EventLogged"});
NavigationKeepAliveRequestUkmMatcher::ExpectNoUkm();
}
// Browser tests to cover pairs of navigation request and fetch keepalive
// request from a search result page.
// Note that both the fetch request and the page URL need to contain specific
// "category" URL param in order for them to be considered as test targets.
class FromGWSNavigationAndKeepAliveRequestBrowserTest
: public ChromeKeepAliveCategoryRequestBrowserTestBase,
public testing::WithParamInterface<std::string> {
protected:
void SetUpOnMainThread() override {
ChromeKeepAliveCategoryRequestBrowserTestBase::SetUpOnMainThread();
server()->RegisterDefaultHandler(base::BindRepeating(
&net::test_server::HandlePrefixedRequest, "/search",
base::BindRepeating(HandleSearchResultPageRequest)));
server()->RegisterDefaultHandler(base::BindRepeating(
&net::test_server::HandlePrefixedRequest, kKeepAliveEndpoint,
base::BindRepeating(HandleKeepAliveRequest)));
}
GURL GetSearchResultPageURL() {
return server()->GetURL(kGoogleHost, kSRPEndpoing);
}
const std::string& method() const { return GetParam(); }
};
INSTANTIATE_TEST_SUITE_P(
All,
FromGWSNavigationAndKeepAliveRequestBrowserTest,
::testing::Values(net::HttpRequestHeaders::kGetMethod,
net::HttpRequestHeaders::kPostMethod),
[](const testing::TestParamInfo<
FromGWSNavigationAndKeepAliveRequestBrowserTest::ParamType>& info) {
return info.param;
});
// Tests the case where a SRP sends a fetch keepalive category request.
// As no navigation request is triggered, the navigation-related UKM metrics
// should not be logged.
IN_PROC_BROWSER_TEST_P(FromGWSNavigationAndKeepAliveRequestBrowserTest,
OneRequestFromSRP) {
const std::string category = "test-prefix1";
const std::string target_url = GetKeepAliveCategoryRequestUrl(category);
ASSERT_TRUE(server()->Start());
// Navigate to search result page.
ASSERT_TRUE(NavigateToURL(web_contents(), GetSearchResultPageURL()));
// Ask the page to send a keepalive request to `target_url` with `category`
// in the URL param.
ASSERT_THAT(EvalJs(web_contents(), JsReplace(R"(
runFetchKeepalive($1, $2);
)",
target_url, method())),
Eq(k200TextResponse));
ExpectCommonUkm(
content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0);
ExpectTimeSortedTimeDeltaUkm(
{"TimeDelta.RequestStarted", "TimeDelta.ResponseReceived",
"TimeDelta.LoaderCompleted", "TimeDelta.EventLogged"});
// No navigation request is triggered, so no navigation-related UKM metrics
// should be logged.
NavigationKeepAliveRequestUkmMatcher::ExpectNoUkm();
}
// Tests the case where a SRP sends a fetch keepalive category request and then
// navigates to another page.
IN_PROC_BROWSER_TEST_P(FromGWSNavigationAndKeepAliveRequestBrowserTest,
OneRequestAndNavigationFromSRP) {
const std::string category = "test-prefix1";
const std::string target_url = GetKeepAliveCategoryRequestUrl(category);
ASSERT_TRUE(server()->Start());
const std::string nav_target_url =
GetRelativeCategoryUrl("/title1", category);
// Navigate to search result page.
ASSERT_TRUE(NavigateToURL(web_contents(), GetSearchResultPageURL()));
// Ask the page to
// 1. Send a keepalive request to `target_url` with `category`.
// 2. Navigate to `nav_target_url` with `category`.
ASSERT_THAT(
EvalJs(web_contents(), JsReplace(R"(
runFetchKeepaliveAndNavigation($1, $2, $3);
)",
target_url, method(), nav_target_url)),
Eq(k200TextResponse));
ExpectCommonUkm(
content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0);
ExpectTimeSortedTimeDeltaUkm(
{"TimeDelta.RequestStarted", "TimeDelta.ResponseReceived",
"TimeDelta.LoaderCompleted", "TimeDelta.EventLogged"});
// The navigation request and the fetch keepalive request shares the same
// category ID, so they should be paired together in the navigation UKM.
ExpectNavigationUkm(/*category_id=*/1, /*navigation_id=*/std::nullopt,
/*keepalive_token=*/std::nullopt);
}
// Tests the case where a SRP sends two different fetch keepalive category
// requests and then navigates to another page.
IN_PROC_BROWSER_TEST_P(FromGWSNavigationAndKeepAliveRequestBrowserTest,
TwoDifferentCategoryRequestsAndNavigationFromSRP) {
const std::string category1 = "test-prefix1";
const std::string category2 = "test-prefix2";
const std::string target_url1 = GetKeepAliveCategoryRequestUrl(category1);
const std::string target_url2 = GetKeepAliveCategoryRequestUrl(category2);
ASSERT_TRUE(server()->Start());
const std::string nav_target_url =
GetRelativeCategoryUrl("/title1", category2);
// Navigate to search result page.
ASSERT_TRUE(NavigateToURL(web_contents(), GetSearchResultPageURL()));
// Ask the page to
// 1. Send a keepalive request to `target_url` with `category1`.
ASSERT_THAT(EvalJs(web_contents(), JsReplace(R"(
runFetchKeepalive($1, $2);
)",
target_url1, method())),
Eq(k200TextResponse));
// 2. Send a keepalive request to `target_url` with `category2`.
// 3. Navigate to `nav_target_url` with `category2`.
ASSERT_THAT(
EvalJs(web_contents(), JsReplace(R"(
runFetchKeepaliveAndNavigation($1, $2, $3);
)",
target_url2, method(), nav_target_url)),
Eq(k200TextResponse));
ExpectCommonUkms(
{{content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0},
{content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/2,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0}});
// Only request with `category2` should be paired with the navigation
// request.
ExpectNavigationUkm(/*category_id=*/2, /*navigation_id=*/std::nullopt,
/*keepalive_token=*/std::nullopt);
}
// Tests the case where a SRP sends two same fetch keepalive category
// requests and then navigates to another page.
IN_PROC_BROWSER_TEST_P(FromGWSNavigationAndKeepAliveRequestBrowserTest,
TwoSameCategoryRequestsAndNavigationFromSRP) {
const std::string category = "test-prefix1";
const std::string target_url1 = GetKeepAliveCategoryRequestUrl(category);
const std::string target_url2 = GetKeepAliveCategoryRequestUrl(category);
ASSERT_TRUE(server()->Start());
const std::string nav_target_url =
GetRelativeCategoryUrl("/title1", category);
// Navigate to search result page.
ASSERT_TRUE(NavigateToURL(web_contents(), GetSearchResultPageURL()));
// Ask the page to
// 1. Send a keepalive request to `target_url` with `category`.
ASSERT_THAT(EvalJs(web_contents(), JsReplace(R"(
runFetchKeepalive($1, $2);
)",
target_url1, method())),
Eq(k200TextResponse));
// 2. Send a keepalive request to `target_url` with `category`.
// 3. Navigate to `nav_target_url` with `category`.
ASSERT_THAT(
EvalJs(web_contents(), JsReplace(R"(
runFetchKeepaliveAndNavigation($1, $2, $3);
)",
target_url2, method(), nav_target_url)),
Eq(k200TextResponse));
ExpectCommonUkms(
{{content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0},
{content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0}});
// Only one request should be paired with the navigation request, event though
// both requests have the same category ID.
ExpectNavigationUkm(/*category_id=*/1, /*navigation_id=*/std::nullopt,
/*keepalive_token=*/std::nullopt);
}
// Tests the case where a SRP sends one fetch keepalive category
// requests and then navigates to another page twice.
IN_PROC_BROWSER_TEST_P(FromGWSNavigationAndKeepAliveRequestBrowserTest,
OneRequestAndTwoSameCategoryNavigationsFromSRP) {
const std::string category = "test-prefix1";
const std::string target_url = GetKeepAliveCategoryRequestUrl(category);
ASSERT_TRUE(server()->Start());
const std::string nav_target_url =
GetRelativeCategoryUrl("/title1", category);
// Navigate to search result page.
ASSERT_TRUE(NavigateToURL(web_contents(), GetSearchResultPageURL()));
// Ask the page to
// 1. Send a keepalive request to `target_url` with `category`.
// 2. Navigate to `nav_target_url` with `category` for two times.
ASSERT_THAT(EvalJs(web_contents(), JsReplace(R"(
runFetchKeepaliveAndNavigations($1, $2, [$3, $4]);
)",
target_url, method(),
nav_target_url, nav_target_url)),
Eq(k200TextResponse));
ExpectCommonUkm(
content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0);
ExpectTimeSortedTimeDeltaUkm(
{"TimeDelta.RequestStarted", "TimeDelta.ResponseReceived",
"TimeDelta.LoaderCompleted", "TimeDelta.EventLogged"});
// Only one navigation should be paired with the fetch keepalive request.
ExpectNavigationUkm(/*category_id=*/1, /*navigation_id=*/std::nullopt,
/*keepalive_token=*/std::nullopt);
}
// Tests the case where a SRP sends one fetch keepalive category
// requests and then perform two different category navigations.
IN_PROC_BROWSER_TEST_P(FromGWSNavigationAndKeepAliveRequestBrowserTest,
OneRequestAndTwoDifferentCategoryNavigationsFromSRP) {
const std::string category1 = "test-prefix1";
const std::string category2 = "test-prefix2";
const std::string target_url = GetKeepAliveCategoryRequestUrl(category2);
ASSERT_TRUE(server()->Start());
const std::string nav_target_url1 =
GetRelativeCategoryUrl("/title1", category1);
const std::string nav_target_url2 =
GetRelativeCategoryUrl("/title1", category2);
// Navigate to search result page.
ASSERT_TRUE(NavigateToURL(web_contents(), GetSearchResultPageURL()));
// Ask the page to
// 1. Send a keepalive request to `target_url` with `category2`.
// 2. Navigate to `nav_target_url1` and `nav_target_url2` in sequence.
ASSERT_THAT(
EvalJs(web_contents(),
JsReplace(R"(
runFetchKeepaliveAndNavigations($1, $2, [$3, $4]);
)",
target_url, method(), nav_target_url1, nav_target_url2)),
Eq(k200TextResponse));
ExpectCommonUkm(
content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/2,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0);
ExpectTimeSortedTimeDeltaUkm(
{"TimeDelta.RequestStarted", "TimeDelta.ResponseReceived",
"TimeDelta.LoaderCompleted", "TimeDelta.EventLogged"});
// Only one navigation should be paired with the fetch keepalive request.
ExpectNavigationUkm(/*category_id=*/2, /*navigation_id=*/std::nullopt,
/*keepalive_token=*/std::nullopt);
}
// Tests the case where a SRP sends two different fetch keepalive category
// requests and then perform two different navigations.
IN_PROC_BROWSER_TEST_P(
FromGWSNavigationAndKeepAliveRequestBrowserTest,
TwoDifferentCategoryRequestAndTwoDifferentCategoryNavigationsFromSRP) {
const std::string category1 = "test-prefix1";
const std::string category2 = "test-prefix2";
const std::string target_url1 = GetKeepAliveCategoryRequestUrl(category1);
const std::string target_url2 = GetKeepAliveCategoryRequestUrl(category2);
ASSERT_TRUE(server()->Start());
const std::string nav_target_url1 =
GetRelativeCategoryUrl("/title1", category1);
const std::string nav_target_url2 =
GetRelativeCategoryUrl("/title1", category2);
// Navigate to search result page.
ASSERT_TRUE(NavigateToURL(web_contents(), GetSearchResultPageURL()));
// Ask the page to
// 1. Send a keepalive request to `target_url1` with `category1`
// 2. Navigate to `nav_target_url1` with `category1`.
ASSERT_THAT(
EvalJs(web_contents(), JsReplace(R"(
runFetchKeepaliveAndNavigation($1, $2, $3);
)",
target_url1, method(), nav_target_url1)),
Eq(k200TextResponse));
// 3. Send a keepalive request to `target_url2` with `category2`
// 4. Navigate to `nav_target_url2` with `category2`.
ASSERT_THAT(
EvalJs(web_contents(), JsReplace(R"(
runFetchKeepaliveAndNavigation($1, $2, $3);
)",
target_url2, method(), nav_target_url2)),
Eq(k200TextResponse));
ExpectCommonUkms(
{{content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0},
{content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/2,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0}});
// Only one navigation should be paired with the fetch keepalive request.
ExpectNavigationUkms({{/*category_id=*/1, /*navigation_id=*/std::nullopt,
/*keepalive_token=*/std::nullopt},
{/*category_id=*/2, /*navigation_id=*/std::nullopt,
/*keepalive_token=*/std::nullopt}});
}
// Tests the case where a SRP sends a fetch keepalive category request and then
// navigates to another page in a new tab.
IN_PROC_BROWSER_TEST_P(FromGWSNavigationAndKeepAliveRequestBrowserTest,
OneRequestAndNewTabNavigationFromSRP) {
const std::string category = "test-prefix1";
const std::string target_url = GetKeepAliveCategoryRequestUrl(category);
ASSERT_TRUE(server()->Start());
const std::string nav_target_url =
GetRelativeCategoryUrl("/title1", category);
// Navigate to search result page.
ASSERT_TRUE(NavigateToURL(web_contents(), GetSearchResultPageURL()));
// Ask the page to
// 1. Send a keepalive request to `target_url` with `category`.
// 2. Open a new tab and navigate to `nav_target_url` with `category`.
ASSERT_THAT(
EvalJs(web_contents(), JsReplace(R"(
runFetchKeepaliveAndNewTabNavigation($1, $2, $3);
)",
target_url, method(), nav_target_url)),
Eq(k200TextResponse));
ExpectCommonUkm(
content::KeepAliveRequestTracker::RequestType::kFetch,
/*category_id=*/1,
/*num_redirects=*/0,
/*num_retries=*/0,
/*is_context_detached=*/false,
content::KeepAliveRequestTracker::RequestStageType::kLoaderCompleted,
content::KeepAliveRequestTracker::RequestStageType::kResponseReceived,
/*keepalive_token=*/std::nullopt,
/*failed_error_code=*/std::nullopt,
/*failed_extended_error_code=*/std::nullopt,
/*completed_error_code=*/net::OK,
/*completed_extended_error_code=*/0);
ExpectTimeSortedTimeDeltaUkm(
{"TimeDelta.RequestStarted", "TimeDelta.ResponseReceived",
"TimeDelta.LoaderCompleted", "TimeDelta.EventLogged"});
// The navigation request and the fetch keepalive request shares the same
// category ID, so they should be paired together in the navigation UKM.
ExpectNavigationUkm(/*category_id=*/1, /*navigation_id=*/std::nullopt,
/*keepalive_token=*/std::nullopt);
}
|