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..73fa6c6ab86bf7afe0bd4e072c002f062af349a2 |
--- /dev/null |
+++ b/components/page_load_metrics/browser/page_load_metrics_web_contents_observer_unittest.cc |
@@ -0,0 +1,171 @@ |
+// 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 "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using testing::Return; |
+using testing::ReturnRef; |
+ |
+namespace page_load_metrics { |
+ |
+class MockWebContentsObserver : public WebContentsObserver { |
+ public: |
+ MockWebContentsObserver() |
+ : WebContentsObserver(nullptr) {} |
+ MOCK_METHOD0(GetLastCommittedURL, const GURL&()); |
+}; |
+ |
+class MockNavigationHandle : public content::NavigationHandle { |
+ public: |
+ MOCK_CONST_METHOD0(GetURL, const GURL&()); |
+ MOCK_CONST_METHOD0(IsInMainFrame, bool()); |
+ MOCK_CONST_METHOD0(HasCommittedDocument, bool()); |
+ MOCK_CONST_METHOD0(HasCommittedErrorPage, bool()); |
+ MOCK_CONST_METHOD0(GetNetErrorCode, net::Error()); |
+}; |
+ |
+class WebContentsObserverTest : public testing::Test { |
+ public: |
+ const char* kHistogramNameFirstLayout = "PageLoad.Timing.FirstLayout"; |
+ const char* kHistogramNameDomContent = |
+ "PageLoad.Timing.DOMContentLoadedEventFired"; |
+ const char* kHistogramNameLoad = "PageLoad.Timing.LoadEventFired"; |
+ |
+ WebContentsObserverTest() : observer_() {} |
+ |
+ protected: |
+ base::HistogramTester histogram_tester_; |
+ MockWebContentsObserver observer_; |
+}; |
+ |
+TEST_F(WebContentsObserverTest, NoMetrics) { |
+ histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
+} |
+ |
+TEST_F(WebContentsObserverTest, NotInMainFrame) { |
+ base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
+ |
+ PageLoadTiming timing; |
+ timing.first_layout = first_layout; |
+ GURL url("https://google.com"); |
+ EXPECT_CALL(observer_, GetLastCommittedURL()).WillRepeatedly( |
+ ReturnRef(url)); |
+ MockNavigationHandle test_handle; |
+ EXPECT_CALL(test_handle, GetURL()).WillRepeatedly( |
+ ReturnRef(url)); |
+ EXPECT_CALL(test_handle, IsInMainFrame()).WillRepeatedly( |
+ Return(true)); |
+ EXPECT_CALL(test_handle, HasCommittedDocument()).WillRepeatedly( |
+ Return(true)); |
+ |
+ // Navigate again to force histogram recording. |
+ observer_.DidCommitNavigation(&test_handle); |
+ |
+ histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 0); |
+} |
+ |
+TEST_F(WebContentsObserverTest, SingleMetricAfterCommit) { |
+ base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
+ |
+ PageLoadTiming timing; |
+ timing.first_layout = first_layout; |
+ GURL url("https://google.com"); |
+ EXPECT_CALL(observer_, GetLastCommittedURL()).WillRepeatedly( |
+ ReturnRef(url)); |
+ MockNavigationHandle test_handle; |
+ EXPECT_CALL(test_handle, GetURL()).WillRepeatedly( |
+ ReturnRef(url)); |
+ EXPECT_CALL(test_handle, IsInMainFrame()).WillRepeatedly( |
+ Return(true)); |
+ EXPECT_CALL(test_handle, HasCommittedDocument()).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); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
+ histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 1); |
+ histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
+ first_layout.InMilliseconds(), 1); |
+} |
+ |
+TEST_F(WebContentsObserverTest, SingleMetricProcessKilled) { |
+ base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
+ |
+ PageLoadTiming timing; |
+ timing.first_layout = first_layout; |
+ GURL url("https://google.com"); |
+ EXPECT_CALL(observer_, GetLastCommittedURL()).WillRepeatedly( |
+ ReturnRef(url)); |
+ observer_.OnMessageReceived( |
+ PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), |
+ nullptr); |
+ observer_.RenderProcessGone(base::TERMINATION_STATUS_NORMAL_TERMINATION); |
+ |
+ 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(WebContentsObserverTest, MultipleMetrics) { |
+ base::TimeDelta first_layout_1 = base::TimeDelta::FromMilliseconds(1); |
+ base::TimeDelta first_layout_2 = base::TimeDelta::FromMilliseconds(20); |
+ base::TimeDelta dom_content = base::TimeDelta::FromMilliseconds(40); |
+ base::TimeDelta load = base::TimeDelta::FromMilliseconds(100); |
+ |
+ PageLoadTiming timing; |
+ timing.first_layout = first_layout_1; |
+ timing.dom_content_loaded_event_start = dom_content; |
+ timing.load_event_start = load; |
+ GURL url("https://google.com"); |
+ EXPECT_CALL(observer_, GetLastCommittedURL()).WillRepeatedly( |
+ ReturnRef(url)); |
+ observer_.OnMessageReceived( |
+ PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), |
+ nullptr); |
+ |
+ observer_.RenderProcessGone(base::TERMINATION_STATUS_NORMAL_TERMINATION); |
+ PageLoadTiming timing2; |
+ timing2.first_layout = first_layout_2; |
+ observer_.OnMessageReceived( |
+ PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing2), |
+ nullptr); |
+ observer_.RenderProcessGone(base::TERMINATION_STATUS_NORMAL_TERMINATION); |
+ |
+ 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 |