| 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 20 matching lines...) Expand all Loading... |
| 72 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); | 79 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); |
| 73 | 80 |
| 74 content::RenderFrameHostTester* subframe_tester = | 81 content::RenderFrameHostTester* subframe_tester = |
| 75 content::RenderFrameHostTester::For(subframe); | 82 content::RenderFrameHostTester::For(subframe); |
| 76 subframe_tester->SimulateNavigationStart(GURL(kDefaultTestUrl2)); | 83 subframe_tester->SimulateNavigationStart(GURL(kDefaultTestUrl2)); |
| 77 subframe_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); | 84 subframe_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); |
| 78 observer_->OnMessageReceived( | 85 observer_->OnMessageReceived( |
| 79 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 86 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 80 subframe); | 87 subframe); |
| 81 subframe_tester->SimulateNavigationStop(); | 88 subframe_tester->SimulateNavigationStop(); |
| 89 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 90 GURL(kDefaultTestUrl2)); |
| 82 | 91 |
| 83 // Navigate again to see if the timing updated for a subframe message. | 92 // Navigate again to see if the timing updated for a subframe message. |
| 84 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 93 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 85 | 94 |
| 86 AssertNoHistogramsLogged(); | 95 AssertNoHistogramsLogged(); |
| 87 } | 96 } |
| 88 | 97 |
| 89 TEST_F(MetricsWebContentsObserverTest, SamePageNoTrigger) { | 98 TEST_F(MetricsWebContentsObserverTest, SamePageNoTrigger) { |
| 90 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | 99 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| 91 | 100 |
| 92 PageLoadTiming timing; | 101 PageLoadTiming timing; |
| 93 timing.navigation_start = base::Time::FromDoubleT(1); | 102 timing.navigation_start = base::Time::FromDoubleT(1); |
| 94 timing.first_layout = first_layout; | 103 timing.first_layout = first_layout; |
| 95 | 104 |
| 96 content::WebContentsTester* web_contents_tester = | 105 content::WebContentsTester* web_contents_tester = |
| 97 content::WebContentsTester::For(web_contents()); | 106 content::WebContentsTester::For(web_contents()); |
| 98 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 107 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 99 | 108 |
| 100 observer_->OnMessageReceived( | 109 observer_->OnMessageReceived( |
| 101 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 110 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 102 web_contents()->GetMainFrame()); | 111 web_contents()->GetMainFrame()); |
| 112 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 113 GURL(kDefaultTestUrl)); |
| 103 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); | 114 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); |
| 104 // A same page navigation shouldn't trigger logging UMA for the original. | 115 // A same page navigation shouldn't trigger logging UMA for the original. |
| 105 AssertNoHistogramsLogged(); | 116 AssertNoHistogramsLogged(); |
| 106 } | 117 } |
| 107 | 118 |
| 108 TEST_F(MetricsWebContentsObserverTest, SamePageNoTriggerUntilTrueNavCommit) { | 119 TEST_F(MetricsWebContentsObserverTest, SamePageNoTriggerUntilTrueNavCommit) { |
| 109 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | 120 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| 110 | 121 |
| 111 PageLoadTiming timing; | 122 PageLoadTiming timing; |
| 112 timing.navigation_start = base::Time::FromDoubleT(1); | 123 timing.navigation_start = base::Time::FromDoubleT(1); |
| 113 timing.first_layout = first_layout; | 124 timing.first_layout = first_layout; |
| 114 | 125 |
| 115 content::WebContentsTester* web_contents_tester = | 126 content::WebContentsTester* web_contents_tester = |
| 116 content::WebContentsTester::For(web_contents()); | 127 content::WebContentsTester::For(web_contents()); |
| 117 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 128 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 118 | 129 |
| 119 observer_->OnMessageReceived( | 130 observer_->OnMessageReceived( |
| 120 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 131 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 121 web_contents()->GetMainFrame()); | 132 web_contents()->GetMainFrame()); |
| 133 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 134 GURL(kDefaultTestUrl)); |
| 122 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); | 135 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); |
| 136 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 137 GURL(kDefaultTestUrlAnchor)); |
| 123 // A same page navigation shouldn't trigger logging UMA for the original. | 138 // A same page navigation shouldn't trigger logging UMA for the original. |
| 124 AssertNoHistogramsLogged(); | 139 AssertNoHistogramsLogged(); |
| 125 | 140 |
| 126 // But we should keep the timing info and log it when we get another | 141 // But we should keep the timing info and log it when we get another |
| 127 // navigation. | 142 // navigation. |
| 128 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 143 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 129 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | 144 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| 130 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | 145 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 131 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); | 146 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); |
| 132 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 147 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 133 first_layout.InMilliseconds(), 1); | 148 first_layout.InMilliseconds(), 1); |
| 134 } | 149 } |
| 135 | 150 |
| 136 TEST_F(MetricsWebContentsObserverTest, SingleMetricAfterCommit) { | 151 TEST_F(MetricsWebContentsObserverTest, SingleMetricAfterCommit) { |
| 137 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | 152 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| 138 | 153 |
| 139 PageLoadTiming timing; | 154 PageLoadTiming timing; |
| 140 timing.navigation_start = base::Time::FromDoubleT(1); | 155 timing.navigation_start = base::Time::FromDoubleT(1); |
| 141 timing.first_layout = first_layout; | 156 timing.first_layout = first_layout; |
| 142 | 157 |
| 143 content::WebContentsTester* web_contents_tester = | 158 content::WebContentsTester* web_contents_tester = |
| 144 content::WebContentsTester::For(web_contents()); | 159 content::WebContentsTester::For(web_contents()); |
| 145 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 160 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 146 | 161 |
| 147 observer_->OnMessageReceived( | 162 observer_->OnMessageReceived( |
| 148 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 163 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 149 web_contents()->GetMainFrame()); | 164 web_contents()->GetMainFrame()); |
| 165 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 166 GURL(kDefaultTestUrl)); |
| 150 | 167 |
| 151 AssertNoHistogramsLogged(); | 168 AssertNoHistogramsLogged(); |
| 152 | 169 |
| 153 // Navigate again to force histogram recording. | 170 // Navigate again to force histogram recording. |
| 154 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 171 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 155 | 172 |
| 156 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | 173 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| 157 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | 174 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 158 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); | 175 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); |
| 159 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 176 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 174 timing.dom_content_loaded_event_start = dom_content; | 191 timing.dom_content_loaded_event_start = dom_content; |
| 175 timing.load_event_start = load; | 192 timing.load_event_start = load; |
| 176 | 193 |
| 177 content::WebContentsTester* web_contents_tester = | 194 content::WebContentsTester* web_contents_tester = |
| 178 content::WebContentsTester::For(web_contents()); | 195 content::WebContentsTester::For(web_contents()); |
| 179 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 196 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 180 | 197 |
| 181 observer_->OnMessageReceived( | 198 observer_->OnMessageReceived( |
| 182 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | 199 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 183 web_contents()->GetMainFrame()); | 200 web_contents()->GetMainFrame()); |
| 201 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 202 GURL(kDefaultTestUrl)); |
| 184 | 203 |
| 185 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 204 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 186 | 205 |
| 187 PageLoadTiming timing2; | 206 PageLoadTiming timing2; |
| 188 timing2.navigation_start = base::Time::FromDoubleT(200); | 207 timing2.navigation_start = base::Time::FromDoubleT(200); |
| 189 timing2.first_layout = first_layout_2; | 208 timing2.first_layout = first_layout_2; |
| 190 | 209 |
| 191 observer_->OnMessageReceived( | 210 observer_->OnMessageReceived( |
| 192 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing2), | 211 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing2), |
| 193 web_contents()->GetMainFrame()); | 212 web_contents()->GetMainFrame()); |
| 213 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 214 GURL(kDefaultTestUrl2)); |
| 194 | 215 |
| 195 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 216 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 196 | 217 |
| 197 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 218 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 198 first_layout_1.InMilliseconds(), 1); | 219 first_layout_1.InMilliseconds(), 1); |
| 199 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 2); | 220 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 2); |
| 200 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 221 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 201 first_layout_1.InMilliseconds(), 1); | 222 first_layout_1.InMilliseconds(), 1); |
| 202 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | 223 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 203 first_layout_2.InMilliseconds(), 1); | 224 first_layout_2.InMilliseconds(), 1); |
| 204 | 225 |
| 205 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); | 226 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); |
| 206 histogram_tester_.ExpectBucketCount(kHistogramNameDomContent, | 227 histogram_tester_.ExpectBucketCount(kHistogramNameDomContent, |
| 207 dom_content.InMilliseconds(), 1); | 228 dom_content.InMilliseconds(), 1); |
| 208 | 229 |
| 209 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 1); | 230 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 1); |
| 210 histogram_tester_.ExpectBucketCount(kHistogramNameLoad, load.InMilliseconds(), | 231 histogram_tester_.ExpectBucketCount(kHistogramNameLoad, load.InMilliseconds(), |
| 211 1); | 232 1); |
| 212 } | 233 } |
| 213 | 234 |
| 235 TEST_F(MetricsWebContentsObserverTest, BackgroundDifferentHistogram) { |
| 236 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| 237 |
| 238 PageLoadTiming timing; |
| 239 timing.navigation_start = base::Time::FromDoubleT(1); |
| 240 timing.first_layout = first_layout; |
| 241 |
| 242 content::WebContentsTester* web_contents_tester = |
| 243 content::WebContentsTester::For(web_contents()); |
| 244 |
| 245 // Simulate "Open link in new tab." |
| 246 observer_->WasHidden(); |
| 247 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 248 |
| 249 observer_->OnMessageReceived( |
| 250 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 251 web_contents()->GetMainFrame()); |
| 252 |
| 253 // Simulate switching to the tab and making another navigation. |
| 254 observer_->WasShown(); |
| 255 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 256 GURL(kDefaultTestUrl)); |
| 257 AssertNoHistogramsLogged(); |
| 258 |
| 259 // Navigate again to force histogram recording. |
| 260 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
| 261 |
| 262 histogram_tester_.ExpectTotalCount(kBGHistogramNameDomContent, 0); |
| 263 histogram_tester_.ExpectTotalCount(kBGHistogramNameLoad, 0); |
| 264 histogram_tester_.ExpectTotalCount(kBGHistogramNameFirstLayout, 1); |
| 265 histogram_tester_.ExpectBucketCount(kBGHistogramNameFirstLayout, |
| 266 first_layout.InMilliseconds(), 1); |
| 267 |
| 268 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| 269 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 270 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
| 271 } |
| 272 |
| 273 TEST_F(MetricsWebContentsObserverTest, DontBackgroundQuickerLoad) { |
| 274 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| 275 |
| 276 PageLoadTiming timing; |
| 277 timing.navigation_start = base::Time::FromDoubleT(1); |
| 278 timing.first_layout = first_layout; |
| 279 |
| 280 observer_->WasHidden(); |
| 281 |
| 282 // Open in new tab |
| 283 content::WebContentsTester* web_contents_tester = |
| 284 content::WebContentsTester::For(web_contents()); |
| 285 |
| 286 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl)); |
| 287 |
| 288 content::RenderFrameHostTester* rfh_tester = |
| 289 content::RenderFrameHostTester::For(main_rfh()); |
| 290 |
| 291 // Switch to the tab |
| 292 observer_->WasShown(); |
| 293 |
| 294 // Start another provisional load |
| 295 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl2)); |
| 296 rfh_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); |
| 297 observer_->OnMessageReceived( |
| 298 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), |
| 299 main_rfh()); |
| 300 rfh_tester->SimulateNavigationStop(); |
| 301 |
| 302 observer_->DidFinishLoad(web_contents()->GetMainFrame(), |
| 303 GURL(kDefaultTestUrl2)); |
| 304 |
| 305 // Navigate again to see if the timing updated for a subframe message. |
| 306 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
| 307 |
| 308 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| 309 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| 310 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); |
| 311 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| 312 first_layout.InMilliseconds(), 1); |
| 313 } |
| 214 } // namespace page_load_metrics | 314 } // namespace page_load_metrics |
| OLD | NEW |