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

Side by Side Diff: components/scheduler/renderer/user_model_unittest.cc

Issue 1683583002: Report user actions when gesture starts and stops in user_model. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed nit. Created 4 years, 9 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/scheduler/renderer/user_model.h" 5 #include "components/scheduler/renderer/user_model.h"
6 6
7 #include "base/metrics/field_trial.h"
8 #include "base/metrics/user_metrics.h"
9 #include "base/metrics/user_metrics_action.h"
7 #include "base/test/simple_test_tick_clock.h" 10 #include "base/test/simple_test_tick_clock.h"
11 #include "base/test/test_simple_task_runner.h"
8 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
10 14
11 namespace scheduler { 15 namespace scheduler {
12 16
13 class UserModelTest : public testing::Test { 17 class UserModelTest : public testing::Test {
14 public: 18 public:
15 UserModelTest() {} 19 UserModelTest() : mock_task_runner_(new base::TestSimpleTaskRunner()) {}
16 ~UserModelTest() override {} 20 ~UserModelTest() override { base::RemoveActionCallback(action_callback_); }
17 21
18 void SetUp() override { 22 void SetUp() override {
19 clock_.reset(new base::SimpleTestTickClock()); 23 clock_.reset(new base::SimpleTestTickClock());
20 clock_->Advance(base::TimeDelta::FromMicroseconds(5000)); 24 clock_->Advance(base::TimeDelta::FromMicroseconds(5000));
21 25
22 user_model_.reset(new UserModel()); 26 user_model_.reset(new UserModel(mock_task_runner_));
27
28 // Register a mock user action callback for testing.
29 num_actions_recorded_ = 0;
30 last_action_recorded_ = "";
31 base::RemoveActionCallback(action_callback_);
Ilya Sherman 2016/03/21 08:09:44 Why is this line needed here? Seems like somethin
beaudoin 2016/03/23 02:46:42 SetUp is the standard ways tests are initialized a
32 action_callback_ = base::Bind(&UserModelTest::RecordComputedAction,
33 base::Unretained(this));
34 base::AddActionCallback(action_callback_);
23 } 35 }
24 36
25 protected: 37 protected:
26 static base::TimeDelta priority_escalation_after_input_duration() { 38 static base::TimeDelta priority_escalation_after_input_duration() {
27 return base::TimeDelta::FromMilliseconds( 39 return base::TimeDelta::FromMilliseconds(
28 UserModel::kGestureEstimationLimitMillis); 40 UserModel::kGestureEstimationLimitMillis);
29 } 41 }
30 42
31 static base::TimeDelta subsequent_input_expected_after_input_duration() { 43 static base::TimeDelta subsequent_input_expected_after_input_duration() {
32 return base::TimeDelta::FromMilliseconds( 44 return base::TimeDelta::FromMilliseconds(
33 UserModel::kExpectSubsequentGestureMillis); 45 UserModel::kExpectSubsequentGestureMillis);
34 } 46 }
35 47
48 void RecordComputedAction(const std::string& action) {
Ilya Sherman 2016/03/21 08:09:44 nit: What does the "Computed" part of this name re
beaudoin 2016/03/23 02:46:41 Gone with UserActionTester.
49 num_actions_recorded_++;
Ilya Sherman 2016/03/21 08:09:44 nit: preincrement
beaudoin 2016/03/23 02:46:42 Gone.
50 last_action_recorded_ = action;
51 }
52
36 scoped_ptr<base::SimpleTestTickClock> clock_; 53 scoped_ptr<base::SimpleTestTickClock> clock_;
37 scoped_ptr<UserModel> user_model_; 54 scoped_ptr<UserModel> user_model_;
55 scoped_refptr<base::TestSimpleTaskRunner> mock_task_runner_;
56 base::ActionCallback action_callback_;
57
58 // Tracks user actions that were recorded.
59 int num_actions_recorded_;
60 std::string last_action_recorded_;
38 }; 61 };
39 62
40 TEST_F(UserModelTest, TimeLeftInUserGesture_NoInput) { 63 TEST_F(UserModelTest, TimeLeftInUserGesture_NoInput) {
41 EXPECT_EQ(base::TimeDelta(), 64 EXPECT_EQ(base::TimeDelta(),
42 user_model_->TimeLeftInUserGesture(clock_->NowTicks())); 65 user_model_->TimeLeftInUserGesture(clock_->NowTicks()));
43 } 66 }
44 67
45 TEST_F(UserModelTest, TimeLeftInUserGesture_ImmediatelyAfterInput) { 68 TEST_F(UserModelTest, TimeLeftInUserGesture_ImmediatelyAfterInput) {
46 user_model_->DidStartProcessingInputEvent( 69 user_model_->DidStartProcessingInputEvent(
47 blink::WebInputEvent::Type::TouchStart, clock_->NowTicks()); 70 blink::WebInputEvent::Type::TouchStart, clock_->NowTicks());
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 253
231 base::TimeDelta prediction_valid_duration; 254 base::TimeDelta prediction_valid_duration;
232 EXPECT_TRUE(user_model_->IsGestureExpectedToContinue( 255 EXPECT_TRUE(user_model_->IsGestureExpectedToContinue(
233 clock_->NowTicks(), &prediction_valid_duration)); 256 clock_->NowTicks(), &prediction_valid_duration));
234 EXPECT_EQ(base::TimeDelta::FromMilliseconds( 257 EXPECT_EQ(base::TimeDelta::FromMilliseconds(
235 UserModel::kMedianGestureDurationMillis) - 258 UserModel::kMedianGestureDurationMillis) -
236 delta, 259 delta,
237 prediction_valid_duration); 260 prediction_valid_duration);
238 } 261 }
239 262
240 TEST_F(UserModelTest, IsGestureExpectedToContinue_LongAfterGestureStarted) { 263 TEST_F(UserModelTest, AreActionsLogged) {
264 // Add the action logging feature.
265 base::FeatureList::ClearInstanceForTesting();
266 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList);
267 feature_list->InitializeFromCommandLine(features::kRecordGestureAction.name,
268 "");
Ilya Sherman 2016/03/21 08:09:44 nit: Prefer std::string() to an empty string liter
beaudoin 2016/03/23 02:46:42 Done.
269 base::FeatureList::SetInstance(std::move(feature_list));
270
271 // Reset user model so it uses the new feature list.
272 user_model_.reset(new UserModel(mock_task_runner_));
273
274 EXPECT_EQ(0, num_actions_recorded_);
275
241 user_model_->DidStartProcessingInputEvent( 276 user_model_->DidStartProcessingInputEvent(
242 blink::WebInputEvent::Type::GestureScrollBegin, clock_->NowTicks()); 277 blink::WebInputEvent::Type::GestureScrollBegin, clock_->NowTicks());
278 mock_task_runner_->RunPendingTasks();
243 279
244 base::TimeDelta delta(base::TimeDelta::FromMilliseconds( 280 EXPECT_EQ(1, num_actions_recorded_);
245 UserModel::kMedianGestureDurationMillis * 2)); 281 EXPECT_EQ("RendererScheduler.UserModel.GestureStart", last_action_recorded_);
282
283 base::TimeDelta delta(base::TimeDelta::FromMilliseconds(500));
246 clock_->Advance(delta); 284 clock_->Advance(delta);
247 285
248 base::TimeDelta prediction_valid_duration; 286 user_model_->DidStartProcessingInputEvent(
249 EXPECT_FALSE(user_model_->IsGestureExpectedToContinue( 287 blink::WebInputEvent::Type::GestureScrollEnd, clock_->NowTicks());
250 clock_->NowTicks(), &prediction_valid_duration)); 288 mock_task_runner_->RunPendingTasks();
251 EXPECT_EQ(base::TimeDelta(), prediction_valid_duration); 289
290 EXPECT_EQ(2, num_actions_recorded_);
291 EXPECT_EQ("RendererScheduler.UserModel.GestureEnd", last_action_recorded_);
Ilya Sherman 2016/03/21 08:09:44 It would probably be better to use the code from b
beaudoin 2016/03/23 02:46:41 Looks like it's almost a drop-in replacement for w
252 } 292 }
253 293
254 } // namespace scheduler 294 } // namespace scheduler
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698