Skip to content

Commit 2889b8f

Browse files
committed
InputTracer: Ensure 0 coordinate values are traced, with unit test
The axis bits in PointerCoords are not set when the value is 0, since that's the default value. Make sure the coordinate values are always traced for pointer events, and add unit tests. Bug: 245989146 Flag: EXEMPT tracing only Test: Presubmit Change-Id: I7c291ef56e025e771c382354e10d6b7dda7a8fa4
1 parent ea395bf commit 2889b8f

2 files changed

Lines changed: 112 additions & 6 deletions

File tree

services/inputflinger/dispatcher/trace/AndroidInputEventProtoConverter.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ const impl::TraceConfig CONFIG_TRACE_ALL{
4141
.matchImeConnectionActive = {}}},
4242
};
4343

44+
template <typename Pointer>
45+
void writeAxisValue(Pointer* pointer, int32_t axis, float value, bool isRedacted) {
46+
auto* axisEntry = pointer->add_axis_value();
47+
axisEntry->set_axis(axis);
48+
49+
if (!isRedacted) {
50+
axisEntry->set_value(value);
51+
}
52+
}
53+
4454
} // namespace internal
4555

4656
/**
@@ -85,15 +95,21 @@ class AndroidInputEventProtoConverter {
8595

8696
const auto& coords = event.pointerCoords[i];
8797
auto bits = BitSet64(coords.bits);
88-
for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) {
89-
const auto axis = bits.clearFirstMarkedBit();
90-
auto axisEntry = pointer->add_axis_value();
91-
axisEntry->set_axis(axis);
9298

93-
if (!isRedacted) {
94-
axisEntry->set_value(coords.values[axisIndex]);
99+
if (isFromSource(event.source, AINPUT_SOURCE_CLASS_POINTER)) {
100+
// Always include the X and Y axes for pointer events, since the
101+
// bits will not be marked if the value is 0.
102+
for (const auto axis : {AMOTION_EVENT_AXIS_X, AMOTION_EVENT_AXIS_Y}) {
103+
if (!bits.hasBit(axis)) {
104+
internal::writeAxisValue(pointer, axis, 0.0f, isRedacted);
105+
}
95106
}
96107
}
108+
109+
for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) {
110+
const auto axis = bits.clearFirstMarkedBit();
111+
internal::writeAxisValue(pointer, axis, coords.values[axisIndex], isRedacted);
112+
}
97113
}
98114
}
99115

services/inputflinger/tests/AndroidInputEventProtoConverter_test.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,96 @@ TEST(AndroidInputEventProtoConverterTest, ToProtoMotionEvent_Redacted) {
256256
TestProtoConverter::toProtoMotionEvent(event, proto, /*isRedacted=*/true);
257257
}
258258

