Index: components/page_load_metrics/renderer/page_load_metrics_render_frame_observer_unittest.cc |
diff --git a/components/page_load_metrics/renderer/page_load_metrics_render_frame_observer_unittest.cc b/components/page_load_metrics/renderer/page_load_metrics_render_frame_observer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f6ddc5560971def2348ef179963d1b429f2a05da |
--- /dev/null |
+++ b/components/page_load_metrics/renderer/page_load_metrics_render_frame_observer_unittest.cc |
@@ -0,0 +1,148 @@ |
+// 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/renderer/page_load_metrics_render_frame_observer.h" |
+ |
+#include "base/time/time.h" |
+#include "base/timer/mock_timer.h" |
+#include "components/page_load_metrics/common/page_load_metrics_messages.h" |
+#include "components/page_load_metrics/common/page_load_timing.h" |
+#include "ipc/ipc_message_macros.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using testing::NiceMock; |
+using testing::Return; |
+using testing::StrictMock; |
+ |
+namespace page_load_metrics { |
+ |
+namespace { |
+ |
+// IPC interceptor class, which we use to verify that certain IPC |
+// messages get sent. |
+class MockIPCInterceptor { |
+ public: |
+ void OnMessage(const IPC::Message& message) { |
+ IPC_BEGIN_MESSAGE_MAP(MockIPCInterceptor, message) |
+ IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated) |
+ IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) |
+ IPC_END_MESSAGE_MAP() |
+ } |
+ |
+ MOCK_METHOD1(OnTimingUpdated, void(PageLoadTiming)); |
+}; |
+ |
+// Implementation of the MainRenderFrameObserver class we're testing, |
+// with the GetTiming() and ShouldSendMetrics() methods stubbed out to make |
+// the rest of the class more testable. |
+class MockMainRenderFrameObserver : public MainRenderFrameObserver { |
+ public: |
+ explicit MockMainRenderFrameObserver() : MainRenderFrameObserver(nullptr) { |
Bryan McQuade
2015/09/04 20:53:18
this no longer needs to be explicit (only construc
Charlie Harrison
2015/09/08 23:05:15
Done.
|
+ EXPECT_CALL(*this, ShouldSendMetrics()).WillRepeatedly(Return(true)); |
Bryan McQuade
2015/09/04 20:53:18
let's change this to:
ON_CALL(...).WillByDefault(R
Charlie Harrison
2015/09/08 23:05:15
Done.
|
+ } |
+ |
+ scoped_ptr<base::Timer> CreateTimer() const override { |
+ if (!mock_timer_) |
+ ADD_FAILURE() << "CreateTimer() called, but no MockTimer available."; |
+ return mock_timer_.Pass(); |
+ } |
+ |
+ // We intercept sent messages and dispatch them to a MockIPCInterceptor, which |
+ // we use to verify that the expected IPC messages get sent. |
+ virtual bool Send(IPC::Message* message) { |
+ interceptor_.OnMessage(*message); |
+ // Our base class deletes the message. |
+ return MainRenderFrameObserver::Send(message); |
+ } |
+ |
+ void set_mock_timer(scoped_ptr<base::Timer> timer) { |
+ ASSERT_EQ(nullptr, mock_timer_); |
+ mock_timer_ = timer.Pass(); |
+ } |
+ |
+ MOCK_CONST_METHOD0(GetTiming, PageLoadTiming()); |
+ MOCK_CONST_METHOD0(ShouldSendMetrics, bool()); |
+ MockIPCInterceptor* ipc_interceptor() { return &interceptor_; } |
+ |
+ private: |
+ StrictMock<MockIPCInterceptor> interceptor_; |
+ mutable scoped_ptr<base::Timer> mock_timer_; |
+}; |
+ |
+typedef testing::Test MainRenderFrameObserverTest; |
+ |
+TEST_F(MainRenderFrameObserverTest, NoMetrics) { |
+ MockMainRenderFrameObserver observer; |
+ scoped_ptr<base::MockTimer> mock_timer(new base::MockTimer(false, false)); |
+ observer.set_mock_timer(mock_timer.Pass()); |
+ observer.DidCommitProvisionalLoad(true, false); |
+ |
+ EXPECT_CALL(observer, GetTiming()).WillRepeatedly(Return(PageLoadTiming())); |
+ observer.DidFinishDocumentLoad(); |
+} |
+ |
+TEST_F(MainRenderFrameObserverTest, SingleMetric) { |
+ base::Time nav_start = base::Time::FromDoubleT(10); |
+ base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(10); |
+ |
+ MockMainRenderFrameObserver observer; |
+ scoped_ptr<base::MockTimer> mock_timer(new base::MockTimer(false, false)); |
+ observer.set_mock_timer(mock_timer.Pass()); |
+ observer.DidCommitProvisionalLoad(true, false); |
+ |
+ PageLoadTiming timing; |
+ timing.navigation_start = nav_start; |
+ timing.first_layout = first_layout; |
+ EXPECT_CALL(observer, GetTiming()).WillRepeatedly(Return(timing)); |
+ |
+ // todo |
Bryan McQuade
2015/09/04 20:53:18
sorry - this todo was here to add expectations on
Charlie Harrison
2015/09/08 23:05:15
Oops also an oversight on my part. Fixing all thes
|
+ observer.DidFinishDocumentLoad(); |
+} |
+ |
+TEST_F(MainRenderFrameObserverTest, MultipleMetrics) { |
+ base::Time nav_start = base::Time::FromDoubleT(10); |
+ base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2); |
+ base::TimeDelta dom_event = base::TimeDelta::FromMillisecondsD(2); |
+ base::TimeDelta load_event = base::TimeDelta::FromMillisecondsD(2); |
+ |
+ MockMainRenderFrameObserver observer; |
+ scoped_ptr<base::MockTimer> mock_timer(new base::MockTimer(false, false)); |
+ observer.set_mock_timer(mock_timer.Pass()); |
+ observer.DidCommitProvisionalLoad(true, false); |
+ |
+ PageLoadTiming timing; |
+ timing.navigation_start = nav_start; |
+ timing.first_layout = first_layout; |
+ timing.dom_content_loaded_event_start = dom_event; |
+ EXPECT_CALL(observer, GetTiming()).WillRepeatedly(Return(timing)); |
+ |
+ // todo |
Bryan McQuade
2015/09/04 20:53:18
similar to above
|
+ observer.DidFinishDocumentLoad(); |
+ |
+ // At this point, we should have triggered the generation of two metrics. |
+ // Verify and reset the observer's expectations before moving on to the next |
+ // part of the test. |
+ testing::Mock::VerifyAndClearExpectations(observer.ipc_interceptor()); |
+ |
+ timing.load_event_start = load_event; |
+ EXPECT_CALL(observer, GetTiming()).WillRepeatedly(Return(timing)); |
+ |
+ // todo |
Bryan McQuade
2015/09/04 20:53:18
similar to above
|
+ observer.DidFinishLoad(); |
+ |
+ // Verify and reset the observer's expectations before moving on to the next |
+ // part of the test. |
+ testing::Mock::VerifyAndClearExpectations(observer.ipc_interceptor()); |
+ |
+ // The PageLoadTiming above includes timing information for the first layout, |
+ // dom content, and load metrics. However, since we've already generated |
+ // timing information for all of these metrics previously, we do not expect |
+ // this invocation to generate any additional metrics. |
+ observer.DidFinishLoad(); |
+} |
+ |
+} // namespace |
+ |
+} // namespace page_load_metrics |