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

// Copyright 2023 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 <tuple>
#include <vector>

#include "base/files/file_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/net/storage_test_utils.h"
#include "chrome/browser/privacy_sandbox/tracking_protection_onboarding_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/subresource_filter/subresource_filter_browser_test_harness.h"
#include "chrome/browser/tpcd/experiment/tpcd_experiment_features.h"
#include "chrome/browser/tpcd/support/top_level_trial_service.h"
#include "chrome/browser/tpcd/support/top_level_trial_service_factory.h"
#include "chrome/browser/tpcd/support/tpcd_support_service.h"
#include "chrome/browser/tpcd/support/tpcd_support_service_factory.h"
#include "chrome/browser/tpcd/support/validity_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/tpcd_pref_names.h"
#include "components/privacy_sandbox/tpcd_utils.h"
#include "components/privacy_sandbox/tracking_protection_onboarding.h"
#include "components/subresource_filter/core/common/common_features.h"
#include "components/subresource_filter/core/common/test_ruleset_utils.h"
#include "components/tpcd/metadata/browser/parser.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/base/features.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_response.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/network_switches.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-shared.h"
#include "url/origin.h"

namespace {

constexpr char kHostA[] =;
constexpr char kHostB[] =;
constexpr char kHostC[] =;

const char kThirdPartyCookieAccessBlockedHistogram[] =;
const char kThirdPartyCookieAllowMechanismHistogram[] =;
const char kWebFeatureHistogram[] =;
const char kThirdPartyCookieIsAdOrNonAdHistogram[] =;
const char kThirdPartyCookieAdBlockedByExperimentHistogram[] =;
const char kCookieReadStatusHistogram[] =;

WebFeature;
ThirdPartyCookieAllowMechanism;

struct Allow3PCMechanismBrowserTestCase {};

const Allow3PCMechanismBrowserTestCase kAllowMechanismTestCases[] =;

}  // namespace

class ThirdPartyCookieDeprecationObserverBaseBrowserTest
    : public subresource_filter::SubresourceFilterBrowserTest {};

class ThirdPartyCookieDeprecationObserverBrowserTest
    : public ThirdPartyCookieDeprecationObserverBaseBrowserTest,
      public testing::WithParamInterface<std::tuple<bool, bool>> {};

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       FirstPartyCookiesReadAndWrite) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       ThirdPartyCookiesReadAndWrite) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       FirstPartyJavaScriptCookieReadAndWrite) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       ThirdPartyJavaScriptCookieReadAndWrite) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       ThirdPartyAdCookieRead) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       ThirdPartyNonAdCookieRead) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       FirstPartyAdCookieRead) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       ThirdPartyAdCookieReadSubframe) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       ThirdPartyAdCookieReadScriptTaggedSubframe) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       ThirdPartyAdCookieReadOnRedirect) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       ThirdPartyAdCookieReadOnNonAdRedirect) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverBrowserTest,
                       ThirdPartyAdJavaScriptCookieRead) {}

class ThirdPartyCookieDeprecationObserverMechanismBrowserTest
    : public ThirdPartyCookieDeprecationObserverBaseBrowserTest,
      public testing::WithParamInterface<
          std::tuple<Allow3PCMechanismBrowserTestCase,
                     /*is_tracking_protection_onboarded:*/ bool>> {};

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverMechanismBrowserTest,
                       ThirdPartyCookiesReadAndWrite) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverMechanismBrowserTest,
                       ThirdPartyJavaScriptCookieReadAndWrite) {}

class ThirdPartyCookieDeprecationObserverSSABrowserTest
    : public ThirdPartyCookieDeprecationObserverBaseBrowserTest,
      public testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverSSABrowserTest,
                       ThirdPartyCookiesReadAndWrite) {}

IN_PROC_BROWSER_TEST_P(ThirdPartyCookieDeprecationObserverSSABrowserTest,
                       ThirdPartyJavaScriptCookieReadAndWrite) {}

class ThirdPartyCookieDeprecationObserverCookieReadBrowserTest
    : public ThirdPartyCookieDeprecationObserverBaseBrowserTest {};

IN_PROC_BROWSER_TEST_F(ThirdPartyCookieDeprecationObserverCookieReadBrowserTest,
                       NotOnboarded_CookieStatusRecorded) {}

IN_PROC_BROWSER_TEST_F(ThirdPartyCookieDeprecationObserverCookieReadBrowserTest,
                       CookieWithHeuristics_StatusRecorded) {}

IN_PROC_BROWSER_TEST_F(ThirdPartyCookieDeprecationObserverCookieReadBrowserTest,
                       CookiesBlockedAndAllowed_StatusRecorded) {}

IN_PROC_BROWSER_TEST_F(ThirdPartyCookieDeprecationObserverCookieReadBrowserTest,
                       PartitionedCookies_StatusNotRecorded) {}

class ThirdPartyCookieDeprecationObserverTriggerBrowserTest
    : public ThirdPartyCookieDeprecationObserverBaseBrowserTest {};

IN_PROC_BROWSER_TEST_F(ThirdPartyCookieDeprecationObserverTriggerBrowserTest,
                       ThirdPartyCookiesSingleWrite) {}

IN_PROC_BROWSER_TEST_F(ThirdPartyCookieDeprecationObserverTriggerBrowserTest,
                       ThirdPartyCookiesSingleRead) {}

IN_PROC_BROWSER_TEST_F(ThirdPartyCookieDeprecationObserverTriggerBrowserTest,
                       ThirdPartyCookiesBothWriteRead) {}