Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: components/page_load_metrics/browser/metrics_web_contents_observer_unittest.cc

Issue 1473153002: PageLoadMetricsObservers observe individual page loads (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Randy/Bryan review: needed to revert ~MWCO destructor for lifecycle reasons Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 19 matching lines...) Expand all
30 class TestPageLoadMetricsEmbedderInterface 30 class TestPageLoadMetricsEmbedderInterface
31 : public PageLoadMetricsEmbedderInterface { 31 : public PageLoadMetricsEmbedderInterface {
32 public: 32 public:
33 TestPageLoadMetricsEmbedderInterface() : is_prerendering_(false) {} 33 TestPageLoadMetricsEmbedderInterface() : is_prerendering_(false) {}
34 rappor::TestRapporService* GetRapporService() override { 34 rappor::TestRapporService* GetRapporService() override {
35 return &rappor_tester_; 35 return &rappor_tester_;
36 } 36 }
37 bool IsPrerendering(content::WebContents* web_contents) override { 37 bool IsPrerendering(content::WebContents* web_contents) override {
38 return is_prerendering_; 38 return is_prerendering_;
39 } 39 }
40 void RegisterObservers(PageLoadTracker* tracker) override {}
40 void set_is_prerendering(bool is_prerendering) { 41 void set_is_prerendering(bool is_prerendering) {
41 is_prerendering_ = is_prerendering; 42 is_prerendering_ = is_prerendering;
42 } 43 }
43 44
44 private: 45 private:
45 bool is_prerendering_; 46 bool is_prerendering_;
46 rappor::TestRapporService rappor_tester_; 47 rappor::TestRapporService rappor_tester_;
47 }; 48 };
48 49
49 class MetricsWebContentsObserverTest 50 class MetricsWebContentsObserverTest
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 if (background) { 82 if (background) {
82 histogram_tester_.ExpectBucketCount(kBackgroundProvisionalEvents, event, 83 histogram_tester_.ExpectBucketCount(kBackgroundProvisionalEvents, event,
83 count); 84 count);
84 num_provisional_events_bg_ += count; 85 num_provisional_events_bg_ += count;
85 } else { 86 } else {
86 histogram_tester_.ExpectBucketCount(kProvisionalEvents, event, count); 87 histogram_tester_.ExpectBucketCount(kProvisionalEvents, event, count);
87 num_provisional_events_ += count; 88 num_provisional_events_ += count;
88 } 89 }
89 } 90 }
90 91
91 void CheckCommittedEvent(CommittedLoadEvent event, 92 void CheckCommittedEvent(CommittedRelevantLoadEvent event,
92 int count, 93 int count,
93 bool background) { 94 bool background) {
94 if (background) { 95 if (background) {
95 histogram_tester_.ExpectBucketCount(kBackgroundCommittedEvents, event, 96 histogram_tester_.ExpectBucketCount(kBackgroundCommittedEvents, event,
96 count); 97 count);
97 num_committed_events_bg_ += count; 98 num_committed_events_bg_ += count;
98 } else { 99 } else {
99 histogram_tester_.ExpectBucketCount(kCommittedEvents, event, count); 100 histogram_tester_.ExpectBucketCount(kCommittedEvents, event, count);
100 num_committed_events_ += count; 101 num_committed_events_ += count;
101 } 102 }
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 GURL about_blank_url = GURL("about:blank"); 500 GURL about_blank_url = GURL("about:blank");
500 web_contents_tester->NavigateAndCommit(about_blank_url); 501 web_contents_tester->NavigateAndCommit(about_blank_url);
501 502
502 observer_->OnMessageReceived( 503 observer_->OnMessageReceived(
503 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), 504 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing),
504 main_rfh()); 505 main_rfh());
505 506
506 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); 507 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl));
507 508
508 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false); 509 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false);
509 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 1, false); 510 CheckCommittedEvent(RELEVANT_LOAD_STARTED, 1, false);
510 CheckErrorEvent(ERR_IPC_FROM_BAD_URL_SCHEME, 1); 511 CheckErrorEvent(ERR_IPC_FROM_BAD_URL_SCHEME, 1);
511 CheckErrorEvent(ERR_IPC_WITH_NO_COMMITTED_LOAD, 1); 512 CheckErrorEvent(ERR_IPC_WITH_NO_RELEVANT_LOAD, 1);
512 CheckTotalEvents(); 513 CheckTotalEvents();
513 } 514 }
514 515
515 TEST_F(MetricsWebContentsObserverTest, NotInMainError) { 516 TEST_F(MetricsWebContentsObserverTest, NotInMainError) {
516 PageLoadTiming timing; 517 PageLoadTiming timing;
517 timing.navigation_start = base::Time::FromDoubleT(1); 518 timing.navigation_start = base::Time::FromDoubleT(1);
518 timing.first_layout = base::TimeDelta::FromMilliseconds(1); 519 timing.first_layout = base::TimeDelta::FromMilliseconds(1);
519 520
520 content::WebContentsTester* web_contents_tester = 521 content::WebContentsTester* web_contents_tester =
521 content::WebContentsTester::For(web_contents()); 522 content::WebContentsTester::For(web_contents());
(...skipping 21 matching lines...) Expand all
543 content::WebContentsTester::For(web_contents()); 544 content::WebContentsTester::For(web_contents());
544 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); 545 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl));
545 546
546 observer_->OnMessageReceived( 547 observer_->OnMessageReceived(
547 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), 548 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing),
548 main_rfh()); 549 main_rfh());
549 // Navigate again to force histogram logging. 550 // Navigate again to force histogram logging.
550 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); 551 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2));
551 552
552 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false); 553 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false);
553 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 2, false); 554 CheckCommittedEvent(RELEVANT_LOAD_STARTED, 2, false);
554 CheckCommittedEvent(COMMITTED_LOAD_FAILED_BEFORE_FIRST_LAYOUT, 1, false); 555 CheckCommittedEvent(RELEVANT_LOAD_FAILED_BEFORE_FIRST_LAYOUT, 1, false);
555 CheckTotalEvents(); 556 CheckTotalEvents();
556 } 557 }
557 558
558 TEST_F(MetricsWebContentsObserverTest, SuccessfulFirstLayoutInForegroundEvent) { 559 TEST_F(MetricsWebContentsObserverTest, SuccessfulFirstLayoutInForegroundEvent) {
559 PageLoadTiming timing; 560 PageLoadTiming timing;
560 timing.navigation_start = base::Time::FromDoubleT(10); 561 timing.navigation_start = base::Time::FromDoubleT(10);
561 timing.first_layout = base::TimeDelta::FromMilliseconds(100); 562 timing.first_layout = base::TimeDelta::FromMilliseconds(100);
562 563
563 content::WebContentsTester* web_contents_tester = 564 content::WebContentsTester* web_contents_tester =
564 content::WebContentsTester::For(web_contents()); 565 content::WebContentsTester::For(web_contents());
565 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); 566 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl));
566 567
567 observer_->OnMessageReceived( 568 observer_->OnMessageReceived(
568 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), 569 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing),
569 main_rfh()); 570 main_rfh());
570 // Navigate again to force histogram logging. 571 // Navigate again to force histogram logging.
571 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); 572 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2));
572 573
573 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false); 574 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 2, false);
574 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 2, false); 575 CheckCommittedEvent(RELEVANT_LOAD_STARTED, 2, false);
575 CheckCommittedEvent(COMMITTED_LOAD_SUCCESSFUL_FIRST_LAYOUT, 1, false); 576 CheckCommittedEvent(RELEVANT_LOAD_SUCCESSFUL_FIRST_LAYOUT, 1, false);
576 CheckTotalEvents(); 577 CheckTotalEvents();
577 } 578 }
578 579
579 TEST_F(MetricsWebContentsObserverTest, 580 TEST_F(MetricsWebContentsObserverTest,
580 SuccessfulFirstLayoutInBackgroundEvent) { 581 SuccessfulFirstLayoutInBackgroundEvent) {
581 PageLoadTiming timing; 582 PageLoadTiming timing;
582 timing.navigation_start = base::Time::FromDoubleT(1); 583 timing.navigation_start = base::Time::FromDoubleT(1);
583 timing.first_layout = base::TimeDelta::FromSeconds(30); 584 timing.first_layout = base::TimeDelta::FromSeconds(30);
584 585
585 content::WebContentsTester* web_contents_tester = 586 content::WebContentsTester* web_contents_tester =
586 content::WebContentsTester::For(web_contents()); 587 content::WebContentsTester::For(web_contents());
587 // Background the tab. 588 // Background the tab.
588 observer_->WasHidden(); 589 observer_->WasHidden();
589 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); 590 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl));
590 591
591 observer_->OnMessageReceived( 592 observer_->OnMessageReceived(
592 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), 593 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing),
593 main_rfh()); 594 main_rfh());
594 595
595 observer_->WasShown(); 596 observer_->WasShown();
596 // Navigate again to force histogram logging. 597 // Navigate again to force histogram logging.
597 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); 598 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2));
598 599
599 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 1, true); 600 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 1, true);
600 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 1, false); 601 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 1, false);
601 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 1, true); 602 CheckCommittedEvent(RELEVANT_LOAD_STARTED, 1, true);
602 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 1, false); 603 CheckCommittedEvent(RELEVANT_LOAD_STARTED, 1, false);
603 CheckCommittedEvent(COMMITTED_LOAD_SUCCESSFUL_FIRST_LAYOUT, 1, true); 604 CheckCommittedEvent(RELEVANT_LOAD_SUCCESSFUL_FIRST_LAYOUT, 1, true);
604 CheckTotalEvents(); 605 CheckTotalEvents();
605 } 606 }
606 607
607 TEST_F(MetricsWebContentsObserverTest, BadIPC) { 608 TEST_F(MetricsWebContentsObserverTest, BadIPC) {
608 PageLoadTiming timing; 609 PageLoadTiming timing;
609 timing.navigation_start = base::Time::FromDoubleT(10); 610 timing.navigation_start = base::Time::FromDoubleT(10);
610 PageLoadTiming timing2; 611 PageLoadTiming timing2;
611 timing2.navigation_start = base::Time::FromDoubleT(100); 612 timing2.navigation_start = base::Time::FromDoubleT(100);
612 613
613 content::WebContentsTester* web_contents_tester = 614 content::WebContentsTester* web_contents_tester =
614 content::WebContentsTester::For(web_contents()); 615 content::WebContentsTester::For(web_contents());
615 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); 616 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl));
616 617
617 observer_->OnMessageReceived( 618 observer_->OnMessageReceived(
618 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), 619 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing),
619 main_rfh()); 620 main_rfh());
620 observer_->OnMessageReceived( 621 observer_->OnMessageReceived(
621 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing2), 622 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing2),
622 main_rfh()); 623 main_rfh());
623 624
624 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 1, false); 625 CheckProvisionalEvent(PROVISIONAL_LOAD_COMMITTED, 1, false);
625 CheckCommittedEvent(COMMITTED_LOAD_STARTED, 1, false); 626 CheckCommittedEvent(RELEVANT_LOAD_STARTED, 1, false);
626 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1); 627 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1);
627 CheckTotalEvents(); 628 CheckTotalEvents();
628 } 629 }
629 630
630 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { 631 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) {
631 // Delete the observer for this test, add it once the navigation has started. 632 // Delete the observer for this test, add it once the navigation has started.
632 observer_.reset(); 633 observer_.reset();
633 PageLoadTiming timing; 634 PageLoadTiming timing;
634 timing.navigation_start = base::Time::FromDoubleT(10); 635 timing.navigation_start = base::Time::FromDoubleT(10);
635 timing.first_layout = base::TimeDelta::FromSeconds(2); 636 timing.first_layout = base::TimeDelta::FromSeconds(2);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 EXPECT_NE(string_it, sample_obj->string_fields.end()); 711 EXPECT_NE(string_it, sample_obj->string_fields.end());
711 EXPECT_EQ(rappor::GetDomainAndRegistrySampleFromGURL(GURL(kDefaultTestUrl)), 712 EXPECT_EQ(rappor::GetDomainAndRegistrySampleFromGURL(GURL(kDefaultTestUrl)),
712 string_it->second); 713 string_it->second);
713 714
714 const auto& flag_it = sample_obj->flag_fields.find("IsSlow"); 715 const auto& flag_it = sample_obj->flag_fields.find("IsSlow");
715 EXPECT_NE(flag_it, sample_obj->flag_fields.end()); 716 EXPECT_NE(flag_it, sample_obj->flag_fields.end());
716 EXPECT_EQ(0u, flag_it->second); 717 EXPECT_EQ(0u, flag_it->second);
717 } 718 }
718 719
719 } // namespace page_load_metrics 720 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698