Refactor VelocityTracker_test
Two changes here:
1) Use std::unique_ptr to pass a newed MotionEvent around. This also
fixes a memory leak in computeAndCheckQuadraticEstimate
2) Make pointerCoords and pointerProperties consistent by making them
both arrays
Bug: none
Test: atest -a libinput_tests inputflinger_tests
Change-Id: I4439342c78b1850a9a43bd8c58501f2bc86f266e
diff --git a/libs/input/tests/VelocityTracker_test.cpp b/libs/input/tests/VelocityTracker_test.cpp
index 515209e..2a4020f 100644
--- a/libs/input/tests/VelocityTracker_test.cpp
+++ b/libs/input/tests/VelocityTracker_test.cpp
@@ -66,7 +66,7 @@
EXPECT_NEAR_BY_FRACTION(actual, target, COEFFICIENT_TOLERANCE);
}
-void failWithMessage(std::string message) {
+static void failWithMessage(std::string message) {
FAIL() << message; // cannot do this directly from a non-void function
}
@@ -77,39 +77,40 @@
};
-MotionEvent* createSimpleMotionEvent(const Position* positions, size_t numSamples) {
+static std::unique_ptr<MotionEvent> createSimpleMotionEvent(const Position* positions,
+ size_t numSamples) {
/**
* Only populate the basic fields of a MotionEvent, such as time and a single axis
* Designed for use with manually-defined tests.
- * Create a new MotionEvent on the heap, caller responsible for destroying the object.
*/
if (numSamples < 1) {
failWithMessage(StringPrintf("Need at least 1 sample to create a MotionEvent."
" Received numSamples=%zu", numSamples));
}
- MotionEvent* event = new MotionEvent();
- PointerCoords coords;
- coords.clear();
- constexpr size_t pointerCount = 1;
- PointerProperties properties[pointerCount];
+ std::unique_ptr<MotionEvent> event = std::make_unique<MotionEvent>();
+ constexpr size_t pointerCount = 1;
+ PointerCoords coords[pointerCount];
+ coords[0].clear();
+
+ PointerProperties properties[pointerCount];
properties[0].id = DEFAULT_POINTER_ID;
properties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
// First sample added separately with initialize
- coords.setAxisValue(AMOTION_EVENT_AXIS_X, positions[0].x);
- coords.setAxisValue(AMOTION_EVENT_AXIS_Y, positions[0].y);
+ coords[0].setAxisValue(AMOTION_EVENT_AXIS_X, positions[0].x);
+ coords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, positions[0].y);
event->initialize(0 /*deviceId*/, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
AMOTION_EVENT_ACTION_MOVE, 0 /*actionButton*/, 0 /*flags*/,
AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0 /*buttonState*/, MotionClassification::NONE,
0 /*xOffset*/, 0 /*yOffset*/, 0 /*xPrecision*/, 0 /*yPrecision*/,
- 0 /*downTime*/, positions[0].time, pointerCount, properties, &coords);
+ 0 /*downTime*/, positions[0].time, pointerCount, properties, coords);
for (size_t i = 1; i < numSamples; i++) {
- coords.setAxisValue(AMOTION_EVENT_AXIS_X, positions[i].x);
- coords.setAxisValue(AMOTION_EVENT_AXIS_Y, positions[i].y);
- event->addSample(positions[i].time, &coords);
+ coords[0].setAxisValue(AMOTION_EVENT_AXIS_X, positions[i].x);
+ coords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, positions[i].y);
+ event->addSample(positions[i].time, coords);
}
return event;
}
@@ -119,8 +120,8 @@
VelocityTracker vt(nullptr);
float Vx, Vy;
- MotionEvent* event = createSimpleMotionEvent(positions, numSamples);
- vt.addMovement(event);
+ std::unique_ptr<MotionEvent> event = createSimpleMotionEvent(positions, numSamples);
+ vt.addMovement(event.get());
vt.getVelocity(DEFAULT_POINTER_ID, &Vx, &Vy);
@@ -134,14 +135,13 @@
default:
FAIL() << "Axis must be either AMOTION_EVENT_AXIS_X or AMOTION_EVENT_AXIS_Y";
}
- delete event;
}
static void computeAndCheckQuadraticEstimate(const Position* positions, size_t numSamples,
const std::array<float, 3>& coefficients) {
VelocityTracker vt("lsq2");
- MotionEvent* event = createSimpleMotionEvent(positions, numSamples);
- vt.addMovement(event);
+ std::unique_ptr<MotionEvent> event = createSimpleMotionEvent(positions, numSamples);
+ vt.addMovement(event.get());
VelocityTracker::Estimator estimator;
EXPECT_TRUE(vt.getEstimator(0, &estimator));
for (size_t i = 0; i< coefficients.size(); i++) {