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 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
|
// 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 <vector>
#include "base/strings/strcat.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/apps/app_service/app_registry_cache_waiter.h"
#include "chrome/browser/apps/link_capturing/link_capturing_feature_test_support.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/web_app_browser_controller.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom-shared.h"
#include "chrome/browser/web_applications/navigation_capturing_metrics.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/metrics/content/subprocess_metrics_provider.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "third_party/blink/public/common/features_generated.h"
#include "third_party/blink/public/mojom/manifest/manifest_launch_handler.mojom-shared.h"
#include "ui/base/window_open_disposition.h"
namespace web_app {
namespace {
constexpr char kLandingPage[] = "/web_apps/simple/index.html";
constexpr char kFinalPage[] = "/web_apps/simple/index2.html";
constexpr char kRedirectFromPage[] = "/web_apps/simple2/index.html";
constexpr char kAppNoManifestUrl[] = "/web_apps/no_manifest.html";
constexpr char kNavigateExistingUrl[] =
"/web_apps/simple_navigate_existing/index.html";
constexpr char kNavigateExistingSecondUrl[] =
"/web_apps/simple_navigate_existing/index2.html";
constexpr char kFocusExistingUrl[] =
"/web_apps/simple_focus_existing/index.html";
constexpr char kFocusExistingSecondUrl[] =
"/web_apps/simple_focus_existing/index2.html";
constexpr char kLaunchParamsEnqueueMetricWithNavigation[] =
"WebApp.NavigationCapturing.LaunchParamsConsumedTime.WithNavigation";
constexpr char kLaunchParamsEnqueueMetricWithoutNavigation[] =
"WebApp.NavigationCapturing.LaunchParamsConsumedTime.WithoutNavigation";
// Actually start a navigation in an existing web contents for the
// `navigate-existing` use-case.
// This is copied from `LoadURLInContents` in browser_navigator.cc.
void LoadURLInContents(content::WebContents* target_contents,
const GURL& url,
const NavigateParams& params) {
content::NavigationController::LoadURLParams load_url_params(url);
load_url_params.initiator_frame_token = params.initiator_frame_token;
load_url_params.initiator_process_id = params.initiator_process_id;
load_url_params.initiator_origin = params.initiator_origin;
load_url_params.initiator_base_url = params.initiator_base_url;
load_url_params.source_site_instance = params.source_site_instance;
load_url_params.referrer = params.referrer;
load_url_params.frame_name = params.frame_name;
load_url_params.frame_tree_node_id = params.frame_tree_node_id;
load_url_params.redirect_chain = params.redirect_chain;
load_url_params.transition_type = params.transition;
load_url_params.extra_headers = params.extra_headers;
load_url_params.should_replace_current_entry =
params.should_replace_current_entry;
load_url_params.is_renderer_initiated = params.is_renderer_initiated;
load_url_params.started_from_context_menu = params.started_from_context_menu;
load_url_params.has_user_gesture = params.user_gesture;
load_url_params.blob_url_loader_factory = params.blob_url_loader_factory;
load_url_params.input_start = params.input_start;
load_url_params.was_activated = params.was_activated;
load_url_params.href_translate = params.href_translate;
load_url_params.reload_type = params.reload_type;
load_url_params.impression = params.impression;
load_url_params.suggested_system_entropy = params.suggested_system_entropy;
target_contents->GetController().LoadURLWithParams(load_url_params);
}
class NavigationCapturingBrowserNavigatorBrowserTest
: public WebAppBrowserTestBase {
public:
NavigationCapturingBrowserNavigatorBrowserTest() {
#if BUILDFLAG(IS_CHROMEOS)
enabled_features = apps::test::GetFeaturesToEnableLinkCapturingUX(
apps::test::LinkCapturingFeatureVersion::kV2DefaultOff);
#else
enabled_features = apps::test::GetFeaturesToEnableLinkCapturingUX(
apps::test::LinkCapturingFeatureVersion::kV2DefaultOn);
#endif
enabled_features.emplace_back(blink::features::kDesktopPWAsTabStrip,
base::FieldTrialParams());
enabled_features.emplace_back(
blink::features::kDesktopPWAsTabStripCustomizations,
base::FieldTrialParams());
feature_list_.InitWithFeaturesAndParameters(
/*enabled_features=*/enabled_features,
/*disabled_features=*/{});
}
void SetUpOnMainThread() override {
WebAppBrowserTestBase::SetUpOnMainThread();
ASSERT_TRUE(embedded_test_server()->Start());
}
GURL GetLandingPage() const {
return embedded_test_server()->GetURL(kLandingPage);
}
GURL GetFinalPage() const {
return embedded_test_server()->GetURL(kFinalPage);
}
GURL GetAppNoManifestUrl() const {
return embedded_test_server()->GetURL(kAppNoManifestUrl);
}
GURL GetNavigateExistingUrl() const {
return embedded_test_server()->GetURL(kNavigateExistingUrl);
}
GURL GetNavigateExistingSecondUrl() const {
return embedded_test_server()->GetURL(kNavigateExistingSecondUrl);
}
GURL GetFocusExistingUrl() const {
return embedded_test_server()->GetURL(kFocusExistingUrl);
}
GURL GetFocusExistingSecondUrl() const {
return embedded_test_server()->GetURL(kFocusExistingSecondUrl);
}
// InstallTestWebApp should not be called with a url that has a manifest link.
// This may cause flaky tests as it will be susceptible to manifest update as
// soon as url loads. Instead, consider using test::InstallWebApp().
webapps::AppId InstallTestWebApp(
const GURL& start_url,
mojom::UserDisplayMode user_display_mode,
blink::mojom::ManifestLaunchHandler_ClientMode client_mode =
blink::mojom::ManifestLaunchHandler_ClientMode::kAuto) {
auto web_app_info =
WebAppInstallInfo::CreateWithStartUrlForTesting(start_url);
web_app_info->scope = start_url.GetWithoutFilename();
web_app_info->user_display_mode = user_display_mode;
web_app_info->launch_handler = blink::Manifest::LaunchHandler(client_mode);
const webapps::AppId app_id =
test::InstallWebApp(profile(), std::move(web_app_info));
#if BUILDFLAG(IS_CHROMEOS)
EXPECT_EQ(apps::test::EnableLinkCapturingByUser(profile(), app_id),
base::ok());
#endif
return app_id;
}
std::pair<Browser*, Browser*> GetTwoDistinctBrowsersForSameApp(
const webapps::AppId& app_id,
const GURL& url_to_navigate_to) {
// First, launch an app browser.
Browser* app_browser_to_use = LaunchWebAppBrowser(app_id);
// Second, create another app browser (LaunchWebAppBrowser() will not work
// since the launch handling mode makes it look for an existing instance).
// Mimic navigation capturing via shift click into a new window.
Browser* second_app_browser = nullptr;
{
NavigateParams params(app_browser_to_use, url_to_navigate_to,
ui::PAGE_TRANSITION_LINK);
params.disposition = WindowOpenDisposition::NEW_WINDOW;
Navigate(¶ms);
second_app_browser = params.browser;
}
EXPECT_NE(nullptr, second_app_browser);
EXPECT_NE(second_app_browser, app_browser_to_use);
test::CompletePageLoadForAllWebContents();
return {app_browser_to_use, second_app_browser};
}
protected:
void AwaitMetricsAvailableFromRenderer() {
content::FetchHistogramsFromChildProcesses();
metrics::SubprocessMetricsProvider::MergeHistogramDeltasForTesting();
}
std::vector<NavigationCapturingDisplayModeResult>
GetNavigationCapturingFinalDisplayMetric(
const base::HistogramTester& tester) {
std::vector<base::Bucket> display_result_buckets =
tester.GetAllSamples("Webapp.NavigationCapturing.FinalDisplay.Result");
std::vector<NavigationCapturingDisplayModeResult> bucket_list;
for (const base::Bucket& bucket : display_result_buckets) {
for (int count = 0; count < bucket.count; count++) {
bucket_list.push_back(
static_cast<NavigationCapturingDisplayModeResult>(bucket.min));
}
}
return bucket_list;
}
base::test::ScopedFeatureList feature_list_;
std::vector<base::test::FeatureRefAndParams> enabled_features;
};
IN_PROC_BROWSER_TEST_F(NavigationCapturingBrowserNavigatorBrowserTest,
NavigateBrowserUserForBrowserTabAppLaunch) {
base::HistogramTester histograms;
// Test that the browser provided in NavigateParams is used when using a
// browser to open a browser tab app in a tab, instead of the most recently
// active browser.
InstallTestWebApp(GetLandingPage(), mojom::UserDisplayMode::kBrowser);
// Create a new browser which will be considered the most recently active one.
Browser* new_browser =
ui_test_utils::OpenNewEmptyWindowAndWaitUntilActivated(profile());
chrome::NewTab(new_browser);
// Do a capturable navigation to the landing page, and ensure that it opens in
// the browser().
ui_test_utils::AllBrowserTabAddedWaiter new_tab_observer;
NavigateParams params(browser(), GetLandingPage(), ui::PAGE_TRANSITION_LINK);
params.source_contents =
new_browser->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
content::WebContents* new_tab = new_tab_observer.Wait();
content::WaitForLoadStop(new_tab);
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
// Ensure that capturing happened.
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppBrowserTabFinalBrowserTab));
ASSERT_TRUE(new_tab);
// Make sure that web contents is a tab in `browser()` and not `new_browser`.
EXPECT_NE(browser()->tab_strip_model()->GetIndexOfWebContents(new_tab),
TabStripModel::kNoTab);
}
// Test that the browser provided in NavigateParams is used when finding an app
// window for navigation capturing to happen for navigate-existing, provided the
// `app_id` is the same.
IN_PROC_BROWSER_TEST_F(NavigationCapturingBrowserNavigatorBrowserTest,
NavigateBrowserUsedForNavigateExistingAppWindow) {
const webapps::AppId& app_id = InstallWebAppFromPageAndCloseAppBrowser(
browser(), GetNavigateExistingUrl());
#if BUILDFLAG(IS_CHROMEOS)
EXPECT_EQ(apps::test::EnableLinkCapturingByUser(profile(), app_id),
base::ok());
#endif
Browser* app_browser_1 = nullptr;
Browser* app_browser_2 = nullptr;
std::tie(app_browser_1, app_browser_2) =
GetTwoDistinctBrowsersForSameApp(app_id, GetNavigateExistingUrl());
EXPECT_TRUE(WebAppBrowserController::IsForWebApp(app_browser_1, app_id));
EXPECT_TRUE(WebAppBrowserController::IsForWebApp(app_browser_2, app_id));
// Do a capturable navigation to the landing page, and ensure that it opens in
// `app_browser_1`.
base::HistogramTester histograms;
ui_test_utils::UrlLoadObserver url_observer(GetNavigateExistingSecondUrl());
{
NavigateParams params(app_browser_1, GetNavigateExistingSecondUrl(),
ui::PAGE_TRANSITION_LINK);
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
params.source_contents =
browser()->tab_strip_model()->GetActiveWebContents();
Navigate(¶ms);
LoadURLInContents(params.navigated_or_inserted_contents,
GetNavigateExistingSecondUrl(), params);
}
url_observer.Wait();
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
content::WebContents* contents_navigation_happened_in =
url_observer.web_contents();
EXPECT_NE(contents_navigation_happened_in,
app_browser_2->tab_strip_model()->GetActiveWebContents());
EXPECT_EQ(contents_navigation_happened_in,
app_browser_1->tab_strip_model()->GetActiveWebContents());
// Ensure that capturing happened.
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppStandaloneFinalStandalone));
// This is measured twice, once for each launch param obtained.
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 2);
}
// Test that the browser provided in NavigateParams is used when finding an app
// window for navigation capturing to happen for focus-existing, provided the
// `app_id` is the same.
IN_PROC_BROWSER_TEST_F(NavigationCapturingBrowserNavigatorBrowserTest,
NavigateBrowserUsedForFocusExistingAppWindow) {
const webapps::AppId& app_id =
InstallWebAppFromPageAndCloseAppBrowser(browser(), GetFocusExistingUrl());
#if BUILDFLAG(IS_CHROMEOS)
EXPECT_EQ(apps::test::EnableLinkCapturingByUser(profile(), app_id),
base::ok());
#endif
// Launch 2 distinct app_browsers for the same app_id. Since `app_browser_2`
// is created last, ensure that is activated.
Browser* app_browser_1 = nullptr;
Browser* app_browser_2 = nullptr;
std::tie(app_browser_1, app_browser_2) =
GetTwoDistinctBrowsersForSameApp(app_id, GetFocusExistingUrl());
EXPECT_TRUE(WebAppBrowserController::IsForWebApp(app_browser_1, app_id));
EXPECT_TRUE(WebAppBrowserController::IsForWebApp(app_browser_2, app_id));
// Do a capturable navigation to the landing page, and ensure that it opens in
// `app_browser_1`. Since the web_app has a client_mode of `focus-existing`,
// `app_browser_1` should be activated with no navigations happening.
base::HistogramTester histograms;
{
NavigateParams params(app_browser_1, GetFocusExistingSecondUrl(),
ui::PAGE_TRANSITION_LINK);
params.source_contents =
browser()->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
}
test::CompletePageLoadForAllWebContents();
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
content::WebContents* contents_to_finish =
app_browser_1->tab_strip_model()->GetActiveWebContents();
// `kFocusExistingUrl` should be obtained first when the browser is launched,
// and `kFocusExistingSecondUrl` is added later when navigation capturing
// happens.
EXPECT_THAT(
apps::test::GetLaunchParamUrlsInContents(contents_to_finish,
"launchParamsTargetUrls"),
testing::ElementsAre(GetFocusExistingUrl(), GetFocusExistingSecondUrl()));
// `app_browser_1` should still be at the starting page.
EXPECT_EQ(GetFocusExistingUrl(), app_browser_1->tab_strip_model()
->GetActiveWebContents()
->GetLastCommittedURL());
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppStandaloneFinalStandalone));
// This is measured twice, once for each launch param obtained. The first
// metric is measured when the navigate-existing container enqueues the launch
// params, while the 2nd metric is measured when the focus-existing container
// does so.
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithoutNavigation, 1);
}
IN_PROC_BROWSER_TEST_F(NavigationCapturingBrowserNavigatorBrowserTest,
FocusExistingWithBrowserAvoidsOutOfScope) {
const webapps::AppId& app_id = InstallTestWebApp(
GetLandingPage(), mojom::UserDisplayMode::kStandalone,
blink::mojom::ManifestLaunchHandler_ClientMode::kFocusExisting);
GURL out_of_scope =
embedded_test_server()->GetURL("/web_apps/simple2/index.html");
// Open the browser and navigate to out-of-scope url.
Browser* app_browser = LaunchWebAppBrowser(app_id);
ASSERT_TRUE(ui_test_utils::NavigateToURL(app_browser, out_of_scope));
content::WaitForLoadStop(
app_browser->tab_strip_model()->GetActiveWebContents());
// Populate the browser for a focus-existing navigation, which should reject
// it because the current web contents is not in-scope of the app. And thus
// create a new window.
base::HistogramTester histograms;
ui_test_utils::BrowserChangeObserver added(
/*browser=*/nullptr,
ui_test_utils::BrowserChangeObserver::ChangeType::kAdded);
{
NavigateParams params(app_browser, GetLandingPage(),
ui::PAGE_TRANSITION_LINK);
params.source_contents =
browser()->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
}
Browser* new_app_browser = added.Wait();
test::CompletePageLoadForAllWebContents();
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
EXPECT_EQ(out_of_scope, app_browser->tab_strip_model()
->GetActiveWebContents()
->GetLastCommittedURL());
EXPECT_EQ(GetLandingPage(), new_app_browser->tab_strip_model()
->GetActiveWebContents()
->GetLastCommittedURL());
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppStandaloneFinalStandalone));
}
IN_PROC_BROWSER_TEST_F(NavigationCapturingBrowserNavigatorBrowserTest,
NavigateExistingIgnoresNonHtml) {
const webapps::AppId& app_id = InstallTestWebApp(
GetLandingPage(), mojom::UserDisplayMode::kStandalone,
blink::mojom::ManifestLaunchHandler_ClientMode::kFocusExisting);
GURL image_url =
embedded_test_server()->GetURL("/web_apps/simple/basic-48.png");
// Open the browser and navigate to an in-scope image (non-html item);
Browser* app_browser = LaunchWebAppBrowser(app_id);
ASSERT_TRUE(ui_test_utils::NavigateToURL(app_browser, image_url));
content::WaitForLoadStop(
app_browser->tab_strip_model()->GetActiveWebContents());
// Do a capturable navigation to the landing page, and ensure that it opens in
// a new browser;
base::HistogramTester histograms;
ui_test_utils::BrowserChangeObserver added(
/*browser=*/nullptr,
ui_test_utils::BrowserChangeObserver::ChangeType::kAdded);
{
NavigateParams params(browser(), GetLandingPage(),
ui::PAGE_TRANSITION_LINK);
params.source_contents =
browser()->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
}
Browser* new_app_browser = added.Wait();
test::CompletePageLoadForAllWebContents();
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
EXPECT_EQ(image_url, app_browser->tab_strip_model()
->GetActiveWebContents()
->GetLastCommittedURL());
EXPECT_EQ(GetLandingPage(), new_app_browser->tab_strip_model()
->GetActiveWebContents()
->GetLastCommittedURL());
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppStandaloneFinalStandalone));
}
IN_PROC_BROWSER_TEST_F(NavigationCapturingBrowserNavigatorBrowserTest,
NavigateBrowserUsedForNavigateExistingToBrowserTab) {
// Test that the browser provided in NavigateParams is used when using a
// browser to open a browser tab, instead of the most recently active browser.
const webapps::AppId& app_id = InstallTestWebApp(
GetAppNoManifestUrl(), mojom::UserDisplayMode::kBrowser,
blink::mojom::ManifestLaunchHandler_ClientMode::kNavigateExisting);
// Navigate to GetAppUrl() in browser() so that a browser tab for the app_id
// exists.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetAppNoManifestUrl()));
content::WaitForLoadStop(
browser()->tab_strip_model()->GetActiveWebContents());
const content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
int tab_index = browser()->tab_strip_model()->GetIndexOfWebContents(contents);
int tab_count_for_browser = browser()->tab_strip_model()->count();
// Create a new browser which will be considered the most recently active
// one.
Browser* new_browser =
ui_test_utils::OpenNewEmptyWindowAndWaitUntilActivated(profile());
chrome::NewTab(new_browser);
// Do a capturable navigation to the landing page, and ensure that it
// opens in the browser().
base::HistogramTester histograms;
ui_test_utils::UrlLoadObserver url_observer(GetFinalPage());
{
NavigateParams params(browser(), GetFinalPage(), ui::PAGE_TRANSITION_LINK);
params.source_contents =
new_browser->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
LoadURLInContents(params.navigated_or_inserted_contents, GetFinalPage(),
params);
}
test::CompletePageLoadForAllWebContents();
url_observer.Wait();
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
content::WebContents* contents_navigation_happened_in =
url_observer.web_contents();
// Ensure that capturing happened.
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppBrowserTabFinalBrowserTab));
EXPECT_NE(contents_navigation_happened_in,
new_browser->tab_strip_model()->GetActiveWebContents());
EXPECT_EQ(contents_navigation_happened_in,
browser()->tab_strip_model()->GetActiveWebContents());
EXPECT_EQ(tab_index, browser()->tab_strip_model()->GetIndexOfWebContents(
contents_navigation_happened_in));
EXPECT_EQ(tab_count_for_browser, browser()->tab_strip_model()->count());
}
IN_PROC_BROWSER_TEST_F(NavigationCapturingBrowserNavigatorBrowserTest,
NavigateBrowserUsedForFocusExistingToBrowserTab) {
// Test that the browser provided in NavigateParams is used when using a
// browser to open a browser tab, instead of the most recently active browser.
const webapps::AppId& app_id = InstallTestWebApp(
GetAppNoManifestUrl(), mojom::UserDisplayMode::kBrowser,
blink::mojom::ManifestLaunchHandler_ClientMode::kFocusExisting);
// Navigate to GetAppUrl() in browser() so that a browser tab for the
// app_id exists.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetAppNoManifestUrl()));
content::WaitForLoadStop(
browser()->tab_strip_model()->GetActiveWebContents());
int tab_count_for_browser = browser()->tab_strip_model()->count();
// Create a new browser which will be considered the most recently active
// one.
Browser* new_browser =
ui_test_utils::OpenNewEmptyWindowAndWaitUntilActivated(profile());
chrome::NewTab(new_browser);
// Do a capturable navigation to the landing page, and ensure that it opens in
// browser(). Since the web_app has a client_mode of `focus-existing`,
// browser() should be activated with no navigations happening.
base::HistogramTester histograms;
{
NavigateParams params(browser(), GetFinalPage(), ui::PAGE_TRANSITION_LINK);
params.source_contents =
new_browser->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
}
test::CompletePageLoadForAllWebContents();
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
// Ensure that capturing happened.
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
// With the absence of a consumer set on the site, launch params will not be
// enqueued, and hence this metric will not be measured.
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 0);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppBrowserTabFinalBrowserTab));
// browser() should still be at the GetAppUrl() page.
EXPECT_EQ(GetAppNoManifestUrl(), browser()
->tab_strip_model()
->GetActiveWebContents()
->GetLastCommittedURL());
EXPECT_EQ(tab_count_for_browser, browser()->tab_strip_model()->count());
}
IN_PROC_BROWSER_TEST_F(
NavigationCapturingBrowserNavigatorBrowserTest,
NavigateBrowserUsedForNavigateExistingToAppBrowserTabStandalone) {
// Test that the app browser provided in NavigateParams is used even if a
// separate browser is populated with a matching tab.
const webapps::AppId& app_id = InstallWebAppFromPageAndCloseAppBrowser(
browser(), GetNavigateExistingUrl());
#if BUILDFLAG(IS_CHROMEOS)
EXPECT_EQ(apps::test::EnableLinkCapturingByUser(profile(), app_id),
base::ok());
#endif
// Launch app in an app browser that will be passed in as the browser for
// NavigateParams.
Browser* app_browser_to_use = LaunchWebAppBrowser(app_id);
// Change the web app's user display mode to kBrowser
base::test::TestFuture<void> future;
provider().scheduler().SetUserDisplayMode(
app_id, mojom::UserDisplayMode::kBrowser, future.GetCallback());
ASSERT_TRUE(future.Wait());
// Navigate to GetNavigateExistingUrl in browser() so that a browser tab for
// the app_id exists.
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GetNavigateExistingUrl()));
content::WebContents* blank_new_tab = chrome::AddAndReturnTabAt(
browser(), GURL(url::kAboutBlankURL), /*index=*/0, /*foreground=*/true);
content::WaitForLoadStop(
browser()->tab_strip_model()->GetActiveWebContents());
// Do a capturable navigation to the landing page, and ensure that it
// opens in the app_browser_to_use instead of browser().
base::HistogramTester histograms;
ui_test_utils::UrlLoadObserver url_observer(GetNavigateExistingSecondUrl());
{
NavigateParams params(app_browser_to_use, GetNavigateExistingSecondUrl(),
ui::PAGE_TRANSITION_LINK);
params.source_contents = blank_new_tab;
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
LoadURLInContents(params.navigated_or_inserted_contents,
GetNavigateExistingSecondUrl(), params);
}
test::CompletePageLoadForAllWebContents();
url_observer.Wait();
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
content::WebContents* contents_navigation_happened_in =
url_observer.web_contents();
EXPECT_NE(contents_navigation_happened_in, blank_new_tab);
// Ensure that capturing happened.
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppBrowserTabFinalStandalone));
EXPECT_EQ(contents_navigation_happened_in,
app_browser_to_use->tab_strip_model()->GetActiveWebContents());
EXPECT_EQ(0, app_browser_to_use->tab_strip_model()->GetIndexOfWebContents(
contents_navigation_happened_in));
EXPECT_EQ(TabStripModel::kNoTab,
browser()->tab_strip_model()->GetIndexOfWebContents(
contents_navigation_happened_in));
}
IN_PROC_BROWSER_TEST_F(
NavigationCapturingBrowserNavigatorBrowserTest,
NavigateBrowserUsedForFocusExistingToAppBrowserTabStandalone) {
// Test that the app browser provided in NavigateParams is used even if a
// separate browser is populated with a matching tab.
const webapps::AppId& app_id =
InstallWebAppFromPageAndCloseAppBrowser(browser(), GetFocusExistingUrl());
#if BUILDFLAG(IS_CHROMEOS)
EXPECT_EQ(apps::test::EnableLinkCapturingByUser(profile(), app_id),
base::ok());
#endif
// Launch app in an app browser that will be passed in as the browser for
// NavigateParams.
Browser* app_browser_to_use = LaunchWebAppBrowser(app_id);
// Change the web app's user display mode to kBrowser
base::test::TestFuture<void> future;
provider().scheduler().SetUserDisplayMode(
app_id, mojom::UserDisplayMode::kBrowser, future.GetCallback());
ASSERT_TRUE(future.Wait());
// Navigate to GetFocusExistingUrl() in browser() so that a browser tab for
// the app_id exists.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetFocusExistingUrl()));
content::WaitForLoadStop(
browser()->tab_strip_model()->GetActiveWebContents());
// Do a capturable navigation to the landing page, and ensure that it opens in
// browser(). Since the web_app has a client_mode of `focus-existing`,
// browser() should be activated with no navigations happening.
base::HistogramTester histograms;
{
NavigateParams params(app_browser_to_use, GetFocusExistingSecondUrl(),
ui::PAGE_TRANSITION_LINK);
params.source_contents =
browser()->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
}
test::CompletePageLoadForAllWebContents();
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
// Ensure that capturing happened.
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppBrowserTabFinalBrowserTab));
EXPECT_EQ(GetFocusExistingUrl(), app_browser_to_use->tab_strip_model()
->GetActiveWebContents()
->GetLastCommittedURL());
}
using LaunchQueueLatencyMetricBrowserTest =
NavigationCapturingBrowserNavigatorBrowserTest;
IN_PROC_BROWSER_TEST_F(LaunchQueueLatencyMetricBrowserTest,
ReloadsDoNotMeasure) {
base::HistogramTester histograms;
// Test that the browser provided in NavigateParams is used when using a
// browser to open a browser tab app in a tab, instead of the most recently
// active browser.
InstallTestWebApp(GetLandingPage(), mojom::UserDisplayMode::kBrowser);
// Create a new browser which will be considered the most recently active one.
Browser* new_browser =
ui_test_utils::OpenNewEmptyWindowAndWaitUntilActivated(profile());
chrome::NewTab(new_browser);
// Do a capturable navigation to the landing page, and ensure that it opens in
// the browser().
ui_test_utils::AllBrowserTabAddedWaiter new_tab_observer;
{
NavigateParams params(browser(), GetLandingPage(),
ui::PAGE_TRANSITION_LINK);
params.source_contents =
new_browser->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
}
// Measure the first set of launch params that are enqueued.
content::WebContents* new_tab = new_tab_observer.Wait();
content::WaitForLoadStop(new_tab);
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
EXPECT_THAT(apps::test::GetLaunchParamUrlsInContents(
new_tab, "launchParamsTargetUrls"),
testing::ElementsAre(GetLandingPage()));
// Reloading the tab should not measure the latency of launch params being
// enqueued again, but the launch params will still be resent.
chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
content::WaitForLoadStop(new_tab);
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
EXPECT_THAT(apps::test::GetLaunchParamUrlsInContents(
new_tab, "launchParamsTargetUrls"),
testing::ElementsAre(GetLandingPage()));
}
using LaunchContainerMetricMeasurementTest =
NavigationCapturingBrowserNavigatorBrowserTest;
IN_PROC_BROWSER_TEST_F(LaunchContainerMetricMeasurementTest,
NavigateExistingStandaloneToTab) {
// Load 'kNavigateExistingUrl` and `kFocusExistingUrl` in new tabs.
chrome::NewTab(browser());
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GetNavigateExistingUrl()));
content::WebContents* target_contents =
browser()->tab_strip_model()->GetActiveWebContents();
chrome::NewTab(browser());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetFocusExistingUrl()));
// Install both apps.
const webapps::AppId& source_app =
InstallWebAppFromPageAndCloseAppBrowser(browser(), GetFocusExistingUrl());
const webapps::AppId& dest_app = InstallWebAppFromPageAndCloseAppBrowser(
browser(), GetNavigateExistingUrl());
#if BUILDFLAG(IS_CHROMEOS)
EXPECT_EQ(apps::test::EnableLinkCapturingByUser(profile(), dest_app),
base::ok());
#endif
// Trigger a navigation to `kNavigateExistingUrl`. This should end up in the
// browser tab.
base::HistogramTester histograms;
{
NavigateParams params(profile(), GetNavigateExistingUrl(),
ui::PAGE_TRANSITION_LINK);
params.source_contents =
browser()->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
LoadURLInContents(target_contents, GetNavigateExistingUrl(), params);
}
content::WaitForLoadStop(target_contents);
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
// Verify that navigation did indeed end up in the browser tab via navigation
// capturing.
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppStandaloneFinalBrowserTab));
}
class NavigationCapturingWithRedirectionBrowserNavigatorTest
: public NavigationCapturingBrowserNavigatorBrowserTest {
public:
void SetUpOnMainThread() override {
// Set up redirection before calling the `SetUpOnMainThread` on the parent
// class, as that will start the server.
embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
&NavigationCapturingWithRedirectionBrowserNavigatorTest::
HandleRedirection,
base::Unretained(this)));
NavigationCapturingBrowserNavigatorBrowserTest::SetUpOnMainThread();
}
GURL GetRedirectFromPage() const {
return embedded_test_server()->GetURL(kRedirectFromPage);
}
std::unique_ptr<net::test_server::HttpResponse> HandleRedirection(
const net::test_server::HttpRequest& request) {
if (request.GetURL() != GetRedirectFromPage()) {
return nullptr;
}
GURL redirect_to = GetLandingPage();
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_code(net::HTTP_TEMPORARY_REDIRECT);
response->set_content_type("text/html");
response->AddCustomHeader("Location", redirect_to.spec());
response->AddCustomHeader("Access-Control-Allow-Origin", "*");
response->set_content(base::StrCat(
{"<!doctype html><p>Redirecting to %s", redirect_to.spec().c_str()}));
return response;
}
};
IN_PROC_BROWSER_TEST_F(NavigationCapturingWithRedirectionBrowserNavigatorTest,
NavigateBrowserUsedForBrowserTabAppLaunch) {
// Test that the browser provided in NavigateParams is respected after it is
// initially captured into an app window, only to be determined to need a
// browser tabbed app after redirection.
InstallTestWebApp(GetLandingPage(), mojom::UserDisplayMode::kBrowser);
InstallTestWebApp(GetRedirectFromPage(), mojom::UserDisplayMode::kStandalone);
// Create a new browser which will be considered the most recently active one.
Browser* new_browser =
ui_test_utils::OpenNewEmptyWindowAndWaitUntilActivated(profile());
chrome::NewTab(new_browser);
// Do a capturable navigation to kRedirectFromPage (which redirects to
// kLandingPage), and ensure that it opens in the browser().
ui_test_utils::AllBrowserTabAddedWaiter new_tab_observer;
base::HistogramTester histograms;
NavigateParams params(browser(), GetRedirectFromPage(),
ui::PAGE_TRANSITION_LINK);
params.source_contents =
new_browser->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
content::WebContents* new_tab = new_tab_observer.Wait();
content::WaitForLoadStop(new_tab);
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
// Ensure that capturing happened.
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 1);
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 1);
// Make sure that web contents is a tab in `browser()` and not `new_browser`.
EXPECT_NE(browser()->tab_strip_model()->GetIndexOfWebContents(new_tab),
TabStripModel::kNoTab);
EXPECT_THAT(
GetNavigationCapturingFinalDisplayMetric(histograms),
testing::ElementsAre(
NavigationCapturingDisplayModeResult::kAppBrowserTabFinalBrowserTab));
}
IN_PROC_BROWSER_TEST_F(NavigationCapturingWithRedirectionBrowserNavigatorTest,
NavigateBrowserUsedForBrowserTabLaunch) {
// Test that the browser provided in NavigateParams is respected after it is
// initially captured into an app window, only to be determined to need a
// browser tab after redirection.
InstallTestWebApp(GetRedirectFromPage(), mojom::UserDisplayMode::kStandalone);
// Create a new browser which will be considered the most recently active one.
Browser* new_browser =
ui_test_utils::OpenNewEmptyWindowAndWaitUntilActivated(profile());
chrome::NewTab(new_browser);
// Do a capturable navigation to kRedirectFromPage (which redirects to
// kLandingPage), and ensure that it opens in the browser().
ui_test_utils::AllBrowserTabAddedWaiter new_tab_observer;
base::HistogramTester histograms;
NavigateParams params(browser(), GetRedirectFromPage(),
ui::PAGE_TRANSITION_LINK);
params.source_contents =
new_browser->tab_strip_model()->GetActiveWebContents();
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
Navigate(¶ms);
content::WebContents* new_tab = new_tab_observer.Wait();
content::WaitForLoadStop(new_tab);
apps::test::FlushLaunchQueuesForAllBrowserTabs();
AwaitMetricsAvailableFromRenderer();
histograms.ExpectUniqueSample(
"WebApp.LaunchSource", apps::LaunchSource::kFromNavigationCapturing, 0);
histograms.ExpectTotalCount(kLaunchParamsEnqueueMetricWithNavigation, 0);
EXPECT_THAT(GetNavigationCapturingFinalDisplayMetric(histograms),
testing::IsEmpty());
// Make sure that web contents is a tab in `browser()` and not `new_browser`.
EXPECT_NE(browser()->tab_strip_model()->GetIndexOfWebContents(new_tab),
TabStripModel::kNoTab);
}
} // namespace
} // namespace web_app
|