blob: 84446828152c717bd2ef4aa8e69aaebc2594cedc [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2018 The Chromium Authors
Daniel Bratell0a36ad92018-10-02 15:04:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Mario Bianucci926a2d92020-07-28 02:04:325#include "ui/base/prediction/input_predictor_unittest_helpers.h"
Daniel Bratell0a36ad92018-10-02 15:04:426
Mario Bianucci926a2d92020-07-28 02:04:327namespace ui {
Daniel Bratell0a36ad92018-10-02 15:04:428
9InputPredictorTest::InputPredictorTest() = default;
10InputPredictorTest::~InputPredictorTest() = default;
11
12void 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 Gef7afddc2019-10-21 23:37:4819 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 Bratell0a36ad92018-10-02 15:04:4224 }
25 InputPredictor::InputData data = {gfx::PointF(x[i], y[i]),
26 FromMilliseconds(timestamp_ms[i])};
27 predictor_->Update(data);
28 }
29}
30
Axel Antoineebf99702019-06-26 17:00:2331void InputPredictorTest::ValidatePredictor(
32 const std::vector<double>& events_x,
33 const std::vector<double>& events_y,
Ella Ge3289d0532019-10-21 21:25:2334 const std::vector<double>& events_time_ms,
35 const std::vector<double>& prediction_time_ms,
Axel Antoineebf99702019-06-26 17:00:2336 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 Ge3289d0532019-10-21 21:25:2341 size_t current_prediction_index = 0;
42 for (size_t i = 0; i < events_time_ms.size(); i++) {
Axel Antoineebf99702019-06-26 17:00:2343 InputPredictor::InputData data = {gfx::PointF(events_x[i], events_y[i]),
Ella Ge3289d0532019-10-21 21:25:2344 FromMilliseconds(events_time_ms[i])};
Axel Antoineebf99702019-06-26 17:00:2345 predictor_->Update(data);
46
47 if (predictor_->HasPrediction()) {
Ella Gef7afddc2019-10-21 23:37:4848 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 Ge3289d0532019-10-21 21:25:2354 current_prediction_index++;
Axel Antoineebf99702019-06-26 17:00:2355 }
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 Bianucci926a2d92020-07-28 02:04:3267} // namespace ui