chromium/third_party/blink/renderer/core/frame/ad_tracker_test.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 "third_party/blink/renderer/core/frame/ad_tracker.h"

#include <memory>

#include "base/containers/contains.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/core/dom/element_traversal.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/html/html_image_element.h"
#include "third_party/blink/renderer/core/probe/async_task_context.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/testing/dummy_page_holder.h"
#include "third_party/blink/renderer/core/testing/sim/sim_request.h"
#include "third_party/blink/renderer/core/testing/sim/sim_test.h"
#include "third_party/blink/renderer/platform/testing/runtime_enabled_features_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"

namespace blink {

namespace {

const unsigned char kSmallGifData[] =;

// The pages include a div with class="test" to ensure the resources in the
// stylesheet are loaded.
const char kPageWithVanillaExternalStylesheet[] =;
const char kPageWithAdExternalStylesheet[] =;
const char kPageWithVanillaScript[] =;
const char kPageWithAdScript[] =;
const char kPageWithFrame[] =;
const char kPageWithStyleTagLoadingVanillaResources[] =;

const char kStylesheetWithVanillaResources[] =;
const char kStylesheetWithAdResources[] =;

class TestAdTracker : public AdTracker {};

void SetIsAdFrame(LocalFrame* frame) {}

}  // namespace

class AdTrackerTest : public testing::Test {};

void AdTrackerTest::SetUp() {}

void AdTrackerTest::TearDown() {}

TEST_F(AdTrackerTest, AnyExecutingScriptsTaggedAsAdResource) {}

TEST_F(AdTrackerTest, BottomScriptTaggedAsAdResource) {}

TEST_F(AdTrackerTest, TopScriptTaggedAsAdResource) {}

// Tests that if neither script in the stack is an ad,
// AnyExecutingScriptsTaggedAsAdResource should return false.
TEST_F(AdTrackerTest, AnyExecutingScriptsTaggedAsAdResource_False) {}

TEST_F(AdTrackerTest, TopOfStackIncluded) {}

TEST_F(AdTrackerTest, AdStackFrameCounting) {}

TEST_F(AdTrackerTest, AsyncTagging) {}

TEST_F(AdTrackerTest, BottommostAdScript) {}

TEST_F(AdTrackerTest, BottommostAsyncAdScript) {}

class AdTrackerSimTest : public SimTest {};

// Script loaded by ad script is tagged as ad.
TEST_F(AdTrackerSimTest, ScriptLoadedWhileExecutingAdScript) {}

// Unknown script running in an ad context should be labeled as ad script.
TEST_F(AdTrackerSimTest, ScriptDetectedByContext) {}

TEST_F(AdTrackerSimTest, EventHandlerForPostMessageFromAdFrame_NoAdInStack) {}

TEST_F(AdTrackerSimTest, RedirectToAdUrl) {}

TEST_F(AdTrackerSimTest, AdResourceDetectedByContext) {}

// When inline script in an ad frame inserts an iframe into a non-ad frame, the
// new frame should be considered as created by ad script (and would therefore
// be tagged as an ad).
TEST_F(AdTrackerSimTest, InlineAdScriptRunningInNonAdContext) {}

// Image loaded by ad script is tagged as ad.
TEST_F(AdTrackerSimTest, ImageLoadedWhileExecutingAdScriptAsyncEnabled) {}

// Image loaded by ad script is tagged as ad.
TEST_F(AdTrackerSimTest, DataURLImageLoadedWhileExecutingAdScriptAsyncEnabled) {}

// Frame loaded by ad script is considered created by ad script.
TEST_F(AdTrackerSimTest, FrameLoadedWhileExecutingAdScript) {}

// A script tagged as an ad in one frame shouldn't cause it to be considered
// an ad when executed in another frame.
TEST_F(AdTrackerSimTest, Contexts) {}

TEST_F(AdTrackerSimTest, SameOriginSubframeFromAdScript) {}

TEST_F(AdTrackerSimTest, SameOriginDocWrittenSubframeFromAdScript) {}

// This test class allows easy running of tests that only differ by whether
// one resource (or a set of resources) is vanilla or an ad.
class AdTrackerVanillaOrAdSimTest : public AdTrackerSimTest,
                                    public ::testing::WithParamInterface<bool> {};

TEST_P(AdTrackerVanillaOrAdSimTest, VanillaExternalStylesheetLoadsResources) {}

TEST_P(AdTrackerVanillaOrAdSimTest, AdExternalStylesheetLoadsResources) {}

TEST_P(AdTrackerVanillaOrAdSimTest, LinkRelStylesheetAddedByScript) {}

TEST_P(AdTrackerVanillaOrAdSimTest, ExternalStylesheetInFrame) {}

// Note that we skip fonts as at rules aren't valid in inline CSS.
TEST_P(AdTrackerVanillaOrAdSimTest, InlineCSSSetByScript) {}

TEST_F(AdTrackerSimTest, StyleTagInMainframe) {}

// This verifies that style tag resources in ad frames are correctly tagged
// according to the heuristic that all requests from an ad frame should also be
// tagged as ads.
TEST_P(AdTrackerVanillaOrAdSimTest, StyleTagInSubframe) {}

TEST_P(AdTrackerVanillaOrAdSimTest, StyleTagAddedByScript) {}

TEST_P(AdTrackerVanillaOrAdSimTest, VanillaImportInStylesheet) {}

TEST_P(AdTrackerVanillaOrAdSimTest, AdImportInStylesheet) {}

TEST_P(AdTrackerVanillaOrAdSimTest, ImageSetInStylesheet) {}

TEST_P(AdTrackerVanillaOrAdSimTest, ConstructableCSSCreatedByScript) {}

// Vanilla resources loaded due to an ad's script's style recalculation
// shouldn't be tagged.
TEST_F(AdTrackerSimTest, StyleRecalcCausedByAdScript) {}

// A dynamically added script with no src is still tagged as an ad if created
// by an ad script.
TEST_F(AdTrackerSimTest, DynamicallyAddedScriptNoSrc_StillTagged) {}

// A dynamically added script with no src isn't tagged as an ad if not created
// by an ad script, even if it's later used by an ad script.
TEST_F(AdTrackerSimTest,
       DynamicallyAddedScriptNoSrc_NotTaggedBasedOnUseByAdScript) {}

TEST_F(AdTrackerSimTest, VanillaModuleScript_ResourceNotTagged) {}

TEST_F(AdTrackerSimTest, AdModuleScript_ResourceTagged) {}

// A resource fetched with ad script at top of stack is still tagged as an ad
// when the ad script defines a sourceURL.
TEST_F(AdTrackerSimTest, AdScriptWithSourceURLAtTopOfStack_StillTagged) {}

// A dynamically added script with no src is still tagged as an ad if created
// by an ad script even if it defines a sourceURL.
TEST_F(AdTrackerSimTest, InlineAdScriptWithSourceURLAtTopOfStack_StillTagged) {}

class AdTrackerDisabledSimTest : public SimTest,
                                 private ScopedAdTaggingForTest {};

TEST_F(AdTrackerDisabledSimTest, VerifyAdTrackingDisabled) {}

INSTANTIATE_TEST_SUITE_P();

}  // namespace blink