Skip to content

Commit 70d901b

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge changes If328c294,I7c291ef5 into main
* changes: Add tests for AndroidInputEventProtoConverter::toProtoKeyEvent InputTracer: Ensure 0 coordinate values are traced, with unit test
2 parents ff4a159 + c233f0a commit 70d901b

3 files changed

Lines changed: 203 additions & 8 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/dispatcher/trace/InputTracingBackendInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct TracedKeyEvent {
5050
uint32_t policyFlags;
5151
int32_t deviceId;
5252
uint32_t source;
53-
ui::LogicalDisplayId displayId;
53+
ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID;
5454
int32_t action;
5555
int32_t keyCode;
5656
int32_t scanCode;

services/inputflinger/tests/AndroidInputEventProtoConverter_test.cpp

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,24 @@ class MockProtoMotion {
6060
MOCK_METHOD(MockProtoPointer*, add_pointer, ());
6161
};
6262

63-
using TestProtoConverter = AndroidInputEventProtoConverter<MockProtoMotion, proto::AndroidKeyEvent,
63+
class MockProtoKey {
64+
public:
65+
MOCK_METHOD(void, set_event_id, (uint32_t));
66+
MOCK_METHOD(void, set_event_time_nanos, (int64_t));
67+
MOCK_METHOD(void, set_down_time_nanos, (int64_t));
68+
MOCK_METHOD(void, set_source, (uint32_t));
69+
MOCK_METHOD(void, set_action, (int32_t));
70+
MOCK_METHOD(void, set_device_id, (uint32_t));
71+
MOCK_METHOD(void, set_display_id, (uint32_t));
72+
MOCK_METHOD(void, set_repeat_count, (uint32_t));
73+
MOCK_METHOD(void, set_flags, (uint32_t));
74+
MOCK_METHOD(void, set_policy_flags, (uint32_t));
75+
MOCK_METHOD(void, set_key_code, (uint32_t));
76+
MOCK_METHOD(void, set_scan_code, (uint32_t));
77+
MOCK_METHOD(void, set_meta_state, (uint32_t));
78+
};
79+
80+
using TestProtoConverter = AndroidInputEventProtoConverter<MockProtoMotion, MockProtoKey,
6481
proto::AndroidWindowInputDispatchEvent,
6582
proto::AndroidInputEventConfig::Decoder>;
6683

@@ -256,6 +273,168 @@ TEST(AndroidInputEventProtoConverterTest, ToProtoMotionEvent_Redacted) {
256273
TestProtoConverter::toProtoMotionEvent(event, proto, /*isRedacted=*/true);
257274
}
258275

276+
// Test any special handling for zero values for pointer events.
277+
TEST(AndroidInputEventProtoConverterTest, ToProtoMotionEvent_ZeroValues) {
278+
TracedMotionEvent event{};
279+
event.id = 0;
280+
event.eventTime = 0;
281+
event.downTime = 0;
282+
event.source = AINPUT_SOURCE_MOUSE;
283+
event.action = AMOTION_EVENT_ACTION_BUTTON_PRESS;
284+
event.deviceId = 0;
285+
event.displayId = ui::LogicalDisplayId(0);
286+
event.classification = {};
287+
event.flags = 0;
288+
event.policyFlags = 0;
289+
event.buttonState = 0;
290+
event.actionButton = 0;
291+
event.xCursorPosition = 0.0f;
292+
event.yCursorPosition = 0.0f;
293+
event.metaState = 0;
294+
event.xPrecision = 0.0f;
295+
event.yPrecision = 0.0f;
296+
event.pointerProperties.emplace_back(PointerProperties{
297+
.id = 0,
298+
.toolType = ToolType::MOUSE,
299+
});
300+
event.pointerProperties.emplace_back(PointerProperties{
301+
.id = 1,
302+
.toolType = ToolType::FINGER,
303+
});
304+
// Zero values for x and y axes are always traced for pointer events.
305+
// However, zero values for other axes may not necessarily be traced.
306+
event.pointerCoords.emplace_back();
307+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_X, 0.0f);
308+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, 1.0f);
309+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.0f);
310+
event.pointerCoords.emplace_back();
311+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_X, 0.0f);
312+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_Y, 0.0f);
313+
event.pointerCoords.back().setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.0f);
314+
315+
testing::StrictMock<MockProtoMotion> proto;
316+
testing::StrictMock<MockProtoPointer> pointer1;
317+
testing::StrictMock<MockProtoPointer> pointer2;
318+
testing::StrictMock<MockProtoAxisValue> axisValue1;
319+
testing::StrictMock<MockProtoAxisValue> axisValue2;
320+
testing::StrictMock<MockProtoAxisValue> axisValue3;
321+
testing::StrictMock<MockProtoAxisValue> axisValue4;
322+
323+
EXPECT_CALL(proto, set_event_id(0));
324+
EXPECT_CALL(proto, set_event_time_nanos(0));
325+
EXPECT_CALL(proto, set_down_time_nanos(0));
326+
EXPECT_CALL(proto, set_source(AINPUT_SOURCE_MOUSE));
327+
EXPECT_CALL(proto, set_action(AMOTION_EVENT_ACTION_BUTTON_PRESS));
328+
EXPECT_CALL(proto, set_device_id(0));
329+
EXPECT_CALL(proto, set_display_id(0));
330+
EXPECT_CALL(proto, set_classification(0));
331+
EXPECT_CALL(proto, set_flags(0));
332+
EXPECT_CALL(proto, set_policy_flags(0));
333+
EXPECT_CALL(proto, set_button_state(0));
334+
EXPECT_CALL(proto, set_action_button(0));
335+
EXPECT_CALL(proto, set_cursor_position_x(0.0f));
336+
EXPECT_CALL(proto, set_cursor_position_y(0.0f));
337+
EXPECT_CALL(proto, set_meta_state(0));
338+
EXPECT_CALL(proto, set_precision_x(0.0f));
339+
EXPECT_CALL(proto, set_precision_y(0.0f));
340+
341+
EXPECT_CALL(proto, add_pointer()).WillOnce(Return(&pointer1)).WillOnce(Return(&pointer2));
342+
343+
EXPECT_CALL(pointer1, set_pointer_id(0));
344+
EXPECT_CALL(pointer1, set_tool_type(AMOTION_EVENT_TOOL_TYPE_MOUSE));
345+
EXPECT_CALL(pointer1, add_axis_value())
346+
.WillOnce(Return(&axisValue1))
347+
.WillOnce(Return(&axisValue2));
348+
EXPECT_CALL(axisValue1, set_axis(AMOTION_EVENT_AXIS_X));
349+
EXPECT_CALL(axisValue1, set_value(0.0f));
350+
EXPECT_CALL(axisValue2, set_axis(AMOTION_EVENT_AXIS_Y));
351+
EXPECT_CALL(axisValue2, set_value(1.0f));
352+
353+
EXPECT_CALL(pointer2, set_pointer_id(1));
354+
EXPECT_CALL(pointer2, set_tool_type(AMOTION_EVENT_TOOL_TYPE_FINGER));
355+
EXPECT_CALL(pointer2, add_axis_value())
356+
.WillOnce(Return(&axisValue3))
357+
.WillOnce(Return(&axisValue4));
358+
EXPECT_CALL(axisValue3, set_axis(AMOTION_EVENT_AXIS_X));
359+
EXPECT_CALL(axisValue3, set_value(0.0f));
360+
EXPECT_CALL(axisValue4, set_axis(AMOTION_EVENT_AXIS_Y));
361+
EXPECT_CALL(axisValue4, set_value(0.0f));
362+
363+
TestProtoConverter::toProtoMotionEvent(event, proto, /*isRedacted=*/false);
364+
}
365+
366+
TEST(AndroidInputEventProtoConverterTest, ToProtoKeyEvent) {
367+
TracedKeyEvent event{};
368+
event.id = 1;
369+
event.eventTime = 2;
370+
event.downTime = 3;
371+
event.source = AINPUT_SOURCE_KEYBOARD;
372+
event.action = AKEY_EVENT_ACTION_DOWN;
373+
event.deviceId = 4;
374+
event.displayId = ui::LogicalDisplayId(5);
375+
event.repeatCount = 6;
376+
event.flags = 7;
377+
event.policyFlags = 8;
378+
event.keyCode = 9;
379+
event.scanCode = 10;
380+
event.metaState = 11;
381+
382+
testing::StrictMock<MockProtoKey> proto;
383+
384+
EXPECT_CALL(proto, set_event_id(1));
385+
EXPECT_CALL(proto, set_event_time_nanos(2));
386+
EXPECT_CALL(proto, set_down_time_nanos(3));
387+
EXPECT_CALL(proto, set_source(AINPUT_SOURCE_KEYBOARD));
388+
EXPECT_CALL(proto, set_action(AKEY_EVENT_ACTION_DOWN));
389+
EXPECT_CALL(proto, set_device_id(4));
390+
EXPECT_CALL(proto, set_display_id(5));
391+
EXPECT_CALL(proto, set_repeat_count(6));
392+
EXPECT_CALL(proto, set_flags(7));
393+
EXPECT_CALL(proto, set_policy_flags(8));
394+
EXPECT_CALL(proto, set_key_code(9));
395+
EXPECT_CALL(proto, set_scan_code(10));
396+
EXPECT_CALL(proto, set_meta_state(11));
397+
398+
TestProtoConverter::toProtoKeyEvent(event, proto, /*isRedacted=*/false);
399+
}
400+
401+
TEST(AndroidInputEventProtoConverterTest, ToProtoKeyEvent_Redacted) {
402+
TracedKeyEvent event{};
403+
event.id = 1;
404+
event.eventTime = 2;
405+
event.downTime = 3;
406+
event.source = AINPUT_SOURCE_KEYBOARD;
407+
event.action = AKEY_EVENT_ACTION_DOWN;
408+
event.deviceId = 4;
409+
event.displayId = ui::LogicalDisplayId(5);
410+
event.repeatCount = 6;
411+
event.flags = 7;
412+
event.policyFlags = 8;
413+
event.keyCode = 9;
414+
event.scanCode = 10;
415+
event.metaState = 11;
416+
417+
testing::StrictMock<MockProtoKey> proto;
418+
419+
EXPECT_CALL(proto, set_event_id(1));
420+
EXPECT_CALL(proto, set_event_time_nanos(2));
421+
EXPECT_CALL(proto, set_down_time_nanos(3));
422+
EXPECT_CALL(proto, set_source(AINPUT_SOURCE_KEYBOARD));
423+
EXPECT_CALL(proto, set_action(AKEY_EVENT_ACTION_DOWN));
424+
EXPECT_CALL(proto, set_device_id(4));
425+
EXPECT_CALL(proto, set_display_id(5));
426+
EXPECT_CALL(proto, set_repeat_count(6));
427+
EXPECT_CALL(proto, set_flags(7));
428+
EXPECT_CALL(proto, set_policy_flags(8));
429+
430+
// Redacted fields
431+
EXPECT_CALL(proto, set_key_code(_)).Times(0);
432+
EXPECT_CALL(proto, set_scan_code(_)).Times(0);
433+
EXPECT_CALL(proto, set_meta_state(_)).Times(0);
434+
435+
TestProtoConverter::toProtoKeyEvent(event, proto, /*isRedacted=*/true);
436+
}
437+
259438
} // namespace
260439

261440
} // namespace android::inputdispatcher::trace

0 commit comments

Comments
 (0)