chromium/chrome/browser/tpcd/heuristics/opener_heuristic_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 "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/field_trial_params.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/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/dips/dips_service.h"
#include "chrome/browser/dips/dips_storage.h"
#include "chrome/browser/dips/dips_test_utils.h"
#include "chrome/browser/dips/dips_utils.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/heuristics/opener_heuristic_metrics.h"
#include "chrome/browser/tpcd/heuristics/opener_heuristic_tab_helper.h"
#include "chrome/browser/tpcd/heuristics/opener_heuristic_utils.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/subresource_filter/content/browser/ad_tagging_browser_test_utils.h"
#include "components/subresource_filter/core/common/test_ruleset_utils.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/hit_test_region_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/cookies/site_for_cookies.h"
#include "net/dns/mock_host_resolver.h"
#include "services/metrics/public/cpp/ukm_source.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/network/public/cpp/features.h"
#include "third_party/blink/public/common/switches.h"
#include "ui/base/window_open_disposition.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/browser.h"
#endif  // !BUILDFLAG(IS_ANDROID)

HasValue;
ValueIs;
NavigationHandle;
RenderFrameHost;
WebContents;
WebContentsObserver;
ElementsAre;
Field;
Optional;
Pair;
EnableForIframeTypes;

namespace {

struct AccessGrantTestCase {};

const AccessGrantTestCase kAccessGrantTestCases[] =;

// Waits for a pop-up to open.
class PopupObserver : public WebContentsObserver {};

// Waits for a navigation in the primary main frame to finish.
class NavigationFinishObserver : public WebContentsObserver {};

}  // namespace

// SubresourceFilterBrowserTest is necessary to test ad-tagging related
// behaviors.
class OpenerHeuristicBrowserTest
    : public subresource_filter::SubresourceFilterBrowserTest {};

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       RootWindowDoesntHavePopupState) {}

// TODO(crbug.com/40276065): Test is flaky on Android.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_PopupsWithOpenerHavePopupState
#else
#define MAYBE_PopupsWithOpenerHavePopupState
#endif
IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       MAYBE_PopupsWithOpenerHavePopupState) {}

// TODO(crbug.com/40925352): Flaky on android.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_PopupsWithoutOpenerDoNotHavePopupState
#else
#define MAYBE_PopupsWithoutOpenerDoNotHavePopupState
#endif
IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       MAYBE_PopupsWithoutOpenerDoNotHavePopupState) {}

// TODO(crbug.com/40925352): Flaky on android.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_NewTabURLsHavePopupState
#else
#define MAYBE_NewTabURLsHavePopupState
#endif
IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       MAYBE_NewTabURLsHavePopupState) {}

class OpenerHeuristicIframeInitiatorBrowserTest
    : public OpenerHeuristicBrowserTest,
      public testing::WithParamInterface<
          ::testing::tuple<EnableForIframeTypes, bool>> {};

IN_PROC_BROWSER_TEST_P(
    OpenerHeuristicIframeInitiatorBrowserTest,
    URLsInitiatedByFirstPartyIframes_HavePopupStateWithFlag) {}

IN_PROC_BROWSER_TEST_P(
    OpenerHeuristicIframeInitiatorBrowserTest,
    URLsInitiatedByThirdPartyIframes_HavePopupStateWithFlag) {}

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       PopupPastInteractionIsReported_WithoutInteraction) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       PopupPastInteractionIsReported_WithoutRedirect) {}

// chrome/browser/ui/browser.h (for changing profile prefs) is not available on
// Android.
#if !BUILDFLAG(IS_ANDROID)
class OpenerHeuristicPastInteractionGrantBrowserTest
    : public OpenerHeuristicBrowserTest,
      public testing::WithParamInterface<AccessGrantTestCase> {};

IN_PROC_BROWSER_TEST_P(OpenerHeuristicPastInteractionGrantBrowserTest,
                       PopupPastInteractionIsReported_WithStorageAccessGrant) {}

