1 package org.robolectric.shadows; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.robolectric.Shadows.shadowOf; 5 6 import android.os.Handler; 7 import android.os.Looper; 8 import android.os.Message; 9 import android.os.Messenger; 10 import androidx.test.ext.junit.runners.AndroidJUnit4; 11 import org.junit.Test; 12 import org.junit.runner.RunWith; 13 14 @RunWith(AndroidJUnit4.class) 15 public class ShadowMessengerTest { 16 17 @Test testMessengerSend()18 public void testMessengerSend() throws Exception { 19 Handler handler = new Handler(); 20 Messenger messenger = new Messenger(handler); 21 22 ShadowLooper.pauseMainLooper(); 23 Message msg = Message.obtain(null, 123); 24 messenger.send(msg); 25 26 assertThat(handler.hasMessages(123)).isTrue(); 27 Looper looper = Looper.myLooper(); 28 shadowOf(looper).runOneTask(); 29 assertThat(handler.hasMessages(123)).isFalse(); 30 } 31 32 @Test getLastMessageSentShouldWork()33 public void getLastMessageSentShouldWork() throws Exception { 34 Handler handler = new Handler(); 35 Messenger messenger = new Messenger(handler); 36 Message msg = Message.obtain(null, 123); 37 Message originalMessage = Message.obtain(msg); 38 messenger.send(msg); 39 40 assertThat(ShadowMessenger.getLastMessageSent().what).isEqualTo(originalMessage.what); 41 } 42 43 @Test createMessengerWithBinder_getLastMessageSentShouldWork()44 public void createMessengerWithBinder_getLastMessageSentShouldWork() throws Exception { 45 Handler handler = new Handler(); 46 Messenger messenger = new Messenger(new Messenger(handler).getBinder()); 47 48 Message msg = Message.obtain(null, 123); 49 Message originalMessage = Message.obtain(msg); 50 messenger.send(msg); 51 52 assertThat(ShadowMessenger.getLastMessageSent().what).isEqualTo(originalMessage.what); 53 } 54 } 55