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

Side by Side Diff: chrome/browser/page_load_metrics/user_input_tracker_unittest.cc

Issue 2569273002: Add constructors to WebInputEvents and setters so we can work at cleaning up these public structs. (Closed)
Patch Set: Rebase Created 3 years, 11 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/page_load_metrics/user_input_tracker.h" 5 #include "chrome/browser/page_load_metrics/user_input_tracker.h"
6 6
7 #include "base/time/time.h" 7 #include "base/time/time.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/WebKit/public/platform/WebInputEvent.h" 9 #include "third_party/WebKit/public/platform/WebInputEvent.h"
10 10
11 namespace page_load_metrics { 11 namespace page_load_metrics {
12 12
13 namespace { 13 namespace {
14 14
15 // UserInputTracker allows events to be at most 2 seconds old. Thus we use 15 // UserInputTracker allows events to be at most 2 seconds old. Thus we use
16 // 2100ms to make sure the age is greater than 2 seconds. 16 // 2100ms to make sure the age is greater than 2 seconds.
17 const int kTooOldMilliseconds = 2100; 17 const int kTooOldMilliseconds = 2100;
18 18
19 double ToMonotonicallyIncreasingSeconds(base::TimeTicks t) { 19 double ToMonotonicallyIncreasingSeconds(base::TimeTicks t) {
20 return (t - base::TimeTicks()).InSecondsF(); 20 return (t - base::TimeTicks()).InSecondsF();
21 } 21 }
22 22
23 class FakeInputEvent : public blink::WebInputEvent { 23 class FakeInputEvent : public blink::WebInputEvent {
24 public: 24 public:
25 FakeInputEvent() : WebInputEvent(sizeof(FakeInputEvent)) { 25 FakeInputEvent(blink::WebInputEvent::Type type = blink::WebInputEvent::Char,
Lei Zhang 2017/02/24 08:43:23 I'm personally biased again default arguments. You
26 timeStampSeconds = ToMonotonicallyIncreasingSeconds(base::TimeTicks::Now()); 26 int modifiers = blink::WebInputEvent::NoModifiers)
27 type = blink::WebInputEvent::Char; 27 : WebInputEvent(
28 } 28 sizeof(FakeInputEvent),
29 type,
30 modifiers,
31 ToMonotonicallyIncreasingSeconds(base::TimeTicks::Now())) {}
29 32
30 base::TimeTicks GetTimeStamp() { 33 base::TimeTicks GetTimeStamp() {
31 return UserInputTracker::GetEventTime(*this); 34 return UserInputTracker::GetEventTime(*this);
32 } 35 }
33 36
34 base::TimeTicks GetTimeStampRounded() { 37 base::TimeTicks GetTimeStampRounded() {
35 return UserInputTracker::RoundToRateLimitedOffset(GetTimeStamp()); 38 return UserInputTracker::RoundToRateLimitedOffset(GetTimeStamp());
36 } 39 }
37 }; 40 };
38 41
(...skipping 28 matching lines...) Expand all
67 EXPECT_EQ(base::TimeTicks(), 70 EXPECT_EQ(base::TimeTicks(),
68 tracker.FindMostRecentUserInputEventBefore(after)); 71 tracker.FindMostRecentUserInputEventBefore(after));
69 } 72 }
70 73
71 TEST_F(UserInputTrackerTest, MultipleEvents) { 74 TEST_F(UserInputTrackerTest, MultipleEvents) {
72 FakeInputEvent e1; 75 FakeInputEvent e1;
73 FakeInputEvent e2; 76 FakeInputEvent e2;
74 77
75 // Make sure that the two events are monotonically increasing, and that both 78 // Make sure that the two events are monotonically increasing, and that both
76 // are in the past. 79 // are in the past.
77 e1.timeStampSeconds = ToMonotonicallyIncreasingSeconds( 80 e1.setTimeStampSeconds(ToMonotonicallyIncreasingSeconds(
78 e2.GetTimeStamp() - base::TimeDelta::FromMilliseconds(100)); 81 e2.GetTimeStamp() - base::TimeDelta::FromMilliseconds(100)));
79 82
80 base::TimeTicks after = 83 base::TimeTicks after =
81 e2.GetTimeStampRounded() + base::TimeDelta::FromMicroseconds(1); 84 e2.GetTimeStampRounded() + base::TimeDelta::FromMicroseconds(1);
82 85
83 { 86 {
84 UserInputTracker tracker; 87 UserInputTracker tracker;
85 tracker.OnInputEvent(e1); 88 tracker.OnInputEvent(e1);
86 tracker.OnInputEvent(e2); 89 tracker.OnInputEvent(e2);
87 90
88 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore( 91 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore(
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 tracker.FindMostRecentUserInputEventBefore(after)); 127 tracker.FindMostRecentUserInputEventBefore(after));
125 } 128 }
126 } 129 }
127 130
128 TEST_F(UserInputTrackerTest, IgnoreEventsOlderThanConsumed) { 131 TEST_F(UserInputTrackerTest, IgnoreEventsOlderThanConsumed) {
129 FakeInputEvent e1; 132 FakeInputEvent e1;
130 FakeInputEvent e2; 133 FakeInputEvent e2;
131 134
132 // Make sure that the two events are monotonically increasing, and that both 135 // Make sure that the two events are monotonically increasing, and that both
133 // are in the past. 136 // are in the past.
134 e1.timeStampSeconds = ToMonotonicallyIncreasingSeconds( 137 e1.setTimeStampSeconds(ToMonotonicallyIncreasingSeconds(
135 e2.GetTimeStamp() - base::TimeDelta::FromMilliseconds(100)); 138 e2.GetTimeStamp() - base::TimeDelta::FromMilliseconds(100)));
136 139
137 base::TimeTicks after = 140 base::TimeTicks after =
138 e2.GetTimeStampRounded() + base::TimeDelta::FromMicroseconds(1); 141 e2.GetTimeStampRounded() + base::TimeDelta::FromMicroseconds(1);
139 142
140 UserInputTracker tracker; 143 UserInputTracker tracker;
141 tracker.OnInputEvent(e2); 144 tracker.OnInputEvent(e2);
142 145
143 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore( 146 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore(
144 e1.GetTimeStampRounded())); 147 e1.GetTimeStampRounded()));
145 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore( 148 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore(
(...skipping 11 matching lines...) Expand all
157 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore( 160 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore(
158 e1.GetTimeStampRounded() + 161 e1.GetTimeStampRounded() +
159 base::TimeDelta::FromMicroseconds(1))); 162 base::TimeDelta::FromMicroseconds(1)));
160 } 163 }
161 164
162 TEST_F(UserInputTrackerTest, ExcludeOldEvents) { 165 TEST_F(UserInputTrackerTest, ExcludeOldEvents) {
163 UserInputTracker tracker; 166 UserInputTracker tracker;
164 FakeInputEvent e1; 167 FakeInputEvent e1;
165 FakeInputEvent e2; 168 FakeInputEvent e2;
166 // make sure e1 is too old to be considered. 169 // make sure e1 is too old to be considered.
167 e1.timeStampSeconds = ToMonotonicallyIncreasingSeconds( 170 e1.setTimeStampSeconds(ToMonotonicallyIncreasingSeconds(
168 e2.GetTimeStamp() - 171 e2.GetTimeStamp() -
169 base::TimeDelta::FromMilliseconds(kTooOldMilliseconds)); 172 base::TimeDelta::FromMilliseconds(kTooOldMilliseconds)));
170 173
171 tracker.OnInputEvent(e1); 174 tracker.OnInputEvent(e1);
172 tracker.OnInputEvent(e2); 175 tracker.OnInputEvent(e2);
173 176
174 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore( 177 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore(
175 e1.GetTimeStampRounded() + 178 e1.GetTimeStampRounded() +
176 base::TimeDelta::FromMilliseconds(1))); 179 base::TimeDelta::FromMilliseconds(1)));
177 EXPECT_EQ(base::TimeTicks(), 180 EXPECT_EQ(base::TimeTicks(),
178 tracker.FindMostRecentUserInputEventBefore( 181 tracker.FindMostRecentUserInputEventBefore(
179 e2.GetTimeStampRounded() + 182 e2.GetTimeStampRounded() +
(...skipping 11 matching lines...) Expand all
191 const size_t kTooManyEntries = UserInputTracker::kMaxTrackedEvents * 5; 194 const size_t kTooManyEntries = UserInputTracker::kMaxTrackedEvents * 5;
192 195
193 UserInputTracker tracker; 196 UserInputTracker tracker;
194 FakeInputEvent e; 197 FakeInputEvent e;
195 198
196 // UserInputTracker DCHECKs that event timestamps aren't after the current 199 // UserInputTracker DCHECKs that event timestamps aren't after the current
197 // time, so to be safe, we use a starting timestamp that is twice 200 // time, so to be safe, we use a starting timestamp that is twice
198 // kTooManyEntries milliseconds in the past, and then synthesize one event for 201 // kTooManyEntries milliseconds in the past, and then synthesize one event for
199 // each of kTooManyEntries after this start point. This guarantees that all 202 // each of kTooManyEntries after this start point. This guarantees that all
200 // events are in the past. 203 // events are in the past.
201 e.timeStampSeconds = ToMonotonicallyIncreasingSeconds( 204 e.setTimeStampSeconds(ToMonotonicallyIncreasingSeconds(
202 e.GetTimeStamp() - 205 e.GetTimeStamp() -
203 base::TimeDelta::FromMilliseconds(kTooManyEntries * 2)); 206 base::TimeDelta::FromMilliseconds(kTooManyEntries * 2)));
204 207
205 // Insert more than kMaxEntries entries. The rate limiting logic should 208 // Insert more than kMaxEntries entries. The rate limiting logic should
206 // prevent more than kMaxEntries entries from actually being inserted. A 209 // prevent more than kMaxEntries entries from actually being inserted. A
207 // DCHECK in OnInputEvent verifies that we don't exceed the expected capacity. 210 // DCHECK in OnInputEvent verifies that we don't exceed the expected capacity.
208 for (size_t i = 0; i < kTooManyEntries; ++i) { 211 for (size_t i = 0; i < kTooManyEntries; ++i) {
209 tracker.OnInputEvent(e); 212 tracker.OnInputEvent(e);
210 e.timeStampSeconds += base::TimeDelta::FromMilliseconds(1).InSecondsF(); 213 e.setTimeStampSeconds(e.timeStampSeconds +
214 base::TimeDelta::FromMilliseconds(1).InSecondsF());
211 } 215 }
212 216
213 // Do a basic sanity check to make sure we can find events in the tracker. 217 // Do a basic sanity check to make sure we can find events in the tracker.
214 EXPECT_NE(base::TimeTicks(), 218 EXPECT_NE(base::TimeTicks(),
215 tracker.FindMostRecentUserInputEventBefore(base::TimeTicks::Now())); 219 tracker.FindMostRecentUserInputEventBefore(base::TimeTicks::Now()));
216 } 220 }
217 221
218 TEST_F(UserInputTrackerTest, IgnoredEventType) { 222 TEST_F(UserInputTrackerTest, IgnoredEventType) {
219 UserInputTracker tracker; 223 UserInputTracker tracker;
220 FakeInputEvent e; 224 FakeInputEvent e(blink::WebInputEvent::MouseMove);
221 e.type = blink::WebInputEvent::MouseMove;
222 tracker.OnInputEvent(e); 225 tracker.OnInputEvent(e);
223 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore( 226 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore(
224 e.GetTimeStampRounded() + 227 e.GetTimeStampRounded() +
225 base::TimeDelta::FromMilliseconds(1))); 228 base::TimeDelta::FromMilliseconds(1)));
226 } 229 }
227 230
228 TEST_F(UserInputTrackerTest, IgnoreRepeatEvents) { 231 TEST_F(UserInputTrackerTest, IgnoreRepeatEvents) {
229 UserInputTracker tracker; 232 UserInputTracker tracker;
230 FakeInputEvent e; 233 FakeInputEvent e(blink::WebInputEvent::Char,
231 e.modifiers |= blink::WebInputEvent::IsAutoRepeat; 234 blink::WebInputEvent::IsAutoRepeat);
232 tracker.OnInputEvent(e); 235 tracker.OnInputEvent(e);
233 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore( 236 EXPECT_EQ(base::TimeTicks(), tracker.FindMostRecentUserInputEventBefore(
234 e.GetTimeStampRounded() + 237 e.GetTimeStampRounded() +
235 base::TimeDelta::FromMilliseconds(1))); 238 base::TimeDelta::FromMilliseconds(1)));
236 } 239 }
237 240
238 } // namespace page_load_metrics 241 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698