#include "components/page_load_metrics/browser/observers/ad_metrics/ads_page_load_metrics_observer.h"
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "components/blocklist/opt_out_blocklist/opt_out_blocklist_data.h"
#include "components/blocklist/opt_out_blocklist/opt_out_blocklist_delegate.h"
#include "components/blocklist/opt_out_blocklist/opt_out_store.h"
#include "components/heavy_ad_intervention/heavy_ad_blocklist.h"
#include "components/heavy_ad_intervention/heavy_ad_features.h"
#include "components/page_load_metrics/browser/metrics_navigation_throttle.h"
#include "components/page_load_metrics/browser/metrics_web_contents_observer.h"
#include "components/page_load_metrics/browser/observers/ad_metrics/frame_tree_data.h"
#include "components/page_load_metrics/browser/observers/page_load_metrics_observer_tester.h"
#include "components/page_load_metrics/browser/page_load_metrics_memory_tracker.h"
#include "components/page_load_metrics/browser/page_load_metrics_observer.h"
#include "components/page_load_metrics/browser/page_load_tracker.h"
#include "components/page_load_metrics/common/page_load_metrics_util.h"
#include "components/page_load_metrics/common/test/page_load_metrics_test_util.h"
#include "components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.h"
#include "components/subresource_filter/content/browser/subresource_filter_observer_manager.h"
#include "components/subresource_filter/content/browser/subresource_filter_test_harness.h"
#include "components/subresource_filter/core/common/load_policy.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/global_request_id.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/fake_local_frame.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_navigation_throttle.h"
#include "content/public/test/test_navigation_throttle_inserter.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/web_contents_tester.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"
#include "net/base/host_port_pair.h"
#include "services/metrics/public/cpp/metrics_utils.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/abseil-cpp/absl/utility/utility.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/mojom/frame/frame.mojom.h"
#include "url/gurl.h"
NavigationSimulator;
RenderFrameHost;
RenderFrameHostTester;
TestNavigationThrottle;
namespace page_load_metrics {
namespace {
FrameTreeNodeId;
struct ExpectedFrameBytes { … };
struct CreativeOriginTest { … };
struct CreativeOriginTestWithThrottling { … };
enum class ResourceCached { … };
enum class FrameType { … };
const base::TimeDelta kParseStartTime = …;
const base::TimeDelta kCreativeEligibleToPaintTime = …;
const base::TimeDelta kCreativeFCPTime = …;
const base::TimeDelta kOtherFrameEligibleToPaintTime = …;
const base::TimeDelta kOtherFrameFCPTime = …;
const char kAdUrl[] = …;
const char kOtherAdUrl[] = …;
const char kNonAdUrl[] = …;
const char kNonAdUrlSameOrigin[] = …;
const char kAllowedUrl[] = …;
const char kMemoryMainFrameMaxHistogramId[] = …;
const char kMemoryUpdateCountHistogramId[] = …;
const int kMaxHeavyAdNetworkBytes = …;
void PopulateRequiredTimingFieldsExceptFEtPAndFCP(
mojom::PageLoadTiming* inout_timing) { … }
class ResourceLoadingCancellingThrottle
: public content::TestNavigationThrottle { … };
class MockNoiseProvider
: public AdsPageLoadMetricsObserver::HeavyAdThresholdNoiseProvider { … };
std::string SuffixedHistogram(const std::string& suffix) { … }
void TestHistograms(const base::HistogramTester& histograms,
const ukm::TestAutoSetUkmRecorder& ukm_recorder,
const std::vector<ExpectedFrameBytes>& ad_frames,
size_t non_ad_cached_kb,
size_t non_ad_uncached_kb) { … }
class ErrorPageWaiter : public content::WebContentsObserver { … };
class FrameRemoteTester : public content::FakeLocalFrame { … };
}
class AdsPageLoadMetricsObserverTest
: public subresource_filter::SubresourceFilterTestHarness,
public blocklist::OptOutBlocklistDelegate,
public testing::WithParamInterface<bool> { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(AdsPageLoadMetricsObserverTest, PageWithNoAds) { … }
TEST_P(AdsPageLoadMetricsObserverTest, PageWithAds) { … }
TEST_P(AdsPageLoadMetricsObserverTest, PageWithAdsButNoAdFrame) { … }
TEST_P(AdsPageLoadMetricsObserverTest, AdFrameMimeTypeBytes) { … }
TEST_P(AdsPageLoadMetricsObserverTest, ResourceBeforeAdFrameCommits) { … }
TEST_P(AdsPageLoadMetricsObserverTest, AdsOriginStatusMetrics) { … }
TEST_P(AdsPageLoadMetricsObserverTest, PageWithAdFrameThatRenavigates) { … }
TEST_P(AdsPageLoadMetricsObserverTest, PageWithNonAdFrameThatRenavigatesToAd) { … }
TEST_P(AdsPageLoadMetricsObserverTest, CountAbortedNavigation) { … }
TEST_P(AdsPageLoadMetricsObserverTest, CountAbortedSecondNavigationForFrame) { … }
TEST_P(AdsPageLoadMetricsObserverTest, TwoResourceLoadsBeforeCommit) { … }
TEST_P(AdsPageLoadMetricsObserverTest, UntaggingAdFrame) { … }
TEST_P(AdsPageLoadMetricsObserverTest, MainFrameResource) { … }
TEST_P(AdsPageLoadMetricsObserverTest, NoBytesLoaded_NoHistogramsRecorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest, NoHistogramWithoutCommit) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
SubresourceFilterDisabled_NoAdsDetected) { … }
TEST_P(AdsPageLoadMetricsObserverTest, FilterAds_DoNotLogMetrics) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
FrameDestroyed_PerFrameHistogramsLogged) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
FrameAbortsCommitMatchingAllowedRule_FrameTracked) { … }
TEST_P(AdsPageLoadMetricsObserverTest, NonAdFrameDestroyed_FrameDeleted) { … }
TEST_P(AdsPageLoadMetricsObserverTest, MainFrameAdBytesRecorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest, MemoryCacheAdBytesRecorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest, AdPageLoadUKM) { … }
TEST_P(AdsPageLoadMetricsObserverTest, ZeroBytesZeroCpuUseFrame_NotRecorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest, ZeroBytesNonZeroCpuFrame_Recorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest, TestCpuTimingMetricsWindowUnactivated) { … }
TEST_P(AdsPageLoadMetricsObserverTest, AdDensityDistributionMoments) { … }
TEST_P(AdsPageLoadMetricsObserverTest, AdDensityOnPageWithoutAdBytes) { … }
TEST_P(AdsPageLoadMetricsObserverTest, TestCpuTimingMetricsWindowedActivated) { … }
TEST_P(AdsPageLoadMetricsObserverTest, TestCpuTimingMetricsNoActivation) { … }
TEST_P(AdsPageLoadMetricsObserverTest, TestCpuTimingMetricsOnActivation) { … }
TEST_P(AdsPageLoadMetricsObserverTest, CreativeOriginStatus) { … }
TEST_P(AdsPageLoadMetricsObserverTest, CreativeOriginStatusWithThrottling) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdFeatureOff_UMARecorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdNetworkUsage_InterventionFired) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdCpuInterventionInBackground) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
HeavyAdNetworkInterventionInBackgrounded) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
HeavyAdNetworkUsageWithNoise_InterventionFired) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
HeavyAdNetworkUsageLessThanNoisedThreshold_NotFired) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
HeavyAdNetworkUsageLessThanNoisedThreshold_CpuTriggers) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdTotalCpuUsage_InterventionFired) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdPeakCpuUsage_InterventionFired) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdFeatureDisabled_NotFired) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
HeavyAdWithUserGesture_NotConsideredHeavy) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdPolicyProvided) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdPageReload_MetricsRecorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdPageReload_InterventionIgnored) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
HeavyAdPageReloadPrivacyMitigationsDisabled_InterventionAllowed) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdBlocklistFull_NotFired) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
HeavyAdBlocklistDisabled_InterventionNotBlocked) { … }
TEST_P(AdsPageLoadMetricsObserverTest, HeavyAdBlocklist_InterventionReported) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
HeavyAdReportingOnly_ReportSentNoUnload) { … }
TEST_P(AdsPageLoadMetricsObserverTest, NoFirstContentfulPaint_NotRecorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest, FirstContentfulPaint_Recorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
FirstContentfulPaintPostDeviceAuction_Recorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
FirstContentfulPaintPostServerAuction_Recorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
FirstContentfulPaintPostAbortedOnDeviceAuction_NotRecorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
FirstContentfulPaintPostServerAndDeviceAuction_Recorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
FirstContentfulPaintOneServerAuctionAndOneDeviceAuction_Recorded) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
MultipleFirstContentfulPaintsInAdWithInOrderIPCs_EarliestUsed) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
MultipleFirstContentfulPaintsInAdWithOutOfOrderIPCs_EarliestUsed) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
MultipleAdFramesWithFirstContentfulPaint) { … }
TEST_P(AdsPageLoadMetricsObserverTest,
FirstContentfulPaintNoAdRootPainted_Recorded) { … }
class AdsMemoryMeasurementTest : public AdsPageLoadMetricsObserverTest { … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(AdsMemoryMeasurementTest, SingleAdFrame_MaxMemoryBytesRecorded) { … }
TEST_P(AdsMemoryMeasurementTest, MultiAdFramesNested_MaxMemoryBytesRecorded) { … }
TEST_P(AdsMemoryMeasurementTest,
MultiAdFramesNonNested_MaxMemoryBytesRecorded) { … }
TEST_P(AdsMemoryMeasurementTest, MainFrame_MaxMemoryBytesRecorded) { … }
}