Permalink
Browse files

Merge pull request #252 from metalhead8816/handler_update

Add implementation of sendMessageAtFrontOfQueue on ShadowHandler
  • Loading branch information...
2 parents c7d0264 + bcc9e9a commit 000fd8390167d808f9dcad9b77b2b9b8da03e196 @jberkel jberkel committed May 8, 2012
@@ -108,6 +108,20 @@ public void run() {
return true;
}
+ private final boolean sendMessageToFrontDelayed(final Message msg, long delayMillis) {
+ messages.add(0, msg);
+ postAtFrontOfQueue(new Runnable() {
+ @Override
+ public void run() {
+ if (messages.contains(msg)) {
+ routeMessage(msg);
+ messages.remove(msg);
+ }
+ }
+ });
+ return true;
+ }
+
private void routeMessage(Message msg) {
if(callback != null) {
callback.handleMessage(msg);
@@ -129,6 +143,11 @@ public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
}
@Implementation
+ public final boolean sendMessageAtFrontOfQueue(Message msg) {
+ return sendMessageToFrontDelayed(msg, 0L);
+ }
+
+ @Implementation
public final Looper getLooper() {
return looper;
}
@@ -195,6 +195,29 @@ public void testPostAtFrontOfQueueThenRunMainLooperOneTaskAtATime_shouldRunFront
}
@Test
+ public void testSendMessageAtFrontOfQueueThenRunMainLooperOneMsgAtATime_shouldRunFrontOfQueueMsgFirst() throws Exception {
+ Handler handler = new Handler();
+
+ ShadowLooper.pauseMainLooper();
+ // Post two messages to handler. Handle first message and confirm that msg posted
+ // to front is removed.
+ handler.obtainMessage(123).sendToTarget();
+ Message frontMsg = handler.obtainMessage(345);
+ boolean result = handler.sendMessageAtFrontOfQueue(frontMsg);
+
+ assertTrue(result);
+
+ assertTrue(handler.hasMessages(123));
+ assertTrue(handler.hasMessages(345));
+ ShadowHandler.runMainLooperOneTask();
+ assertTrue(handler.hasMessages(123));
+ assertFalse(handler.hasMessages(345));
+ ShadowHandler.runMainLooperOneTask();
+ assertFalse(handler.hasMessages(123));
+ assertFalse(handler.hasMessages(345));
+ }
+
+ @Test
public void sendEmptyMessage_addMessageToQueue() {
Robolectric.pauseMainLooper();
Handler handler = new Handler();

0 comments on commit 000fd83

Please sign in to comment.