| 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" |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 dom_content.InMilliseconds(), 1); | 275 dom_content.InMilliseconds(), 1); |
| 276 | 276 |
| 277 histogram_tester_.ExpectTotalCount(kHistogramLoad, 1); | 277 histogram_tester_.ExpectTotalCount(kHistogramLoad, 1); |
| 278 histogram_tester_.ExpectBucketCount(kHistogramLoad, load.InMilliseconds(), 1); | 278 histogram_tester_.ExpectBucketCount(kHistogramLoad, load.InMilliseconds(), 1); |
| 279 } | 279 } |
| 280 | 280 |
| 281 TEST_F(MetricsWebContentsObserverTest, BackgroundDifferentHistogram) { | 281 TEST_F(MetricsWebContentsObserverTest, BackgroundDifferentHistogram) { |
| 282 base::TimeDelta first_layout = base::TimeDelta::FromSeconds(2); | 282 base::TimeDelta first_layout = base::TimeDelta::FromSeconds(2); |
| 283 | 283 |
| 284 PageLoadTiming timing; | 284 PageLoadTiming timing; |
| 285 timing.navigation_start = base::Time::FromDoubleT( | 285 timing.navigation_start = base::Time::FromDoubleT(1); |
| 286 (base::TimeTicks::Now() - base::TimeTicks::UnixEpoch()).InSecondsF()); | |
| 287 timing.first_layout = first_layout; | 286 timing.first_layout = first_layout; |
| 288 | 287 |
| 289 content::WebContentsTester* web_contents_tester = | 288 content::WebContentsTester* web_contents_tester = |
| 290 content::WebContentsTester::For(web_contents()); | 289 content::WebContentsTester::For(web_contents()); |
| 291 | 290 |
| 292 // Simulate "Open link in new tab." | 291 // Simulate "Open link in new tab." |
| 293 observer_->WasHidden(); | 292 observer_->WasHidden(); |
| 294 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 293 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 295 | 294 |
| 296 observer_->OnMessageReceived( | 295 observer_->OnMessageReceived( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 312 histogram_tester_.ExpectTotalCount(kBackgroundHistogramFirstTextPaint, 0); | 311 histogram_tester_.ExpectTotalCount(kBackgroundHistogramFirstTextPaint, 0); |
| 313 | 312 |
| 314 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 0); | 313 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 0); |
| 315 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); | 314 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); |
| 316 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 0); | 315 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 0); |
| 317 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); | 316 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); |
| 318 } | 317 } |
| 319 | 318 |
| 320 TEST_F(MetricsWebContentsObserverTest, OnlyBackgroundLaterEvents) { | 319 TEST_F(MetricsWebContentsObserverTest, OnlyBackgroundLaterEvents) { |
| 321 PageLoadTiming timing; | 320 PageLoadTiming timing; |
| 322 timing.navigation_start = base::Time::FromDoubleT( | 321 timing.navigation_start = base::Time::FromDoubleT(1); |
| 323 (base::TimeTicks::Now() - base::TimeTicks::UnixEpoch()).InSecondsF() - 1); | 322 // Set these events at 1 microsecond so they are definitely occur before we |
| 324 | 323 // background the tab later in the test. |
| 325 timing.response_start = base::TimeDelta::FromMilliseconds(1); | 324 timing.response_start = base::TimeDelta::FromMicroseconds(1); |
| 326 timing.dom_content_loaded_event_start = base::TimeDelta::FromMilliseconds(1); | 325 timing.dom_content_loaded_event_start = base::TimeDelta::FromMicroseconds(1); |
| 327 | 326 |
| 328 content::WebContentsTester* web_contents_tester = | 327 content::WebContentsTester* web_contents_tester = |
| 329 content::WebContentsTester::For(web_contents()); | 328 content::WebContentsTester::For(web_contents()); |
| 330 | 329 |
| 331 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 330 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 332 observer_->OnMessageReceived( | 331 observer_->OnMessageReceived( |
| 333 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 332 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 334 web_contents()->GetMainFrame()); | 333 web_contents()->GetMainFrame()); |
| 335 | 334 |
| 336 // Background the tab, then forground it. | 335 // Background the tab, then forground it. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 358 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 1); | 357 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 1); |
| 359 histogram_tester_.ExpectBucketCount( | 358 histogram_tester_.ExpectBucketCount( |
| 360 kHistogramDomContentLoaded, | 359 kHistogramDomContentLoaded, |
| 361 timing.dom_content_loaded_event_start.InMilliseconds(), 1); | 360 timing.dom_content_loaded_event_start.InMilliseconds(), 1); |
| 362 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); | 361 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); |
| 363 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 0); | 362 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 0); |
| 364 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); | 363 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); |
| 365 } | 364 } |
| 366 | 365 |
| 367 TEST_F(MetricsWebContentsObserverTest, DontBackgroundQuickerLoad) { | 366 TEST_F(MetricsWebContentsObserverTest, DontBackgroundQuickerLoad) { |
| 368 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | 367 // Set this event at 1 microsecond so it occurs before we foreground later in |
| 368 // the test. |
| 369 base::TimeDelta first_layout = base::TimeDelta::FromMicroseconds(1); |
| 369 | 370 |
| 370 PageLoadTiming timing; | 371 PageLoadTiming timing; |
| 371 timing.navigation_start = base::Time::FromDoubleT(1); | 372 timing.navigation_start = base::Time::FromDoubleT(1); |
| 372 timing.first_layout = first_layout; | 373 timing.first_layout = first_layout; |
| 373 | 374 |
| 374 observer_->WasHidden(); | 375 observer_->WasHidden(); |
| 375 | 376 |
| 376 // Open in new tab | 377 // Open in new tab |
| 377 content::WebContentsTester* web_contents_tester = | 378 content::WebContentsTester* web_contents_tester = |
| 378 content::WebContentsTester::For(web_contents()); | 379 content::WebContentsTester::For(web_contents()); |
| 379 | 380 |
| 380 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl)); | 381 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl)); |
| 381 | 382 |
| 382 content::RenderFrameHostTester* rfh_tester = | 383 content::RenderFrameHostTester* rfh_tester = |
| 383 content::RenderFrameHostTester::For(main_rfh()); | 384 content::RenderFrameHostTester::For(main_rfh()); |
| 384 | 385 |
| 385 // Switch to the tab | 386 // Switch to the tab |
| 386 observer_->WasShown(); | 387 observer_->WasShown(); |
| 387 | 388 |
| 388 // Start another provisional load | 389 // Start another provisional load |
| 389 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl2)); | 390 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl2)); |
| 390 rfh_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); | 391 rfh_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); |
| 391 observer_->OnMessageReceived( | 392 observer_->OnMessageReceived( |
| 392 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 393 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 393 main_rfh()); | 394 main_rfh()); |
| 394 rfh_tester->SimulateNavigationStop(); | 395 rfh_tester->SimulateNavigationStop(); |
| 395 | 396 |
| 396 // Navigate again to see if the timing updated for a subframe message. | 397 // Navigate again to see if the timing updated for the foregrounded load. |
| 397 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 398 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 398 | 399 |
| 399 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 0); | 400 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 0); |
| 400 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); | 401 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); |
| 401 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 1); | 402 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 1); |
| 402 histogram_tester_.ExpectBucketCount(kHistogramFirstLayout, | 403 histogram_tester_.ExpectBucketCount(kHistogramFirstLayout, |
| 403 first_layout.InMilliseconds(), 1); | 404 first_layout.InMilliseconds(), 1); |
| 404 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); | 405 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); |
| 405 } | 406 } |
| 406 | 407 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 | 535 |
| 535 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false); | 536 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false); |
| 536 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 2, false); | 537 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 2, false); |
| 537 CheckCommittedEvent(COMMITTED_LOAD_SUCCESSFUL_FIRST_LAYOUT, 1, false); | 538 CheckCommittedEvent(COMMITTED_LOAD_SUCCESSFUL_FIRST_LAYOUT, 1, false); |
| 538 CheckTotalEvents(); | 539 CheckTotalEvents(); |
| 539 } | 540 } |
| 540 | 541 |
| 541 TEST_F(MetricsWebContentsObserverTest, | 542 TEST_F(MetricsWebContentsObserverTest, |
| 542 SuccessfulFirstLayoutInBackgroundEvent) { | 543 SuccessfulFirstLayoutInBackgroundEvent) { |
| 543 PageLoadTiming timing; | 544 PageLoadTiming timing; |
| 544 timing.navigation_start = base::Time::FromDoubleT( | 545 timing.navigation_start = base::Time::FromDoubleT(1); |
| 545 (base::TimeTicks::Now() - base::TimeTicks::UnixEpoch()).InSecondsF() - 1); | |
| 546 | |
| 547 timing.first_layout = base::TimeDelta::FromSeconds(30); | 546 timing.first_layout = base::TimeDelta::FromSeconds(30); |
| 548 | 547 |
| 549 content::WebContentsTester* web_contents_tester = | 548 content::WebContentsTester* web_contents_tester = |
| 550 content::WebContentsTester::For(web_contents()); | 549 content::WebContentsTester::For(web_contents()); |
| 551 // Background the tab. | 550 // Background the tab. |
| 552 observer_->WasHidden(); | 551 observer_->WasHidden(); |
| 553 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 552 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 554 | 553 |
| 555 observer_->OnMessageReceived( | 554 observer_->OnMessageReceived( |
| 556 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 555 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 EXPECT_NE(string_it, sample_obj->string_fields.end()); | 670 EXPECT_NE(string_it, sample_obj->string_fields.end()); |
| 672 EXPECT_EQ(rappor::GetDomainAndRegistrySampleFromGURL(GURL(kDefaultTestUrl)), | 671 EXPECT_EQ(rappor::GetDomainAndRegistrySampleFromGURL(GURL(kDefaultTestUrl)), |
| 673 string_it->second); | 672 string_it->second); |
| 674 | 673 |
| 675 const auto& flag_it = sample_obj->flag_fields.find("IsSlow"); | 674 const auto& flag_it = sample_obj->flag_fields.find("IsSlow"); |
| 676 EXPECT_NE(flag_it, sample_obj->flag_fields.end()); | 675 EXPECT_NE(flag_it, sample_obj->flag_fields.end()); |
| 677 EXPECT_EQ(0u, flag_it->second); | 676 EXPECT_EQ(0u, flag_it->second); |
| 678 } | 677 } |
| 679 | 678 |
| 680 } // namespace page_load_metrics | 679 } // namespace page_load_metrics |
| OLD | NEW |