chromium/chrome/browser/subresource_filter/ad_tagging_browsertest.cc

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

#include <string>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/subresource_filter/subresource_filter_browser_test_harness.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/embedder_support/switches.h"
#include "components/metrics/content/subprocess_metrics_provider.h"
#include "components/page_load_metrics/browser/observers/ad_metrics/ads_page_load_metrics_observer.h"
#include "components/page_load_metrics/browser/observers/ad_metrics/frame_tree_data.h"
#include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h"
#include "components/subresource_filter/content/browser/ad_tagging_browser_test_utils.h"
#include "components/subresource_filter/content/browser/subresource_filter_observer_test_utils.h"
#include "components/subresource_filter/core/common/test_ruleset_utils.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/test/back_forward_cache_util.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_frame_navigation_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/frame/from_ad_state.h"
#include "url/gurl.h"

namespace subresource_filter {

namespace {

RenderFrameHost;
CreateAllowlistSuffixRule;
CreateSuffixRule;

// Creates a link element with rel="stylesheet" and href equal to the specified
// url. Returns immediately after adding the element to the DOM.
void AddExternalStylesheet(const content::ToRenderFrameHost& adapter,
                           const GURL& url) {}

class AdTaggingPageLoadMetricsTestWaiter
    : public page_load_metrics::PageLoadMetricsTestWaiter {};

class AdTaggingBrowserTest : public SubresourceFilterBrowserTest {};

content::RenderFrameHost* AdTaggingBrowserTest::CreateDocWrittenFrameImpl(
    const content::ToRenderFrameHost& adapter,
    bool ad_script) {}

content::RenderFrameHost*
AdTaggingBrowserTest::CreateFrameWithDocWriteAbortedLoadImpl(
    const content::ToRenderFrameHost& adapter,
    bool ad_script) {}

content::RenderFrameHost*
AdTaggingBrowserTest::CreateFrameWithWindowStopAbortedLoadImpl(
    const content::ToRenderFrameHost& adapter,
    bool ad_script) {}

// Given a RenderFrameHost, navigates the page to the given |url| and waits
// for the navigation to complete before returning.
void AdTaggingBrowserTest::NavigateFrame(
    content::RenderFrameHost* render_frame_host,
    const GURL& url) {}

IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest,
                       AdContentSettingAllowed_AdTaggingDisabled) {}

IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest,
                       AdContentSettingBlocked_AdTaggingEnabled) {}

IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest, FramesByURL) {}

const char kSubresourceFilterOriginStatusHistogram[] =;

IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest, VerifySameOriginWithoutNavigate) {}

IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest, VerifyCrossOriginWithoutNavigate) {}

// Ad script creates a frame and navigates it cross origin.
IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest,
                       VerifyCrossOriginWithImmediateNavigate) {}

// Ad script creates a frame and navigates it same origin.
// It is then renavigated cross origin.
IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest,
                       VerifySameOriginWithCrossOriginRenavigate) {}

// Test that a subframe with a non-ad url but loaded by ad script is an ad.
IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest, FrameLoadedByAdScript) {}

// Test that same-origin doc.write created iframes are tagged as ads.
IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest, SameOriginFrameTagging) {}

// Test that the children same-origin doc.write created iframes are tagged as
// ads where appropriate.
IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest,
                       ChildrenOfSameOriginFrames_CorrectlyTagged) {}

// Test that frames with an aborted initial load due to a doc.write are still
// correctly tagged as ads or not.
IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest,
                       FramesWithDocWriteAbortedLoad_StillCorrectlyTagged) {}

// Test that frames with an aborted initial load due to a window.stop are still
// correctly tagged as ads or not.
IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest,
                       FramesWithWindowStopAbortedLoad_StillCorrectlyTagged) {}

// Test that the children of a frame with its initial load aborted due to a
// doc.write are reported correctly as vanilla or ad frames.
IN_PROC_BROWSER_TEST_F(
    AdTaggingBrowserTest,
    ChildrenOfFrameWithDocWriteAbortedLoad_StillCorrectlyTagged) {}

// Test that the children of a frame with its initial load aborted due to a
// window.stop are reported correctly as vanilla or ad frames.
// This test is flaky. See crbug.com/1069346.
IN_PROC_BROWSER_TEST_F(
    AdTaggingBrowserTest,
    ChildrenOfFrameWithWindowStopAbortedLoad_StillCorrectlyTagged) {}

// Test that navigating a frame to a URL with a less restrictive load policy
// updates the latest filter list result, but not the most-restrictive one.
IN_PROC_BROWSER_TEST_F(
    AdTaggingBrowserTest,
    NavigationToLessRestrictiveUrl_MostRestrictiveLoadPolicyUnchanged) {}

// Basic vanilla stylesheet with vanilla font and image.
IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest,
                       VanillaExternalStylesheet_ResourcesNotTagged) {}

