Index: components/page_load_metrics/browser/page_load_metrics_web_contents_observer_unittest.cc |
diff --git a/components/page_load_metrics/browser/page_load_metrics_web_contents_observer_unittest.cc b/components/page_load_metrics/browser/page_load_metrics_web_contents_observer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..67fad4a5000b8ba49ab4eb524fc714812017a5b8 |
--- /dev/null |
+++ b/components/page_load_metrics/browser/page_load_metrics_web_contents_observer_unittest.cc |
@@ -0,0 +1,192 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/page_load_metrics/browser/page_load_metrics_web_contents_observer.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/process/kill.h" |
+#include "base/test/histogram_tester.h" |
+#include "base/time/time.h" |
+#include "components/page_load_metrics/common/page_load_metrics_messages.h" |
+#include "content/public/browser/navigation_handle.h" |
+#include "content/public/test/mock_navigation_handle.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using testing::Return; |
+using testing::ReturnRef; |
+ |
+namespace content { |
+ |
+class RenderFrameHost; |
+ |
+} // namespace content |
+ |
+namespace page_load_metrics { |
+ |
+namespace { |
+ |
+void SetNavigationHandleExpectations(content::MockNavigationHandle* handle) { |
+ ON_CALL(*handle, IsInMainFrame()).WillByDefault(Return(true)); |
+ ON_CALL(*handle, IsSamePage()).WillByDefault(Return(false)); |
+ ON_CALL(*handle, HasCommittedDocument()).WillByDefault(Return(true)); |
+} |
+ |
+} // namespace |
+ |
+class MockMetricsWebContentsObserver : public MetricsWebContentsObserver { |
+ public: |
+ MockMetricsWebContentsObserver() : MetricsWebContentsObserver(nullptr) { |
+ url_ = GURL("https://google.com"); |
Bryan McQuade
2015/09/09 16:26:25
generally preferred to initialize members in the i
Charlie Harrison
2015/09/09 17:32:03
Done.
|
+ EXPECT_CALL(*this, GetLastCommittedURL()).WillRepeatedly(ReturnRef(url_)); |
Bryan McQuade
2015/09/09 16:26:25
should these be ON_CALL since they are default beh
Charlie Harrison
2015/09/09 17:32:03
yupp, my mistake
Bryan McQuade
2015/09/09 17:33:28
(and also WillByDefault rather than WillRepeatedly
|
+ EXPECT_CALL(*this, IsCurrentMainFrame(testing::_)) |
+ .WillRepeatedly(Return(true)); |
+ } |
+ MOCK_METHOD0(GetLastCommittedURL, const GURL&()); |
+ MOCK_METHOD1(IsCurrentMainFrame, bool(content::RenderFrameHost*)); |
+ |
+ private: |
+ GURL url_; |
+ content::TestBrowserThreadBundle thread_bundle_; |
+}; |
+ |
+class MetricsWebContentsObserverTest : public testing::Test { |
+ public: |
+ const char* kHistogramNameFirstLayout = "PageLoad.Timing.FirstLayout"; |
+ const char* kHistogramNameDomContent = |
+ "PageLoad.Timing.DOMContentLoadedEventFired"; |
+ const char* kHistogramNameLoad = "PageLoad.Timing.LoadEventFired"; |
+ |
+ MetricsWebContentsObserverTest() : observer_() {} |
+ |
+ protected: |
+ base::HistogramTester histogram_tester_; |
+ MockMetricsWebContentsObserver observer_; |
Bryan McQuade
2015/09/09 17:33:28
the use of ON_CALL instead of EXPECT_CALL above ma
Charlie Harrison
2015/09/09 19:42:34
Done, I was getting those logs.
|
+}; |
+ |
+TEST_F(MetricsWebContentsObserverTest, NoMetrics) { |
+ histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
Bryan McQuade
2015/09/09 17:33:28
since we end up doing these 3 lines in a few place
Charlie Harrison
2015/09/09 19:42:34
Done.
|
+ histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
+} |
+ |
+TEST_F(MetricsWebContentsObserverTest, NotInMainFrame) { |
+ base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
+ |
+ PageLoadTiming timing; |
+ timing.navigation_start = base::Time::FromDoubleT(1); |
+ timing.first_layout = first_layout; |
+ GURL url("https://google.com"); |
+ content::MockNavigationHandle test_handle(url); |
+ SetNavigationHandleExpectations(&test_handle); |
Bryan McQuade
2015/09/09 17:33:28
rather than calling this in each test, I'd add a
v
Charlie Harrison
2015/09/09 19:42:34
Done.
|
+ EXPECT_CALL(test_handle, IsInMainFrame()).WillRepeatedly(Return(false)); |
+ |
+ observer_.DidCommitNavigation(&test_handle); |
+ observer_.OnMessageReceived( |
+ PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), |
+ nullptr); |
+ // Navigate again to force histogram recording. |
+ observer_.DidCommitNavigation(&test_handle); |
+ |
+ histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
Bryan McQuade
2015/09/09 17:33:28
AssertNoHistogramsLogged
Charlie Harrison
2015/09/09 19:42:34
Done.
|
+ histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
+} |
+ |
+TEST_F(MetricsWebContentsObserverTest, SamePage) { |
+ base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
+ |
+ PageLoadTiming timing; |
+ timing.navigation_start = base::Time::FromDoubleT(1); |
+ timing.first_layout = first_layout; |
+ GURL url("https://google.com"); |
+ content::MockNavigationHandle test_handle(url); |
+ SetNavigationHandleExpectations(&test_handle); |
+ EXPECT_CALL(test_handle, IsSamePage()).WillRepeatedly(Return(true)); |
+ |
+ observer_.DidCommitNavigation(&test_handle); |
+ observer_.OnMessageReceived( |
+ PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), |
+ nullptr); |
+ // Navigate again to force histogram recording. |
+ observer_.DidCommitNavigation(&test_handle); |
+ |
+ histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
Bryan McQuade
2015/09/09 17:33:28
AssertNoHistogramsLogged
Charlie Harrison
2015/09/09 19:42:34
Done.
|
+ histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
+} |
+ |
+TEST_F(MetricsWebContentsObserverTest, SingleMetricAfterCommit) { |
+ base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
+ |
+ PageLoadTiming timing; |
+ timing.navigation_start = base::Time::FromDoubleT(1); |
+ timing.first_layout = first_layout; |
+ GURL url("https://google.com"); |
+ content::MockNavigationHandle test_handle(url); |
+ SetNavigationHandleExpectations(&test_handle); |
+ observer_.DidCommitNavigation(&test_handle); |
+ observer_.OnMessageReceived( |
+ PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), |
+ nullptr); |
+ |
Bryan McQuade
2015/09/09 17:33:28
let's call AssertNoHistogramsLogged here just to c
Charlie Harrison
2015/09/09 19:42:34
Done.
|
+ // Navigate again to force histogram recording. |
+ observer_.DidCommitNavigation(&test_handle); |
+ |
+ histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); |
+ histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
+ first_layout.InMilliseconds(), 1); |
+} |
+ |
+TEST_F(MetricsWebContentsObserverTest, MultipleMetricsAfterCommits) { |
+ base::TimeDelta first_layout_1 = base::TimeDelta::FromMilliseconds(1); |
+ base::TimeDelta first_layout_2 = base::TimeDelta::FromMilliseconds(20); |
+ base::TimeDelta response = base::TimeDelta::FromMilliseconds(10); |
+ base::TimeDelta dom_content = base::TimeDelta::FromMilliseconds(40); |
+ base::TimeDelta load = base::TimeDelta::FromMilliseconds(100); |
+ |
+ PageLoadTiming timing; |
+ timing.navigation_start = base::Time::FromDoubleT(1); |
+ timing.first_layout = first_layout_1; |
+ timing.response_start = response; |
+ timing.dom_content_loaded_event_start = dom_content; |
+ timing.load_event_start = load; |
+ GURL url("https://google.com"); |
+ content::MockNavigationHandle test_handle(url); |
+ SetNavigationHandleExpectations(&test_handle); |
+ observer_.DidCommitNavigation(&test_handle); |
+ observer_.OnMessageReceived( |
+ PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), |
+ nullptr); |
+ |
+ observer_.DidCommitNavigation(&test_handle); |
+ PageLoadTiming timing2; |
+ timing2.navigation_start = base::Time::FromDoubleT(200); |
+ timing2.first_layout = first_layout_2; |
+ observer_.OnMessageReceived( |
+ PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing2), |
+ nullptr); |
+ observer_.DidCommitNavigation(&test_handle); |
+ |
+ histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
+ first_layout_1.InMilliseconds(), 1); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 2); |
+ histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
+ first_layout_1.InMilliseconds(), 1); |
+ histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
+ first_layout_2.InMilliseconds(), 1); |
+ |
+ histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 1); |
+ histogram_tester_.ExpectBucketCount(kHistogramNameDomContent, |
+ dom_content.InMilliseconds(), 1); |
+ |
+ histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 1); |
+ histogram_tester_.ExpectBucketCount(kHistogramNameLoad, load.InMilliseconds(), |
+ 1); |
+} |
+ |
+} // namespace page_load_metrics |