1515
1616import android .os .Handler ;
1717import android .os .Looper ;
18+ import android .os .Message ;
1819import io .reactivex .Scheduler ;
1920import io .reactivex .Scheduler .Worker ;
2021import io .reactivex .android .testutil .CountingRunnable ;
3435import org .robolectric .ParameterizedRobolectricTestRunner ;
3536import org .robolectric .annotation .Config ;
3637import org .robolectric .shadows .ShadowLooper ;
38+ import org .robolectric .shadows .ShadowMessageQueue ;
3739
3840import static java .util .concurrent .TimeUnit .MINUTES ;
3941import static java .util .concurrent .TimeUnit .SECONDS ;
4345import static org .junit .Assert .assertSame ;
4446import static org .junit .Assert .assertTrue ;
4547import static org .junit .Assert .fail ;
48+ import static org .robolectric .Shadows .shadowOf ;
4649import static org .robolectric .shadows .ShadowLooper .pauseMainLooper ;
4750import static org .robolectric .shadows .ShadowLooper .runUiThreadTasks ;
4851import static org .robolectric .shadows .ShadowLooper .runUiThreadTasksIncludingDelayedTasks ;
@@ -61,9 +64,11 @@ public static Collection<Object[]> data() {
6164 }
6265
6366 private Scheduler scheduler ;
67+ private boolean async ;
6468
6569 public HandlerSchedulerTest (boolean async ) {
6670 this .scheduler = new HandlerScheduler (new Handler (Looper .getMainLooper ()), async );
71+ this .async = async ;
6772 }
6873
6974 @ Before
@@ -774,6 +779,47 @@ public void workerSchedulePeriodicallyInputValidation() {
774779 }
775780 }
776781
782+ @ Test
783+ public void directScheduleSetAsync () {
784+ ShadowMessageQueue mainMessageQueue = shadowOf (Looper .getMainLooper ().getQueue ());
785+
786+ scheduler .scheduleDirect (new Runnable () {
787+ @ Override public void run () {
788+ }
789+ });
790+
791+ Message message = mainMessageQueue .getHead ();
792+ assertEquals (async , message .isAsynchronous ());
793+ }
794+
795+ @ Test
796+ public void workerScheduleSetAsync () {
797+ ShadowMessageQueue mainMessageQueue = shadowOf (Looper .getMainLooper ().getQueue ());
798+
799+ Worker worker = scheduler .createWorker ();
800+ worker .schedule (new Runnable () {
801+ @ Override public void run () {
802+ }
803+ });
804+
805+ Message message = mainMessageQueue .getHead ();
806+ assertEquals (async , message .isAsynchronous ());
807+ }
808+
809+ @ Test
810+ public void workerSchedulePeriodicallySetAsync () {
811+ ShadowMessageQueue mainMessageQueue = shadowOf (Looper .getMainLooper ().getQueue ());
812+
813+ Worker worker = scheduler .createWorker ();
814+ worker .schedulePeriodically (new Runnable () {
815+ @ Override public void run () {
816+ }
817+ }, 1 , 1 , MINUTES );
818+
819+ Message message = mainMessageQueue .getHead ();
820+ assertEquals (async , message .isAsynchronous ());
821+ }
822+
777823 private static void idleMainLooper (long amount , TimeUnit unit ) {
778824 // TODO delete this when https://github.com/robolectric/robolectric/pull/2592 is released.
779825 ShadowLooper .idleMainLooper (unit .toMillis (amount ));
0 commit comments