Chromium Code Reviews| 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..6eeb126ba06eac76079284e5daeaa8fd5da97e23 |
| --- /dev/null |
| +++ b/components/page_load_metrics/renderer/page_load_metrics_render_frame_observer_unittest.cc |
| @@ -0,0 +1,154 @@ |
| +// 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 TestMainRenderFrameObserver : public MainRenderFrameObserver { |
| + public: |
| + explicit TestMainRenderFrameObserver() : MainRenderFrameObserver(nullptr) {} |
| + |
| + PageLoadTiming GetTiming() const override { return timing_; } |
| + |
| + 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); |
| + } |
| + |
| + // The production ShouldSendMetrics implementation depends on content and |
| + // Blink. In order to reduce dependencies for this unit test, we stub out |
| + // this method to always return true. |
| + bool ShouldSendMetrics() const override { return true; } |
| + |
| + void set_timing(const PageLoadTiming& timing) { timing_ = timing; } |
|
Bryan McQuade
2015/09/02 18:57:09
i realize i wrote this, but reviewing the code now
Charlie Harrison
2015/09/03 14:00:53
Will do.
|
| + |
| + void set_mock_timer(scoped_ptr<base::Timer> timer) { |
|
Bryan McQuade
2015/09/02 18:57:09
same - seems this should be very gmockable, but ma
Charlie Harrison
2015/09/03 14:00:53
Gmock expects everything to be copyable, and scope
|
| + ASSERT_EQ(nullptr, mock_timer_); |
| + mock_timer_ = timer.Pass(); |
| + } |
| + |
| + MockIPCInterceptor* ipc_interceptor() { return &interceptor_; } |
| + |
| + private: |
| + StrictMock<MockIPCInterceptor> interceptor_; |
| + PageLoadTiming timing_; |
| + mutable scoped_ptr<base::Timer> mock_timer_; |
| +}; |
| + |
| +typedef testing::Test MainRenderFrameObserverTest; |
| + |
| +TEST_F(MainRenderFrameObserverTest, NoMetrics) { |
| + TestMainRenderFrameObserver observer; |
| + scoped_ptr<base::MockTimer> mock_timer(new base::MockTimer(false, false)); |
| + observer.set_mock_timer(mock_timer.Pass()); |
| + observer.DidCommitProvisionalLoad(true, false); |
| + |
| + observer.set_timing(PageLoadTiming()); |
| + observer.DidFinishDocumentLoad(); |
| +} |
| + |
| +TEST_F(MainRenderFrameObserverTest, SingleMetric) { |
| + base::Time nav_start = base::Time::FromDoubleT(10); |
| + base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(10); |
| + |
| + TestMainRenderFrameObserver 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; |
| + observer.set_timing(timing); |
| + |
| + // todo |
| + 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); |
| + |
| + TestMainRenderFrameObserver 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; |
| + observer.set_timing(timing); |
| + |
| + // todo |
| + 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; |
| + observer.set_timing(timing); |
| + |
| + // todo |
| + 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 |