259+
// Test any special handling for zero values for pointer events.
260+
TEST(AndroidInputEventProtoConverterTest, ToProtoMotionEvent_ZeroValues) {
261+
TracedMotionEvent event{};
262+
event.id = 0;
263+
event.eventTime = 0;
264+
event.downTime = 0;
265+
event.source = AINPUT_SOURCE_MOUSE;
266+
event.action = AMOTION_EVENT_ACTION_BUTTON_PRESS;
267+
event.deviceId = 0;
268+
event.displayId = ui::LogicalDisplayId(0);
269+
event.classification = {};
270+
event.flags = 0;
271+
event.policyFlags = 0;
272+
event.buttonState = 0;
273+
event.actionButton = 0;
274+
event.xCursorPosition = 0.0f;
275+
event.yCursorPosition = 0.0f;
276+
event.metaState = 0;
277+
event.xPrecision = 0.0f;
278+
event.yPrecision = 0.0f;
279+
event.pointerProperties.emplace_back(PointerProperties{
280+
.id = 0,
281+
.toolType = ToolType::MOUSE,
282+
});
283+
event.pointerProperties.emplace_back(PointerProperties{
284+
.id = 1,
285+
.toolType = ToolType::FINGER,
286+
});
287+
// Zero values for x and y axes are always traced for pointer events.
288+
// However, zero values for other axes may not necessarily be traced.
289+
event.pointerCoords.emplace_back();
290+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_X, 0.0f);
291+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, 1.0f);
292+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.0f);
293+
event.pointerCoords.emplace_back();
294+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_X, 0.0f);
295+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, 0.0f);
296+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.0f);
297+
298+
testing::StrictMock<MockProtoMotion> proto;
299+
testing::StrictMock<MockProtoPointer> pointer1;
300+
testing::StrictMock<MockProtoPointer> pointer2;
301+
testing::StrictMock<MockProtoAxisValue> axisValue1;
302+
testing::StrictMock<MockProtoAxisValue> axisValue2;
303+
testing::StrictMock<MockProtoAxisValue> axisValue3;
304+
testing::StrictMock<MockProtoAxisValue> axisValue4;
305+
306+
EXPECT_CALL(proto, set_event_id(0));
307+
EXPECT_CALL(proto, set_event_time_nanos(0));
308+
EXPECT_CALL(proto, set_down_time_nanos(0));
309+
EXPECT_CALL(proto, set_source(AINPUT_SOURCE_MOUSE));
310+
EXPECT_CALL(proto, set_action(AMOTION_EVENT_ACTION_BUTTON_PRESS));
311+
EXPECT_CALL(proto, set_device_id(0));
312+
EXPECT_CALL(proto, set_display_id(0));
313+
EXPECT_CALL(proto, set_classification(0));
314+
EXPECT_CALL(proto, set_flags(0));
315+
EXPECT_CALL(proto, set_policy_flags(0));
316+
EXPECT_CALL(proto, set_button_state(0));
317+
EXPECT_CALL(proto, set_action_button(0));
318+
EXPECT_CALL(proto, set_cursor_position_x(0.0f));
319+
EXPECT_CALL(proto, set_cursor_position_y(0.0f));
320+
EXPECT_CALL(proto, set_meta_state(0));
321+
EXPECT_CALL(proto, set_precision_x(0.0f));
322+
EXPECT_CALL(proto, set_precision_y(0.0f));
323+
324+
EXPECT_CALL(proto, add_pointer()).WillOnce(Return(&pointer1)).WillOnce(Return(&pointer2));
325+
326+
EXPECT_CALL(pointer1, set_pointer_id(0));
327+
EXPECT_CALL(pointer1, set_tool_type(AMOTION_EVENT_TOOL_TYPE_MOUSE));
328+
EXPECT_CALL(pointer1, add_axis_value())
329+
.WillOnce(Return(&axisValue1))
330+
.WillOnce(Return(&axisValue2));
331+
EXPECT_CALL(axisValue1, set_axis(AMOTION_EVENT_AXIS_X));
332+
EXPECT_CALL(axisValue1, set_value(0.0f));
333+
EXPECT_CALL(axisValue2, set_axis(AMOTION_EVENT_AXIS_Y));
334+
EXPECT_CALL(axisValue2, set_value(1.0f));
335+
336+
EXPECT_CALL(pointer2, set_pointer_id(1));
337+
EXPECT_CALL(pointer2, set_tool_type(AMOTION_EVENT_TOOL_TYPE_FINGER));
338+
EXPECT_CALL(pointer2, add_axis_value())
339+
.WillOnce(Return(&axisValue3))
340+
.WillOnce(Return(&axisValue4));
341+
EXPECT_CALL(axisValue3, set_axis(AMOTION_EVENT_AXIS_X));
342+
EXPECT_CALL(axisValue3, set_value(0.0f));
343+
EXPECT_CALL(axisValue4, set_axis(AMOTION_EVENT_AXIS_Y));
344+
EXPECT_CALL(axisValue4, set_value(0.0f));
345+
346+
TestProtoConverter::toProtoMotionEvent(event, proto, /*isRedacted=*/false);
347+
}
348+
259349
} // namespace
260350

261351
} // namespace android::inputdispatcher::trace

0 commit comments

Comments
 (0)