Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: components/page_load_metrics/renderer/metrics_render_frame_observer_unittest.cc

Issue 1312213010: PageLoadMetrics renderer and browser implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Relax DCHECK constraints slightly for tests Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/page_load_metrics/renderer/metrics_render_frame_observer.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/time/time.h"
9 #include "base/timer/mock_timer.h"
10 #include "components/page_load_metrics/common/page_load_metrics_messages.h"
11 #include "components/page_load_metrics/common/page_load_timing.h"
12 #include "ipc/ipc_message_macros.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using testing::NiceMock;
17 using testing::Return;
18 using testing::StrictMock;
19
20 namespace page_load_metrics {
21
22 // IPC interceptor class, which we use to verify that certain IPC
23 // messages get sent.
24 class MockIPCInterceptor {
25 public:
26 void OnMessage(const IPC::Message& message) {
27 IPC_BEGIN_MESSAGE_MAP(MockIPCInterceptor, message)
28 IPC_MESSAGE_HANDLER(PageLoadMetricsMsg_TimingUpdated, OnTimingUpdated)
29 IPC_MESSAGE_UNHANDLED(ADD_FAILURE())
30 IPC_END_MESSAGE_MAP()
31 }
32
33 MOCK_METHOD1(OnTimingUpdated, void(PageLoadTiming));
34 };
35
36 // Implementation of the MetricsRenderFrameObserver class we're testing,
37 // with the GetTiming() and ShouldSendMetrics() methods stubbed out to make
38 // the rest of the class more testable.
39 class MockMetricsRenderFrameObserver : public MetricsRenderFrameObserver {
40 public:
41 MockMetricsRenderFrameObserver() : MetricsRenderFrameObserver(nullptr) {
42 ON_CALL(*this, ShouldSendMetrics()).WillByDefault(Return(true));
43 }
44
45 scoped_ptr<base::Timer> CreateTimer() const override {
46 if (!mock_timer_)
47 ADD_FAILURE() << "CreateTimer() called, but no MockTimer available.";
48 return mock_timer_.Pass();
49 }
50
51 // We intercept sent messages and dispatch them to a MockIPCInterceptor, which
52 // we use to verify that the expected IPC messages get sent.
53 virtual bool Send(IPC::Message* message) {
54 interceptor_.OnMessage(*message);
55 delete message;
56 return true;
57 }
58
59 void set_mock_timer(scoped_ptr<base::Timer> timer) {
60 ASSERT_EQ(nullptr, mock_timer_);
61 mock_timer_ = timer.Pass();
62 }
63
64 MOCK_CONST_METHOD0(GetTiming, PageLoadTiming());
65 MOCK_CONST_METHOD0(ShouldSendMetrics, bool());
66 MockIPCInterceptor* ipc_interceptor() { return &interceptor_; }
67
68 private:
69 StrictMock<MockIPCInterceptor> interceptor_;
70 mutable scoped_ptr<base::Timer> mock_timer_;
71 };
72
73 typedef testing::Test MetricsRenderFrameObserverTest;
74
75 TEST_F(MetricsRenderFrameObserverTest, NoMetrics) {
76 NiceMock<MockMetricsRenderFrameObserver> observer;
77 base::MockTimer* mock_timer = new base::MockTimer(false, false);
78 observer.set_mock_timer(make_scoped_ptr(mock_timer));
79 observer.DidCommitProvisionalLoad(true, false);
80
81 EXPECT_CALL(observer, GetTiming()).WillRepeatedly(Return(PageLoadTiming()));
82 observer.DidChangePerformanceTiming();
83 ASSERT_FALSE(mock_timer->IsRunning());
84 }
85
86 TEST_F(MetricsRenderFrameObserverTest, SingleMetric) {
87 base::Time nav_start = base::Time::FromDoubleT(10);
88 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(10);
89
90 NiceMock<MockMetricsRenderFrameObserver> observer;
91 base::MockTimer* mock_timer = new base::MockTimer(false, false);
92 observer.set_mock_timer(make_scoped_ptr(mock_timer));
93 observer.DidCommitProvisionalLoad(true, false);
94
95 PageLoadTiming timing;
96 timing.navigation_start = nav_start;
97 timing.first_layout = first_layout;
98 EXPECT_CALL(observer, GetTiming()).WillRepeatedly(Return(timing));
99
100 EXPECT_CALL(*observer.ipc_interceptor(), OnTimingUpdated(timing));
101
102 observer.DidChangePerformanceTiming();
103 mock_timer->Fire();
104 }
105
106 TEST_F(MetricsRenderFrameObserverTest, MultipleMetrics) {
107 base::Time nav_start = base::Time::FromDoubleT(10);
108 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2);
109 base::TimeDelta dom_event = base::TimeDelta::FromMillisecondsD(2);
110 base::TimeDelta load_event = base::TimeDelta::FromMillisecondsD(2);
111
112 NiceMock<MockMetricsRenderFrameObserver> observer;
113 base::MockTimer* mock_timer = new base::MockTimer(false, false);
114 observer.set_mock_timer(make_scoped_ptr(mock_timer));
115 observer.DidCommitProvisionalLoad(true, false);
116
117 PageLoadTiming timing;
118 timing.navigation_start = nav_start;
119 timing.first_layout = first_layout;
120 timing.dom_content_loaded_event_start = dom_event;
121 EXPECT_CALL(observer, GetTiming()).WillRepeatedly(Return(timing));
122
123 EXPECT_CALL(*observer.ipc_interceptor(), OnTimingUpdated(timing));
124 observer.DidChangePerformanceTiming();
125 mock_timer->Fire();
126
127 // At this point, we should have triggered the generation of two metrics.
128 // Verify and reset the observer's expectations before moving on to the next
129 // part of the test.
130 testing::Mock::VerifyAndClearExpectations(observer.ipc_interceptor());
131
132 timing.load_event_start = load_event;
133 EXPECT_CALL(observer, GetTiming()).WillRepeatedly(Return(timing));
134
135 EXPECT_CALL(*observer.ipc_interceptor(), OnTimingUpdated(timing));
136 observer.DidChangePerformanceTiming();
137 mock_timer->Fire();
138
139 // Verify and reset the observer's expectations before moving on to the next
140 // part of the test.
141 testing::Mock::VerifyAndClearExpectations(observer.ipc_interceptor());
142
143 // The PageLoadTiming above includes timing information for the first layout,
144 // dom content, and load metrics. However, since we've already generated
145 // timing information for all of these metrics previously, we do not expect
146 // this invocation to generate any additional metrics.
147 observer.DidChangePerformanceTiming();
148 ASSERT_FALSE(mock_timer->IsRunning());
149 }
150
151 TEST_F(MetricsRenderFrameObserverTest, MultipleNavigations) {
152 base::Time nav_start = base::Time::FromDoubleT(10);
153 base::TimeDelta first_layout = base::TimeDelta::FromMillisecondsD(2);
154 base::TimeDelta dom_event = base::TimeDelta::FromMillisecondsD(2);
155 base::TimeDelta load_event = base::TimeDelta::FromMillisecondsD(2);
156
157 NiceMock<MockMetricsRenderFrameObserver> observer;
158 base::MockTimer* mock_timer = new base::MockTimer(false, false);
159 observer.set_mock_timer(make_scoped_ptr(mock_timer));
160 observer.DidCommitProvisionalLoad(true, false);
161
162 PageLoadTiming timing;
163 timing.navigation_start = nav_start;
164 timing.first_layout = first_layout;
165 timing.dom_content_loaded_event_start = dom_event;
166 timing.load_event_start = load_event;
167 EXPECT_CALL(observer, GetTiming()).WillRepeatedly(Return(timing));
168 EXPECT_CALL(*observer.ipc_interceptor(), OnTimingUpdated(timing));
169 observer.DidChangePerformanceTiming();
170 mock_timer->Fire();
171
172 // At this point, we should have triggered the generation of two metrics.
173 // Verify and reset the observer's expectations before moving on to the next
174 // part of the test.
175 testing::Mock::VerifyAndClearExpectations(observer.ipc_interceptor());
176
177 base::Time nav_start_2 = base::Time::FromDoubleT(100);
178 base::TimeDelta first_layout_2 = base::TimeDelta::FromMillisecondsD(20);
179 base::TimeDelta dom_event_2 = base::TimeDelta::FromMillisecondsD(20);
180 base::TimeDelta load_event_2 = base::TimeDelta::FromMillisecondsD(20);
181 PageLoadTiming timing_2;
182 timing_2.navigation_start = nav_start_2;
183 timing_2.first_layout = first_layout_2;
184 timing_2.dom_content_loaded_event_start = dom_event_2;
185 timing_2.load_event_start = load_event_2;
186
187 base::MockTimer* mock_timer2 = new base::MockTimer(false, false);
188 observer.set_mock_timer(make_scoped_ptr(mock_timer2));
189 observer.DidCommitProvisionalLoad(true, false);
190 EXPECT_CALL(*observer.ipc_interceptor(), OnTimingUpdated(timing));
191 observer.DidChangePerformanceTiming();
192 mock_timer2->Fire();
193 }
194
195 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698