-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ActivityTest.java
69 lines (59 loc) · 2.66 KB
/
ActivityTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package android.app;
import static com.google.common.truth.Truth.assertThat;
import android.graphics.drawable.ColorDrawable;
import android.widget.Button;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.internal.DoNotInstrument;
import org.robolectric.testapp.ActivityWithAnotherTheme;
import org.robolectric.testapp.ActivityWithoutTheme;
import org.robolectric.testapp.R;
@DoNotInstrument
@RunWith(AndroidJUnit4.class)
public class ActivityTest {
@Rule
public ActivityTestRule<ActivityWithAnotherTheme> activityWithAnotherThemeRule =
new ActivityTestRule<>(ActivityWithAnotherTheme.class, false, false);
@Rule
public ActivityTestRule<ActivityWithoutTheme> activityWithoutThemeRule =
new ActivityTestRule<>(ActivityWithoutTheme.class, false, false);
@Before
public void setUp() throws Exception {
ActivityWithAnotherTheme.setThemeBeforeContentView = null;
}
@Test
public void whenSetOnActivityInManifest_activityGetsThemeFromActivityInManifest() {
Activity activity = activityWithAnotherThemeRule.launchActivity(null);
Button theButton = activity.findViewById(R.id.button);
ColorDrawable background = (ColorDrawable) theButton.getBackground();
assertThat(background.getColor()).isEqualTo(0xffff0000);
}
@Test
public void
whenExplicitlySetOnActivity_afterSetContentView_activityGetsThemeFromActivityInManifest() {
Activity activity = activityWithAnotherThemeRule.launchActivity(null);
activity.setTheme(R.style.Theme_Robolectric);
Button theButton = activity.findViewById(R.id.button);
ColorDrawable background = (ColorDrawable) theButton.getBackground();
assertThat(background.getColor()).isEqualTo(0xffff0000);
}
@Test
public void whenExplicitlySetOnActivity_beforeSetContentView_activityUsesNewTheme() {
ActivityWithAnotherTheme.setThemeBeforeContentView = R.style.Theme_Robolectric;
Activity activity = activityWithAnotherThemeRule.launchActivity(null);
Button theButton = activity.findViewById(R.id.button);
ColorDrawable background = (ColorDrawable) theButton.getBackground();
assertThat(background.getColor()).isEqualTo(0xff00ff00);
}
@Test
public void whenNotSetOnActivityInManifest_activityGetsThemeFromApplicationInManifest() {
Activity activity = activityWithoutThemeRule.launchActivity(null);
Button theButton = activity.findViewById(R.id.button);
ColorDrawable background = (ColorDrawable) theButton.getBackground();
assertThat(background.getColor()).isEqualTo(0xff00ff00);
}
}