From 1693d7e48f976c2615100378c7e98d245e0213be Mon Sep 17 00:00:00 2001 From: Brian Carlstrom Date: Wed, 11 Dec 2013 22:46:45 -0800 Subject: [PATCH] Make libutils Looper independent of frameworks/native Change-Id: I1dacca10a3cd7601abc3451b69ed761bf71c38fe --- include/utils/Looper.h | 139 +++++++++++++++++++++++------ libutils/Looper.cpp | 40 ++++----- libutils/tests/Looper_test.cpp | 158 ++++++++++++++++----------------- 3 files changed, 213 insertions(+), 124 deletions(-) diff --git a/include/utils/Looper.h b/include/utils/Looper.h index 2e0651a3c..15c989176 100644 --- a/include/utils/Looper.h +++ b/include/utils/Looper.h @@ -22,18 +22,28 @@ #include #include -#include - #include -/* - * Declare a concrete type for the NDK's looper forward declaration. - */ -struct ALooper { -}; - namespace android { +/* + * NOTE: Since Looper is used to implement the NDK ALooper, the Looper + * enums and the signature of Looper_callbackFunc need to align with + * that implementation. + */ + +/** + * For callback-based event loops, this is the prototype of the function + * that is called when a file descriptor event occurs. + * It is given the file descriptor it is associated with, + * a bitmask of the poll events that were triggered (typically EVENT_INPUT), + * and the data pointer that was originally supplied. + * + * Implementations should return 1 to continue receiving callbacks, or 0 + * to have this file descriptor and callback unregistered from the looper. + */ +typedef int (*Looper_callbackFunc)(int fd, int events, void* data); + /** * A message that can be posted to a Looper. */ @@ -93,7 +103,7 @@ public: /** * Handles a poll event for the given file descriptor. * It is given the file descriptor it is associated with, - * a bitmask of the poll events that were triggered (typically ALOOPER_EVENT_INPUT), + * a bitmask of the poll events that were triggered (typically EVENT_INPUT), * and the data pointer that was originally supplied. * * Implementations should return 1 to continue receiving callbacks, or 0 @@ -102,34 +112,113 @@ public: virtual int handleEvent(int fd, int events, void* data) = 0; }; - /** - * Wraps a ALooper_callbackFunc function pointer. + * Wraps a Looper_callbackFunc function pointer. */ class SimpleLooperCallback : public LooperCallback { protected: virtual ~SimpleLooperCallback(); public: - SimpleLooperCallback(ALooper_callbackFunc callback); + SimpleLooperCallback(Looper_callbackFunc callback); virtual int handleEvent(int fd, int events, void* data); private: - ALooper_callbackFunc mCallback; + Looper_callbackFunc mCallback; }; - /** * A polling loop that supports monitoring file descriptor events, optionally * using callbacks. The implementation uses epoll() internally. * * A looper can be associated with a thread although there is no requirement that it must be. */ -class Looper : public ALooper, public RefBase { +class Looper : public RefBase { protected: virtual ~Looper(); public: + enum { + /** + * Result from Looper_pollOnce() and Looper_pollAll(): + * The poll was awoken using wake() before the timeout expired + * and no callbacks were executed and no other file descriptors were ready. + */ + POLL_WAKE = -1, + + /** + * Result from Looper_pollOnce() and Looper_pollAll(): + * One or more callbacks were executed. + */ + POLL_CALLBACK = -2, + + /** + * Result from Looper_pollOnce() and Looper_pollAll(): + * The timeout expired. + */ + POLL_TIMEOUT = -3, + + /** + * Result from Looper_pollOnce() and Looper_pollAll(): + * An error occurred. + */ + POLL_ERROR = -4, + }; + + /** + * Flags for file descriptor events that a looper can monitor. + * + * These flag bits can be combined to monitor multiple events at once. + */ + enum { + /** + * The file descriptor is available for read operations. + */ + EVENT_INPUT = 1 << 0, + + /** + * The file descriptor is available for write operations. + */ + EVENT_OUTPUT = 1 << 1, + + /** + * The file descriptor has encountered an error condition. + * + * The looper always sends notifications about errors; it is not necessary + * to specify this event flag in the requested event set. + */ + EVENT_ERROR = 1 << 2, + + /** + * The file descriptor was hung up. + * For example, indicates that the remote end of a pipe or socket was closed. + * + * The looper always sends notifications about hangups; it is not necessary + * to specify this event flag in the requested event set. + */ + EVENT_HANGUP = 1 << 3, + + /** + * The file descriptor is invalid. + * For example, the file descriptor was closed prematurely. + * + * The looper always sends notifications about invalid file descriptors; it is not necessary + * to specify this event flag in the requested event set. + */ + EVENT_INVALID = 1 << 4, + }; + + enum { + /** + * Option for Looper_prepare: this looper will accept calls to + * Looper_addFd() that do not have a callback (that is provide NULL + * for the callback). In this case the caller of Looper_pollOnce() + * or Looper_pollAll() MUST check the return from these functions to + * discover when data is available on such fds and process it. + */ + PREPARE_ALLOW_NON_CALLBACKS = 1<<0 + }; + /** * Creates a looper. * @@ -152,16 +241,16 @@ public: * If the timeout is zero, returns immediately without blocking. * If the timeout is negative, waits indefinitely until an event appears. * - * Returns ALOOPER_POLL_WAKE if the poll was awoken using wake() before + * Returns POLL_WAKE if the poll was awoken using wake() before * the timeout expired and no callbacks were invoked and no other file * descriptors were ready. * - * Returns ALOOPER_POLL_CALLBACK if one or more callbacks were invoked. + * Returns POLL_CALLBACK if one or more callbacks were invoked. * - * Returns ALOOPER_POLL_TIMEOUT if there was no data before the given + * Returns POLL_TIMEOUT if there was no data before the given * timeout expired. * - * Returns ALOOPER_POLL_ERROR if an error occurred. + * Returns POLL_ERROR if an error occurred. * * Returns a value >= 0 containing an identifier if its file descriptor has data * and it has no callback function (requiring the caller here to handle it). @@ -179,7 +268,7 @@ public: /** * Like pollOnce(), but performs all pending callbacks until all * data has been consumed or a file descriptor is available with no callback. - * This function will never return ALOOPER_POLL_CALLBACK. + * This function will never return POLL_CALLBACK. */ int pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData); inline int pollAll(int timeoutMillis) { @@ -200,8 +289,8 @@ public: * * "fd" is the file descriptor to be added. * "ident" is an identifier for this event, which is returned from pollOnce(). - * The identifier must be >= 0, or ALOOPER_POLL_CALLBACK if providing a non-NULL callback. - * "events" are the poll events to wake up on. Typically this is ALOOPER_EVENT_INPUT. + * The identifier must be >= 0, or POLL_CALLBACK if providing a non-NULL callback. + * "events" are the poll events to wake up on. Typically this is EVENT_INPUT. * "callback" is the function to call when there is an event on the file descriptor. * "data" is a private data pointer to supply to the callback. * @@ -211,7 +300,7 @@ public: * data on the file descriptor. It should execute any events it has pending, * appropriately reading from the file descriptor. The 'ident' is ignored in this case. * - * (2) If "callback" is NULL, the 'ident' will be returned by ALooper_pollOnce + * (2) If "callback" is NULL, the 'ident' will be returned by Looper_pollOnce * when its file descriptor has data available, requiring the caller to take * care of processing it. * @@ -225,7 +314,7 @@ public: * easier to avoid races when the callback is removed from a different thread. * See removeFd() for details. */ - int addFd(int fd, int ident, int events, ALooper_callbackFunc callback, void* data); + int addFd(int fd, int ident, int events, Looper_callbackFunc callback, void* data); int addFd(int fd, int ident, int events, const sp& callback, void* data); /** @@ -308,7 +397,7 @@ public: * If the thread already has a looper, it is returned. Otherwise, a new * one is created, associated with the thread, and returned. * - * The opts may be ALOOPER_PREPARE_ALLOW_NON_CALLBACKS or 0. + * The opts may be PREPARE_ALLOW_NON_CALLBACKS or 0. */ static sp prepare(int opts); diff --git a/libutils/Looper.cpp b/libutils/Looper.cpp index c51df2d1a..9a2dd6cc4 100644 --- a/libutils/Looper.cpp +++ b/libutils/Looper.cpp @@ -43,7 +43,7 @@ void WeakMessageHandler::handleMessage(const Message& message) { // --- SimpleLooperCallback --- -SimpleLooperCallback::SimpleLooperCallback(ALooper_callbackFunc callback) : +SimpleLooperCallback::SimpleLooperCallback(Looper_callbackFunc callback) : mCallback(callback) { } @@ -139,7 +139,7 @@ sp Looper::getForThread() { } sp Looper::prepare(int opts) { - bool allowNonCallbacks = opts & ALOOPER_PREPARE_ALLOW_NON_CALLBACKS; + bool allowNonCallbacks = opts & PREPARE_ALLOW_NON_CALLBACKS; sp looper = Looper::getForThread(); if (looper == NULL) { looper = new Looper(allowNonCallbacks); @@ -147,7 +147,7 @@ sp Looper::prepare(int opts) { } if (looper->getAllowNonCallbacks() != allowNonCallbacks) { ALOGW("Looper already prepared for this thread with a different value for the " - "ALOOPER_PREPARE_ALLOW_NON_CALLBACKS option."); + "LOOPER_PREPARE_ALLOW_NON_CALLBACKS option."); } return looper; } @@ -212,7 +212,7 @@ int Looper::pollInner(int timeoutMillis) { } // Poll. - int result = ALOOPER_POLL_WAKE; + int result = POLL_WAKE; mResponses.clear(); mResponseIndex = 0; @@ -234,7 +234,7 @@ int Looper::pollInner(int timeoutMillis) { goto Done; } ALOGW("Poll failed with an unexpected error, errno=%d", errno); - result = ALOOPER_POLL_ERROR; + result = POLL_ERROR; goto Done; } @@ -243,7 +243,7 @@ int Looper::pollInner(int timeoutMillis) { #if DEBUG_POLL_AND_WAKE ALOGD("%p ~ pollOnce - timeout", this); #endif - result = ALOOPER_POLL_TIMEOUT; + result = POLL_TIMEOUT; goto Done; } @@ -265,10 +265,10 @@ int Looper::pollInner(int timeoutMillis) { ssize_t requestIndex = mRequests.indexOfKey(fd); if (requestIndex >= 0) { int events = 0; - if (epollEvents & EPOLLIN) events |= ALOOPER_EVENT_INPUT; - if (epollEvents & EPOLLOUT) events |= ALOOPER_EVENT_OUTPUT; - if (epollEvents & EPOLLERR) events |= ALOOPER_EVENT_ERROR; - if (epollEvents & EPOLLHUP) events |= ALOOPER_EVENT_HANGUP; + if (epollEvents & EPOLLIN) events |= EVENT_INPUT; + if (epollEvents & EPOLLOUT) events |= EVENT_OUTPUT; + if (epollEvents & EPOLLERR) events |= EVENT_ERROR; + if (epollEvents & EPOLLHUP) events |= EVENT_HANGUP; pushResponse(events, mRequests.valueAt(requestIndex)); } else { ALOGW("Ignoring unexpected epoll events 0x%x on fd %d that is " @@ -304,7 +304,7 @@ Done: ; mLock.lock(); mSendingMessage = false; - result = ALOOPER_POLL_CALLBACK; + result = POLL_CALLBACK; } else { // The last message left at the head of the queue determines the next wakeup time. mNextMessageUptime = messageEnvelope.uptime; @@ -318,7 +318,7 @@ Done: ; // Invoke all response callbacks. for (size_t i = 0; i < mResponses.size(); i++) { Response& response = mResponses.editItemAt(i); - if (response.request.ident == ALOOPER_POLL_CALLBACK) { + if (response.request.ident == POLL_CALLBACK) { int fd = response.request.fd; int events = response.events; void* data = response.request.data; @@ -333,7 +333,7 @@ Done: ; // Clear the callback reference in the response structure promptly because we // will not clear the response vector itself until the next poll. response.request.callback.clear(); - result = ALOOPER_POLL_CALLBACK; + result = POLL_CALLBACK; } } return result; @@ -344,7 +344,7 @@ int Looper::pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outDat int result; do { result = pollOnce(timeoutMillis, outFd, outEvents, outData); - } while (result == ALOOPER_POLL_CALLBACK); + } while (result == POLL_CALLBACK); return result; } else { nsecs_t endTime = systemTime(SYSTEM_TIME_MONOTONIC) @@ -352,14 +352,14 @@ int Looper::pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outDat for (;;) { int result = pollOnce(timeoutMillis, outFd, outEvents, outData); - if (result != ALOOPER_POLL_CALLBACK) { + if (result != POLL_CALLBACK) { return result; } nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); timeoutMillis = toMillisecondTimeoutDelay(now, endTime); if (timeoutMillis == 0) { - return ALOOPER_POLL_TIMEOUT; + return POLL_TIMEOUT; } } } @@ -401,7 +401,7 @@ void Looper::pushResponse(int events, const Request& request) { mResponses.push(response); } -int Looper::addFd(int fd, int ident, int events, ALooper_callbackFunc callback, void* data) { +int Looper::addFd(int fd, int ident, int events, Looper_callbackFunc callback, void* data) { return addFd(fd, ident, events, callback ? new SimpleLooperCallback(callback) : NULL, data); } @@ -422,12 +422,12 @@ int Looper::addFd(int fd, int ident, int events, const sp& callb return -1; } } else { - ident = ALOOPER_POLL_CALLBACK; + ident = POLL_CALLBACK; } int epollEvents = 0; - if (events & ALOOPER_EVENT_INPUT) epollEvents |= EPOLLIN; - if (events & ALOOPER_EVENT_OUTPUT) epollEvents |= EPOLLOUT; + if (events & EVENT_INPUT) epollEvents |= EPOLLIN; + if (events & EVENT_OUTPUT) epollEvents |= EPOLLOUT; { // acquire lock AutoMutex _l(mLock); diff --git a/libutils/tests/Looper_test.cpp b/libutils/tests/Looper_test.cpp index 8bf2ba297..00077e676 100644 --- a/libutils/tests/Looper_test.cpp +++ b/libutils/tests/Looper_test.cpp @@ -119,8 +119,8 @@ TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndNotAwoken_WaitsForTimeout) { EXPECT_NEAR(100, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. equal timeout"; - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be LOOPER_POLL_TIMEOUT"; } TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndAwokenBeforeWaiting_ImmediatelyReturns) { @@ -132,8 +132,8 @@ TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndAwokenBeforeWaiting_Immediately EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because wake() was called before waiting"; - EXPECT_EQ(ALOOPER_POLL_WAKE, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because loop was awoken"; + EXPECT_EQ(Looper::POLL_WAKE, result) + << "pollOnce result should be Looper::POLL_CALLBACK because loop was awoken"; } TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndAwokenWhileWaiting_PromptlyReturns) { @@ -146,8 +146,8 @@ TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndAwokenWhileWaiting_PromptlyRetu EXPECT_NEAR(100, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. equal wake delay"; - EXPECT_EQ(ALOOPER_POLL_WAKE, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because loop was awoken"; + EXPECT_EQ(Looper::POLL_WAKE, result) + << "pollOnce result should be Looper::POLL_CALLBACK because loop was awoken"; } TEST_F(LooperTest, PollOnce_WhenZeroTimeoutAndNoRegisteredFDs_ImmediatelyReturns) { @@ -157,15 +157,15 @@ TEST_F(LooperTest, PollOnce_WhenZeroTimeoutAndNoRegisteredFDs_ImmediatelyReturns EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should be approx. zero"; - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be Looper::POLL_TIMEOUT"; } TEST_F(LooperTest, PollOnce_WhenZeroTimeoutAndNoSignalledFDs_ImmediatelyReturns) { Pipe pipe; StubCallbackHandler handler(true); - handler.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); + handler.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); StopWatch stopWatch("pollOnce"); int result = mLooper->pollOnce(0); @@ -173,8 +173,8 @@ TEST_F(LooperTest, PollOnce_WhenZeroTimeoutAndNoSignalledFDs_ImmediatelyReturns) EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should be approx. zero"; - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be Looper::POLL_TIMEOUT"; EXPECT_EQ(0, handler.callbackCount) << "callback should not have been invoked because FD was not signalled"; } @@ -184,7 +184,7 @@ TEST_F(LooperTest, PollOnce_WhenZeroTimeoutAndSignalledFD_ImmediatelyInvokesCall StubCallbackHandler handler(true); ASSERT_EQ(OK, pipe.writeSignal()); - handler.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); + handler.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); StopWatch stopWatch("pollOnce"); int result = mLooper->pollOnce(0); @@ -192,21 +192,21 @@ TEST_F(LooperTest, PollOnce_WhenZeroTimeoutAndSignalledFD_ImmediatelyInvokesCall EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should be approx. zero"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because FD was signalled"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because FD was signalled"; EXPECT_EQ(1, handler.callbackCount) << "callback should be invoked exactly once"; EXPECT_EQ(pipe.receiveFd, handler.fd) << "callback should have received pipe fd as parameter"; - EXPECT_EQ(ALOOPER_EVENT_INPUT, handler.events) - << "callback should have received ALOOPER_EVENT_INPUT as events"; + EXPECT_EQ(Looper::EVENT_INPUT, handler.events) + << "callback should have received Looper::EVENT_INPUT as events"; } TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndNoSignalledFDs_WaitsForTimeoutAndReturns) { Pipe pipe; StubCallbackHandler handler(true); - handler.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); + handler.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); StopWatch stopWatch("pollOnce"); int result = mLooper->pollOnce(100); @@ -214,8 +214,8 @@ TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndNoSignalledFDs_WaitsForTimeoutA EXPECT_NEAR(100, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. equal timeout"; - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be Looper::POLL_TIMEOUT"; EXPECT_EQ(0, handler.callbackCount) << "callback should not have been invoked because FD was not signalled"; } @@ -225,7 +225,7 @@ TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndSignalledFDBeforeWaiting_Immedi StubCallbackHandler handler(true); pipe.writeSignal(); - handler.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); + handler.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); StopWatch stopWatch("pollOnce"); int result = mLooper->pollOnce(100); @@ -235,14 +235,14 @@ TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndSignalledFDBeforeWaiting_Immedi << "signal should actually have been written"; EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should be approx. zero"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because FD was signalled"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because FD was signalled"; EXPECT_EQ(1, handler.callbackCount) << "callback should be invoked exactly once"; EXPECT_EQ(pipe.receiveFd, handler.fd) << "callback should have received pipe fd as parameter"; - EXPECT_EQ(ALOOPER_EVENT_INPUT, handler.events) - << "callback should have received ALOOPER_EVENT_INPUT as events"; + EXPECT_EQ(Looper::EVENT_INPUT, handler.events) + << "callback should have received Looper::EVENT_INPUT as events"; } TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndSignalledFDWhileWaiting_PromptlyInvokesCallbackAndReturns) { @@ -250,7 +250,7 @@ TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndSignalledFDWhileWaiting_Promptl StubCallbackHandler handler(true); sp delayedWriteSignal = new DelayedWriteSignal(100, & pipe); - handler.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); + handler.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); delayedWriteSignal->run(); StopWatch stopWatch("pollOnce"); @@ -261,21 +261,21 @@ TEST_F(LooperTest, PollOnce_WhenNonZeroTimeoutAndSignalledFDWhileWaiting_Promptl << "signal should actually have been written"; EXPECT_NEAR(100, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. equal signal delay"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because FD was signalled"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because FD was signalled"; EXPECT_EQ(1, handler.callbackCount) << "callback should be invoked exactly once"; EXPECT_EQ(pipe.receiveFd, handler.fd) << "callback should have received pipe fd as parameter"; - EXPECT_EQ(ALOOPER_EVENT_INPUT, handler.events) - << "callback should have received ALOOPER_EVENT_INPUT as events"; + EXPECT_EQ(Looper::EVENT_INPUT, handler.events) + << "callback should have received Looper::EVENT_INPUT as events"; } TEST_F(LooperTest, PollOnce_WhenCallbackAddedThenRemoved_CallbackShouldNotBeInvoked) { Pipe pipe; StubCallbackHandler handler(true); - handler.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); + handler.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); pipe.writeSignal(); // would cause FD to be considered signalled mLooper->removeFd(pipe.receiveFd); @@ -287,8 +287,8 @@ TEST_F(LooperTest, PollOnce_WhenCallbackAddedThenRemoved_CallbackShouldNotBeInvo << "signal should actually have been written"; EXPECT_NEAR(100, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. equal timeout because FD was no longer registered"; - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be Looper::POLL_TIMEOUT"; EXPECT_EQ(0, handler.callbackCount) << "callback should not be invoked"; } @@ -297,7 +297,7 @@ TEST_F(LooperTest, PollOnce_WhenCallbackReturnsFalse_CallbackShouldNotBeInvokedA Pipe pipe; StubCallbackHandler handler(false); - handler.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); + handler.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); // First loop: Callback is registered and FD is signalled. pipe.writeSignal(); @@ -310,8 +310,8 @@ TEST_F(LooperTest, PollOnce_WhenCallbackReturnsFalse_CallbackShouldNotBeInvokedA << "signal should actually have been written"; EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. equal zero because FD was already signalled"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because FD was signalled"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because FD was signalled"; EXPECT_EQ(1, handler.callbackCount) << "callback should be invoked"; @@ -326,8 +326,8 @@ TEST_F(LooperTest, PollOnce_WhenCallbackReturnsFalse_CallbackShouldNotBeInvokedA << "signal should actually have been written"; EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. equal zero because timeout was zero"; - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be Looper::POLL_TIMEOUT"; EXPECT_EQ(1, handler.callbackCount) << "callback should not be invoked this time"; } @@ -339,7 +339,7 @@ TEST_F(LooperTest, PollOnce_WhenNonCallbackFdIsSignalled_ReturnsIdent) { Pipe pipe; pipe.writeSignal(); - mLooper->addFd(pipe.receiveFd, expectedIdent, ALOOPER_EVENT_INPUT, NULL, expectedData); + mLooper->addFd(pipe.receiveFd, expectedIdent, Looper::EVENT_INPUT, NULL, expectedData); StopWatch stopWatch("pollOnce"); int fd; @@ -356,15 +356,15 @@ TEST_F(LooperTest, PollOnce_WhenNonCallbackFdIsSignalled_ReturnsIdent) { << "pollOnce result should be the ident of the FD that was signalled"; EXPECT_EQ(pipe.receiveFd, fd) << "pollOnce should have returned the received pipe fd"; - EXPECT_EQ(ALOOPER_EVENT_INPUT, events) - << "pollOnce should have returned ALOOPER_EVENT_INPUT as events"; + EXPECT_EQ(Looper::EVENT_INPUT, events) + << "pollOnce should have returned Looper::EVENT_INPUT as events"; EXPECT_EQ(expectedData, data) << "pollOnce should have returned the data"; } TEST_F(LooperTest, AddFd_WhenCallbackAdded_ReturnsOne) { Pipe pipe; - int result = mLooper->addFd(pipe.receiveFd, 0, ALOOPER_EVENT_INPUT, NULL, NULL); + int result = mLooper->addFd(pipe.receiveFd, 0, Looper::EVENT_INPUT, NULL, NULL); EXPECT_EQ(1, result) << "addFd should return 1 because FD was added"; @@ -372,7 +372,7 @@ TEST_F(LooperTest, AddFd_WhenCallbackAdded_ReturnsOne) { TEST_F(LooperTest, AddFd_WhenIdentIsNegativeAndCallbackIsNull_ReturnsError) { Pipe pipe; - int result = mLooper->addFd(pipe.receiveFd, -1, ALOOPER_EVENT_INPUT, NULL, NULL); + int result = mLooper->addFd(pipe.receiveFd, -1, Looper::EVENT_INPUT, NULL, NULL); EXPECT_EQ(-1, result) << "addFd should return -1 because arguments were invalid"; @@ -397,7 +397,7 @@ TEST_F(LooperTest, RemoveFd_WhenCallbackNotAdded_ReturnsZero) { TEST_F(LooperTest, RemoveFd_WhenCallbackAddedThenRemovedTwice_ReturnsOnceFirstTimeAndReturnsZeroSecondTime) { Pipe pipe; StubCallbackHandler handler(false); - handler.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); + handler.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); // First time. int result = mLooper->removeFd(pipe.receiveFd); @@ -417,8 +417,8 @@ TEST_F(LooperTest, PollOnce_WhenCallbackAddedTwice_OnlySecondCallbackShouldBeInv StubCallbackHandler handler1(true); StubCallbackHandler handler2(true); - handler1.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); - handler2.setCallback(mLooper, pipe.receiveFd, ALOOPER_EVENT_INPUT); // replace it + handler1.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); + handler2.setCallback(mLooper, pipe.receiveFd, Looper::EVENT_INPUT); // replace it pipe.writeSignal(); // would cause FD to be considered signalled StopWatch stopWatch("pollOnce"); @@ -429,8 +429,8 @@ TEST_F(LooperTest, PollOnce_WhenCallbackAddedTwice_OnlySecondCallbackShouldBeInv << "signal should actually have been written"; EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because FD was already signalled"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because FD was signalled"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because FD was signalled"; EXPECT_EQ(0, handler1.callbackCount) << "original handler callback should not be invoked because it was replaced"; EXPECT_EQ(1, handler2.callbackCount) @@ -447,8 +447,8 @@ TEST_F(LooperTest, SendMessage_WhenOneMessageIsEnqueue_ShouldInvokeHandlerDuring EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because message was already sent"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because message was sent"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because message was sent"; EXPECT_EQ(size_t(1), handler->messages.size()) << "handled message"; EXPECT_EQ(MSG_TEST1, handler->messages[0].what) @@ -469,8 +469,8 @@ TEST_F(LooperTest, SendMessage_WhenMultipleMessagesAreEnqueued_ShouldInvokeHandl EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because message was already sent"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because message was sent"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because message was sent"; EXPECT_EQ(size_t(3), handler1->messages.size()) << "handled message"; EXPECT_EQ(MSG_TEST1, handler1->messages[0].what) @@ -495,8 +495,8 @@ TEST_F(LooperTest, SendMessageDelayed_WhenSentToTheFuture_ShouldInvokeHandlerAft EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "first poll should end quickly because next message timeout was computed"; - EXPECT_EQ(ALOOPER_POLL_WAKE, result) - << "pollOnce result should be ALOOPER_POLL_WAKE due to wakeup"; + EXPECT_EQ(Looper::POLL_WAKE, result) + << "pollOnce result should be Looper::POLL_WAKE due to wakeup"; EXPECT_EQ(size_t(0), handler->messages.size()) << "no message handled yet"; @@ -509,16 +509,16 @@ TEST_F(LooperTest, SendMessageDelayed_WhenSentToTheFuture_ShouldInvokeHandlerAft << "handled message"; EXPECT_NEAR(100, elapsedMillis, TIMING_TOLERANCE_MS) << "second poll should end around the time of the delayed message dispatch"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because message was sent"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because message was sent"; result = mLooper->pollOnce(100); elapsedMillis = ns2ms(stopWatch.elapsedTime()); EXPECT_NEAR(100 + 100, elapsedMillis, TIMING_TOLERANCE_MS) << "third poll should timeout"; - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT because there were no messages left"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be Looper::POLL_TIMEOUT because there were no messages left"; } TEST_F(LooperTest, SendMessageDelayed_WhenSentToThePast_ShouldInvokeHandlerDuringNextPoll) { @@ -531,8 +531,8 @@ TEST_F(LooperTest, SendMessageDelayed_WhenSentToThePast_ShouldInvokeHandlerDurin EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because message was already sent"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because message was sent"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because message was sent"; EXPECT_EQ(size_t(1), handler->messages.size()) << "handled message"; EXPECT_EQ(MSG_TEST1, handler->messages[0].what) @@ -549,8 +549,8 @@ TEST_F(LooperTest, SendMessageDelayed_WhenSentToThePresent_ShouldInvokeHandlerDu EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because message was already sent"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because message was sent"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because message was sent"; EXPECT_EQ(size_t(1), handler->messages.size()) << "handled message"; EXPECT_EQ(MSG_TEST1, handler->messages[0].what) @@ -568,8 +568,8 @@ TEST_F(LooperTest, SendMessageAtTime_WhenSentToTheFuture_ShouldInvokeHandlerAfte EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "first poll should end quickly because next message timeout was computed"; - EXPECT_EQ(ALOOPER_POLL_WAKE, result) - << "pollOnce result should be ALOOPER_POLL_WAKE due to wakeup"; + EXPECT_EQ(Looper::POLL_WAKE, result) + << "pollOnce result should be Looper::POLL_WAKE due to wakeup"; EXPECT_EQ(size_t(0), handler->messages.size()) << "no message handled yet"; @@ -582,16 +582,16 @@ TEST_F(LooperTest, SendMessageAtTime_WhenSentToTheFuture_ShouldInvokeHandlerAfte << "handled message"; EXPECT_NEAR(100, elapsedMillis, TIMING_TOLERANCE_MS) << "second poll should end around the time of the delayed message dispatch"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because message was sent"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because message was sent"; result = mLooper->pollOnce(100); elapsedMillis = ns2ms(stopWatch.elapsedTime()); EXPECT_NEAR(100 + 100, elapsedMillis, TIMING_TOLERANCE_MS) << "third poll should timeout"; - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT because there were no messages left"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be Looper::POLL_TIMEOUT because there were no messages left"; } TEST_F(LooperTest, SendMessageAtTime_WhenSentToThePast_ShouldInvokeHandlerDuringNextPoll) { @@ -605,8 +605,8 @@ TEST_F(LooperTest, SendMessageAtTime_WhenSentToThePast_ShouldInvokeHandlerDuring EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because message was already sent"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because message was sent"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because message was sent"; EXPECT_EQ(size_t(1), handler->messages.size()) << "handled message"; EXPECT_EQ(MSG_TEST1, handler->messages[0].what) @@ -624,8 +624,8 @@ TEST_F(LooperTest, SendMessageAtTime_WhenSentToThePresent_ShouldInvokeHandlerDur EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because message was already sent"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because message was sent"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because message was sent"; EXPECT_EQ(size_t(1), handler->messages.size()) << "handled message"; EXPECT_EQ(MSG_TEST1, handler->messages[0].what) @@ -645,15 +645,15 @@ TEST_F(LooperTest, RemoveMessage_WhenRemovingAllMessagesForHandler_ShouldRemoveT EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because message was sent so looper was awoken"; - EXPECT_EQ(ALOOPER_POLL_WAKE, result) - << "pollOnce result should be ALOOPER_POLL_WAKE because looper was awoken"; + EXPECT_EQ(Looper::POLL_WAKE, result) + << "pollOnce result should be Looper::POLL_WAKE because looper was awoken"; EXPECT_EQ(size_t(0), handler->messages.size()) << "no messages to handle"; result = mLooper->pollOnce(0); - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT because there was nothing to do"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be Looper::POLL_TIMEOUT because there was nothing to do"; EXPECT_EQ(size_t(0), handler->messages.size()) << "no messages to handle"; } @@ -673,8 +673,8 @@ TEST_F(LooperTest, RemoveMessage_WhenRemovingSomeMessagesForHandler_ShouldRemove EXPECT_NEAR(0, elapsedMillis, TIMING_TOLERANCE_MS) << "elapsed time should approx. zero because message was sent so looper was awoken"; - EXPECT_EQ(ALOOPER_POLL_CALLBACK, result) - << "pollOnce result should be ALOOPER_POLL_CALLBACK because two messages were sent"; + EXPECT_EQ(Looper::POLL_CALLBACK, result) + << "pollOnce result should be Looper::POLL_CALLBACK because two messages were sent"; EXPECT_EQ(size_t(2), handler->messages.size()) << "no messages to handle"; EXPECT_EQ(MSG_TEST2, handler->messages[0].what) @@ -684,8 +684,8 @@ TEST_F(LooperTest, RemoveMessage_WhenRemovingSomeMessagesForHandler_ShouldRemove result = mLooper->pollOnce(0); - EXPECT_EQ(ALOOPER_POLL_TIMEOUT, result) - << "pollOnce result should be ALOOPER_POLL_TIMEOUT because there was nothing to do"; + EXPECT_EQ(Looper::POLL_TIMEOUT, result) + << "pollOnce result should be Looper::POLL_TIMEOUT because there was nothing to do"; EXPECT_EQ(size_t(2), handler->messages.size()) << "no more messages to handle"; }