Update InputConsumer_test.cpp to use getFdStateDebug
Updated InputConsumer_test.cpp to use getFdStateDebug, and reverted the
changes introduced by LooperInterface in InputConsumerNoResampling
Bug: 297226446
Flag: EXEMPT refactor
Test: TEST=libinput_tests; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST --gtest_filter="InputConsumerTest*"
Change-Id: I7621fdf0923e16794142316a126d81e5faf3d708
diff --git a/libs/input/InputConsumerNoResampling.cpp b/libs/input/InputConsumerNoResampling.cpp
index f33afe7..cdbc186 100644
--- a/libs/input/InputConsumerNoResampling.cpp
+++ b/libs/input/InputConsumerNoResampling.cpp
@@ -46,27 +46,6 @@
const bool DEBUG_TRANSPORT_CONSUMER =
__android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Consumer", ANDROID_LOG_INFO);
-/**
- * RealLooper is a wrapper of Looper. All the member functions exclusively call the internal looper.
- * This class' behavior is the same as Looper.
- */
-class RealLooper final : public LooperInterface {
-public:
- RealLooper(sp<Looper> looper) : mLooper{looper} {}
-
- int addFd(int fd, int ident, int events, const sp<LooperCallback>& callback,
- void* data) override {
- return mLooper->addFd(fd, ident, events, callback, data);
- }
-
- int removeFd(int fd) override { return mLooper->removeFd(fd); }
-
- sp<Looper> getLooper() const override { return mLooper; }
-
-private:
- sp<Looper> mLooper;
-};
-
std::unique_ptr<KeyEvent> createKeyEvent(const InputMessage& msg) {
std::unique_ptr<KeyEvent> event = std::make_unique<KeyEvent>();
event->initialize(msg.body.key.eventId, msg.body.key.deviceId, msg.body.key.source,
@@ -201,12 +180,12 @@
// --- InputConsumerNoResampling ---
InputConsumerNoResampling::InputConsumerNoResampling(const std::shared_ptr<InputChannel>& channel,
- std::shared_ptr<LooperInterface> looper,
+ sp<Looper> looper,
InputConsumerCallbacks& callbacks,
std::unique_ptr<Resampler> resampler)
: mChannel{channel},
mLooper{looper},
- mCallbacks(callbacks),
+ mCallbacks{callbacks},
mResampler{std::move(resampler)},
mFdEvents(0) {
LOG_ALWAYS_FATAL_IF(mLooper == nullptr);
@@ -218,13 +197,6 @@
setFdEvents(ALOOPER_EVENT_INPUT);
}
-InputConsumerNoResampling::InputConsumerNoResampling(const std::shared_ptr<InputChannel>& channel,
- sp<Looper> looper,
- InputConsumerCallbacks& callbacks,
- std::unique_ptr<Resampler> resampler)
- : InputConsumerNoResampling(channel, std::make_shared<RealLooper>(looper), callbacks,
- std::move(resampler)) {}
-
InputConsumerNoResampling::~InputConsumerNoResampling() {
ensureCalledOnLooperThread(__func__);
consumeBatchedInputEvents(/*requestedFrameTime=*/std::nullopt);
@@ -556,7 +528,7 @@
void InputConsumerNoResampling::ensureCalledOnLooperThread(const char* func) const {
sp<Looper> callingThreadLooper = Looper::getForThread();
- if (callingThreadLooper != mLooper->getLooper()) {
+ if (callingThreadLooper != mLooper) {
LOG(FATAL) << "The function " << func << " can only be called on the looper thread";
}
}
diff --git a/libs/input/tests/Android.bp b/libs/input/tests/Android.bp
index 3ec167a..81c6175 100644
--- a/libs/input/tests/Android.bp
+++ b/libs/input/tests/Android.bp
@@ -27,7 +27,6 @@
"Resampler_test.cpp",
"RingBuffer_test.cpp",
"TestInputChannel.cpp",
- "TestLooper.cpp",
"TfLiteMotionPredictor_test.cpp",
"TouchResampling_test.cpp",
"TouchVideoFrame_test.cpp",
diff --git a/libs/input/tests/InputConsumer_test.cpp b/libs/input/tests/InputConsumer_test.cpp
index dec78aa..d708316 100644
--- a/libs/input/tests/InputConsumer_test.cpp
+++ b/libs/input/tests/InputConsumer_test.cpp
@@ -18,16 +18,15 @@
#include <memory>
#include <optional>
-#include <utility>
#include <TestEventMatchers.h>
#include <TestInputChannel.h>
-#include <TestLooper.h>
#include <android-base/logging.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <input/BlockingQueue.h>
#include <input/InputEventBuilders.h>
+#include <utils/Looper.h>
#include <utils/StrongPointer.h>
namespace android {
@@ -46,13 +45,20 @@
protected:
InputConsumerTest()
: mClientTestChannel{std::make_shared<TestInputChannel>("TestChannel")},
- mTestLooper{std::make_shared<TestLooper>()} {
- Looper::setForThread(mTestLooper->getLooper());
+ mLooper{sp<Looper>::make(/*allowNonCallbacks=*/false)} {
+ Looper::setForThread(mLooper);
mConsumer =
- std::make_unique<InputConsumerNoResampling>(mClientTestChannel, mTestLooper, *this,
+ std::make_unique<InputConsumerNoResampling>(mClientTestChannel, mLooper, *this,
std::make_unique<LegacyResampler>());
}
+ void invokeLooperCallback() const {
+ sp<LooperCallback> callback;
+ ASSERT_TRUE(mLooper->getFdStateDebug(mClientTestChannel->getFd(), /*ident=*/nullptr,
+ /*events=*/nullptr, &callback, /*data=*/nullptr));
+ callback->handleEvent(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT, /*data=*/nullptr);
+ }
+
void assertOnBatchedInputEventPendingWasCalled() {
ASSERT_GT(mOnBatchedInputEventPendingInvocationCount, 0UL)
<< "onBatchedInputEventPending has not been called.";
@@ -66,7 +72,7 @@
}
std::shared_ptr<TestInputChannel> mClientTestChannel;
- std::shared_ptr<TestLooper> mTestLooper;
+ sp<Looper> mLooper;
std::unique_ptr<InputConsumerNoResampling> mConsumer;
BlockingQueue<std::unique_ptr<KeyEvent>> mKeyEvents;
@@ -128,7 +134,7 @@
mClientTestChannel->assertNoSentMessages();
- mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT);
+ invokeLooperCallback();
assertOnBatchedInputEventPendingWasCalled();
@@ -166,7 +172,7 @@
mClientTestChannel->assertNoSentMessages();
- mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT);
+ invokeLooperCallback();
assertOnBatchedInputEventPendingWasCalled();
@@ -199,7 +205,7 @@
.action(AMOTION_EVENT_ACTION_DOWN)
.build());
- mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT);
+ invokeLooperCallback();
assertReceivedMotionEvent(AllOf(WithDeviceId(0), WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
mClientTestChannel->enqueueMessage(InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/1}
@@ -220,7 +226,7 @@
.action(AMOTION_EVENT_ACTION_DOWN)
.build());
- mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT);
+ invokeLooperCallback();
assertReceivedMotionEvent(AllOf(WithDeviceId(1), WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
mClientTestChannel->enqueueMessage(InputMessageBuilder{InputMessage::Type::MOTION, /*seq=*/5}
@@ -228,7 +234,7 @@
.action(AMOTION_EVENT_ACTION_UP)
.build());
- mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT);
+ invokeLooperCallback();
assertReceivedMotionEvent(AllOf(WithDeviceId(0), WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Not(MotionEventIsResampled())));
diff --git a/libs/input/tests/TestInputChannel.cpp b/libs/input/tests/TestInputChannel.cpp
index d5f00b6..26a0ca2 100644
--- a/libs/input/tests/TestInputChannel.cpp
+++ b/libs/input/tests/TestInputChannel.cpp
@@ -19,6 +19,11 @@
#include <TestInputChannel.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <array>
+
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <binder/IBinder.h>
@@ -27,13 +32,25 @@
namespace android {
namespace {
-constexpr int FAKE_FD{-1};
+
+/**
+ * Returns a stub file descriptor by opening a socket pair and closing one of the fds. The returned
+ * fd can be used to construct an InputChannel.
+ */
+base::unique_fd generateFileDescriptor() {
+ std::array<int, 2> kFileDescriptors;
+ LOG_IF(FATAL, ::socketpair(AF_UNIX, SOCK_SEQPACKET, 0, kFileDescriptors.data()) != 0)
+ << "TestInputChannel. Failed to create socket pair.";
+ LOG_IF(FATAL, ::close(kFileDescriptors[1]) != 0)
+ << "TestInputChannel. Failed to close file descriptor.";
+ return base::unique_fd{kFileDescriptors[0]};
+}
} // namespace
// --- TestInputChannel ---
TestInputChannel::TestInputChannel(const std::string& name)
- : InputChannel{name, base::unique_fd(FAKE_FD), sp<BBinder>::make()} {}
+ : InputChannel{name, generateFileDescriptor(), sp<BBinder>::make()} {}
void TestInputChannel::enqueueMessage(const InputMessage& message) {
mReceivedMessages.push(message);
diff --git a/libs/input/tests/TestLooper.cpp b/libs/input/tests/TestLooper.cpp
deleted file mode 100644
index e0f01ed..0000000
--- a/libs/input/tests/TestLooper.cpp
+++ /dev/null
@@ -1,51 +0,0 @@
-/**
- * Copyright 2024 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <TestLooper.h>
-
-#include <android-base/logging.h>
-
-namespace android {
-
-TestLooper::TestLooper() : mLooper(sp<Looper>::make(/*allowNonCallbacks=*/false)) {}
-
-int TestLooper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callback,
- void* data) {
- mCallbacks[fd] = callback;
- constexpr int SUCCESS{1};
- return SUCCESS;
-}
-
-int TestLooper::removeFd(int fd) {
- if (auto it = mCallbacks.find(fd); it != mCallbacks.cend()) {
- mCallbacks.erase(fd);
- constexpr int SUCCESS{1};
- return SUCCESS;
- }
- constexpr int FAILURE{0};
- return FAILURE;
-}
-
-void TestLooper::invokeCallback(int fd, int events) {
- auto it = mCallbacks.find(fd);
- LOG_IF(FATAL, it == mCallbacks.cend()) << "Fd does not exist in mCallbacks.";
- mCallbacks[fd]->handleEvent(fd, events, /*data=*/nullptr);
-}
-
-sp<Looper> TestLooper::getLooper() const {
- return mLooper;
-}
-} // namespace android
\ No newline at end of file
diff --git a/libs/input/tests/TestLooper.h b/libs/input/tests/TestLooper.h
deleted file mode 100644
index 3242bc7..0000000
--- a/libs/input/tests/TestLooper.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/**
- * Copyright 2024 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include <map>
-
-#include <input/LooperInterface.h>
-
-namespace android {
-/**
- * TestLooper provides a mechanism to directly trigger Looper's callback.
- */
-class TestLooper final : public LooperInterface {
-public:
- TestLooper();
-
- /**
- * Adds a file descriptor to mCallbacks. Ident, events, and data parameters are ignored. If
- * addFd is called with an existent file descriptor and a different callback, the previous
- * callback is overwritten.
- */
- int addFd(int fd, int ident, int events, const sp<LooperCallback>& callback,
- void* data) override;
-
- /**
- * Removes a file descriptor from mCallbacks. If fd is not in mCallbacks, returns FAILURE.
- */
- int removeFd(int fd) override;
-
- /**
- * Calls handleEvent of the file descriptor. Fd must be in mCallbacks. Otherwise, invokeCallback
- * fatally logs.
- */
- void invokeCallback(int fd, int events);
-
- sp<Looper> getLooper() const override;
-
-private:
- std::map<int /*fd*/, sp<LooperCallback>> mCallbacks;
- sp<Looper> mLooper;
-};
-} // namespace android
\ No newline at end of file