chromium/chrome/browser/dips/dips_navigation_flow_detector_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 "chrome/browser/dips/dips_navigation_flow_detector.h"

#include "base/base64.h"
#include "base/test/bind.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "base/test/test_future.h"
#include "base/test/test_timeouts.h"
#include "base/types/expected.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/dips/dips_navigation_flow_detector_wrapper.h"
#include "chrome/browser/dips/dips_service.h"
#include "chrome/browser/dips/dips_test_utils.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/platform_browser_test.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/privacy_sandbox/privacy_sandbox_attestations/privacy_sandbox_attestations.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/attribution_data_model.h"
#include "content/public/browser/network_service_instance.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 "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/metrics/public/cpp/metrics_utils.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/network/test/trust_token_request_handler.h"
#include "services/network/test/trust_token_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ssl/cert_verifier_browser_test.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/browser/scoped_authenticator_environment_for_testing.h"
#include "content/public/common/content_switches.h"
#include "device/fido/virtual_fido_device_factory.h"
#endif  // !BUILDFLAG(IS_ANDROID)

namespace {

AttributionData;

std::vector<url::Origin> GetOrigins(const AttributionData& data) {}

const char* StringifyBooleanMetric(ukm::TestAutoSetUkmRecorder* ukm_recorder,
                                   const ukm::mojom::UkmEntry* entry,
                                   std::string metric_name) {}

const std::string StringifyNumericMetric(
    ukm::TestAutoSetUkmRecorder* ukm_recorder,
    const ukm::mojom::UkmEntry* entry,
    std::string metric_name) {}

std::string StringifyEntry(ukm::TestAutoSetUkmRecorder* ukm_recorder,
                           const ukm::mojom::UkmEntry* entry) {}

auto BooleanMetricIsAbsentOr(bool value) {}

const char kUkmEventName[] =;
const char kSiteA[] =;
const char kSiteB[] =;
const char kSiteC[] =;
const char kSiteD[] =;
}  // namespace

class DipsNavigationFlowDetectorTest : public PlatformBrowserTest {};

class DipsNavigationFlowDetectorPrerenderTest
    : public DipsNavigationFlowDetectorTest {};

class DipsNavigationFlowDetectorPATApiTest
    : public DipsNavigationFlowDetectorTest {};

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmNotEmittedWhenLessThanThreePagesVisited) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmNotEmittedWhenSameSiteWithPriorPage) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmNotEmittedWhenSameSiteWithNextPage) {}

// TODO - crbug.com/353556432: flaky on Linux release builds
#if BUILDFLAG(IS_LINUX) && defined(NDEBUG)
#define MAYBE_UkmNotEmittedWhenSiteDidNotAccessStorage
#else
#define MAYBE_UkmNotEmittedWhenSiteDidNotAccessStorage
#endif
IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       MAYBE_UkmNotEmittedWhenSiteDidNotAccessStorage) {}

// TODO - crbug.com/353556432: flaky on Linux release builds
#if BUILDFLAG(IS_LINUX) && defined(NDEBUG)
#define MAYBE_UkmNotEmittedWhenCookiesReadViaHeaders
#else
#define MAYBE_UkmNotEmittedWhenCookiesReadViaHeaders
#endif
IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       MAYBE_UkmNotEmittedWhenCookiesReadViaHeaders) {}

// TODO - crbug.com/353556432: flaky on Linux release builds
#if BUILDFLAG(IS_LINUX) && defined(NDEBUG)
#define MAYBE_UkmNotEmittedForCookieAccessInPrerenders
#else
#define MAYBE_UkmNotEmittedForCookieAccessInPrerenders
#endif
IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorPrerenderTest,
                       MAYBE_UkmNotEmittedForCookieAccessInPrerenders) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorPATApiTest,
                       UkmNotEmittedWhenOnlyStorageAccessIsTopicsApi) {}

IN_PROC_BROWSER_TEST_F(
    DipsNavigationFlowDetectorPATApiTest,
    UkmNotEmittedWhenOnlyStorageAccessIsProtectedAudienceApi) {}

IN_PROC_BROWSER_TEST_F(
    DipsNavigationFlowDetectorPATApiTest,
    UkmNotEmittedWhenOnlyStorageAccessIsPrivateStateTokensApi) {}

IN_PROC_BROWSER_TEST_F(
    DipsNavigationFlowDetectorPATApiTest,
    UkmNotEmittedWhenOnlyStorageAccessIsAttributionReportingApi) {}

// TODO - crbug.com/353556432: flaky on Linux release builds
#if BUILDFLAG(IS_LINUX) && defined(NDEBUG)
#define MAYBE_UkmEmitsWhenVisitingABA
#else
#define MAYBE_UkmEmitsWhenVisitingABA
#endif
IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       MAYBE_UkmEmitsWhenVisitingABA) {}

// TODO - crbug.com/353556432: flaky on Linux release builds
#if BUILDFLAG(IS_LINUX) && defined(NDEBUG)
#define MAYBE_UkmEmitsWhenWritingCookiesInHeaders
#else
#define MAYBE_UkmEmitsWhenWritingCookiesInHeaders
#endif
IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       MAYBE_UkmEmitsWhenWritingCookiesInHeaders) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmEmitsWhenIframeWritesCookiesInHeaders) {}

IN_PROC_BROWSER_TEST_F(
    DipsNavigationFlowDetectorTest,
    UkmNotEmittedWhenReadingNonexistentCookiesWithJavascript) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmEmitsWhenReadingCookiesWithJavascript) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmEmitsWhenWritingCookiesWithJavascript) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmEmitsWhenLocalStorageAccessed) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmCorrectWhenEntryAndExitRendererInitiated) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmCorrectWhenOnlyEntryRendererInitiated) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmCorrectWhenOnlyExitRendererInitiated) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmReportsNegativeDurationAsZero) {}

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorTest,
                       UkmReportsPriorInteraction) {}

// WebAuthn tests do not work on Android because there is currently no way to
// install a virtual authenticator.
// TODO(crbug.com/40269763): Implement automated testing once the infrastructure
// permits it (Requires mocking the Android Platform Authenticator i.e. GMS
// Core).
#if !BUILDFLAG(IS_ANDROID)
class DipsNavigationFlowDetectorWebAuthnTest : public CertVerifierBrowserTest {};

IN_PROC_BROWSER_TEST_F(DipsNavigationFlowDetectorWebAuthnTest, UkmReportsWAA) {}
#endif  // !BUILDFLAG(IS_ANDROID)