| 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 11 matching lines...) Expand all Loading... |
| 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.Timing.NavigationToFirstLayout"; |
| 27 const char kHistogramNameDomContent[] = | 27 const char kHistogramNameDomContent[] = |
| 28 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired"; | 28 "PageLoad.Timing.NavigationToDOMContentLoadedEventFired"; |
| 29 const char kHistogramNameLoad[] = | 29 const char kHistogramNameLoad[] = |
| 30 "PageLoad.Timing.NavigationToLoadEventFired"; | 30 "PageLoad.Timing.NavigationToLoadEventFired"; |
| 31 | 31 |
| 32 const char kBGHistogramNameFirstLayout[] = |
| 33 "PageLoad.Timing.BG.NavigationToFirstLayout"; |
| 34 const char kBGHistogramNameDomContent[] = |
| 35 "PageLoad.Timing.BG.NavigationToDOMContentLoadedEventFired"; |
| 36 const char kBGHistogramNameLoad[] = |
| 37 "PageLoad.Timing.BG.NavigationToLoadEventFired"; |
| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 timing.navigation_start = base::Time::FromDoubleT(1); | 100 timing.navigation_start = base::Time::FromDoubleT(1); |
| 94 timing.first_layout = first_layout; | 101 timing.first_layout = first_layout; |
| 95 | 102 |
| 96 content::WebContentsTester* web_contents_tester = | 103 content::WebContentsTester* web_contents_tester = |
| 97 content::WebContentsTester::For(web_contents()); | 104 content::WebContentsTester::For(web_contents()); |
| 98 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 105 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 99 | 106 |
| 100 observer_->OnMessageReceived( | 107 observer_->OnMessageReceived( |
| 101 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 108 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 102 web_contents()->GetMainFrame()); | 109 web_contents()->GetMainFrame()); |
| 110 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 111 GURL(kDefaultTestUrl)); |
| 103 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); | 112 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); |
| 104 // A same page navigation shouldn't trigger logging UMA for the original. | 113 // A same page navigation shouldn't trigger logging UMA for the original. |
| 105 AssertNoHistogramsLogged(); | 114 AssertNoHistogramsLogged(); |
| 106 } | 115 } |
| 107 | 116 |
| 108 TEST_F(MetricsWebContentsObserverTest, SamePageNoTriggerUntilTrueNavCommit) { | 117 TEST_F(MetricsWebContentsObserverTest, SamePageNoTriggerUntilTrueNavCommit) { |
| 109 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | 118 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| 110 | 119 |
| 111 PageLoadTiming timing; | 120 PageLoadTiming timing; |
| 112 timing.navigation_start = base::Time::FromDoubleT(1); | 121 timing.navigation_start = base::Time::FromDoubleT(1); |
| 113 timing.first_layout = first_layout; | 122 timing.first_layout = first_layout; |
| 114 | 123 |
| 115 content::WebContentsTester* web_contents_tester = | 124 content::WebContentsTester* web_contents_tester = |
| 116 content::WebContentsTester::For(web_contents()); | 125 content::WebContentsTester::For(web_contents()); |
| 117 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 126 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 118 | 127 |
| 119 observer_->OnMessageReceived( | 128 observer_->OnMessageReceived( |
| 120 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 129 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 121 web_contents()->GetMainFrame()); | 130 web_contents()->GetMainFrame()); |
| 131 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 132 GURL(kDefaultTestUrl)); |
| 122 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); | 133 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); |
| 134 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 135 GURL(kDefaultTestUrlAnchor)); |
| 123 // A same page navigation shouldn't trigger logging UMA for the original. | 136 // A same page navigation shouldn't trigger logging UMA for the original. |
| 124 AssertNoHistogramsLogged(); | 137 AssertNoHistogramsLogged(); |
| 125 | 138 |
| 126 // But we should keep the timing info and log it when we get another | 139 // But we should keep the timing info and log it when we get another |
| 127 // navigation. | 140 // navigation. |
| 128 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 141 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 129 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | 142 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| 130 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | 143 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 131 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); | 144 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); |
| 132 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 145 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 133 first_layout.InMilliseconds(), 1); | 146 first_layout.InMilliseconds(), 1); |
| 134 } | 147 } |
| 135 | 148 |
| 136 TEST_F(MetricsWebContentsObserverTest, SingleMetricAfterCommit) { | 149 TEST_F(MetricsWebContentsObserverTest, SingleMetricAfterCommit) { |
| 137 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | 150 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| 138 | 151 |
| 139 PageLoadTiming timing; | 152 PageLoadTiming timing; |
| 140 timing.navigation_start = base::Time::FromDoubleT(1); | 153 timing.navigation_start = base::Time::FromDoubleT(1); |
| 141 timing.first_layout = first_layout; | 154 timing.first_layout = first_layout; |
| 142 | 155 |
| 143 content::WebContentsTester* web_contents_tester = | 156 content::WebContentsTester* web_contents_tester = |
| 144 content::WebContentsTester::For(web_contents()); | 157 content::WebContentsTester::For(web_contents()); |
| 145 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 158 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 146 | 159 |
| 147 observer_->OnMessageReceived( | 160 observer_->OnMessageReceived( |
| 148 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 161 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 149 web_contents()->GetMainFrame()); | 162 web_contents()->GetMainFrame()); |
| 163 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 164 GURL(kDefaultTestUrl)); |
| 150 | 165 |
| 151 AssertNoHistogramsLogged(); | 166 AssertNoHistogramsLogged(); |
| 152 | 167 |
| 153 // Navigate again to force histogram recording. | 168 // Navigate again to force histogram recording. |
| 154 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 169 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 155 | 170 |
| 156 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | 171 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| 157 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | 172 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 158 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); | 173 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); |
| 159 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 174 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 174 timing.dom_content_loaded_event_start = dom_content; | 189 timing.dom_content_loaded_event_start = dom_content; |
| 175 timing.load_event_start = load; | 190 timing.load_event_start = load; |
| 176 | 191 |
| 177 content::WebContentsTester* web_contents_tester = | 192 content::WebContentsTester* web_contents_tester = |
| 178 content::WebContentsTester::For(web_contents()); | 193 content::WebContentsTester::For(web_contents()); |
| 179 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 194 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 180 | 195 |
| 181 observer_->OnMessageReceived( | 196 observer_->OnMessageReceived( |
| 182 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 197 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 183 web_contents()->GetMainFrame()); | 198 web_contents()->GetMainFrame()); |
| 199 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 200 GURL(kDefaultTestUrl)); |
| 184 | 201 |
| 185 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 202 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 186 | 203 |
| 187 PageLoadTiming timing2; | 204 PageLoadTiming timing2; |
| 188 timing2.navigation_start = base::Time::FromDoubleT(200); | 205 timing2.navigation_start = base::Time::FromDoubleT(200); |
| 189 timing2.first_layout = first_layout_2; | 206 timing2.first_layout = first_layout_2; |
| 190 | 207 |
| 191 observer_->OnMessageReceived( | 208 observer_->OnMessageReceived( |
| 192 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing2), | 209 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing2), |
| 193 web_contents()->GetMainFrame()); | 210 web_contents()->GetMainFrame()); |
| 211 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 212 GURL(kDefaultTestUrl2)); |
| 194 | 213 |
| 195 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 214 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 196 | 215 |
| 197 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 216 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 198 first_layout_1.InMilliseconds(), 1); | 217 first_layout_1.InMilliseconds(), 1); |
| 199 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 2); | 218 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 2); |
| 200 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 219 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 201 first_layout_1.InMilliseconds(), 1); | 220 first_layout_1.InMilliseconds(), 1); |
| 202 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 221 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 203 first_layout_2.InMilliseconds(), 1); | 222 first_layout_2.InMilliseconds(), 1); |
| 204 | 223 |
| 205 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); | 224 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); |
| 206 histogram_tester_.ExpectBucketCount(kHistogramNameDomContent, | 225 histogram_tester_.ExpectBucketCount(kHistogramNameDomContent, |
| 207 dom_content.InMilliseconds(), 1); | 226 dom_content.InMilliseconds(), 1); |
| 208 | 227 |
| 209 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 1); | 228 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 1); |
| 210 histogram_tester_.ExpectBucketCount(kHistogramNameLoad, load.InMilliseconds(), | 229 histogram_tester_.ExpectBucketCount(kHistogramNameLoad, load.InMilliseconds(), |
| 211 1); | 230 1); |
| 212 } | 231 } |
| 213 | 232 |
| 233 TEST_F(MetricsWebContentsObserverTest, BackgroundDifferentHistogram) { |
| 234 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| 235 |
| 236 PageLoadTiming timing; |
| 237 timing.navigation_start = base::Time::FromDoubleT(1); |
| 238 timing.first_layout = first_layout; |
| 239 |
| 240 content::WebContentsTester* web_contents_tester = |
| 241 content::WebContentsTester::For(web_contents()); |
| 242 |
| 243 // Simulate "Open link in new tab." |
| 244 observer_->WasHidden(); |
| 245 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 246 |
| 247 observer_->OnMessageReceived( |
| 248 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 249 web_contents()->GetMainFrame()); |
| 250 |
| 251 // Simulate switching to the tab and making another navigation. |
| 252 observer_->WasShown(); |
| 253 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 254 GURL(kDefaultTestUrl)); |
| 255 AssertNoHistogramsLogged(); |
| 256 |
| 257 // Navigate again to force histogram recording. |
| 258 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 259 |
| 260 histogram_tester_.ExpectTotalCount(kBGHistogramNameDomContent, 0); |
| 261 histogram_tester_.ExpectTotalCount(kBGHistogramNameLoad, 0); |
| 262 histogram_tester_.ExpectTotalCount(kBGHistogramNameFirstLayout, 1); |
| 263 histogram_tester_.ExpectBucketCount(kBGHistogramNameFirstLayout, |
| 264 first_layout.InMilliseconds(), 1); |
| 265 |
| 266 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| 267 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 268 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
| 269 } |
| 270 |
| 214 } // namespace page_load_metrics | 271 } // namespace page_load_metrics |
| OLD | NEW |