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