Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 numActionsRecorded_ = 0; | |
| 30 lastActionRecorded_ = ""; | |
| 31 base::RemoveActionCallback(action_callback_); | |
| 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) { | |
| 49 numActionsRecorded_++; | |
| 50 lastActionRecorded_ = 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 numActionsRecorded_; | |
| 60 std::string lastActionRecorded_; | |
|
Alexei Svitkine (slow)
2016/03/17 21:38:46
hacker_style, please.
beaudoin
2016/03/18 16:08:36
Done.
| |
| 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 Loading... | |
| 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 ""); | |
| 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, numActionsRecorded_); | |
| 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, numActionsRecorded_); |
| 245 UserModel::kMedianGestureDurationMillis * 2)); | 281 EXPECT_EQ("RendererScheduler.UserModel.GestureStart", lastActionRecorded_); |
| 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, numActionsRecorded_); | |
| 291 EXPECT_EQ("RendererScheduler.UserModel.GestureEnd", lastActionRecorded_); | |
| 252 } | 292 } |
| 253 | 293 |
| 254 } // namespace scheduler | 294 } // namespace scheduler |
| OLD | NEW |