Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/page_load_metrics/browser/metrics_web_contents_observer.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/process/kill.h" | |
| 9 #include "base/test/histogram_tester.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "components/page_load_metrics/common/page_load_metrics_messages.h" | |
| 12 #include "content/public/browser/navigation_handle.h" | |
| 13 #include "content/public/test/test_renderer_host.h" | |
| 14 #include "content/public/test/web_contents_tester.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 class RenderFrameHost; | |
|
clamy
2015/09/11 11:52:32
You should include the header and not forward decl
Charlie Harrison
2015/09/11 14:40:52
Done.
| |
| 19 | |
| 20 } // namespace content | |
| 21 | |
| 22 namespace page_load_metrics { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const char kDefaultTestUrl[] = "https://google.com"; | |
| 27 const char kDefaultTestUrlAnchor[] = "https://google.com#samepage"; | |
| 28 const char kDefaultTestUrl2[] = "https://whatever.com"; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 class MetricsWebContentsObserverTest : | |
| 33 public content::RenderViewHostTestHarness { | |
| 34 public: | |
| 35 const char* kHistogramNameFirstLayout = | |
| 36 "PageLoadTiming.Navigation.To.FirstLayout"; | |
| 37 const char* kHistogramNameDomContent = | |
| 38 "PageLoadTiming.Navigation.To.DOMContentLoadedEventFired"; | |
| 39 const char* kHistogramNameLoad = | |
| 40 "PageLoadTiming.Navigation.To.LoadEventFired"; | |
| 41 | |
| 42 MetricsWebContentsObserverTest() { | |
| 43 } | |
| 44 | |
| 45 void SetUp() override { | |
| 46 RenderViewHostTestHarness::SetUp(); | |
| 47 observer_ = make_scoped_ptr( | |
| 48 new MetricsWebContentsObserver(web_contents())); | |
| 49 } | |
| 50 | |
| 51 void AssertNoHistogramsLogged() { | |
| 52 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | |
| 53 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | |
| 54 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); | |
| 55 } | |
| 56 | |
| 57 protected: | |
| 58 base::HistogramTester histogram_tester_; | |
| 59 scoped_ptr<MetricsWebContentsObserver> observer_; | |
| 60 }; | |
| 61 | |
| 62 TEST_F(MetricsWebContentsObserverTest, NoMetrics) { | |
| 63 AssertNoHistogramsLogged(); | |
| 64 } | |
| 65 | |
| 66 TEST_F(MetricsWebContentsObserverTest, NotInMainFrame) { | |
| 67 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | |
| 68 | |
| 69 PageLoadTiming timing; | |
| 70 timing.navigation_start = base::Time::FromDoubleT(1); | |
| 71 timing.first_layout = first_layout; | |
| 72 | |
| 73 content::WebContentsTester* web_contents_tester = | |
| 74 content::WebContentsTester::For(web_contents()); | |
| 75 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | |
| 76 | |
| 77 content::RenderFrameHostTester* rfh_tester = | |
| 78 content::RenderFrameHostTester::For(main_rfh()); | |
| 79 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); | |
| 80 | |
| 81 content::RenderFrameHostTester* subframe_tester = | |
| 82 content::RenderFrameHostTester::For(subframe); | |
| 83 subframe_tester->SimulateNavigationStart(GURL(kDefaultTestUrl2)); | |
| 84 subframe_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); | |
| 85 observer_->OnMessageReceived( | |
| 86 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | |
| 87 subframe); | |
| 88 subframe_tester->SimulateNavigationStop(); | |
| 89 | |
| 90 // Navigate again to see if the timing updated for a subframe message. | |
| 91 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | |
| 92 | |
| 93 AssertNoHistogramsLogged(); | |
| 94 } | |
| 95 | |
| 96 TEST_F(MetricsWebContentsObserverTest, SamePageNoTrigger) { | |
| 97 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | |
| 98 | |
| 99 PageLoadTiming timing; | |
| 100 timing.navigation_start = base::Time::FromDoubleT(1); | |
| 101 timing.first_layout = first_layout; | |
| 102 | |
| 103 content::WebContentsTester* web_contents_tester = | |
| 104 content::WebContentsTester::For(web_contents()); | |
| 105 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | |
| 106 | |
| 107 observer_->OnMessageReceived( | |
| 108 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | |
| 109 web_contents()->GetMainFrame()); | |
| 110 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); | |
| 111 // A same page navigation shouldn't trigger logging UMA for the original. | |
| 112 AssertNoHistogramsLogged(); | |
| 113 } | |
| 114 | |
| 115 TEST_F(MetricsWebContentsObserverTest, SamePageNoTriggerUntilTrueNavCommit) { | |
| 116 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | |
| 117 | |
| 118 PageLoadTiming timing; | |
| 119 timing.navigation_start = base::Time::FromDoubleT(1); | |
| 120 timing.first_layout = first_layout; | |
| 121 | |
| 122 content::WebContentsTester* web_contents_tester = | |
| 123 content::WebContentsTester::For(web_contents()); | |
| 124 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | |
| 125 | |
| 126 observer_->OnMessageReceived( | |
| 127 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | |
| 128 web_contents()->GetMainFrame()); | |
| 129 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); | |
| 130 // A same page navigation shouldn't trigger logging UMA for the original. | |
| 131 AssertNoHistogramsLogged(); | |
| 132 | |
| 133 // But we should keep the timing info and log it when we get another | |
| 134 // navigation. | |
| 135 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | |
| 136 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | |
| 137 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | |
| 138 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); | |
| 139 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | |
| 140 first_layout.InMilliseconds(), 1); | |
| 141 | |
| 142 } | |
| 143 | |
| 144 TEST_F(MetricsWebContentsObserverTest, SingleMetricAfterCommit) { | |
| 145 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | |
| 146 | |
| 147 PageLoadTiming timing; | |
| 148 timing.navigation_start = base::Time::FromDoubleT(1); | |
| 149 timing.first_layout = first_layout; | |
| 150 | |
| 151 content::WebContentsTester* web_contents_tester = | |
| 152 content::WebContentsTester::For(web_contents()); | |
| 153 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | |
| 154 | |
| 155 observer_->OnMessageReceived( | |
| 156 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | |
| 157 web_contents()->GetMainFrame()); | |
| 158 | |
| 159 AssertNoHistogramsLogged(); | |
| 160 | |
| 161 // Navigate again to force histogram recording. | |
| 162 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | |
| 163 | |
| 164 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | |
| 165 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | |
| 166 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); | |
| 167 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | |
| 168 first_layout.InMilliseconds(), 1); | |
| 169 } | |
| 170 | |
| 171 TEST_F(MetricsWebContentsObserverTest, MultipleMetricsAfterCommits) { | |
| 172 base::TimeDelta first_layout_1 = base::TimeDelta::FromMilliseconds(1); | |
| 173 base::TimeDelta first_layout_2 = base::TimeDelta::FromMilliseconds(20); | |
| 174 base::TimeDelta response = base::TimeDelta::FromMilliseconds(10); | |
| 175 base::TimeDelta dom_content = base::TimeDelta::FromMilliseconds(40); | |
| 176 base::TimeDelta load = base::TimeDelta::FromMilliseconds(100); | |
| 177 | |
| 178 PageLoadTiming timing; | |
| 179 timing.navigation_start = base::Time::FromDoubleT(1); | |
| 180 timing.first_layout = first_layout_1; | |
| 181 timing.response_start = response; | |
| 182 timing.dom_content_loaded_event_start = dom_content; | |
| 183 timing.load_event_start = load; | |
| 184 | |
| 185 content::WebContentsTester* web_contents_tester = | |
| 186 content::WebContentsTester::For(web_contents()); | |
| 187 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | |
| 188 | |
| 189 observer_->OnMessageReceived( | |
| 190 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing), | |
| 191 web_contents()->GetMainFrame()); | |
| 192 | |
| 193 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | |
| 194 | |
| 195 PageLoadTiming timing2; | |
| 196 timing2.navigation_start = base::Time::FromDoubleT(200); | |
| 197 timing2.first_layout = first_layout_2; | |
| 198 | |
| 199 observer_->OnMessageReceived( | |
| 200 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing2), | |
| 201 web_contents()->GetMainFrame()); | |
| 202 | |
| 203 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | |
| 204 | |
| 205 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | |
| 206 first_layout_1.InMilliseconds(), 1); | |
| 207 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 2); | |
| 208 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | |
| 209 first_layout_1.InMilliseconds(), 1); | |
| 210 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | |
| 211 first_layout_2.InMilliseconds(), 1); | |
| 212 | |
| 213 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); | |
| 214 histogram_tester_.ExpectBucketCount(kHistogramNameDomContent, | |
| 215 dom_content.InMilliseconds(), 1); | |
| 216 | |
| 217 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 1); | |
| 218 histogram_tester_.ExpectBucketCount(kHistogramNameLoad, load.InMilliseconds(), | |
| 219 1); | |
| 220 } | |
| 221 | |
| 222 } // namespace page_load_metrics | |
| OLD | NEW |