diff options
author | 2024-09-18 18:23:14 +0000 | |
---|---|---|
committer | 2024-09-20 14:22:50 +0000 | |
commit | 87f1c01c4e9ddac9f15cbe76c819a1831b49b0de (patch) | |
tree | 1b8b14b592343dc0468802ea773c72d8fda68a8c | |
parent | 00cf5d01b7e51a3ea73f9032ef375c54d2068e43 (diff) |
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
-rw-r--r-- | include/input/InputConsumerNoResampling.h | 13 | ||||
-rw-r--r-- | include/input/LooperInterface.h | 39 | ||||
-rw-r--r-- | libs/input/InputConsumerNoResampling.cpp | 34 | ||||
-rw-r--r-- | libs/input/tests/Android.bp | 1 | ||||
-rw-r--r-- | libs/input/tests/InputConsumer_test.cpp | 28 | ||||
-rw-r--r-- | libs/input/tests/TestInputChannel.cpp | 21 | ||||
-rw-r--r-- | libs/input/tests/TestLooper.cpp | 51 | ||||
-rw-r--r-- | libs/input/tests/TestLooper.h | 56 |
8 files changed, 40 insertions, 203 deletions
diff --git a/include/input/InputConsumerNoResampling.h b/include/input/InputConsumerNoResampling.h index 10c2aa01f9..c98b9cf8c1 100644 --- a/include/input/InputConsumerNoResampling.h +++ b/include/input/InputConsumerNoResampling.h @@ -22,7 +22,6 @@ #include <input/Input.h> #include <input/InputTransport.h> -#include <input/LooperInterface.h> #include <input/Resampler.h> #include <utils/Looper.h> @@ -72,16 +71,6 @@ public: class InputConsumerNoResampling final { public: /** - * This constructor is exclusively for test code. Any real use of InputConsumerNoResampling must - * use the constructor that takes an sp<Looper> parameter instead of - * std::shared_ptr<LooperInterface>. - */ - explicit InputConsumerNoResampling(const std::shared_ptr<InputChannel>& channel, - std::shared_ptr<LooperInterface> looper, - InputConsumerCallbacks& callbacks, - std::unique_ptr<Resampler> resampler); - - /** * @param callbacks are used to interact with InputConsumerNoResampling. They're called whenever * the event is ready to consume. * @param looper needs to be sp and not shared_ptr because it inherits from @@ -126,7 +115,7 @@ public: private: std::shared_ptr<InputChannel> mChannel; - std::shared_ptr<LooperInterface> mLooper; + sp<Looper> mLooper; InputConsumerCallbacks& mCallbacks; std::unique_ptr<Resampler> mResampler; diff --git a/include/input/LooperInterface.h b/include/input/LooperInterface.h deleted file mode 100644 index 2d6719c965..0000000000 --- a/include/input/LooperInterface.h +++ /dev/null @@ -1,39 +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 <utils/Looper.h> -#include <utils/StrongPointer.h> - -namespace android { - -/** - * LooperInterface allows the use of TestLooper in InputConsumerNoResampling without reassigning to - * Looper. LooperInterface is needed to control how InputConsumerNoResampling consumes and batches - * InputMessages. - */ -class LooperInterface { -public: - virtual ~LooperInterface() = default; - - virtual int addFd(int fd, int ident, int events, const sp<LooperCallback>& callback, - void* data) = 0; - virtual int removeFd(int fd) = 0; - - virtual sp<Looper> getLooper() const = 0; -}; -} // namespace android diff --git a/libs/input/InputConsumerNoResampling.cpp b/libs/input/InputConsumerNoResampling.cpp index f33afe75fc..cdbc1869c3 100644 --- a/libs/input/InputConsumerNoResampling.cpp +++ b/libs/input/InputConsumerNoResampling.cpp @@ -46,27 +46,6 @@ using std::chrono::nanoseconds; 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 @@ using android::base::Result; // --- 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 @@ InputConsumerNoResampling::InputConsumerNoResampling(const std::shared_ptr<Input 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 @@ bool InputConsumerNoResampling::consumeBatchedInputEvents( 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 3ec167a288..81c6175805 100644 --- a/libs/input/tests/Android.bp +++ b/libs/input/tests/Android.bp @@ -27,7 +27,6 @@ cc_test { "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 dec78aa21e..d708316236 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 @@ class InputConsumerTest : public testing::Test, public InputConsumerCallbacks { 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 @@ protected: } 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 @@ TEST_F(InputConsumerTest, MessageStreamBatchedInMotionEvent) { mClientTestChannel->assertNoSentMessages(); - mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT); + invokeLooperCallback(); assertOnBatchedInputEventPendingWasCalled(); @@ -166,7 +172,7 @@ TEST_F(InputConsumerTest, LastBatchedSampleIsLessThanResampleTime) { mClientTestChannel->assertNoSentMessages(); - mTestLooper->invokeCallback(mClientTestChannel->getFd(), ALOOPER_EVENT_INPUT); + invokeLooperCallback(); assertOnBatchedInputEventPendingWasCalled(); @@ -199,7 +205,7 @@ TEST_F(InputConsumerTest, BatchedEventsMultiDeviceConsumption) { .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 @@ TEST_F(InputConsumerTest, BatchedEventsMultiDeviceConsumption) { .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 @@ TEST_F(InputConsumerTest, BatchedEventsMultiDeviceConsumption) { .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 d5f00b699b..26a0ca2106 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 e0f01ed25d..0000000000 --- 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 3242bc7699..0000000000 --- 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 |