Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Daniel Bratell | 0a36ad9 | 2018-10-02 15:04:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 5 | #include "ui/base/prediction/input_predictor_unittest_helpers.h" |
Daniel Bratell | 0a36ad9 | 2018-10-02 15:04:42 | [diff] [blame] | 6 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 7 | namespace ui { |
Daniel Bratell | 0a36ad9 | 2018-10-02 15:04:42 | [diff] [blame] | 8 | |
| 9 | InputPredictorTest::InputPredictorTest() = default; |
| 10 | InputPredictorTest::~InputPredictorTest() = default; |
| 11 | |
| 12 | void InputPredictorTest::ValidatePredictor( |
| 13 | const std::vector<double>& x, |
| 14 | const std::vector<double>& y, |
| 15 | const std::vector<double>& timestamp_ms) { |
| 16 | predictor_->Reset(); |
| 17 | for (size_t i = 0; i < timestamp_ms.size(); i++) { |
| 18 | if (predictor_->HasPrediction()) { |
Ella Ge | f7afddc | 2019-10-21 23:37:48 | [diff] [blame] | 19 | auto result = |
| 20 | predictor_->GeneratePrediction(FromMilliseconds(timestamp_ms[i])); |
| 21 | EXPECT_TRUE(result); |
| 22 | EXPECT_NEAR(result->pos.x(), x[i], kEpsilon); |
| 23 | EXPECT_NEAR(result->pos.y(), y[i], kEpsilon); |
Daniel Bratell | 0a36ad9 | 2018-10-02 15:04:42 | [diff] [blame] | 24 | } |
| 25 | InputPredictor::InputData data = {gfx::PointF(x[i], y[i]), |
| 26 | FromMilliseconds(timestamp_ms[i])}; |
| 27 | predictor_->Update(data); |
| 28 | } |
| 29 | } |
| 30 | |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 31 | void InputPredictorTest::ValidatePredictor( |
| 32 | const std::vector<double>& events_x, |
| 33 | const std::vector<double>& events_y, |
Ella Ge | 3289d053 | 2019-10-21 21:25:23 | [diff] [blame] | 34 | const std::vector<double>& events_time_ms, |
| 35 | const std::vector<double>& prediction_time_ms, |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 36 | const std::vector<double>& predicted_x, |
| 37 | const std::vector<double>& predicted_y) { |
| 38 | predictor_->Reset(); |
| 39 | std::vector<double> computed_x; |
| 40 | std::vector<double> computed_y; |
Ella Ge | 3289d053 | 2019-10-21 21:25:23 | [diff] [blame] | 41 | size_t current_prediction_index = 0; |
| 42 | for (size_t i = 0; i < events_time_ms.size(); i++) { |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 43 | InputPredictor::InputData data = {gfx::PointF(events_x[i], events_y[i]), |
Ella Ge | 3289d053 | 2019-10-21 21:25:23 | [diff] [blame] | 44 | FromMilliseconds(events_time_ms[i])}; |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 45 | predictor_->Update(data); |
| 46 | |
| 47 | if (predictor_->HasPrediction()) { |
Ella Ge | f7afddc | 2019-10-21 23:37:48 | [diff] [blame] | 48 | auto result = predictor_->GeneratePrediction( |
| 49 | FromMilliseconds(prediction_time_ms[current_prediction_index])); |
| 50 | EXPECT_TRUE(result); |
| 51 | computed_x.push_back(result->pos.x()); |
| 52 | computed_y.push_back(result->pos.y()); |
| 53 | EXPECT_GT(result->time_stamp, base::TimeTicks()); |
Ella Ge | 3289d053 | 2019-10-21 21:25:23 | [diff] [blame] | 54 | current_prediction_index++; |
Axel Antoine | ebf9970 | 2019-06-26 17:00:23 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | EXPECT_TRUE(computed_x.size() == predicted_x.size()); |
| 59 | if (computed_x.size() == predicted_x.size()) { |
| 60 | for (size_t i = 0; i < predicted_x.size(); i++) { |
| 61 | EXPECT_NEAR(computed_x[i], predicted_x[i], kEpsilon); |
| 62 | EXPECT_NEAR(computed_y[i], predicted_y[i], kEpsilon); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Mario Bianucci | 926a2d9 | 2020-07-28 02:04:32 | [diff] [blame] | 67 | } // namespace ui |