Add getFdStateDebug to access Looper's callbacks
Added getFdStateDebug to access Looper's internal state.
Flag: EXEMPT testing
Test: TEST=libutils_test; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST --gtest_filter="LooperTest.getFdStateDebug*"
Change-Id: I253ed4a6fa1040053117dcea3be80e55eef9a9b0
diff --git a/libutils/Looper_test.cpp b/libutils/Looper_test.cpp
index c859f9c..a7a9987 100644
--- a/libutils/Looper_test.cpp
+++ b/libutils/Looper_test.cpp
@@ -410,6 +410,52 @@
<< "addFd should return -1 because arguments were invalid";
}
+class LooperCallbackStub final : public LooperCallback {
+ public:
+ LooperCallbackStub(std::function<int()> callback) : mCallback{callback} {}
+
+ int handleEvent(int /*fd*/, int /*events*/, void* /*data*/) override { return mCallback(); }
+
+ private:
+ std::function<int()> mCallback;
+};
+
+TEST_F(LooperTest, getFdStateDebug_WhenFdIsInRequests_ReturnsTrue) {
+ Pipe pipe;
+ const int fd = pipe.receiveFd;
+ constexpr int expectedIdent{Looper::POLL_CALLBACK};
+ sp<LooperCallback> expectedCallback =
+ sp<LooperCallbackStub>::make([]() constexpr -> int { return 0; });
+ void* expectedData = this;
+
+ EXPECT_EQ(1, mLooper->addFd(fd, expectedIdent, Looper::EVENT_INPUT, expectedCallback,
+ expectedData));
+
+ int ident;
+ int events;
+ sp<LooperCallback> callback;
+ void* data;
+
+ EXPECT_TRUE(mLooper->getFdStateDebug(fd, &ident, &events, &callback, &data));
+
+ EXPECT_EQ(ident, expectedIdent);
+ EXPECT_EQ(events, Looper::EVENT_INPUT);
+ EXPECT_EQ(callback, expectedCallback);
+ EXPECT_EQ(data, expectedData);
+}
+
+TEST_F(LooperTest, getFdStateDebug_WhenFdIsNotInRequests_ReturnsFalse) {
+ Pipe pipe;
+ const int notAddedFd = pipe.receiveFd;
+
+ int ident;
+ int events;
+ sp<LooperCallback> callback;
+ void* data;
+
+ EXPECT_FALSE(mLooper->getFdStateDebug(notAddedFd, &ident, &events, &callback, &data));
+}
+
TEST_F(LooperTest, RemoveFd_WhenCallbackNotAdded_ReturnsZero) {
int result = mLooper->removeFd(1);