// Basic ad stylesheet with vanilla font and image.
IN_PROC_BROWSER_TEST_F(AdTaggingBrowserTest,
                       AdExternalStylesheet_ResourcesTagged) {}

// Regression test for crbug.com/336753737.
IN_PROC_BROWSER_TEST_F(
    AdTaggingBrowserTest,
    SameDocumentHistoryNavigationAbortedByParentHistoryNavigation) {}

class AdClickMetricsBrowserTest : public AdTaggingBrowserTest {};

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest, BrowserInitiated) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       WindowOpen_FromAdScriptWithoutGesture) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       WindowOpen_FromNonAdScriptWithGesture) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       WindowOpen_FromAdScriptWithGesture) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       WindowOpen_FromAdIframeWithoutGesture) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       WindowOpen_FromAdIframeWithGesture) {}

IN_PROC_BROWSER_TEST_F(
    AdClickMetricsBrowserTest,
    AdClickMetrics_AnchorClickTopNavigationFromCrossOriginAdIframe) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       AdClickMetrics_AnchorClickPopupFromCrossOriginAdIframe) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       SetTopLocationFromCrossOriginAdIframe) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       SetTopLocationFromCrossOriginNonAdIframe) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       NavigateCrossOriginIframeFromNonAdScript) {}

IN_PROC_BROWSER_TEST_F(AdClickMetricsBrowserTest,
                       NavigateCrossOriginIframeFromAdScript) {}

class AdTaggingEventFromSubframeBrowserTest
    : public AdTaggingBrowserTest,
      public ::testing::WithParamInterface<
          std::tuple<bool /* cross_origin */, bool /* from_ad_frame */>> {};

// crbug.com/997410. The test is flaky on multiple platforms.
IN_PROC_BROWSER_TEST_P(AdTaggingEventFromSubframeBrowserTest,
                       DISABLED_WindowOpenFromSubframe) {}

INSTANTIATE_TEST_SUITE_P();

class AdTaggingEventWithScriptInStackBrowserTest
    : public AdTaggingBrowserTest,
      public ::testing::WithParamInterface<bool /* from_ad_script */> {};

// crbug.com/998405. The test is flaky on multiple platforms.
IN_PROC_BROWSER_TEST_P(AdTaggingEventWithScriptInStackBrowserTest,
                       DISABLED_WindowOpenWithScriptInStack) {}

INSTANTIATE_TEST_SUITE_P();

class AdTaggingFencedFrameBrowserTest : public AdTaggingBrowserTest {};

// Test that a fenced frame itself can be tagged as an ad and that iframes
// nested within it see it as an ancestor ad frame.
IN_PROC_BROWSER_TEST_F(AdTaggingFencedFrameBrowserTest,
                       FencedFrameMatchingSuffixRuleIsTagged) {}

// Test that a fenced frame created by ad script is tagged as an ad.
IN_PROC_BROWSER_TEST_F(AdTaggingFencedFrameBrowserTest,
                       FencedFrameCreatedByAdScriptIsTagged) {}

// Test that a fenced frame not matching any rules, created in an ad-frame is
// also tagged as an ad.
IN_PROC_BROWSER_TEST_F(AdTaggingFencedFrameBrowserTest,
                       FencedFrameWithAdParentIsTagged) {}

// Test ad tagging for iframes nested within a fenced frame.
IN_PROC_BROWSER_TEST_F(AdTaggingFencedFrameBrowserTest,
                       IFrameNestedInFencedFrameIsTagged) {}

// Test ad tagging for fenced frames nested within a fenced frame.
IN_PROC_BROWSER_TEST_F(AdTaggingFencedFrameBrowserTest,
                       FencedFrameNestedInFencedFrameIsTagged) {}

IN_PROC_BROWSER_TEST_F(AdTaggingFencedFrameBrowserTest,
                       AdClickMetrics_PopupFromAdFencedFrame) {}

IN_PROC_BROWSER_TEST_F(AdTaggingFencedFrameBrowserTest,
                       AdClickMetrics_PopupFromNonAdFencedFrame) {}

IN_PROC_BROWSER_TEST_F(
    AdTaggingFencedFrameBrowserTest,
    AdClickMetrics_TopNavigationFromOpaqueModeAdFencedFrame) {}

IN_PROC_BROWSER_TEST_F(AdTaggingFencedFrameBrowserTest,
                       AdClickMetrics_ReloadFromOpaqueModeAdFencedFrame) {}

IN_PROC_BROWSER_TEST_F(
    AdTaggingFencedFrameBrowserTest,
    AdClickMetrics_AnchorClickTopNavigationFromOpaqueModeAdFencedFrame) {}

IN_PROC_BROWSER_TEST_F(
    AdTaggingFencedFrameBrowserTest,
    AdClickMetrics_TopNavigationFromOpaqueModeNonAdFencedFrame) {}

}  // namespace

}  // namespace subresource_filter