1 package android.app; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.graphics.drawable.ColorDrawable; 6 import android.widget.Button; 7 import androidx.test.core.app.ActivityScenario; 8 import androidx.test.ext.junit.runners.AndroidJUnit4; 9 import org.junit.Before; 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 import org.robolectric.annotation.internal.DoNotInstrument; 13 import org.robolectric.testapp.ActivityWithAnotherTheme; 14 import org.robolectric.testapp.ActivityWithoutTheme; 15 import org.robolectric.testapp.R; 16 17 @DoNotInstrument 18 @RunWith(AndroidJUnit4.class) 19 public class ActivityInstrTest { 20 21 @Before setUp()22 public void setUp() { 23 ActivityWithAnotherTheme.setThemeBeforeContentView = null; 24 } 25 26 @Test whenSetOnActivityInManifest_activityGetsThemeFromActivityInManifest()27 public void whenSetOnActivityInManifest_activityGetsThemeFromActivityInManifest() { 28 try (ActivityScenario<ActivityWithAnotherTheme> scenario = 29 ActivityScenario.launch(ActivityWithAnotherTheme.class)) { 30 scenario.onActivity( 31 activity -> { 32 Button theButton = activity.findViewById(R.id.button); 33 ColorDrawable background = (ColorDrawable) theButton.getBackground(); 34 assertThat(background.getColor()).isEqualTo(0xffff0000); 35 }); 36 } 37 } 38 39 @Test 40 public void whenExplicitlySetOnActivity_afterSetContentView_activityGetsThemeFromActivityInManifest()41 whenExplicitlySetOnActivity_afterSetContentView_activityGetsThemeFromActivityInManifest() { 42 try (ActivityScenario<ActivityWithAnotherTheme> scenario = 43 ActivityScenario.launch(ActivityWithAnotherTheme.class)) { 44 scenario.onActivity( 45 activity -> { 46 activity.setTheme(R.style.Theme_Robolectric); 47 Button theButton = activity.findViewById(R.id.button); 48 ColorDrawable background = (ColorDrawable) theButton.getBackground(); 49 assertThat(background.getColor()).isEqualTo(0xffff0000); 50 }); 51 } 52 } 53 54 @Test whenExplicitlySetOnActivity_beforeSetContentView_activityUsesNewTheme()55 public void whenExplicitlySetOnActivity_beforeSetContentView_activityUsesNewTheme() { 56 ActivityWithAnotherTheme.setThemeBeforeContentView = R.style.Theme_Robolectric; 57 try (ActivityScenario<ActivityWithAnotherTheme> scenario = 58 ActivityScenario.launch(ActivityWithAnotherTheme.class)) { 59 scenario.onActivity( 60 activity -> { 61 Button theButton = activity.findViewById(R.id.button); 62 ColorDrawable background = (ColorDrawable) theButton.getBackground(); 63 assertThat(background.getColor()).isEqualTo(0xff00ff00); 64 }); 65 } 66 } 67 68 @Test whenNotSetOnActivityInManifest_activityGetsThemeFromApplicationInManifest()69 public void whenNotSetOnActivityInManifest_activityGetsThemeFromApplicationInManifest() { 70 try (ActivityScenario<ActivityWithoutTheme> scenario = 71 ActivityScenario.launch(ActivityWithoutTheme.class)) { 72 scenario.onActivity( 73 activity -> { 74 Button theButton = activity.findViewById(R.id.button); 75 ColorDrawable background = (ColorDrawable) theButton.getBackground(); 76 assertThat(background.getColor()).isEqualTo(0xff00ff00); 77 }); 78 } 79 } 80 81 } 82