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
This commit is contained in:
Paul Ramirez 2024-09-16 20:44:57 +00:00
parent 511a1733ec
commit 7d9c9af946
5 changed files with 698 additions and 3074 deletions

View file

@ -496,6 +496,21 @@ int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callb
return 1;
}
bool Looper::getFdStateDebug(int fd, int* ident, int* events, sp<LooperCallback>* cb, void** data) {
AutoMutex _l(mLock);
if (auto seqNumIt = mSequenceNumberByFd.find(fd); seqNumIt != mSequenceNumberByFd.cend()) {
if (auto reqIt = mRequests.find(seqNumIt->second); reqIt != mRequests.cend()) {
const Request& request = reqIt->second;
if (ident) *ident = request.ident;
if (events) *events = request.events;
if (cb) *cb = request.callback;
if (data) *data = request.data;
return true;
}
}
return false;
}
int Looper::removeFd(int fd) {
AutoMutex _l(mLock);
const auto& it = mSequenceNumberByFd.find(fd);

View file

@ -410,6 +410,52 @@ TEST_F(LooperTest, AddFd_WhenNoCallbackAndAllowNonCallbacksIsFalse_ReturnsError)
<< "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);

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -322,6 +322,12 @@ public:
int addFd(int fd, int ident, int events, Looper_callbackFunc callback, void* data);
int addFd(int fd, int ident, int events, const sp<LooperCallback>& callback, void* data);
/**
* May be useful for testing, instead of executing a looper on another thread for code expecting
* a looper, you can call callbacks directly.
*/
bool getFdStateDebug(int fd, int* ident, int* events, sp<LooperCallback>* cb, void** data);
/**
* Removes a previously added file descriptor from the looper.
*