#include "components/page_load_metrics/browser/observers/ad_metrics/ads_page_load_metrics_observer.h"
#include <memory>
#include <string>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/escape.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/subresource_filter/subresource_filter_browser_test_harness.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/heavy_ad_intervention/heavy_ad_features.h"
#include "components/page_load_metrics/browser/ads_page_load_metrics_test_waiter.h"
#include "components/page_load_metrics/browser/observers/ad_metrics/ad_intervention_browser_test_utils.h"
#include "components/page_load_metrics/browser/observers/ad_metrics/frame_tree_data.h"
#include "components/page_load_metrics/browser/observers/use_counter_page_load_metrics_observer.h"
#include "components/page_load_metrics/browser/page_load_metrics_memory_tracker.h"
#include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h"
#include "components/performance_manager/public/v8_memory/v8_detailed_memory.h"
#include "components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.h"
#include "components/subresource_filter/content/shared/browser/ruleset_service.h"
#include "components/subresource_filter/core/browser/subresource_filter_features.h"
#include "components/subresource_filter/core/common/activation_scope.h"
#include "components/subresource_filter/core/common/common_features.h"
#include "components/subresource_filter/core/common/test_ruleset_utils.h"
#include "components/subresource_filter/core/mojom/subresource_filter.mojom.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/common/content_features.h"
#include "content/public/common/isolated_world_ids.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "media/base/media_switches.h"
#include "net/base/net_errors.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 "services/metrics/public/cpp/metrics_utils.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_source.h"
#include "services/network/public/cpp/features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom.h"
#include "ui/gfx/geometry/size.h"
#include "url/gurl.h"
#include "url/url_constants.h"
namespace {
OriginStatus;
OriginStatusWithThrottling;
FrameTreeNodeId;
const char kAdsInterventionRecordedHistogram[] = …;
const char kCrossOriginHistogramId[] = …;
const char kCreativeOriginHistogramId[] = …;
const char kCreativeOriginWithThrottlingHistogramId[] = …;
const char kAdUserActivationHistogramId[] = …;
const char kPeakWindowdPercentHistogramId[] = …;
const char kHeavyAdInterventionTypeHistogramId[] = …;
const char kMemoryMainFrameMaxHistogramId[] = …;
const char kMemoryUpdateCountHistogramId[] = …;
}
class AdsPageLoadMetricsObserverBrowserTest
: public subresource_filter::SubresourceFilterBrowserTest { … };
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
OriginStatusMetricEmbedded) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
DISABLED_OriginStatusMetricEmbeddedEmpty) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
OriginStatusMetricSame) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
OriginStatusMetricCross) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
DISABLED_AverageViewportAdDensity) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
AverageViewportAdDensity_ImageAd) { … }
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_PageAdDensityRecordsPageMax …
#else
#define MAYBE_PageAdDensityRecordsPageMax …
#endif
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
MAYBE_PageAdDensityRecordsPageMax) { … }
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_PageAdDensityMultipleFrames …
#else
#define MAYBE_PageAdDensityMultipleFrames …
#endif
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
MAYBE_PageAdDensityMultipleFrames) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
DISABLED_PageAdDensityIgnoreDisplayNoneFrame) { … }
class CreativeOriginAdsPageLoadMetricsObserverBrowserTest
: public AdsPageLoadMetricsObserverBrowserTest { … };
IN_PROC_BROWSER_TEST_F(CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
CreativeOriginStatusSame) { … }
IN_PROC_BROWSER_TEST_F(CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
CreativeOriginStatusCross) { … }
IN_PROC_BROWSER_TEST_F(CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
CreativeOriginStatusSameNested) { … }
IN_PROC_BROWSER_TEST_F(CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
CreativeOriginStatusCrossNested) { … }
IN_PROC_BROWSER_TEST_F(CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
CreativeOriginStatusCrossDoubleNested) { … }
IN_PROC_BROWSER_TEST_F(CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
CreativeOriginStatusNoCreativeDesignated) { … }
IN_PROC_BROWSER_TEST_F(CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
CreativeOriginStatusNoSubframes) { … }
IN_PROC_BROWSER_TEST_F(CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
DISABLED_CreativeOriginStatusWithThrottlingUnknown) { … }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || \
BUILDFLAG(IS_MAC)
#define MAYBE_CreativeOriginStatusWithThrottlingNestedThrottled …
#else
#define MAYBE_CreativeOriginStatusWithThrottlingNestedThrottled …
#endif
IN_PROC_BROWSER_TEST_F(
CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
MAYBE_CreativeOriginStatusWithThrottlingNestedThrottled) { … }
IN_PROC_BROWSER_TEST_F(
CreativeOriginAdsPageLoadMetricsObserverBrowserTest,
DISABLED_CreativeOriginStatusWithThrottlingNestedUnthrottled) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
UserActivationSetOnFrame) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
UserActivationSetOnFrameAfterSameOriginActivation) { … }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || \
BUILDFLAG(IS_MAC)
#define MAYBE_DocOverwritesNavigation …
#else
#define MAYBE_DocOverwritesNavigation …
#endif
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
MAYBE_DocOverwritesNavigation) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
DocWriteAboutBlankAdframe) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
SubresourceFilter) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest, FrameDepth) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
FirstContentfulPaintRecorded) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
VisibleAdframeRecorded) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
DisplayNoneAdframeRecorded) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest, FramePixelSize) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
FrameWithSmallAreaNotConsideredVisible) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
AdFrameRecordMediaStatusNotPlayed) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
DISABLED_AdFrameRecordMediaStatusPlayed) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
SameDomainFrameCreatedByAdScript_NotRecorddedAsAd) { … }
IN_PROC_BROWSER_TEST_F(
AdsPageLoadMetricsObserverBrowserTest,
FrameCreatedByAdScriptNavigatedToAllowListRule_NotRecorddedAsAd) { … }
enum class ReduceTransferSizeUpdatedIPCTestCase { … };
class AdsPageLoadMetricsObserverResourceBrowserTest
: public subresource_filter::SubresourceFilterBrowserTest,
public ::testing::WithParamInterface<
ReduceTransferSizeUpdatedIPCTestCase> { … };
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
ReceivedAdResources) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
ReceivedMainResourceAds) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
ReceivedSubframeNavigationAds) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
ReceivedAdResourceMetrics) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
IncompleteResourcesRecordedToFrameMetrics) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
HeavyAdInterventionFired_ReportSent) { … }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
#define MAYBE_HeavyAdInterventionFired_ReportsToAllChildren …
#else
#define MAYBE_HeavyAdInterventionFired_ReportsToAllChildren …
#endif
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
MAYBE_HeavyAdInterventionFired_ReportsToAllChildren) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
HeavyAdInterventionEnabled_ErrorPageLoaded) { … }
class AdsPageLoadMetricsObserverResourceBrowserTestWithoutHeavyAdIntervention
: public AdsPageLoadMetricsObserverResourceBrowserTest { … };
INSTANTIATE_TEST_SUITE_P(…);
IN_PROC_BROWSER_TEST_P(
AdsPageLoadMetricsObserverResourceBrowserTestWithoutHeavyAdIntervention,
ErrorPageNotLoaded) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
HeavyAdInterventionNoHeavyAd_FieldTrialNotActive) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
HeavyAdInterventionBlocklistFull_InterventionBlocked) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
HeavyAdInterventionIncognitoMode_InterventionFired) { … }
IN_PROC_BROWSER_TEST_P(AdsPageLoadMetricsObserverResourceBrowserTest,
RecordedUKMMetrics) { … }
void WaitForRAF(content::DOMMessageQueue* message_queue) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
FrameRAFTriggersCpuUpdate) { … }
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS_LACROS) || \
BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_TwoRAFFramesTriggerCpuUpdates …
#else
#define MAYBE_TwoRAFFramesTriggerCpuUpdates …
#endif
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
MAYBE_TwoRAFFramesTriggerCpuUpdates) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
AggregateCpuTriggersCpuUpdateOverSubframeNavigate) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
SubframeNavigate_CpuTimesCumulative) { … }
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
DisallowedAdFrames_NotMeasured) { … }
class DummyMemoryObserver
: public performance_manager::v8_memory::V8DetailedMemoryObserverAnySeq { … };
class AdsMemoryMeasurementBrowserTest
: public subresource_filter::SubresourceFilterBrowserTest { … };
IN_PROC_BROWSER_TEST_F(AdsMemoryMeasurementBrowserTest,
SingleAdFrame_MaxMemoryBytesRecorded) { … }
class AdsPageLoadMetricsObserverPrerenderingBrowserTest
: public AdsPageLoadMetricsObserverBrowserTest { … };
IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverPrerenderingBrowserTest,
NoMetricsInPrerendering) { … }