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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 dom_content.InMilliseconds(), 1); | 295 dom_content.InMilliseconds(), 1); |
296 | 296 |
297 histogram_tester_.ExpectTotalCount(kHistogramLoad, 1); | 297 histogram_tester_.ExpectTotalCount(kHistogramLoad, 1); |
298 histogram_tester_.ExpectBucketCount(kHistogramLoad, load.InMilliseconds(), 1); | 298 histogram_tester_.ExpectBucketCount(kHistogramLoad, load.InMilliseconds(), 1); |
299 } | 299 } |
300 | 300 |
301 TEST_F(MetricsWebContentsObserverTest, BackgroundDifferentHistogram) { | 301 TEST_F(MetricsWebContentsObserverTest, BackgroundDifferentHistogram) { |
302 base::TimeDelta first_layout = base::TimeDelta::FromSeconds(2); | 302 base::TimeDelta first_layout = base::TimeDelta::FromSeconds(2); |
303 | 303 |
304 PageLoadTiming timing; | 304 PageLoadTiming timing; |
305 timing.navigation_start = base::Time::FromDoubleT( | 305 timing.navigation_start = base::Time::FromDoubleT(1); |
306 (base::TimeTicks::Now() - base::TimeTicks::UnixEpoch()).InSecondsF()); | |
307 timing.first_layout = first_layout; | 306 timing.first_layout = first_layout; |
308 | 307 |
309 content::WebContentsTester* web_contents_tester = | 308 content::WebContentsTester* web_contents_tester = |
310 content::WebContentsTester::For(web_contents()); | 309 content::WebContentsTester::For(web_contents()); |
311 | 310 |
312 // Simulate "Open link in new tab." | 311 // Simulate "Open link in new tab." |
313 observer_->WasHidden(); | 312 observer_->WasHidden(); |
314 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 313 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
315 | 314 |
316 observer_->OnMessageReceived( | 315 observer_->OnMessageReceived( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 observer_->OnMessageReceived( | 348 observer_->OnMessageReceived( |
350 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 349 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
351 web_contents()->GetMainFrame()); | 350 web_contents()->GetMainFrame()); |
352 | 351 |
353 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 352 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
354 AssertNoHistogramsLogged(); | 353 AssertNoHistogramsLogged(); |
355 } | 354 } |
356 | 355 |
357 TEST_F(MetricsWebContentsObserverTest, OnlyBackgroundLaterEvents) { | 356 TEST_F(MetricsWebContentsObserverTest, OnlyBackgroundLaterEvents) { |
358 PageLoadTiming timing; | 357 PageLoadTiming timing; |
359 timing.navigation_start = base::Time::FromDoubleT( | 358 timing.navigation_start = base::Time::FromDoubleT(1); |
360 (base::TimeTicks::Now() - base::TimeTicks::UnixEpoch()).InSecondsF() - 1); | 359 // Set these events at 1 microsecond so they are definitely occur before we |
361 | 360 // background the tab later in the test. |
362 timing.response_start = base::TimeDelta::FromMilliseconds(1); | 361 timing.response_start = base::TimeDelta::FromMicroseconds(1); |
363 timing.dom_content_loaded_event_start = base::TimeDelta::FromMilliseconds(1); | 362 timing.dom_content_loaded_event_start = base::TimeDelta::FromMicroseconds(1); |
364 | 363 |
365 content::WebContentsTester* web_contents_tester = | 364 content::WebContentsTester* web_contents_tester = |
366 content::WebContentsTester::For(web_contents()); | 365 content::WebContentsTester::For(web_contents()); |
367 | 366 |
368 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 367 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
369 observer_->OnMessageReceived( | 368 observer_->OnMessageReceived( |
370 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 369 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
371 web_contents()->GetMainFrame()); | 370 web_contents()->GetMainFrame()); |
372 | 371 |
373 // Background the tab, then forground it. | 372 // Background the tab, then forground it. |
(...skipping 21 matching lines...) Expand all Loading... |
395 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 1); | 394 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 1); |
396 histogram_tester_.ExpectBucketCount( | 395 histogram_tester_.ExpectBucketCount( |
397 kHistogramDomContentLoaded, | 396 kHistogramDomContentLoaded, |
398 timing.dom_content_loaded_event_start.InMilliseconds(), 1); | 397 timing.dom_content_loaded_event_start.InMilliseconds(), 1); |
399 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); | 398 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); |
400 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 0); | 399 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 0); |
401 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); | 400 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); |
402 } | 401 } |
403 | 402 |
404 TEST_F(MetricsWebContentsObserverTest, DontBackgroundQuickerLoad) { | 403 TEST_F(MetricsWebContentsObserverTest, DontBackgroundQuickerLoad) { |
405 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | 404 // Set this event at 1 microsecond so it occurs before we foreground later in |
| 405 // the test. |
| 406 base::TimeDelta first_layout = base::TimeDelta::FromMicroseconds(1); |
406 | 407 |
407 PageLoadTiming timing; | 408 PageLoadTiming timing; |
408 timing.navigation_start = base::Time::FromDoubleT(1); | 409 timing.navigation_start = base::Time::FromDoubleT(1); |
409 timing.first_layout = first_layout; | 410 timing.first_layout = first_layout; |
410 | 411 |
411 observer_->WasHidden(); | 412 observer_->WasHidden(); |
412 | 413 |
413 // Open in new tab | 414 // Open in new tab |
414 content::WebContentsTester* web_contents_tester = | 415 content::WebContentsTester* web_contents_tester = |
415 content::WebContentsTester::For(web_contents()); | 416 content::WebContentsTester::For(web_contents()); |
416 | 417 |
417 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl)); | 418 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl)); |
418 | 419 |
419 content::RenderFrameHostTester* rfh_tester = | 420 content::RenderFrameHostTester* rfh_tester = |
420 content::RenderFrameHostTester::For(main_rfh()); | 421 content::RenderFrameHostTester::For(main_rfh()); |
421 | 422 |
422 // Switch to the tab | 423 // Switch to the tab |
423 observer_->WasShown(); | 424 observer_->WasShown(); |
424 | 425 |
425 // Start another provisional load | 426 // Start another provisional load |
426 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl2)); | 427 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl2)); |
427 rfh_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); | 428 rfh_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); |
428 observer_->OnMessageReceived( | 429 observer_->OnMessageReceived( |
429 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 430 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
430 main_rfh()); | 431 main_rfh()); |
431 rfh_tester->SimulateNavigationStop(); | 432 rfh_tester->SimulateNavigationStop(); |
432 | 433 |
433 // Navigate again to see if the timing updated for a subframe message. | 434 // Navigate again to see if the timing updated for the foregrounded load. |
434 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 435 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
435 | 436 |
436 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 0); | 437 histogram_tester_.ExpectTotalCount(kHistogramDomContentLoaded, 0); |
437 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); | 438 histogram_tester_.ExpectTotalCount(kHistogramLoad, 0); |
438 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 1); | 439 histogram_tester_.ExpectTotalCount(kHistogramFirstLayout, 1); |
439 histogram_tester_.ExpectBucketCount(kHistogramFirstLayout, | 440 histogram_tester_.ExpectBucketCount(kHistogramFirstLayout, |
440 first_layout.InMilliseconds(), 1); | 441 first_layout.InMilliseconds(), 1); |
441 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); | 442 histogram_tester_.ExpectTotalCount(kHistogramFirstTextPaint, 0); |
442 } | 443 } |
443 | 444 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 | 572 |
572 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false); | 573 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false); |
573 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 2, false); | 574 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 2, false); |
574 CheckCommittedEvent(COMMITTED_LOAD_SUCCESSFUL_FIRST_LAYOUT, 1, false); | 575 CheckCommittedEvent(COMMITTED_LOAD_SUCCESSFUL_FIRST_LAYOUT, 1, false); |
575 CheckTotalEvents(); | 576 CheckTotalEvents(); |
576 } | 577 } |
577 | 578 |
578 TEST_F(MetricsWebContentsObserverTest, | 579 TEST_F(MetricsWebContentsObserverTest, |
579 SuccessfulFirstLayoutInBackgroundEvent) { | 580 SuccessfulFirstLayoutInBackgroundEvent) { |
580 PageLoadTiming timing; | 581 PageLoadTiming timing; |
581 timing.navigation_start = base::Time::FromDoubleT( | 582 timing.navigation_start = base::Time::FromDoubleT(1); |
582 (base::TimeTicks::Now() - base::TimeTicks::UnixEpoch()).InSecondsF() - 1); | |
583 | |
584 timing.first_layout = base::TimeDelta::FromSeconds(30); | 583 timing.first_layout = base::TimeDelta::FromSeconds(30); |
585 | 584 |
586 content::WebContentsTester* web_contents_tester = | 585 content::WebContentsTester* web_contents_tester = |
587 content::WebContentsTester::For(web_contents()); | 586 content::WebContentsTester::For(web_contents()); |
588 // Background the tab. | 587 // Background the tab. |
589 observer_->WasHidden(); | 588 observer_->WasHidden(); |
590 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 589 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
591 | 590 |
592 observer_->OnMessageReceived( | 591 observer_->OnMessageReceived( |
593 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 592 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 EXPECT_NE(string_it, sample_obj->string_fields.end()); | 710 EXPECT_NE(string_it, sample_obj->string_fields.end()); |
712 EXPECT_EQ(rappor::GetDomainAndRegistrySampleFromGURL(GURL(kDefaultTestUrl)), | 711 EXPECT_EQ(rappor::GetDomainAndRegistrySampleFromGURL(GURL(kDefaultTestUrl)), |
713 string_it->second); | 712 string_it->second); |
714 | 713 |
715 const auto& flag_it = sample_obj->flag_fields.find("IsSlow"); | 714 const auto& flag_it = sample_obj->flag_fields.find("IsSlow"); |
716 EXPECT_NE(flag_it, sample_obj->flag_fields.end()); | 715 EXPECT_NE(flag_it, sample_obj->flag_fields.end()); |
717 EXPECT_EQ(0u, flag_it->second); | 716 EXPECT_EQ(0u, flag_it->second); |
718 } | 717 } |
719 | 718 |
720 } // namespace page_load_metrics | 719 } // namespace page_load_metrics |
OLD | NEW |