Chromium Code Reviews| Index: chrome/browser/page_load_metrics/observers/ads_page_load_metrics_observer_unittest.cc |
| diff --git a/chrome/browser/page_load_metrics/observers/ads_page_load_metrics_observer_unittest.cc b/chrome/browser/page_load_metrics/observers/ads_page_load_metrics_observer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c61afb69a55e2680db784744fe373cc9a50bb253 |
| --- /dev/null |
| +++ b/chrome/browser/page_load_metrics/observers/ads_page_load_metrics_observer_unittest.cc |
| @@ -0,0 +1,397 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/page_load_metrics/observers/ads_page_load_metrics_observer.h" |
| + |
| +#include <string> |
| + |
| +#include "base/macros.h" |
| +#include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_test_harness.h" |
| +#include "chrome/browser/page_load_metrics/page_load_tracker.h" |
| +#include "content/public/browser/render_frame_host.h" |
| +#include "content/public/test/navigation_simulator.h" |
| +#include "content/public/test/test_renderer_host.h" |
| +#include "url/gurl.h" |
| + |
| +using content::RenderFrameHost; |
| +using content::RenderFrameHostTester; |
| +using content::NavigationSimulator; |
| + |
| +namespace { |
| + |
| +enum class ResourceCached { NOT_CACHED, CACHED }; |
| + |
| +} // namespace |
| + |
| +class AdsPageLoadMetricsObserverTest |
| + : public page_load_metrics::PageLoadMetricsObserverTestHarness { |
| + public: |
| + AdsPageLoadMetricsObserverTest() {} |
| + |
| + RenderFrameHost* NavigateMainFrame(const GURL& url) { |
| + RenderFrameHost* main_frame = web_contents()->GetMainFrame(); |
| + auto navigation_simulator = |
| + NavigationSimulator::CreateRendererInitiated(url, main_frame); |
| + navigation_simulator->Commit(); |
| + return navigation_simulator->GetFinalRenderFrameHost(); |
| + } |
| + |
| + RenderFrameHost* RenavigateFrame(const GURL& url, |
| + content::RenderFrameHost* frame) { |
| + auto navigation_simulator = |
| + NavigationSimulator::CreateRendererInitiated(url, frame); |
| + navigation_simulator->Commit(); |
| + return navigation_simulator->GetFinalRenderFrameHost(); |
| + } |
| + |
| + RenderFrameHost* CreateAndNavigateSubFrame(const GURL& url, |
| + const std::string& frame_name, |
| + content::RenderFrameHost* parent) { |
| + RenderFrameHost* subframe = |
| + RenderFrameHostTester::For(parent)->AppendChild(frame_name); |
| + auto navigation_simulator = |
| + NavigationSimulator::CreateRendererInitiated(url, subframe); |
| + navigation_simulator->Commit(); |
| + return navigation_simulator->GetFinalRenderFrameHost(); |
| + } |
| + |
| + void LoadResource(RenderFrameHost* frame, |
| + ResourceCached resource_cached, |
| + int resource_size_in_kb) { |
| + page_load_metrics::ExtraRequestInfo request( |
| + GURL(), frame->GetFrameTreeNodeId(), |
|
Bryan McQuade
2017/04/17 22:00:04
just to make sure i understand, a FrameTreeNodeId
jkarlin
2017/04/24 17:27:59
Correct, see: https://cs.chromium.org/chromium/src
|
| + resource_cached == ResourceCached::CACHED, resource_size_in_kb * 1024, |
| + false /* data_reduction_proxy_used */, |
| + 0 /* original_network_content_length */); |
| + SimulateLoadedResource(request); |
| + } |
| + |
| + protected: |
| + void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { |
| + tracker->AddObserver(base::MakeUnique<AdsPageLoadMetricsObserver>()); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(AdsPageLoadMetricsObserverTest); |
| +}; |
| + |
| +TEST_F(AdsPageLoadMetricsObserverTest, PageWithNoAds) { |
| + RenderFrameHost* main_frame = NavigateMainFrame(GURL("https://foo.com/")); |
| + RenderFrameHost* frame1 = CreateAndNavigateSubFrame( |
| + GURL("https://bar.com/frame1"), "foo name", main_frame); |
| + RenderFrameHost* frame2 = |
| + CreateAndNavigateSubFrame(GURL("https://bar.com/frame2"), "", main_frame); |
| + LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); |
| + LoadResource(frame1, ResourceCached::NOT_CACHED, 1); |
| + LoadResource(frame2, ResourceCached::NOT_CACHED, 1); |
| + |
| + // Navigate again to trigger histograms. |
| + RenavigateFrame(GURL("https://bar.com/"), main_frame); |
| + |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.PageHasNoAds", 1, 1); |
| + histogram_tester().ExpectTotalCount( |
| + "PageLoad.Clients.Ads.Google.AdFrameCount", 0); |
| + histogram_tester().ExpectTotalCount( |
| + "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 0); |
| +} |
| + |
| +TEST_F(AdsPageLoadMetricsObserverTest, ResourceBeforeAdFrameCommits) { |
| + RenderFrameHost* main_frame = NavigateMainFrame(GURL("https://foo.com/")); |
| + |
| + LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); |
| + |
| + // Assume that the next frame's id will be the main frame + 1 and load a |
| + // resource for that frame. Make sure it gets counted. |
| + page_load_metrics::ExtraRequestInfo request( |
| + GURL(), main_frame->GetFrameTreeNodeId() + 1, false /* cached */, |
| + 1024 /* size */, false /* data_reduction_proxy_used */, |
| + 0 /* original_network_content_length */); |
| + SimulateLoadedResource(request); |
| + |
| + CreateAndNavigateSubFrame(GURL("https://foo.com/frame2"), |
| + "google_ads_iframe_1", main_frame); |
| + |
| + // Navigate again to trigger histograms. |
| + RenavigateFrame(GURL("https://bar.com/"), main_frame); |
| + |
| + // 2KB total were loaded from network, one of which was in an ad frame. |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.PageHasNoAds", 0, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.AdFrameCount", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.TopLevelSubFrameCount", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.PercentTopLevelSubFramesAreAdFrames", 100, |
| + 1); |
| + |
| + // Individual Ad Frame Metrics |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 100, |
| + 1); |
| + |
| + // Page percentages |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", 50, |
| + 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", |
| + 100, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes." |
| + "PercentPageNetworkBytesFromAllAdFrames", |
| + 50, 1); |
| + |
| + // Page byte counts |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytes", 2, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", 2, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", 1, 1); |
| +} |
| + |
| +TEST_F(AdsPageLoadMetricsObserverTest, PageWithAdFrames) { |
| + RenderFrameHost* main_frame = NavigateMainFrame(GURL("http://foo.com/")); |
| + RenderFrameHost* non_ad_frame = CreateAndNavigateSubFrame( |
| + GURL("https://foo.com/frame1"), "foo name", main_frame); |
| + |
| + // Create 5 ad frames with the 5th nested inside the 4th. Verify that the |
| + // nested ad frame doesn't get counted separately (but that its bytes are |
| + // still counted). Also verify that the various ad signals (urls and names) |
| + // are properly detected. |
| + RenderFrameHost* ad_frame1 = CreateAndNavigateSubFrame( |
| + GURL("http://foo.com/iframe1"), "google_ads_iframe_1", main_frame); |
| + RenderFrameHost* ad_frame2 = CreateAndNavigateSubFrame( |
| + GURL("https://bar.com/frame1"), "google_ads_frame_1", main_frame); |
| + RenderFrameHost* ad_frame3 = CreateAndNavigateSubFrame( |
| + GURL("http://tpc.googlesyndication.com/safeframe/"), "", main_frame); |
| + RenderFrameHost* ad_frame4 = CreateAndNavigateSubFrame( |
| + GURL("https://tpc.googlesyndication.com/safeframe/1"), "", main_frame); |
| + RenderFrameHost* nested_ad_frame4 = CreateAndNavigateSubFrame( |
| + GURL("https://tpc.googlesyndication.com/safeframe/2"), "", ad_frame4); |
| + |
| + // Create an addditional ad frame without content, it shouldn't be counted |
| + // in some percentage calculations. |
| + CreateAndNavigateSubFrame( |
| + GURL("https://tpc.googlesyndication.com/safeframe/3"), "", main_frame); |
| + |
| + // 7 bytes total in page, 5 from ads. 4 total bytes from network, 3 of those |
| + // are from ads. |
| + LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); |
| + LoadResource(non_ad_frame, ResourceCached::CACHED, 1); |
| + LoadResource(ad_frame1, ResourceCached::CACHED, 1); |
| + LoadResource(ad_frame2, ResourceCached::NOT_CACHED, 1); |
| + LoadResource(ad_frame3, ResourceCached::NOT_CACHED, 1); |
| + LoadResource(ad_frame4, ResourceCached::NOT_CACHED, 1); |
| + LoadResource(nested_ad_frame4, ResourceCached::CACHED, 1); |
| + |
| + // Navigate again to trigger histograms. |
| + RenavigateFrame(GURL("https://bar.com/"), main_frame); |
| + |
| + // Individual Ad Frame Metrics |
| + histogram_tester().ExpectBucketCount( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 1, 3); |
| + histogram_tester().ExpectBucketCount( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 2, 1); |
| + histogram_tester().ExpectBucketCount( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 1, 3); |
| + histogram_tester().ExpectBucketCount( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 0, 2); |
| + histogram_tester().ExpectBucketCount( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 0, 1); |
| + histogram_tester().ExpectBucketCount( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 100, |
| + 2); |
| + histogram_tester().ExpectBucketCount( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 50, |
| + 1); |
| + |
| + // Counts |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.PageHasNoAds", 0, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.AdFrameCount", 5, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.TopLevelSubFrameCount", 6, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", 5, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.PercentTopLevelSubFramesAreAdFrames", 83, 1); |
| + |
| + // Page percentages |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", 71, |
| + 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", |
| + 60, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes." |
| + "PercentPageNetworkBytesFromAllAdFrames", |
| + 75, 1); |
| + |
| + // Page byte counts |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 5, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", 3, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytes", 7, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", 4, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", 2, 1); |
| +} |
| + |
| +TEST_F(AdsPageLoadMetricsObserverTest, PageWithAdFrameThatRenavigates) { |
| + RenderFrameHost* main_frame = NavigateMainFrame(GURL("http://foo.com/")); |
| + RenderFrameHost* ad_frame = CreateAndNavigateSubFrame( |
| + GURL("http://foo.com/iframe1"), "google_ads_iframe_1", main_frame); |
| + |
| + LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); |
| + LoadResource(ad_frame, ResourceCached::NOT_CACHED, 1); |
| + |
| + // Navigate the ad frame again. |
| + ad_frame = RenavigateFrame(GURL("https://bar.com/iframe1"), ad_frame); |
| + |
| + // In total, 3 bytes for entire page and 2 bytes in one ad frame. |
| + LoadResource(ad_frame, ResourceCached::NOT_CACHED, 1); |
| + |
| + // Navigate again to trigger histograms. |
| + RenavigateFrame(GURL("https://bar.com/"), main_frame); |
| + |
| + // Individual Ad Frame Metrics |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 2, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 2, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 100, |
| + 1); |
| + |
| + // Counts |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.PageHasNoAds", 0, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.AdFrameCount", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.TopLevelSubFrameCount", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.PercentTopLevelSubFramesAreAdFrames", 100, |
| + 1); |
| + |
| + // Page percentages |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", 66, |
| + 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", |
| + 100, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes." |
| + "PercentPageNetworkBytesFromAllAdFrames", |
| + 66, 1); |
| + |
| + // Page byte counts |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 2, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", 2, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytes", 3, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", 3, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", 1, 1); |
| +} |
| + |
| +TEST_F(AdsPageLoadMetricsObserverTest, PageWithNonAdFrameThatRenavigatesToAd) { |
| + // Main frame. |
| + RenderFrameHost* main_frame = NavigateMainFrame(GURL("http://foo.com/")); |
| + |
| + // Sub frame that is not an ad. |
| + RenderFrameHost* sub_frame = CreateAndNavigateSubFrame( |
| + GURL("http://foo.com/iframe1"), "foo", main_frame); |
| + |
| + // Child of the sub-frame that is an ad. |
| + RenderFrameHost* sub_frame_child_ad = CreateAndNavigateSubFrame( |
| + GURL("http://bar.com/iframe1"), "google_ads_iframe_1", sub_frame); |
| + |
| + LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); |
| + LoadResource(sub_frame, ResourceCached::NOT_CACHED, 1); |
| + LoadResource(sub_frame_child_ad, ResourceCached::NOT_CACHED, 1); |
| + |
| + // Navigate the subframe again, this time it's an ad. |
| + sub_frame = RenavigateFrame( |
| + GURL("https://tpc.googlesyndication.com/safeframe/1"), sub_frame); |
| + |
| + LoadResource(sub_frame, ResourceCached::NOT_CACHED, 1); |
| + |
| + // In total, 4 bytes were loaded for the entire page and 2 bytes from ad |
| + // frames (the original child ad frame and the renavigated frame which |
| + // turned into an ad). |
| + |
| + // Navigate again to trigger histograms. |
| + RenavigateFrame(GURL("https://bar.com/"), main_frame); |
| + |
| + // Individual Ad Frame Metrics |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 1, 2); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 1, 2); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 100, |
| + 2); |
| + |
| + // Counts |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.PageHasNoAds", 0, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.AdFrameCount", 2, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.TopLevelSubFrameCount", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", 1, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.PercentTopLevelSubFramesAreAdFrames", 100, |
| + 1); |
| + |
| + // Page percentages |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", 50, |
| + 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", |
| + 100, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes." |
| + "PercentPageNetworkBytesFromAllAdFrames", |
| + 50, 1); |
| + |
| + // Page byte counts |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 2, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", 2, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytes", 4, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", 4, 1); |
| + histogram_tester().ExpectUniqueSample( |
| + "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", 2, 1); |
| +} |