• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.testapp.ActivityWithAnotherTheme;
13 import org.robolectric.testapp.ActivityWithoutTheme;
14 import org.robolectric.testapp.R;
15 
16 @RunWith(AndroidJUnit4.class)
17 public class ActivityInstrTest {
18 
19   @Before
setUp()20   public void setUp() {
21     ActivityWithAnotherTheme.setThemeBeforeContentView = null;
22   }
23 
24   @Test
whenSetOnActivityInManifest_activityGetsThemeFromActivityInManifest()25   public void whenSetOnActivityInManifest_activityGetsThemeFromActivityInManifest() {
26     try (ActivityScenario<ActivityWithAnotherTheme> scenario =
27         ActivityScenario.launch(ActivityWithAnotherTheme.class)) {
28       scenario.onActivity(
29           activity -> {
30             Button theButton = activity.findViewById(R.id.button);
31             ColorDrawable background = (ColorDrawable) theButton.getBackground();
32             assertThat(background.getColor()).isEqualTo(0xffff0000);
33           });
34     }
35   }
36 
37   @Test
38   public void
whenExplicitlySetOnActivity_afterSetContentView_activityGetsThemeFromActivityInManifest()39       whenExplicitlySetOnActivity_afterSetContentView_activityGetsThemeFromActivityInManifest() {
40     try (ActivityScenario<ActivityWithAnotherTheme> scenario =
41         ActivityScenario.launch(ActivityWithAnotherTheme.class)) {
42       scenario.onActivity(
43           activity -> {
44             activity.setTheme(R.style.Theme_Robolectric);
45             Button theButton = activity.findViewById(R.id.button);
46             ColorDrawable background = (ColorDrawable) theButton.getBackground();
47             assertThat(background.getColor()).isEqualTo(0xffff0000);
48           });
49     }
50   }
51 
52   @Test
whenExplicitlySetOnActivity_beforeSetContentView_activityUsesNewTheme()53   public void whenExplicitlySetOnActivity_beforeSetContentView_activityUsesNewTheme() {
54     ActivityWithAnotherTheme.setThemeBeforeContentView = R.style.Theme_Robolectric;
55     try (ActivityScenario<ActivityWithAnotherTheme> scenario =
56         ActivityScenario.launch(ActivityWithAnotherTheme.class)) {
57       scenario.onActivity(
58           activity -> {
59             Button theButton = activity.findViewById(R.id.button);
60             ColorDrawable background = (ColorDrawable) theButton.getBackground();
61             assertThat(background.getColor()).isEqualTo(0xff00ff00);
62           });
63     }
64   }
65 
66   @Test
whenNotSetOnActivityInManifest_activityGetsThemeFromApplicationInManifest()67   public void whenNotSetOnActivityInManifest_activityGetsThemeFromApplicationInManifest() {
68     try (ActivityScenario<ActivityWithoutTheme> scenario =
69         ActivityScenario.launch(ActivityWithoutTheme.class)) {
70       scenario.onActivity(
71           activity -> {
72             Button theButton = activity.findViewById(R.id.button);
73             ColorDrawable background = (ColorDrawable) theButton.getBackground();
74             assertThat(background.getColor()).isEqualTo(0xff00ff00);
75           });
76     }
77   }
78 }
79