Skip to content

Commit 7d9c9af

Browse files
author
Paul Ramirez
committed
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
1 parent 511a173 commit 7d9c9af

File tree

5 files changed

+707
-3083
lines changed

5 files changed

+707
-3083
lines changed

libutils/Looper.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,21 @@ int Looper::addFd(int fd, int ident, int events, const sp<LooperCallback>& callb
496496
return 1;
497497
}
498498

499+
bool Looper::getFdStateDebug(int fd, int* ident, int* events, sp<LooperCallback>* cb, void** data) {
500+
AutoMutex _l(mLock);
501+
if (auto seqNumIt = mSequenceNumberByFd.find(fd); seqNumIt != mSequenceNumberByFd.cend()) {
502+
if (auto reqIt = mRequests.find(seqNumIt->second); reqIt != mRequests.cend()) {
503+
const Request& request = reqIt->second;
504+
if (ident) *ident = request.ident;
505+
if (events) *events = request.events;
506+
if (cb) *cb = request.callback;
507+
if (data) *data = request.data;
508+
return true;
509+
}
510+
}
511+
return false;
512+
}
513+
499514
int Looper::removeFd(int fd) {
500515
AutoMutex _l(mLock);
501516
const auto& it = mSequenceNumberByFd.find(fd);

libutils/Looper_test.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,52 @@ TEST_F(LooperTest, AddFd_WhenNoCallbackAndAllowNonCallbacksIsFalse_ReturnsError)
410410
<< "addFd should return -1 because arguments were invalid";
411411
}
412412

413+
class LooperCallbackStub final : public LooperCallback {
414+
public:
415+
LooperCallbackStub(std::function<int()> callback) : mCallback{callback} {}
416+
417+
int handleEvent(int /*fd*/, int /*events*/, void* /*data*/) override { return mCallback(); }
418+
419+
private:
420+
std::function<int()> mCallback;
421+
};
422+
423+
TEST_F(LooperTest, getFdStateDebug_WhenFdIsInRequests_ReturnsTrue) {
424+
Pipe pipe;
425+
const int fd = pipe.receiveFd;
426+
constexpr int expectedIdent{Looper::POLL_CALLBACK};
427+
sp<LooperCallback> expectedCallback =
428+
sp<LooperCallbackStub>::make([]() constexpr -> int { return 0; });
429+
void* expectedData = this;
430+
431+
EXPECT_EQ(1, mLooper->addFd(fd, expectedIdent, Looper::EVENT_INPUT, expectedCallback,
432+
expectedData));
433+
434+
int ident;
435+
int events;
436+
sp<LooperCallback> callback;
437+
void* data;
438+
439+
EXPECT_TRUE(mLooper->getFdStateDebug(fd, &ident, &events, &callback, &data));
440+
441+
EXPECT_EQ(ident, expectedIdent);
442+
EXPECT_EQ(events, Looper::EVENT_INPUT);
443+
EXPECT_EQ(callback, expectedCallback);
444+
EXPECT_EQ(data, expectedData);
445+
}
446+
447+
TEST_F(LooperTest, getFdStateDebug_WhenFdIsNotInRequests_ReturnsFalse) {
448+
Pipe pipe;
449+
const int notAddedFd = pipe.receiveFd;
450+
451+
int ident;
452+
int events;
453+
sp<LooperCallback> callback;
454+
void* data;
455+
456+
EXPECT_FALSE(mLooper->getFdStateDebug(notAddedFd, &ident, &events, &callback, &data));
457+
}
458+
413459
TEST_F(LooperTest, RemoveFd_WhenCallbackNotAdded_ReturnsZero) {
414460
int result = mLooper->removeFd(1);
415461

0 commit comments

Comments
 (0)