OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/page_load_metrics/observers/ads_page_load_metrics_obser ver.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/macros.h" | |
10 #include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_ test_harness.h" | |
11 #include "chrome/browser/page_load_metrics/page_load_tracker.h" | |
12 #include "content/public/browser/render_frame_host.h" | |
13 #include "content/public/test/navigation_simulator.h" | |
14 #include "content/public/test/test_renderer_host.h" | |
15 #include "url/gurl.h" | |
16 | |
17 using content::RenderFrameHost; | |
18 using content::RenderFrameHostTester; | |
19 using content::NavigationSimulator; | |
20 | |
21 namespace { | |
22 | |
23 enum class ResourceCached { NOT_CACHED, CACHED }; | |
24 | |
25 } // namespace | |
26 | |
27 class AdsPageLoadMetricsObserverTest | |
28 : public page_load_metrics::PageLoadMetricsObserverTestHarness { | |
29 public: | |
30 AdsPageLoadMetricsObserverTest() {} | |
31 | |
32 RenderFrameHost* NavigateMainFrame(const GURL& url) { | |
33 RenderFrameHost* main_frame = web_contents()->GetMainFrame(); | |
34 auto navigation_simulator = | |
35 NavigationSimulator::CreateRendererInitiated(url, main_frame); | |
36 navigation_simulator->Commit(); | |
37 return navigation_simulator->GetFinalRenderFrameHost(); | |
38 } | |
39 | |
40 RenderFrameHost* RenavigateFrame(const GURL& url, | |
41 content::RenderFrameHost* frame) { | |
42 auto navigation_simulator = | |
43 NavigationSimulator::CreateRendererInitiated(url, frame); | |
44 navigation_simulator->Commit(); | |
45 return navigation_simulator->GetFinalRenderFrameHost(); | |
46 } | |
47 | |
48 RenderFrameHost* CreateAndNavigateSubFrame(const GURL& url, | |
49 const std::string& frame_name, | |
50 content::RenderFrameHost* parent) { | |
51 RenderFrameHost* subframe = | |
52 RenderFrameHostTester::For(parent)->AppendChild(frame_name); | |
53 auto navigation_simulator = | |
54 NavigationSimulator::CreateRendererInitiated(url, subframe); | |
55 navigation_simulator->Commit(); | |
56 return navigation_simulator->GetFinalRenderFrameHost(); | |
57 } | |
58 | |
59 void LoadResource(RenderFrameHost* frame, | |
60 ResourceCached resource_cached, | |
61 int resource_size_in_kb) { | |
62 page_load_metrics::ExtraRequestInfo request( | |
63 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
| |
64 resource_cached == ResourceCached::CACHED, resource_size_in_kb * 1024, | |
65 false /* data_reduction_proxy_used */, | |
66 0 /* original_network_content_length */); | |
67 SimulateLoadedResource(request); | |
68 } | |
69 | |
70 protected: | |
71 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { | |
72 tracker->AddObserver(base::MakeUnique<AdsPageLoadMetricsObserver>()); | |
73 } | |
74 | |
75 private: | |
76 DISALLOW_COPY_AND_ASSIGN(AdsPageLoadMetricsObserverTest); | |
77 }; | |
78 | |
79 TEST_F(AdsPageLoadMetricsObserverTest, PageWithNoAds) { | |
80 RenderFrameHost* main_frame = NavigateMainFrame(GURL("https://foo.com/")); | |
81 RenderFrameHost* frame1 = CreateAndNavigateSubFrame( | |
82 GURL("https://bar.com/frame1"), "foo name", main_frame); | |
83 RenderFrameHost* frame2 = | |
84 CreateAndNavigateSubFrame(GURL("https://bar.com/frame2"), "", main_frame); | |
85 LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); | |
86 LoadResource(frame1, ResourceCached::NOT_CACHED, 1); | |
87 LoadResource(frame2, ResourceCached::NOT_CACHED, 1); | |
88 | |
89 // Navigate again to trigger histograms. | |
90 RenavigateFrame(GURL("https://bar.com/"), main_frame); | |
91 | |
92 histogram_tester().ExpectUniqueSample( | |
93 "PageLoad.Clients.Ads.Google.PageHasNoAds", 1, 1); | |
94 histogram_tester().ExpectTotalCount( | |
95 "PageLoad.Clients.Ads.Google.AdFrameCount", 0); | |
96 histogram_tester().ExpectTotalCount( | |
97 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 0); | |
98 } | |
99 | |
100 TEST_F(AdsPageLoadMetricsObserverTest, ResourceBeforeAdFrameCommits) { | |
101 RenderFrameHost* main_frame = NavigateMainFrame(GURL("https://foo.com/")); | |
102 | |
103 LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); | |
104 | |
105 // Assume that the next frame's id will be the main frame + 1 and load a | |
106 // resource for that frame. Make sure it gets counted. | |
107 page_load_metrics::ExtraRequestInfo request( | |
108 GURL(), main_frame->GetFrameTreeNodeId() + 1, false /* cached */, | |
109 1024 /* size */, false /* data_reduction_proxy_used */, | |
110 0 /* original_network_content_length */); | |
111 SimulateLoadedResource(request); | |
112 | |
113 CreateAndNavigateSubFrame(GURL("https://foo.com/frame2"), | |
114 "google_ads_iframe_1", main_frame); | |
115 | |
116 // Navigate again to trigger histograms. | |
117 RenavigateFrame(GURL("https://bar.com/"), main_frame); | |
118 | |
119 // 2KB total were loaded from network, one of which was in an ad frame. | |
120 histogram_tester().ExpectUniqueSample( | |
121 "PageLoad.Clients.Ads.Google.PageHasNoAds", 0, 1); | |
122 histogram_tester().ExpectUniqueSample( | |
123 "PageLoad.Clients.Ads.Google.AdFrameCount", 1, 1); | |
124 histogram_tester().ExpectUniqueSample( | |
125 "PageLoad.Clients.Ads.Google.TopLevelSubFrameCount", 1, 1); | |
126 histogram_tester().ExpectUniqueSample( | |
127 "PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", 1, 1); | |
128 histogram_tester().ExpectUniqueSample( | |
129 "PageLoad.Clients.Ads.Google.PercentTopLevelSubFramesAreAdFrames", 100, | |
130 1); | |
131 | |
132 // Individual Ad Frame Metrics | |
133 histogram_tester().ExpectUniqueSample( | |
134 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 1, 1); | |
135 histogram_tester().ExpectUniqueSample( | |
136 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 1, 1); | |
137 histogram_tester().ExpectUniqueSample( | |
138 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 100, | |
139 1); | |
140 | |
141 // Page percentages | |
142 histogram_tester().ExpectUniqueSample( | |
143 "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", 50, | |
144 1); | |
145 histogram_tester().ExpectUniqueSample( | |
146 "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", | |
147 100, 1); | |
148 histogram_tester().ExpectUniqueSample( | |
149 "PageLoad.Clients.Ads.Google.Bytes." | |
150 "PercentPageNetworkBytesFromAllAdFrames", | |
151 50, 1); | |
152 | |
153 // Page byte counts | |
154 histogram_tester().ExpectUniqueSample( | |
155 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 1, 1); | |
156 histogram_tester().ExpectUniqueSample( | |
157 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", 1, 1); | |
158 histogram_tester().ExpectUniqueSample( | |
159 "PageLoad.Clients.Ads.Google.Bytes.PageBytes", 2, 1); | |
160 histogram_tester().ExpectUniqueSample( | |
161 "PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", 2, 1); | |
162 histogram_tester().ExpectUniqueSample( | |
163 "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", 1, 1); | |
164 } | |
165 | |
166 TEST_F(AdsPageLoadMetricsObserverTest, PageWithAdFrames) { | |
167 RenderFrameHost* main_frame = NavigateMainFrame(GURL("http://foo.com/")); | |
168 RenderFrameHost* non_ad_frame = CreateAndNavigateSubFrame( | |
169 GURL("https://foo.com/frame1"), "foo name", main_frame); | |
170 | |
171 // Create 5 ad frames with the 5th nested inside the 4th. Verify that the | |
172 // nested ad frame doesn't get counted separately (but that its bytes are | |
173 // still counted). Also verify that the various ad signals (urls and names) | |
174 // are properly detected. | |
175 RenderFrameHost* ad_frame1 = CreateAndNavigateSubFrame( | |
176 GURL("http://foo.com/iframe1"), "google_ads_iframe_1", main_frame); | |
177 RenderFrameHost* ad_frame2 = CreateAndNavigateSubFrame( | |
178 GURL("https://bar.com/frame1"), "google_ads_frame_1", main_frame); | |
179 RenderFrameHost* ad_frame3 = CreateAndNavigateSubFrame( | |
180 GURL("http://tpc.googlesyndication.com/safeframe/"), "", main_frame); | |
181 RenderFrameHost* ad_frame4 = CreateAndNavigateSubFrame( | |
182 GURL("https://tpc.googlesyndication.com/safeframe/1"), "", main_frame); | |
183 RenderFrameHost* nested_ad_frame4 = CreateAndNavigateSubFrame( | |
184 GURL("https://tpc.googlesyndication.com/safeframe/2"), "", ad_frame4); | |
185 | |
186 // Create an addditional ad frame without content, it shouldn't be counted | |
187 // in some percentage calculations. | |
188 CreateAndNavigateSubFrame( | |
189 GURL("https://tpc.googlesyndication.com/safeframe/3"), "", main_frame); | |
190 | |
191 // 7 bytes total in page, 5 from ads. 4 total bytes from network, 3 of those | |
192 // are from ads. | |
193 LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); | |
194 LoadResource(non_ad_frame, ResourceCached::CACHED, 1); | |
195 LoadResource(ad_frame1, ResourceCached::CACHED, 1); | |
196 LoadResource(ad_frame2, ResourceCached::NOT_CACHED, 1); | |
197 LoadResource(ad_frame3, ResourceCached::NOT_CACHED, 1); | |
198 LoadResource(ad_frame4, ResourceCached::NOT_CACHED, 1); | |
199 LoadResource(nested_ad_frame4, ResourceCached::CACHED, 1); | |
200 | |
201 // Navigate again to trigger histograms. | |
202 RenavigateFrame(GURL("https://bar.com/"), main_frame); | |
203 | |
204 // Individual Ad Frame Metrics | |
205 histogram_tester().ExpectBucketCount( | |
206 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 1, 3); | |
207 histogram_tester().ExpectBucketCount( | |
208 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 2, 1); | |
209 histogram_tester().ExpectBucketCount( | |
210 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 1, 3); | |
211 histogram_tester().ExpectBucketCount( | |
212 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 0, 2); | |
213 histogram_tester().ExpectBucketCount( | |
214 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 0, 1); | |
215 histogram_tester().ExpectBucketCount( | |
216 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 100, | |
217 2); | |
218 histogram_tester().ExpectBucketCount( | |
219 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 50, | |
220 1); | |
221 | |
222 // Counts | |
223 histogram_tester().ExpectUniqueSample( | |
224 "PageLoad.Clients.Ads.Google.PageHasNoAds", 0, 1); | |
225 histogram_tester().ExpectUniqueSample( | |
226 "PageLoad.Clients.Ads.Google.AdFrameCount", 5, 1); | |
227 histogram_tester().ExpectUniqueSample( | |
228 "PageLoad.Clients.Ads.Google.TopLevelSubFrameCount", 6, 1); | |
229 histogram_tester().ExpectUniqueSample( | |
230 "PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", 5, 1); | |
231 histogram_tester().ExpectUniqueSample( | |
232 "PageLoad.Clients.Ads.Google.PercentTopLevelSubFramesAreAdFrames", 83, 1); | |
233 | |
234 // Page percentages | |
235 histogram_tester().ExpectUniqueSample( | |
236 "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", 71, | |
237 1); | |
238 histogram_tester().ExpectUniqueSample( | |
239 "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", | |
240 60, 1); | |
241 histogram_tester().ExpectUniqueSample( | |
242 "PageLoad.Clients.Ads.Google.Bytes." | |
243 "PercentPageNetworkBytesFromAllAdFrames", | |
244 75, 1); | |
245 | |
246 // Page byte counts | |
247 histogram_tester().ExpectUniqueSample( | |
248 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 5, 1); | |
249 histogram_tester().ExpectUniqueSample( | |
250 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", 3, 1); | |
251 histogram_tester().ExpectUniqueSample( | |
252 "PageLoad.Clients.Ads.Google.Bytes.PageBytes", 7, 1); | |
253 histogram_tester().ExpectUniqueSample( | |
254 "PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", 4, 1); | |
255 histogram_tester().ExpectUniqueSample( | |
256 "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", 2, 1); | |
257 } | |
258 | |
259 TEST_F(AdsPageLoadMetricsObserverTest, PageWithAdFrameThatRenavigates) { | |
260 RenderFrameHost* main_frame = NavigateMainFrame(GURL("http://foo.com/")); | |
261 RenderFrameHost* ad_frame = CreateAndNavigateSubFrame( | |
262 GURL("http://foo.com/iframe1"), "google_ads_iframe_1", main_frame); | |
263 | |
264 LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); | |
265 LoadResource(ad_frame, ResourceCached::NOT_CACHED, 1); | |
266 | |
267 // Navigate the ad frame again. | |
268 ad_frame = RenavigateFrame(GURL("https://bar.com/iframe1"), ad_frame); | |
269 | |
270 // In total, 3 bytes for entire page and 2 bytes in one ad frame. | |
271 LoadResource(ad_frame, ResourceCached::NOT_CACHED, 1); | |
272 | |
273 // Navigate again to trigger histograms. | |
274 RenavigateFrame(GURL("https://bar.com/"), main_frame); | |
275 | |
276 // Individual Ad Frame Metrics | |
277 histogram_tester().ExpectUniqueSample( | |
278 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 2, 1); | |
279 histogram_tester().ExpectUniqueSample( | |
280 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 2, 1); | |
281 histogram_tester().ExpectUniqueSample( | |
282 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 100, | |
283 1); | |
284 | |
285 // Counts | |
286 histogram_tester().ExpectUniqueSample( | |
287 "PageLoad.Clients.Ads.Google.PageHasNoAds", 0, 1); | |
288 histogram_tester().ExpectUniqueSample( | |
289 "PageLoad.Clients.Ads.Google.AdFrameCount", 1, 1); | |
290 histogram_tester().ExpectUniqueSample( | |
291 "PageLoad.Clients.Ads.Google.TopLevelSubFrameCount", 1, 1); | |
292 histogram_tester().ExpectUniqueSample( | |
293 "PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", 1, 1); | |
294 histogram_tester().ExpectUniqueSample( | |
295 "PageLoad.Clients.Ads.Google.PercentTopLevelSubFramesAreAdFrames", 100, | |
296 1); | |
297 | |
298 // Page percentages | |
299 histogram_tester().ExpectUniqueSample( | |
300 "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", 66, | |
301 1); | |
302 histogram_tester().ExpectUniqueSample( | |
303 "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", | |
304 100, 1); | |
305 histogram_tester().ExpectUniqueSample( | |
306 "PageLoad.Clients.Ads.Google.Bytes." | |
307 "PercentPageNetworkBytesFromAllAdFrames", | |
308 66, 1); | |
309 | |
310 // Page byte counts | |
311 histogram_tester().ExpectUniqueSample( | |
312 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 2, 1); | |
313 histogram_tester().ExpectUniqueSample( | |
314 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", 2, 1); | |
315 histogram_tester().ExpectUniqueSample( | |
316 "PageLoad.Clients.Ads.Google.Bytes.PageBytes", 3, 1); | |
317 histogram_tester().ExpectUniqueSample( | |
318 "PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", 3, 1); | |
319 histogram_tester().ExpectUniqueSample( | |
320 "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", 1, 1); | |
321 } | |
322 | |
323 TEST_F(AdsPageLoadMetricsObserverTest, PageWithNonAdFrameThatRenavigatesToAd) { | |
324 // Main frame. | |
325 RenderFrameHost* main_frame = NavigateMainFrame(GURL("http://foo.com/")); | |
326 | |
327 // Sub frame that is not an ad. | |
328 RenderFrameHost* sub_frame = CreateAndNavigateSubFrame( | |
329 GURL("http://foo.com/iframe1"), "foo", main_frame); | |
330 | |
331 // Child of the sub-frame that is an ad. | |
332 RenderFrameHost* sub_frame_child_ad = CreateAndNavigateSubFrame( | |
333 GURL("http://bar.com/iframe1"), "google_ads_iframe_1", sub_frame); | |
334 | |
335 LoadResource(main_frame, ResourceCached::NOT_CACHED, 1); | |
336 LoadResource(sub_frame, ResourceCached::NOT_CACHED, 1); | |
337 LoadResource(sub_frame_child_ad, ResourceCached::NOT_CACHED, 1); | |
338 | |
339 // Navigate the subframe again, this time it's an ad. | |
340 sub_frame = RenavigateFrame( | |
341 GURL("https://tpc.googlesyndication.com/safeframe/1"), sub_frame); | |
342 | |
343 LoadResource(sub_frame, ResourceCached::NOT_CACHED, 1); | |
344 | |
345 // In total, 4 bytes were loaded for the entire page and 2 bytes from ad | |
346 // frames (the original child ad frame and the renavigated frame which | |
347 // turned into an ad). | |
348 | |
349 // Navigate again to trigger histograms. | |
350 RenavigateFrame(GURL("https://bar.com/"), main_frame); | |
351 | |
352 // Individual Ad Frame Metrics | |
353 histogram_tester().ExpectUniqueSample( | |
354 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", 1, 2); | |
355 histogram_tester().ExpectUniqueSample( | |
356 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", 1, 2); | |
357 histogram_tester().ExpectUniqueSample( | |
358 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", 100, | |
359 2); | |
360 | |
361 // Counts | |
362 histogram_tester().ExpectUniqueSample( | |
363 "PageLoad.Clients.Ads.Google.PageHasNoAds", 0, 1); | |
364 histogram_tester().ExpectUniqueSample( | |
365 "PageLoad.Clients.Ads.Google.AdFrameCount", 2, 1); | |
366 histogram_tester().ExpectUniqueSample( | |
367 "PageLoad.Clients.Ads.Google.TopLevelSubFrameCount", 1, 1); | |
368 histogram_tester().ExpectUniqueSample( | |
369 "PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", 1, 1); | |
370 histogram_tester().ExpectUniqueSample( | |
371 "PageLoad.Clients.Ads.Google.PercentTopLevelSubFramesAreAdFrames", 100, | |
372 1); | |
373 | |
374 // Page percentages | |
375 histogram_tester().ExpectUniqueSample( | |
376 "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", 50, | |
377 1); | |
378 histogram_tester().ExpectUniqueSample( | |
379 "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", | |
380 100, 1); | |
381 histogram_tester().ExpectUniqueSample( | |
382 "PageLoad.Clients.Ads.Google.Bytes." | |
383 "PercentPageNetworkBytesFromAllAdFrames", | |
384 50, 1); | |
385 | |
386 // Page byte counts | |
387 histogram_tester().ExpectUniqueSample( | |
388 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", 2, 1); | |
389 histogram_tester().ExpectUniqueSample( | |
390 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", 2, 1); | |
391 histogram_tester().ExpectUniqueSample( | |
392 "PageLoad.Clients.Ads.Google.Bytes.PageBytes", 4, 1); | |
393 histogram_tester().ExpectUniqueSample( | |
394 "PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", 4, 1); | |
395 histogram_tester().ExpectUniqueSample( | |
396 "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", 2, 1); | |
397 } | |
OLD | NEW |