OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/page_load_metrics/browser/metrics_web_contents_observer.h" | 5 #include "components/page_load_metrics/browser/metrics_web_contents_observer.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "components/page_load_metrics/common/page_load_metrics_messages.h" | 11 #include "components/page_load_metrics/common/page_load_metrics_messages.h" |
12 #include "content/public/browser/navigation_handle.h" | 12 #include "content/public/browser/navigation_handle.h" |
13 #include "content/public/browser/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
14 #include "content/public/test/test_renderer_host.h" | 14 #include "content/public/test/test_renderer_host.h" |
15 #include "content/public/test/web_contents_tester.h" | 15 #include "content/public/test/web_contents_tester.h" |
16 | 16 |
17 namespace page_load_metrics { | 17 namespace page_load_metrics { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const char kDefaultTestUrl[] = "https://google.com"; | 21 const char kDefaultTestUrl[] = "https://google.com"; |
22 const char kDefaultTestUrlAnchor[] = "https://google.com#samepage"; | 22 const char kDefaultTestUrlAnchor[] = "https://google.com#samepage"; |
23 const char kDefaultTestUrl2[] = "https://whatever.com"; | 23 const char kDefaultTestUrl2[] = "https://whatever.com"; |
24 | 24 |
25 const char kHistogramNameFirstLayout[] = | 25 const char kHistogramNameFirstLayout[] = |
26 "PageLoad.Timing.NavigationToFirstLayout"; | 26 "PageLoad.Timing2.NavigationToFirstLayout"; |
27 const char kHistogramNameDomContent[] = | 27 const char kHistogramNameDomContent[] = |
28 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired"; | 28 "PageLoad.Timing2.NavigationToDOMContentLoadedEventFired"; |
29 const char kHistogramNameLoad[] = | 29 const char kHistogramNameLoad[] = |
30 "PageLoad.Timing.NavigationToLoadEventFired"; | 30 "PageLoad.Timing2.NavigationToLoadEventFired"; |
31 | 31 |
| 32 const char kBGHistogramNameFirstLayout[] = |
| 33 "PageLoad.Timing2.NavigationToFirstLayout.BG"; |
| 34 const char kBGHistogramNameDomContent[] = |
| 35 "PageLoad.Timing2.NavigationToDOMContentLoadedEventFired.BG"; |
| 36 const char kBGHistogramNameLoad[] = |
| 37 "PageLoad.Timing2.NavigationToLoadEventFired.BG"; |
32 } // namespace | 38 } // namespace |
33 | 39 |
34 class MetricsWebContentsObserverTest | 40 class MetricsWebContentsObserverTest |
35 : public content::RenderViewHostTestHarness { | 41 : public content::RenderViewHostTestHarness { |
36 public: | 42 public: |
37 MetricsWebContentsObserverTest() {} | 43 MetricsWebContentsObserverTest() {} |
38 | 44 |
39 void SetUp() override { | 45 void SetUp() override { |
40 RenderViewHostTestHarness::SetUp(); | 46 RenderViewHostTestHarness::SetUp(); |
41 observer_ = make_scoped_ptr(new MetricsWebContentsObserver(web_contents())); | 47 observer_ = make_scoped_ptr(new MetricsWebContentsObserver(web_contents())); |
| 48 observer_->WasShown(); |
42 } | 49 } |
43 | 50 |
44 void AssertNoHistogramsLogged() { | 51 void AssertNoHistogramsLogged() { |
45 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | 52 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
46 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | 53 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
47 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); | 54 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
48 } | 55 } |
49 | 56 |
50 protected: | 57 protected: |
51 base::HistogramTester histogram_tester_; | 58 base::HistogramTester histogram_tester_; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 | 211 |
205 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); | 212 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); |
206 histogram_tester_.ExpectBucketCount(kHistogramNameDomContent, | 213 histogram_tester_.ExpectBucketCount(kHistogramNameDomContent, |
207 dom_content.InMilliseconds(), 1); | 214 dom_content.InMilliseconds(), 1); |
208 | 215 |
209 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 1); | 216 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 1); |
210 histogram_tester_.ExpectBucketCount(kHistogramNameLoad, load.InMilliseconds(), | 217 histogram_tester_.ExpectBucketCount(kHistogramNameLoad, load.InMilliseconds(), |
211 1); | 218 1); |
212 } | 219 } |
213 | 220 |
| 221 TEST_F(MetricsWebContentsObserverTest, BackgroundDifferentHistogram) { |
| 222 base::TimeDelta first_layout = base::TimeDelta::FromSeconds(2); |
| 223 |
| 224 PageLoadTiming timing; |
| 225 timing.navigation_start = base::Time::FromDoubleT( |
| 226 (base::TimeTicks::Now() - base::TimeTicks::UnixEpoch()).InSecondsF()); |
| 227 timing.first_layout = first_layout; |
| 228 |
| 229 content::WebContentsTester* web_contents_tester = |
| 230 content::WebContentsTester::For(web_contents()); |
| 231 |
| 232 // Simulate "Open link in new tab." |
| 233 observer_->WasHidden(); |
| 234 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 235 |
| 236 observer_->OnMessageReceived( |
| 237 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 238 web_contents()->GetMainFrame()); |
| 239 |
| 240 // Simulate switching to the tab and making another navigation. |
| 241 observer_->WasShown(); |
| 242 AssertNoHistogramsLogged(); |
| 243 |
| 244 // Navigate again to force histogram recording. |
| 245 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 246 |
| 247 histogram_tester_.ExpectTotalCount(kBGHistogramNameDomContent, 0); |
| 248 histogram_tester_.ExpectTotalCount(kBGHistogramNameLoad, 0); |
| 249 histogram_tester_.ExpectTotalCount(kBGHistogramNameFirstLayout, 1); |
| 250 histogram_tester_.ExpectBucketCount(kBGHistogramNameFirstLayout, |
| 251 first_layout.InMilliseconds(), 1); |
| 252 |
| 253 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| 254 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 255 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
| 256 } |
| 257 |
| 258 TEST_F(MetricsWebContentsObserverTest, OnlyBackgroundLaterEvents) { |
| 259 PageLoadTiming timing; |
| 260 timing.navigation_start = base::Time::FromDoubleT( |
| 261 (base::TimeTicks::Now() - base::TimeTicks::UnixEpoch()).InSecondsF() - 1); |
| 262 |
| 263 timing.response_start = base::TimeDelta::FromMilliseconds(1); |
| 264 timing.dom_content_loaded_event_start = base::TimeDelta::FromMilliseconds(1); |
| 265 |
| 266 content::WebContentsTester* web_contents_tester = |
| 267 content::WebContentsTester::For(web_contents()); |
| 268 |
| 269 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 270 observer_->OnMessageReceived( |
| 271 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 272 web_contents()->GetMainFrame()); |
| 273 |
| 274 // Background the tab, then forground it. |
| 275 observer_->WasHidden(); |
| 276 observer_->WasShown(); |
| 277 timing.first_layout = base::TimeDelta::FromSeconds(3); |
| 278 observer_->OnMessageReceived( |
| 279 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 280 web_contents()->GetMainFrame()); |
| 281 |
| 282 // Navigate again to force histogram recording. |
| 283 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 284 |
| 285 histogram_tester_.ExpectTotalCount(kBGHistogramNameDomContent, 0); |
| 286 histogram_tester_.ExpectTotalCount(kBGHistogramNameLoad, 0); |
| 287 histogram_tester_.ExpectTotalCount(kBGHistogramNameFirstLayout, 1); |
| 288 histogram_tester_.ExpectBucketCount(kBGHistogramNameFirstLayout, |
| 289 timing.first_layout.InMilliseconds(), 1); |
| 290 |
| 291 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); |
| 292 histogram_tester_.ExpectBucketCount( |
| 293 kHistogramNameDomContent, |
| 294 timing.dom_content_loaded_event_start.InMilliseconds(), 1); |
| 295 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 296 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
| 297 } |
| 298 |
| 299 TEST_F(MetricsWebContentsObserverTest, DontBackgroundQuickerLoad) { |
| 300 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| 301 |
| 302 PageLoadTiming timing; |
| 303 timing.navigation_start = base::Time::FromDoubleT(1); |
| 304 timing.first_layout = first_layout; |
| 305 |
| 306 observer_->WasHidden(); |
| 307 |
| 308 // Open in new tab |
| 309 content::WebContentsTester* web_contents_tester = |
| 310 content::WebContentsTester::For(web_contents()); |
| 311 |
| 312 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl)); |
| 313 |
| 314 content::RenderFrameHostTester* rfh_tester = |
| 315 content::RenderFrameHostTester::For(main_rfh()); |
| 316 |
| 317 // Switch to the tab |
| 318 observer_->WasShown(); |
| 319 |
| 320 // Start another provisional load |
| 321 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl2)); |
| 322 rfh_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); |
| 323 observer_->OnMessageReceived( |
| 324 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 325 main_rfh()); |
| 326 rfh_tester->SimulateNavigationStop(); |
| 327 |
| 328 // Navigate again to see if the timing updated for a subframe message. |
| 329 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 330 |
| 331 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| 332 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 333 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); |
| 334 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 335 first_layout.InMilliseconds(), 1); |
| 336 } |
214 } // namespace page_load_metrics | 337 } // namespace page_load_metrics |
OLD | NEW |