chromium/chrome/browser/page_load_metrics/observers/gws_abandoned_page_load_metrics_observer_browsertest.cc

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/page_load_metrics/google/browser/gws_abandoned_page_load_metrics_observer.h"

#include <vector>

#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/page_load_metrics/integration_tests/metric_integration_test.h"
#include "chrome/browser/page_load_metrics/observers/chrome_gws_abandoned_page_load_metrics_observer.h"
#include "chrome/browser/page_load_metrics/observers/gws_page_load_metrics_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h"
#include "components/page_load_metrics/browser/page_load_metrics_util.h"
#include "components/page_load_metrics/common/test/page_load_metrics_test_util.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_navigation_throttle.h"
#include "content/public/test/test_navigation_throttle_inserter.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "services/network/public/cpp/network_quality_tracker.h"

namespace {

const char kSRPDomain[] =;
const char kSRPPath[] =;
const char kSRPRedirectPath[] =;

std::unique_ptr<net::test_server::HttpResponse> SRPHandler(
    const net::test_server::HttpRequest& request) {}

std::unique_ptr<net::test_server::HttpResponse> SRPRedirectHandler(
    const net::test_server::HttpRequest& request) {}

}  // namespace

AbandonReason;
NavigationMilestone;
PageLoadMetricsTestWaiter;

class GWSAbandonedPageLoadMetricsObserverBrowserTest
    : public MetricIntegrationTest {};

// Test that a successful navigation to SRP will log all the navigation
// milestones metrics and none of the abandonment metrics.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest, Search) {}

// Test that a successful navigation to a non-SRP page will not log any
// navigation milestones metrics nor any of the abandonment metrics.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       NonSearch) {}

IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       NonSRPRedirectToSRP) {}

IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SRPRedirectToNonSRP) {}

// Test that a successful history navigation to SRP will log all the navigation
// milestones metrics and none of the abandonment metrics, except if the history
// navigation is served from BFCache. In that case, no metric will be recorded.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       HistoryNavigationToSrp) {}

// Test that no metric will be recorded for prerender navigation and activation
// to SRP.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       PrerenderToSrp) {}

// Test SRP navigations that are cancelled by a new navigation, at various
// points during the navigation.  Note we are only testing with throttleable
// milestones for this test since the new navigation might take a while to
// arrive on the browser side, and the oldnavigation might have advanced if
// it's not actually paused.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchCancelledByNewNavigation) {}

// Test SRP navigations that are cancelled by `content::WebContents::Stop()`
// (which can be triggered by e.g. the stop button), at various points during
// the navigation.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchCancelledByWebContentsStop) {}

// Test SRP navigations that are abandoned because the WebContents is hidden
// at various points during the navigation. Note that the navigation itself
// might continue to commit, but we will count it as "abandoned" as soon as it's
// hidden and stop recording navigation milestones metrics after that.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchTabHidden) {}

// Similar to SearchTabHidden, but the navigation starts out with a non-SRP
// URL, that later redirects to SRP.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       NonSRPRedirectToSRP_Hidden) {}

// Similar to SearchTabHidden, but after the navigation is abandoned for the
// first time due to hiding, it gets abandoned again for the second time by
// explicit cancellation.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchTabHiddenThenStopped) {}

// Similar to above, but the navigation starts out with a non-SRP URL, that
// later redirects to SRP.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchTabHiddenThenStopped_NonSRPRedirectToSRP) {}

// Test that if a navigation was abandoned by hiding multiple times, only the
// first hiding will be logged.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchTabHiddenMultipleTimes) {}

// Test SRP navigations that are cancelled by closing the WebContents at various
// points during the navigation. Note we are only testing with throttleable
// milestones for this teset since the close notification might take a while to
// arrive on the browser side, and the navigation might have advanced if it's
// not actually paused.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchCancelledByTabClose) {}

// Test SRP navigations that are cancelled by a NavigationThrottle at various
// points during the navigation.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchCancelledByNavigationThrottle) {}

// Test SRP navigations that are turned to commit an error page by
// a NavigationThrottle at various points during the navigation. Note that the
// navigation itself will commit, but since it's committing an error page
// instead of SRP, we will count it as "abandoned" as soon as it's turned into
// an error page, and stop recording navigation milestone metrics after that.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchTurnedToErrorPageByNavigationThrottle) {}

// Test SRP navigations that are cancelled because the renderer process picked
// for it crashed. Note that this is only checking the case where the crash
// happens after we get the final response, since the final RenderFrameHost for
// the navigation only start being exposed at that point.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       SearchCancelledByRenderProcessGone) {}

// TODO(crbug.com/352578800): Flaky.
IN_PROC_BROWSER_TEST_F(GWSAbandonedPageLoadMetricsObserverBrowserTest,
                       DISABLED_TabHidden) {}

// TODO(https://crbug.com/347706997): Test backgrounded case.