Skip to content

Commit 6925c95

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Refactor interceptKeyBeforeQueueing tests in FakeInputDispatcherPolicy" into main
2 parents 2561547 + b8fbfb3 commit 6925c95

3 files changed

Lines changed: 28 additions & 23 deletions

File tree

services/inputflinger/tests/FakeInputDispatcherPolicy.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,6 @@ void FakeInputDispatcherPolicy::assertNotifyInputChannelBrokenWasCalled(const sp
198198
ASSERT_EQ(token, *receivedToken);
199199
}
200200

201-
void FakeInputDispatcherPolicy::setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
202-
mInterceptKeyTimeout = timeout;
203-
}
204-
205201
std::chrono::nanoseconds FakeInputDispatcherPolicy::getKeyWaitingForEventsTimeout() {
206202
return 500ms;
207203
}
@@ -210,8 +206,9 @@ void FakeInputDispatcherPolicy::setStaleEventTimeout(std::chrono::nanoseconds ti
210206
mStaleEventTimeout = timeout;
211207
}
212208

213-
void FakeInputDispatcherPolicy::setConsumeKeyBeforeDispatching(bool consumeKeyBeforeDispatching) {
214-
mConsumeKeyBeforeDispatching = consumeKeyBeforeDispatching;
209+
void FakeInputDispatcherPolicy::setInterceptKeyBeforeDispatchingResult(
210+
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult> result) {
211+
mInterceptKeyBeforeDispatchingResult = result;
215212
}
216213

217214
void FakeInputDispatcherPolicy::assertFocusedDisplayNotified(ui::LogicalDisplayId expectedDisplay) {
@@ -404,7 +401,9 @@ bool FakeInputDispatcherPolicy::filterInputEvent(const InputEvent& inputEvent,
404401
void FakeInputDispatcherPolicy::interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) {
405402
if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
406403
// Clear intercept state when we handled the event.
407-
mInterceptKeyTimeout = 0ms;
404+
if (std::holds_alternative<nsecs_t>(mInterceptKeyBeforeDispatchingResult)) {
405+
mInterceptKeyBeforeDispatchingResult = nsecs_t(0);
406+
}
408407
}
409408
}
410409

@@ -414,17 +413,20 @@ void FakeInputDispatcherPolicy::interceptMotionBeforeQueueing(ui::LogicalDisplay
414413
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
415414
FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&,
416415
uint32_t) {
417-
if (mConsumeKeyBeforeDispatching) {
418-
return inputdispatcher::KeyEntry::InterceptKeyResult::SKIP;
416+
if (std::holds_alternative<inputdispatcher::KeyEntry::InterceptKeyResult>(
417+
mInterceptKeyBeforeDispatchingResult)) {
418+
return mInterceptKeyBeforeDispatchingResult;
419419
}
420420

421-
nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
421+
nsecs_t delay =
422+
std::chrono::nanoseconds(std::get<nsecs_t>(mInterceptKeyBeforeDispatchingResult))
423+
.count();
422424
if (delay == 0) {
423425
return inputdispatcher::KeyEntry::InterceptKeyResult::CONTINUE;
424426
}
425427

426428
// Clear intercept state so we could dispatch the event in next wake.
427-
mInterceptKeyTimeout = 0ms;
429+
mInterceptKeyBeforeDispatchingResult = nsecs_t(0);
428430
return delay;
429431
}
430432

services/inputflinger/tests/FakeInputDispatcherPolicy.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
9696
void assertDropTargetEquals(const InputDispatcherInterface& dispatcher,
9797
const sp<IBinder>& targetToken);
9898
void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token);
99-
/**
100-
* Set policy timeout. A value of zero means next key will not be intercepted.
101-
*/
102-
void setInterceptKeyTimeout(std::chrono::milliseconds timeout);
10399
std::chrono::nanoseconds getKeyWaitingForEventsTimeout() override;
104100
void setStaleEventTimeout(std::chrono::nanoseconds timeout);
105101
void assertUserActivityNotPoked();
@@ -116,7 +112,12 @@ class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
116112
void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler);
117113
void assertUnhandledKeyReported(int32_t keycode);
118114
void assertUnhandledKeyNotReported();
119-
void setConsumeKeyBeforeDispatching(bool consumeKeyBeforeDispatching);
115+
/**
116+
* Set policy timeout or the interception result.
117+
* A timeout value of zero means next key will not be intercepted.
118+
*/
119+
void setInterceptKeyBeforeDispatchingResult(
120+
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult> result);
120121
void assertFocusedDisplayNotified(ui::LogicalDisplayId expectedDisplay);
121122

122123
private:
@@ -145,11 +146,10 @@ class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
145146
std::condition_variable mNotifyUserActivity;
146147
std::queue<UserActivityPokeEvent> mUserActivityPokeEvents;
147148

148-
std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
149-
150149
std::chrono::nanoseconds mStaleEventTimeout = 1000ms;
151150

152-
bool mConsumeKeyBeforeDispatching = false;
151+
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
152+
mInterceptKeyBeforeDispatchingResult;
153153

154154
BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
155155

services/inputflinger/tests/InputDispatcher_test.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5491,7 +5491,8 @@ TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
54915491
generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT);
54925492
const std::chrono::milliseconds interceptKeyTimeout = 50ms;
54935493
const nsecs_t injectTime = keyArgs.eventTime;
5494-
mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
5494+
mFakePolicy->setInterceptKeyBeforeDispatchingResult(
5495+
std::chrono::nanoseconds(interceptKeyTimeout).count());
54955496
mDispatcher->notifyKey(keyArgs);
54965497
// The dispatching time should be always greater than or equal to intercept key timeout.
54975498
window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
@@ -5519,7 +5520,7 @@ TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
55195520

55205521
// Set a value that's significantly larger than the default consumption timeout. If the
55215522
// implementation is correct, the actual value doesn't matter; it won't slow down the test.
5522-
mFakePolicy->setInterceptKeyTimeout(600ms);
5523+
mFakePolicy->setInterceptKeyBeforeDispatchingResult(std::chrono::nanoseconds(600ms).count());
55235524
mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
55245525
// Window should receive key event immediately when same key up.
55255526
window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
@@ -7438,7 +7439,8 @@ TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyConsumedKey) {
74387439

74397440
window->consumeFocusEvent(true);
74407441

7441-
mFakePolicy->setConsumeKeyBeforeDispatching(true);
7442+
mFakePolicy->setInterceptKeyBeforeDispatchingResult(
7443+
inputdispatcher::KeyEntry::InterceptKeyResult::SKIP);
74427444

74437445
mDispatcher->notifyKey(
74447446
KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
@@ -7464,7 +7466,8 @@ TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserAct
74647466

74657467
window->consumeFocusEvent(true);
74667468

7467-
mFakePolicy->setConsumeKeyBeforeDispatching(true);
7469+
mFakePolicy->setInterceptKeyBeforeDispatchingResult(
7470+
inputdispatcher::KeyEntry::InterceptKeyResult::SKIP);
74687471

74697472
mDispatcher->notifyKey(
74707473
KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());

0 commit comments

Comments
 (0)