15
15
16
16
import android .os .Handler ;
17
17
import android .os .Looper ;
18
+ import android .os .Message ;
18
19
import io .reactivex .Scheduler ;
19
20
import io .reactivex .Scheduler .Worker ;
20
21
import io .reactivex .android .testutil .CountingRunnable ;
34
35
import org .robolectric .ParameterizedRobolectricTestRunner ;
35
36
import org .robolectric .annotation .Config ;
36
37
import org .robolectric .shadows .ShadowLooper ;
38
+ import org .robolectric .shadows .ShadowMessageQueue ;
37
39
38
40
import static java .util .concurrent .TimeUnit .MINUTES ;
39
41
import static java .util .concurrent .TimeUnit .SECONDS ;
43
45
import static org .junit .Assert .assertSame ;
44
46
import static org .junit .Assert .assertTrue ;
45
47
import static org .junit .Assert .fail ;
48
+ import static org .robolectric .Shadows .shadowOf ;
46
49
import static org .robolectric .shadows .ShadowLooper .pauseMainLooper ;
47
50
import static org .robolectric .shadows .ShadowLooper .runUiThreadTasks ;
48
51
import static org .robolectric .shadows .ShadowLooper .runUiThreadTasksIncludingDelayedTasks ;
@@ -61,9 +64,11 @@ public static Collection<Object[]> data() {
61
64
}
62
65
63
66
private Scheduler scheduler ;
67
+ private boolean async ;
64
68
65
69
public HandlerSchedulerTest (boolean async ) {
66
70
this .scheduler = new HandlerScheduler (new Handler (Looper .getMainLooper ()), async );
71
+ this .async = async ;
67
72
}
68
73
69
74
@ Before
@@ -774,6 +779,47 @@ public void workerSchedulePeriodicallyInputValidation() {
774
779
}
775
780
}
776
781
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
+
777
823
private static void idleMainLooper (long amount , TimeUnit unit ) {
778
824
// TODO delete this when https://github.com/robolectric/robolectric/pull/2592 is released.
779
825
ShadowLooper .idleMainLooper (unit .toMillis (amount ));
0 commit comments