Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c8e38c0f authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add getFdStateDebug to access Looper's callbacks" into main

parents e322d543 7d9c9af9
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -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);
+46 −0
Original line number Diff line number Diff line
@@ -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);

+291 −1812

File changed.

Preview size limit exceeded, changes collapsed.

+349 −1271

File changed.

Preview size limit exceeded, changes collapsed.

+6 −0
Original line number Diff line number Diff line
@@ -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.
     *