// TODO(crbug.com/40947612) Flaky on mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_AdTaggedPopupPastInteractionIsReported_WithStorageAccessGrant
#else
#define MAYBE_AdTaggedPopupPastInteractionIsReported_WithStorageAccessGrant
#endif
IN_PROC_BROWSER_TEST_P(
    OpenerHeuristicPastInteractionGrantBrowserTest,
    MAYBE_AdTaggedPopupPastInteractionIsReported_WithStorageAccessGrant) {}

INSTANTIATE_TEST_SUITE_P();
#endif  // !BUILDFLAG(IS_ANDROID)

// TODO(crbug.com/40918571): Test is flaky on Android.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_PopupPastInteractionIsReported_ServerRedirect
#else
#define MAYBE_PopupPastInteractionIsReported_ServerRedirect
#endif
IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       MAYBE_PopupPastInteractionIsReported_ServerRedirect) {}

// TODO(crbug.com/40282438): Flaky on Android.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_PopupPastInteractionIsReported_ClientRedirect
#else
#define MAYBE_PopupPastInteractionIsReported_ClientRedirect
#endif
IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       MAYBE_PopupPastInteractionIsReported_ClientRedirect) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       PopupPastInteractionIsReportedOnlyOnce) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       PopupPastInteractionIsFollowedByPostPopupCookieAccess) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest, PopupInteraction) {}

// chrome/browser/ui/browser.h (for changing profile prefs) is not available on
// Android.
#if !BUILDFLAG(IS_ANDROID)
class OpenerHeuristicCurrentInteractionGrantBrowserTest
    : public OpenerHeuristicBrowserTest,
      public testing::WithParamInterface<AccessGrantTestCase> {};

IN_PROC_BROWSER_TEST_P(OpenerHeuristicCurrentInteractionGrantBrowserTest,
                       PopupInteractionWithStorageAccessGrant) {}

IN_PROC_BROWSER_TEST_P(OpenerHeuristicCurrentInteractionGrantBrowserTest,
                       AdTaggedPopupInteractionWithStorageAccessGrant) {}

INSTANTIATE_TEST_SUITE_P();
#endif  // !BUILDFLAG(IS_ANDROID)

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       PopupInteractionIsOnlyReportedOnce) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       PopupInteraction_IgnoreUncommitted) {}

#if BUILDFLAG(IS_MAC)
// Very flaky on macOS 11 Tests: https://crbug.com/1486448
#define MAYBE_PopupInteraction_IsFollowedByPostPopupCookieAccess
#else
#define MAYBE_PopupInteraction_IsFollowedByPostPopupCookieAccess
#endif
IN_PROC_BROWSER_TEST_F(
    OpenerHeuristicBrowserTest,
    MAYBE_PopupInteraction_IsFollowedByPostPopupCookieAccess) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       TopLevelIsReported_PastInteraction_NoSameSiteIframe) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       TopLevelIsReported_HasSameSiteIframe) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest, TopLevel_PopupProvider) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest, TopLevel_PopupId) {}

// TODO(crbug.com/41484288): Flaky on mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_TopLevel_PastInteraction_AdTagged
#else
#define MAYBE_TopLevel_PastInteraction_AdTagged
#endif
IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       MAYBE_TopLevel_PastInteraction_AdTagged) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       TopLevel_CurrentInteraction_AdTagged) {}

IN_PROC_BROWSER_TEST_F(OpenerHeuristicBrowserTest,
                       PastAndCurrentInteractionAreBothReportedToDipsDb) {}

// chrome/browser/ui/browser.h (for changing profile prefs) is not available on
// Android.
#if !BUILDFLAG(IS_ANDROID)
class OpenerHeuristicBackfillGrantBrowserTest
    : public OpenerHeuristicBrowserTest,
      public testing::WithParamInterface<bool> {};

// Test the backfill grants created by OpenerHeuristicService when tracking
// protection is onboarded.
IN_PROC_BROWSER_TEST_P(OpenerHeuristicBackfillGrantBrowserTest,
                       TrackingProtectionOnboardingCreatesBackfillGrants) {}

INSTANTIATE_TEST_SUITE_P();
#endif  // !BUILDFLAG(IS_ANDROID)