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 "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" | 5 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
25 | 25 |
26 namespace page_load_metrics { | 26 namespace page_load_metrics { |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 const char kDefaultTestUrl[] = "https://google.com/"; | 30 const char kDefaultTestUrl[] = "https://google.com/"; |
31 const char kDefaultTestUrlAnchor[] = "https://google.com/#samepage"; | 31 const char kDefaultTestUrlAnchor[] = "https://google.com/#samepage"; |
32 const char kDefaultTestUrl2[] = "https://whatever.com/"; | 32 const char kDefaultTestUrl2[] = "https://whatever.com/"; |
| 33 const char kFilteredCommitUrl[] = "https://whatever.com/ignore-on-commit"; |
33 | 34 |
34 // Simple PageLoadMetricsObserver that copies observed PageLoadTimings into the | 35 // Simple PageLoadMetricsObserver that copies observed PageLoadTimings into the |
35 // provided std::vector, so they can be analyzed by unit tests. | 36 // provided std::vector, so they can be analyzed by unit tests. |
36 class TestPageLoadMetricsObserver : public PageLoadMetricsObserver { | 37 class TestPageLoadMetricsObserver : public PageLoadMetricsObserver { |
37 public: | 38 public: |
38 explicit TestPageLoadMetricsObserver( | 39 TestPageLoadMetricsObserver(std::vector<PageLoadTiming>* updated_timings, |
39 std::vector<PageLoadTiming>* updated_timings, | 40 std::vector<PageLoadTiming>* complete_timings, |
40 std::vector<PageLoadTiming>* complete_timings, | 41 std::vector<GURL>* observed_committed_urls) |
41 std::vector<GURL>* observed_committed_urls) | |
42 : updated_timings_(updated_timings), | 42 : updated_timings_(updated_timings), |
43 complete_timings_(complete_timings), | 43 complete_timings_(complete_timings), |
44 observed_committed_urls_(observed_committed_urls) {} | 44 observed_committed_urls_(observed_committed_urls) {} |
45 | 45 |
46 void OnStart(content::NavigationHandle* navigation_handle, | 46 void OnStart(content::NavigationHandle* navigation_handle, |
47 const GURL& currently_committed_url, | 47 const GURL& currently_committed_url, |
48 bool started_in_foreground) override { | 48 bool started_in_foreground) override { |
49 observed_committed_urls_->push_back(currently_committed_url); | 49 observed_committed_urls_->push_back(currently_committed_url); |
50 } | 50 } |
51 | 51 |
(...skipping 12 matching lines...) Expand all Loading... |
64 const PageLoadExtraInfo& extra_info) override { | 64 const PageLoadExtraInfo& extra_info) override { |
65 return STOP_OBSERVING; | 65 return STOP_OBSERVING; |
66 } | 66 } |
67 | 67 |
68 private: | 68 private: |
69 std::vector<PageLoadTiming>* const updated_timings_; | 69 std::vector<PageLoadTiming>* const updated_timings_; |
70 std::vector<PageLoadTiming>* const complete_timings_; | 70 std::vector<PageLoadTiming>* const complete_timings_; |
71 std::vector<GURL>* const observed_committed_urls_; | 71 std::vector<GURL>* const observed_committed_urls_; |
72 }; | 72 }; |
73 | 73 |
| 74 // Test PageLoadMetricsObserver that stops observing page loads with certain |
| 75 // substrings in the URL. |
| 76 class FilteringPageLoadMetricsObserver : public PageLoadMetricsObserver { |
| 77 public: |
| 78 explicit FilteringPageLoadMetricsObserver( |
| 79 std::vector<GURL>* completed_filtered_urls) |
| 80 : completed_filtered_urls_(completed_filtered_urls) {} |
| 81 |
| 82 ObservePolicy OnCommit(content::NavigationHandle* handle) override { |
| 83 const bool should_ignore = |
| 84 handle->GetURL().spec().find("ignore-on-commit") != std::string::npos; |
| 85 return should_ignore ? STOP_OBSERVING : CONTINUE_OBSERVING; |
| 86 } |
| 87 |
| 88 void OnComplete(const PageLoadTiming& timing, |
| 89 const PageLoadExtraInfo& extra_info) override { |
| 90 completed_filtered_urls_->push_back(extra_info.committed_url); |
| 91 } |
| 92 |
| 93 private: |
| 94 std::vector<GURL>* const completed_filtered_urls_; |
| 95 }; |
| 96 |
74 class TestPageLoadMetricsEmbedderInterface | 97 class TestPageLoadMetricsEmbedderInterface |
75 : public PageLoadMetricsEmbedderInterface { | 98 : public PageLoadMetricsEmbedderInterface { |
76 public: | 99 public: |
77 TestPageLoadMetricsEmbedderInterface() | 100 TestPageLoadMetricsEmbedderInterface() |
78 : is_prerendering_(false), is_ntp_(false) {} | 101 : is_prerendering_(false), is_ntp_(false) {} |
79 | 102 |
80 bool IsPrerendering(content::WebContents* web_contents) override { | 103 bool IsPrerendering(content::WebContents* web_contents) override { |
81 return is_prerendering_; | 104 return is_prerendering_; |
82 } | 105 } |
83 bool IsNewTabPageUrl(const GURL& url) override { return is_ntp_; } | 106 bool IsNewTabPageUrl(const GURL& url) override { return is_ntp_; } |
84 void set_is_prerendering(bool is_prerendering) { | 107 void set_is_prerendering(bool is_prerendering) { |
85 is_prerendering_ = is_prerendering; | 108 is_prerendering_ = is_prerendering; |
86 } | 109 } |
87 void set_is_ntp(bool is_ntp) { is_ntp_ = is_ntp; } | 110 void set_is_ntp(bool is_ntp) { is_ntp_ = is_ntp; } |
88 void RegisterObservers(PageLoadTracker* tracker) override { | 111 void RegisterObservers(PageLoadTracker* tracker) override { |
89 tracker->AddObserver(base::MakeUnique<TestPageLoadMetricsObserver>( | 112 tracker->AddObserver(base::MakeUnique<TestPageLoadMetricsObserver>( |
90 &updated_timings_, &complete_timings_, &observed_committed_urls_)); | 113 &updated_timings_, &complete_timings_, &observed_committed_urls_)); |
| 114 tracker->AddObserver(base::MakeUnique<FilteringPageLoadMetricsObserver>( |
| 115 &completed_filtered_urls_)); |
91 } | 116 } |
92 const std::vector<PageLoadTiming>& updated_timings() const { | 117 const std::vector<PageLoadTiming>& updated_timings() const { |
93 return updated_timings_; | 118 return updated_timings_; |
94 } | 119 } |
95 const std::vector<PageLoadTiming>& complete_timings() const { | 120 const std::vector<PageLoadTiming>& complete_timings() const { |
96 return complete_timings_; | 121 return complete_timings_; |
97 } | 122 } |
98 | 123 |
99 // currently_committed_urls passed to OnStart(). | 124 // currently_committed_urls passed to OnStart(). |
100 const std::vector<GURL>& observed_committed_urls_from_on_start() const { | 125 const std::vector<GURL>& observed_committed_urls_from_on_start() const { |
101 return observed_committed_urls_; | 126 return observed_committed_urls_; |
102 } | 127 } |
103 | 128 |
| 129 // committed URLs passed to FilteringPageLoadMetricsObserver::OnComplete(). |
| 130 const std::vector<GURL>& completed_filtered_urls() const { |
| 131 return completed_filtered_urls_; |
| 132 } |
| 133 |
104 private: | 134 private: |
105 std::vector<PageLoadTiming> updated_timings_; | 135 std::vector<PageLoadTiming> updated_timings_; |
106 std::vector<PageLoadTiming> complete_timings_; | 136 std::vector<PageLoadTiming> complete_timings_; |
107 std::vector<GURL> observed_committed_urls_; | 137 std::vector<GURL> observed_committed_urls_; |
| 138 std::vector<GURL> completed_filtered_urls_; |
108 bool is_prerendering_; | 139 bool is_prerendering_; |
109 bool is_ntp_; | 140 bool is_ntp_; |
110 }; | 141 }; |
111 | 142 |
112 } // namespace | 143 } // namespace |
113 | 144 |
114 class MetricsWebContentsObserverTest : public ChromeRenderViewHostTestHarness { | 145 class MetricsWebContentsObserverTest : public ChromeRenderViewHostTestHarness { |
115 public: | 146 public: |
116 MetricsWebContentsObserverTest() : num_errors_(0) {} | 147 MetricsWebContentsObserverTest() : num_errors_(0) {} |
117 | 148 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 return embedder_interface_->complete_timings().size(); | 200 return embedder_interface_->complete_timings().size(); |
170 } | 201 } |
171 int CountUpdatedTimingReported() { | 202 int CountUpdatedTimingReported() { |
172 return embedder_interface_->updated_timings().size(); | 203 return embedder_interface_->updated_timings().size(); |
173 } | 204 } |
174 | 205 |
175 const std::vector<GURL>& observed_committed_urls_from_on_start() const { | 206 const std::vector<GURL>& observed_committed_urls_from_on_start() const { |
176 return embedder_interface_->observed_committed_urls_from_on_start(); | 207 return embedder_interface_->observed_committed_urls_from_on_start(); |
177 } | 208 } |
178 | 209 |
| 210 const std::vector<GURL>& completed_filtered_urls() const { |
| 211 return embedder_interface_->completed_filtered_urls(); |
| 212 } |
| 213 |
179 protected: | 214 protected: |
180 base::HistogramTester histogram_tester_; | 215 base::HistogramTester histogram_tester_; |
181 TestPageLoadMetricsEmbedderInterface* embedder_interface_; | 216 TestPageLoadMetricsEmbedderInterface* embedder_interface_; |
182 MetricsWebContentsObserver* observer_; | 217 MetricsWebContentsObserver* observer_; |
183 | 218 |
184 private: | 219 private: |
185 int num_errors_; | 220 int num_errors_; |
186 | 221 |
187 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); | 222 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); |
188 }; | 223 }; |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 DeleteContents(); | 551 DeleteContents(); |
517 | 552 |
518 histogram_tester_.ExpectTotalCount( | 553 histogram_tester_.ExpectTotalCount( |
519 internal::kPageLoadCompletedAfterAppBackground, 2); | 554 internal::kPageLoadCompletedAfterAppBackground, 2); |
520 histogram_tester_.ExpectBucketCount( | 555 histogram_tester_.ExpectBucketCount( |
521 internal::kPageLoadCompletedAfterAppBackground, false, 1); | 556 internal::kPageLoadCompletedAfterAppBackground, false, 1); |
522 histogram_tester_.ExpectBucketCount( | 557 histogram_tester_.ExpectBucketCount( |
523 internal::kPageLoadCompletedAfterAppBackground, true, 1); | 558 internal::kPageLoadCompletedAfterAppBackground, true, 1); |
524 } | 559 } |
525 | 560 |
| 561 TEST_F(MetricsWebContentsObserverTest, StopObservingOnCommit) { |
| 562 content::WebContentsTester* web_contents_tester = |
| 563 content::WebContentsTester::For(web_contents()); |
| 564 ASSERT_TRUE(completed_filtered_urls().empty()); |
| 565 |
| 566 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 567 ASSERT_TRUE(completed_filtered_urls().empty()); |
| 568 |
| 569 // kFilteredCommitUrl should stop observing in OnCommit, and thus should not |
| 570 // reach OnComplete(). |
| 571 web_contents_tester->NavigateAndCommit(GURL(kFilteredCommitUrl)); |
| 572 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl)}), |
| 573 completed_filtered_urls()); |
| 574 |
| 575 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 576 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl)}), |
| 577 completed_filtered_urls()); |
| 578 |
| 579 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 580 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl), GURL(kDefaultTestUrl2)}), |
| 581 completed_filtered_urls()); |
| 582 } |
| 583 |
526 } // namespace page_load_metrics | 584 } // namespace page_load_metrics |
OLD | NEW |