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/mock_navigation_handle.h" | |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | |
| 15 #include "testing/gmock/include/gmock/gmock.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 using testing::Return; | |
| 19 using testing::ReturnRef; | |
| 20 using testing::NiceMock; | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 class RenderFrameHost; | |
| 25 | |
| 26 } // namespace content | |
| 27 | |
| 28 namespace page_load_metrics { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 const char kDefaultTestUrl[] = "https://google.com"; | |
| 33 | |
| 34 void SetNavigationHandleExpectations(content::MockNavigationHandle* handle) { | |
| 35 ON_CALL(*handle, IsInMainFrame()).WillByDefault(Return(true)); | |
| 36 ON_CALL(*handle, IsSamePage()).WillByDefault(Return(false)); | |
| 37 ON_CALL(*handle, HasCommittedDocument()).WillByDefault(Return(true)); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 class MockMetricsWebContentsObserver : public MetricsWebContentsObserver { | |
| 43 public: | |
| 44 MockMetricsWebContentsObserver() | |
| 45 : MetricsWebContentsObserver(nullptr), url_(kDefaultTestUrl) { | |
| 46 ON_CALL(*this, GetLastCommittedURL()).WillByDefault(ReturnRef(url_)); | |
| 47 ON_CALL(*this, IsCurrentMainFrame(testing::_)).WillByDefault(Return(true)); | |
| 48 } | |
| 49 MOCK_METHOD0(GetLastCommittedURL, const GURL&()); | |
| 50 MOCK_METHOD1(IsCurrentMainFrame, bool(content::RenderFrameHost*)); | |
| 51 | |
| 52 private: | |
| 53 GURL url_; | |
|
Bryan McQuade
2015/09/10 00:45:58
since you set this in the initializer list, you sh
| |
| 54 content::TestBrowserThreadBundle thread_bundle_; | |
| 55 }; | |
| 56 | |
| 57 class MetricsWebContentsObserverTest : public testing::Test { | |
| 58 public: | |
| 59 const char* kHistogramNameFirstLayout = "PageLoad.Timing.FirstLayout"; | |
| 60 const char* kHistogramNameDomContent = | |
| 61 "PageLoad.Timing.DOMContentLoadedEventFired"; | |
| 62 const char* kHistogramNameLoad = "PageLoad.Timing.LoadEventFired"; | |
| 63 | |
| 64 MetricsWebContentsObserverTest() | |
| 65 : observer_(), mock_nav_handle_(GURL(kDefaultTestUrl)) {} | |
| 66 | |
| 67 void SetUp() override { SetNavigationHandleExpectations(&mock_nav_handle_); } | |
| 68 | |
| 69 void AssertNoHistogramsLogged() { | |
| 70 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | |
| 71 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | |
| 72 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); | |
| 73 } | |
| 74 | |
| 75 protected: | |
| 76 base::HistogramTester histogram_tester_; | |
| 77 NiceMock<MockMetricsWebContentsObserver> observer_; | |
| 78 NiceMock<content::MockNavigationHandle> mock_nav_handle_; | |
| 79 }; | |
| 80 | |
| 81 TEST_F(MetricsWebContentsObserverTest, NoMetrics) { | |
| 82 AssertNoHistogramsLogged(); | |
| 83 } | |
| 84 | |
| 85 TEST_F(MetricsWebContentsObserverTest, NotInMainFrame) { | |
| 86 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | |
| 87 | |
| 88 PageLoadTiming timing; | |
| 89 timing.navigation_start = base::Time::FromDoubleT(1); | |
| 90 timing.first_layout = first_layout; | |
| 91 EXPECT_CALL(mock_nav_handle_, IsInMainFrame()).WillRepeatedly(Return(false)); | |
| 92 | |
| 93 observer_.DidCommitNavigation(&mock_nav_handle_); | |
| 94 observer_.OnMessageReceived( | |
| 95 PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), | |
| 96 nullptr); | |
| 97 // Navigate again to force histogram recording. | |
| 98 observer_.DidCommitNavigation(&mock_nav_handle_); | |
| 99 AssertNoHistogramsLogged(); | |
| 100 } | |
| 101 | |
| 102 TEST_F(MetricsWebContentsObserverTest, SamePage) { | |
| 103 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | |
| 104 | |
| 105 PageLoadTiming timing; | |
| 106 timing.navigation_start = base::Time::FromDoubleT(1); | |
| 107 timing.first_layout = first_layout; | |
| 108 EXPECT_CALL(mock_nav_handle_, IsSamePage()).WillRepeatedly(Return(true)); | |
| 109 | |
| 110 observer_.DidCommitNavigation(&mock_nav_handle_); | |
| 111 observer_.OnMessageReceived( | |
| 112 PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), | |
| 113 nullptr); | |
| 114 // Navigate again to force histogram recording. | |
| 115 observer_.DidCommitNavigation(&mock_nav_handle_); | |
| 116 AssertNoHistogramsLogged(); | |
| 117 } | |
| 118 | |
| 119 TEST_F(MetricsWebContentsObserverTest, SingleMetricAfterCommit) { | |
| 120 base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); | |
| 121 | |
| 122 PageLoadTiming timing; | |
| 123 timing.navigation_start = base::Time::FromDoubleT(1); | |
| 124 timing.first_layout = first_layout; | |
| 125 observer_.DidCommitNavigation(&mock_nav_handle_); | |
| 126 observer_.OnMessageReceived( | |
| 127 PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), | |
| 128 nullptr); | |
| 129 | |
| 130 AssertNoHistogramsLogged(); | |
| 131 | |
| 132 // Navigate again to force histogram recording. | |
| 133 observer_.DidCommitNavigation(&mock_nav_handle_); | |
| 134 | |
| 135 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); | |
| 136 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); | |
| 137 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); | |
| 138 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | |
| 139 first_layout.InMilliseconds(), 1); | |
| 140 } | |
| 141 | |
| 142 TEST_F(MetricsWebContentsObserverTest, MultipleMetricsAfterCommits) { | |
| 143 base::TimeDelta first_layout_1 = base::TimeDelta::FromMilliseconds(1); | |
| 144 base::TimeDelta first_layout_2 = base::TimeDelta::FromMilliseconds(20); | |
| 145 base::TimeDelta response = base::TimeDelta::FromMilliseconds(10); | |
| 146 base::TimeDelta dom_content = base::TimeDelta::FromMilliseconds(40); | |
| 147 base::TimeDelta load = base::TimeDelta::FromMilliseconds(100); | |
| 148 | |
| 149 PageLoadTiming timing; | |
| 150 timing.navigation_start = base::Time::FromDoubleT(1); | |
| 151 timing.first_layout = first_layout_1; | |
| 152 timing.response_start = response; | |
| 153 timing.dom_content_loaded_event_start = dom_content; | |
| 154 timing.load_event_start = load; | |
| 155 observer_.DidCommitNavigation(&mock_nav_handle_); | |
| 156 observer_.OnMessageReceived( | |
| 157 PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), | |
| 158 nullptr); | |
| 159 | |
| 160 observer_.DidCommitNavigation(&mock_nav_handle_); | |
| 161 PageLoadTiming timing2; | |
| 162 timing2.navigation_start = base::Time::FromDoubleT(200); | |
| 163 timing2.first_layout = first_layout_2; | |
| 164 observer_.OnMessageReceived( | |
| 165 PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing2), | |
| 166 nullptr); | |
| 167 observer_.DidCommitNavigation(&mock_nav_handle_); | |
| 168 | |
| 169 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | |
| 170 first_layout_1.InMilliseconds(), 1); | |
| 171 histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 2); | |
| 172 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | |
| 173 first_layout_1.InMilliseconds(), 1); | |
| 174 histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, | |
| 175 first_layout_2.InMilliseconds(), 1); | |
| 176 | |
| 177 histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); | |
| 178 histogram_tester_.ExpectBucketCount(kHistogramNameDomContent, | |
| 179 dom_content.InMilliseconds(), 1); | |
| 180 | |
| 181 histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 1); | |
| 182 histogram_tester_.ExpectBucketCount(kHistogramNameLoad, load.InMilliseconds(), | |
| 183 1); | |
| 184 } | |
| 185 | |
| 186 } // namespace page_load_metrics | |
| OLD | NEW |