Chromium Code Reviews| 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..dc47c488be91dc07e2b83dc617ca1a69d76eb52a |
| --- /dev/null |
| +++ b/components/page_load_metrics/browser/page_load_metrics_web_contents_observer_unittest.cc |
| @@ -0,0 +1,200 @@ |
| +// 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" |
| + |
| +namespace page_load_metrics { |
| + |
| +class MockWebContentsObserver : public WebContentsObserver { |
| + public: |
| + MockWebContentsObserver(std::string url) |
| + : WebContentsObserver(nullptr), url_(GURL(url)) {} |
| + const GURL& GetLastCommittedURL() override { return url_; } |
|
Bryan McQuade
2015/09/02 18:47:08
you might consider using gmock for this.
gmock al
Charlie Harrison
2015/09/03 14:00:52
Done.
|
| + void SetLastCommittedURL(std::string url) { url_ = GURL(url); } |
| + |
| + private: |
| + GURL url_; |
| +}; |
| + |
| +class MockNavigationHandle : public content::NavigationHandle { |
| + public: |
| + MockNavigationHandle(const GURL& url, const bool is_main_frame) |
| + : is_main_frame_(is_main_frame), |
| + has_committed_document_(false), |
| + url_(url) {} |
| + const GURL& GetURL() const override { return url_; } |
|
Bryan McQuade
2015/09/02 18:47:08
these also seem like good candidates for gmock.
Charlie Harrison
2015/09/03 14:00:52
Done.
|
| + bool IsInMainFrame() const override { return is_main_frame_; } |
|
Bryan McQuade
2015/09/02 18:47:08
can use testing::Return() to set a gmock expectati
Charlie Harrison
2015/09/03 14:00:52
Done.
|
| + bool HasCommittedDocument() const override { return has_committed_document_; } |
|
Bryan McQuade
2015/09/02 18:47:08
same
Charlie Harrison
2015/09/03 14:00:52
Done.
|
| + void SetCommittedDocument(bool committed) { |
| + has_committed_document_ = committed; |
| + } |
| + MOCK_CONST_METHOD0(HasCommittedErrorPage, bool()); |
| + MOCK_CONST_METHOD0(GetNetErrorCode, net::Error()); |
| + |
| + private: |
| + bool is_main_frame_; |
| + bool has_committed_document_; |
| + GURL url_; |
| +}; |
| + |
| +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_("https://google.com") {} |
| + |
| + 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, PendingCommit) { |
| + PageLoadTiming timing; |
| + timing.navigation_start = base::Time::FromDoubleT(5); |
| + timing.first_layout = base::TimeDelta::FromMilliseconds(1); |
| + MockNavigationHandle test_handle(GURL("https://google.com"), true); |
| + test_handle.SetCommittedDocument(true); |
| + observer_.DidCommitNavigation(&test_handle); |
| + observer_.OnMessageReceived( |
| + PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), timing), |
| + nullptr); |
| + |
| + PageLoadTiming pending_timing; |
| + pending_timing.navigation_start = base::Time::FromDoubleT(10); |
| + pending_timing.first_layout = base::TimeDelta::FromMillisecondsD(200); |
| + observer_.OnMessageReceived( |
| + PageLoadMetricsMsg_TimingUpdated(observer_.routing_id(), pending_timing), |
| + nullptr); |
| + // Navigate again to force histogram recording. |
| + observer_.DidCommitNavigation(&test_handle); |
| + // Navigate once more to force the pending timings to be recorded. |
| + observer_.DidCommitNavigation(&test_handle); |
| + |
| + histogram_tester_.ExpectTotalCount(kHistogramNameFirstLayout, 2); |
| + histogram_tester_.ExpectBucketCount(kHistogramNameFirstLayout, |
| + timing.first_layout.InMilliseconds(), 1); |
| + histogram_tester_.ExpectBucketCount( |
| + kHistogramNameFirstLayout, pending_timing.first_layout.InMilliseconds(), |
| + 1); |
| + |
| + histogram_tester_.ExpectTotalCount(kHistogramNameDomContent, 0); |
| + |
| + histogram_tester_.ExpectTotalCount(kHistogramNameLoad, 0); |
| +} |
| + |
| +TEST_F(WebContentsObserverTest, NotInMainFrame) { |
| + base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| + |
| + PageLoadTiming timing; |
| + timing.first_layout = first_layout; |
| + MockNavigationHandle test_handle(GURL("https://google.com"), false); |
| + test_handle.SetCommittedDocument(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, 0); |
| +} |
| + |
| +TEST_F(WebContentsObserverTest, SingleMetricAfterCommit) { |
| + base::TimeDelta first_layout = base::TimeDelta::FromMilliseconds(1); |
| + |
| + PageLoadTiming timing; |
| + timing.first_layout = first_layout; |
| + MockNavigationHandle test_handle(GURL("https://google.com"), true); |
| + test_handle.SetCommittedDocument(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; |
| + 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; |
| + 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 |