• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package android.app;
2 
3 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
4 import static com.google.common.truth.Truth.assertThat;
5 import static org.robolectric.annotation.LooperMode.Mode.PAUSED;
6 
7 import android.os.Handler;
8 import android.os.Looper;
9 import androidx.test.ext.junit.runners.AndroidJUnit4;
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.robolectric.annotation.LooperMode;
15 import org.robolectric.annotation.internal.DoNotInstrument;
16 
17 /**
18  * Tests to verify android.app.Instrumentation APIs behave consistently between Robolectric and
19  * device.
20  */
21 @DoNotInstrument
22 @RunWith(AndroidJUnit4.class)
23 @LooperMode(PAUSED)
24 public final class InstrumentationTest {
25 
26   /**
27    * Verify that runOnMainSync main looper synchronization is consistent between on device and
28    * robolectric.
29    */
30   @Test
runOnMainSync()31   public void runOnMainSync() {
32     final List<String> events = new ArrayList<>();
33     Handler mainHandler = new Handler(Looper.getMainLooper());
34 
35     mainHandler.post(() -> events.add("before runOnMainSync"));
36     getInstrumentation()
37         .runOnMainSync(
38             new Runnable() {
39               @Override
40               public void run() {
41                 events.add("in runOnMainSync");
42                 // as expected, on device tests become flaky and fail deterministically on
43                 // Robolectric with this line, as runOnMainSync does not drain the main looper
44                 // after runnable executes
45                 // mainHandler.post(() -> events.add("post from runOnMainSync"));
46               }
47             });
48 
49     assertThat(events).containsExactly("before runOnMainSync", "in runOnMainSync").inOrder();
50   }
51 }
52